Solved "spawns" command execution

Discussion in 'Spigot Plugin Help' started by MaltePB, Aug 6, 2018.

Thread Status:
Not open for further replies.
  1. Hello, I was just wondering is there any plugin that will execute a command everytime you get teleported to the spawn? i need a command executed everytime you teleport to the spawn, Thanks.
     
    • Friendly Friendly x 1
  2. I know i could do that, but it's only when they TYPE /spawn. I want it to be when they get teleported to spawn or another command gets executed by the console, not when the player types the command. But thank you for your time :)
     
    • Friendly Friendly x 1
  3. You want something to be executed every time when anything like plugins, console, player itself, other players... no matter what teleports a player to a specific location?

    I'm pretty sure there is no plugin which does this for you (at least i never heared of something specific like this).

    But it would pretty easy to implement a plugin like this as @Pam_ suggested.
    Are you familiar with plugin development?
     
    • Friendly Friendly x 1
  4. @MaltePB i was borded and it took me 20 minutes to implement your plugin (expected 10 minutes...). Didn't test it yet, have fun.

    The uncompiled source file is included to the JAR, the exported config.yml is self-explaining:
    Code (YAML):
    'world,0,64,0': 'say <PLAYER> tpt to spawn.|say yes, <PLAYER> really does!'
    'world_nether,0,64,0'
    : 'say <PLAYER> tpt to nether spawn.|say yes, <PLAYER> really does!'
    'world_the_end,0,64,0'
    : 'say <PLAYER> tpt to end spawn.|say yes, <PLAYER> really does!'
    Positions specified by world name and x, y, z block coordinates.
    Commands specified separated by | and <PLAYER> will be replaced by the player name. Pretty sure, they must be without /.
    Be careful, the commands will be executed as console, so the player's permissions won't be checked for a command.

    No commands ingame. Configurable by the config.yml only.
    If you don't want to look into the JAR, here is the code, too:
    Code (Java):
    package minecraft.spigot.community.michel_0.tpcmd;
    import java.util.HashMap;
    import java.util.Map;
    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerTeleportEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin implements Listener {
        private Map<Location,String> targets;
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            this.targets = new HashMap<>();
            this.getServer().getPluginManager().registerEvents(this, this);
            for (String key : this.getConfig().getKeys(false)) {
                String[] locSeg = key.split(",");
                if (locSeg.length == 4) {
                    if (this.getServer().getWorld(locSeg[0]) != null) {
                        try {
                            this.targets.put(new Location(this.getServer().getWorld(locSeg[0]), Integer.parseInt(locSeg[1]), Integer.parseInt(locSeg[2]), Integer.parseInt(locSeg[3])), this.getConfig().getString(key));
                        } catch (NumberFormatException e) {
                            this.getLogger().info("\"" + key + "\" contains non-valid integers! Ignoring it.");
                        }
                    } else {
                        this.getLogger().info("\"" + locSeg[0] + "\" is not a valid world! Ignoring it.");
                    }
                } else {
                    this.getLogger().info("\"" + key + "\" is not a valid location! Ignoring it.");
                }
            }
        }
        @EventHandler
        private void onTeleport(PlayerTeleportEvent event) {
            for (Location loc : this.targets.keySet()) {
                if (event.getTo().getWorld().getName().equals(loc.getWorld().getName())) {
                    if (event.getTo().getBlockX() == loc.getBlockX() && event.getTo().getBlockY() == loc.getBlockY() && event.getTo().getBlockZ() == loc.getBlockZ()) {
                        String[] cmds = this.targets.get(loc).split("|");
                        for (String cmd : cmds) {
                            this.getServer().dispatchCommand(this.getServer().getConsoleSender(), cmd.replace("<PLAYER>", event.getPlayer().getName()));
                        }
                    }
                }
            }
        }
    }

    EDIT1: Ooops, forget to check that it is the same world, one moment…
    EDIT2: Fixed, now Version 1.1
     

    Attached Files:

    #6 Michel_0, Aug 6, 2018
    Last edited: Aug 6, 2018
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.