TPA » problems with tpaccept and tpdeny

Discussion in 'Spigot Plugin Development' started by Zmars, Jan 6, 2020.

  1. Hi!

    I've been trying to write a program on Tpa for a long time, which would include / tpaccept and / tpdeny. Unfortunately, I still can't do it. Could you please advise me what I am doing wrong? I enclose my code that shows no errors. I will be glad for any feedback. Have a nice day.

    Code (Java):
    package Adabra.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class TPA implements Listener {
        public boolean ad;
        public void onChat (AsyncPlayerChatEvent e) {
            String message = e.getMessage();
            if (message == "/tpaccept")  {
                ad = true;
            else if (message == "/tpdeny") {
                ad = false;
        public void onCommand(CommandSender p, Command arg1, String arg2, String[] args) {
            if (p.hasPermission("")){
                    Player pl = Bukkit.getPlayer(args[0]);
                    if (pl == null) {
                        p.sendMessage("§cHrac s tímto jmenem neni online.");
                    if (arg1.getName().equalsIgnoreCase("tpa")) {
                        pl.sendMessage("§7Hrac §3" + p + " §7by se k tobe chtel teleportovat.");
                        pl.sendMessage("§7Pro potvrzeni teleportace napis §a/tpaccept§7, pro odmitnuti §c/tpdeny§7.");
                        if(ad == true) {
                            Location ploc = pl.getLocation();
                            Player s = (Player) p;
                        else p.sendMessage("§7Hrac odmitl tvou zadost o teleportaci.");
                else p.sendMessage("§cMusis pouzit: /tp jmeno_hrace");
            else p.sendMessage("§cNa toto nemas opravneni.");
    The messages are in Czech. But it does not matter.

  2. Well. Let me point out a few things:
    No listener needed.
    If you want to implement a command, you need to do two things: implement CommandExecutor and register the command (preferably in your main class)

    Now you have two Commands: /tpa and /tpaccept. Imagine the following: Two players are sending a tpa-request to some other players. You only got one variable, so the first to enter either /tpaccept or /tpdeny decides for the other player.

    Solution: Map of stored requests with a player linked in a helper class (or your main class, I'm not your mom, you can do whatever you want)
    Implement three commands: /tpa, /tpaccept and /tpadeny
    /tpa: adds the request to the map
    /tpaaccept: request in map? -> teleport, else do nothing
    /tpadeny: request in map? -> error message, else d nothing.

    You have a nice day as well!
    • Agree Agree x 1
  3. So there is a problem, I know about maps, you´re talking about. But I dont know, how to use hashmap. I tried to watch some tutorials. Thanks
  4. Also:
    • You may not compare strings in java with ==, you need to use equals method.
    • Your code does not stop when pl is null.
  5. Optic_Fusion1

    Resource Staff

    also this isn't a resource, so remove the resource tag
  6. A Map<Key,Value> is something like a dictionary. Suppose Key is English and Value is Czech. Then map.get("Player") would return "Hrac" (google translate is my friend, so don't take offense please). You can put a value in a map by calling map.put(key, value). Notice that key may be present once and only one. if you call map.put("Player", "Hrac") and map.put("Player", "Spieler"), only the first would be stored in the map.

    Map is an Interface, this means no methods (at least let's suppose that's the case for simplicity) are implemented. If you want to instantiate a Map, you thus can't do this:
    Code (Text):
    Map<Key,Value> map = new Map<>();
    Instead, you need to use something that implements Map. What does implement Map? You need to look in the docs for that, but as you said one prominent example is HashMap. Meaning you can instantiate your Map like this:

    Code (Text):
    Map<Key,Value> map = new HashMap<>();
    Now let's get concrete and add a player and a request:

    Code (Text):
    Map<Player,Request> map = new Map<>();
    Request is a class that I have made up, maybe it's defined like this:

    Code (Java):
    public class Request {
        private Location loc;

        public Request(Location loc) {this.loc = loc;}

        public Location getLocation() {return this.loc;}
    Now on your /tpa command, you add a request:

    Code (Java):

        final Player player = Bukkit.getPlayer(args[0]);
        map.put(player, location);
    (Note that this is simplified, you need to check if args exist and the player exists and so on)

    /tpaaccept or /tpadeny:

    Code (Java):
    if sender instanceof blabla {
        if (map.contains(player)) {
            // teleport or not
    and voilá, you have your tpa-plugin (and I have written a tutorial that is way too long)
    Have fun coding!
    • Like Like x 1
  7. you need to use commands. chat event doesnt call for / messages

    overwrite the onCommand method in your JavaPlugin instance or create a class that implements CommandExecutor and register it with your plugin.