Solved Calling multiple commands from a single class?

Discussion in 'Plugin Development' started by xiTango, May 19, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    xiTango

    Hello all! I have made a /tpa plugin and it works when all the commands are in the main class file, however I want to have a main class and then a separate teleport class which contains the code for the /tpa command.

    However when I do this, only the /tpa command works and not /tpaccept or tpdeny, there are no errors in the console, it simply just does nothing.

    Here is my main class:
    main class (open)

    Code:
    package com.xiTango.xiEssentials;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.xiTango.xiEssentials.commands.feed;
    import com.xiTango.xiEssentials.commands.heal;
    import com.xiTango.xiEssentials.commands.setspawn;
    import com.xiTango.xiEssentials.commands.spawn;
    import com.xiTango.xiEssentials.commands.teleport;
    
    public class main extends JavaPlugin{
       
        public void onEnable(){
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new PlayerJoin(), this);
    
                getCommand("tpa").setExecutor(new teleport());
                getCommand("tpaccept").setExecutor(new teleport());
                getCommand("tpadeny").setExecutor(new teleport());
        }
    }
    

    And my teleport class:
    teleport class (open)

    Code:
    package com.xiTango.xiEssentials.commands;
    
    import java.util.HashMap;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class teleport implements CommandExecutor{
       
        HashMap<Player, Player> tpa = new HashMap<Player, Player>();
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tpa")){
                if (args.length == 1){
                    Player target = player.getServer().getPlayer(args[0]);
                    if (target == player){
                        player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.RED +
                                "You can't send a teleport request to yourself!");
                    return false;
                    }
                    if(target != null){
                        tpa.put(target, player);
                    player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.AQUA +
                            "Teleport request sent to " + target.getName() + "!");
                    target.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.AQUA + player.getName() +
                            " is requesting to teleport to you! Type /tpaccept or /tpdeny!");
                   
                    }else{
                        player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.RED +
                        "Specified player is not online!");
                    }
                }else{
                    player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.RED +
                        "Correct usage: /tpa <player>");
                }
            }else{
            if (cmd.getName().equalsIgnoreCase("tpaccept")){
                if(tpa.get(player) != null){
                    tpa.get(player).teleport(player);
                    player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.AQUA + "Teleport request accepted!");
                    tpa.get(player).sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.AQUA + player.getName() +
                            " accepted your teleport request!");
                    tpa.put(player, null);
                }
            }else{
            if(cmd.getName().equalsIgnoreCase("tpdeny")){
                if(tpa.get(player) != null){
                    player.sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.RED + "Teleport request denied!");
                    tpa.get(player).sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "ElectroCraft" + ChatColor.GRAY + "] " + ChatColor.RED + player.getName() +
                            " denied your teleport request!");
                    tpa.put(player, null);
                }
            }
            }
        }
        return false;
        }
    }
    

    I have put the correct commands in my plugin.yml, but here it is anyway:
    plugin.yml (open)

    Code:
    name: xiEssentials
    author: xiTango
    version: 1.0
    main: com.xiTango.xiEssentials.main
    
    commands:
      heal:
        description: Heals yourself or others.
      feed:
        description: Feeds yourself or others.
      spawn:
        description: Teleports you to spawn.
      setspawn:
        description: Sets the server spawn.
      tpa:
        description: Send a teleport request to another player.
      tpaccept:
        description: Accept a teleport request from another player.
      tpdeny:
        description: Deny a teleport request from another player.
    

    Thankyou for any help anyone may offer!
     
  2. Online

    timtower Administrator Administrator Moderator

    @xiTango That is because the one for accept and deny are different instances than tpa itself.
    Make a field, put an instance in, use setExecutor for the commands on that single instance.
     
  3. Offline

    DoggyCode™

    Any errors in console?
     
  4. Offline

    xiTango

    I am sort of new to this, so I don't really know what you mean by that, is there anything that I could search on google to find out how to do those things?
     
  5. Online

    timtower Administrator Administrator Moderator

    @xiTango Yes you can.
    Call new teleport once, not 3 times, put it in a variable.
    @DoggyCode™ It is an instancing error, 3 lists that should be 1
     
    DoggyCode™ likes this.
  6. Offline

    xiTango

    Would i do it like this?

    teleport tpa = new teleport();
     
  7. Online

    timtower Administrator Administrator Moderator

  8. Offline

    xiTango

    When I do that and put it in my main class like so:

    Code:
    package com.xiTango.xiEssentials;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.xiTango.xiEssentials.commands.feed;
    import com.xiTango.xiEssentials.commands.heal;
    import com.xiTango.xiEssentials.commands.setspawn;
    import com.xiTango.xiEssentials.commands.spawn;
    import com.xiTango.xiEssentials.commands.teleport;
    
    public class main extends JavaPlugin{
       
        public void onEnable(){
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new PlayerJoin(), this);
                teleport tpa = new teleport();
               
                getCommand("tpa").setExecutor(new tpa());
                getCommand("tpaccept").setExecutor(new tpa());
                getCommand("tpadeny").setExecutor(new tpa());
        }
    }
    
    It says the the tpa cannot be resolved to a type?
     
  9. Offline

    DoggyCode™

    I see. He created a new teleport object three times.

    Anyways, for you, @xiTango , although this doesn't directly affect the outcome, you should read up on Java naming conventions.

    • Class(es) should always start with a big letter and then follow camel-case (example: TeleportCommand or Teleport).
    • Variable(s) should start with a small letter and then follow camel-case (example: maxPlayers or max). However, this isn't always the case, some people also have it all capitalized (example: CLIENT_ID or MAX_VALUE; in special cases, but then underscores are used for separation of words unlike camel-case where capitalization is used)

    So instead of new teleport(); it should be new Teleport(); you'll have to change the name on your class tho.
     
    Last edited by a moderator: May 19, 2016
  10. Offline

    xiTango

    Ok thanks for that, but my code still doesn't seem to work after changing those things...
     
  11. Online

    timtower Administrator Administrator Moderator

  12. Offline

    xiTango

    Thanks for the help! I got it working! :D I am not sure what I had done wrong, but I ended up reverting the Teleport class to the original one I had, and it worked, so maybe I changed something accidentally.
     
  13. Online

    timtower Administrator Administrator Moderator

    @xiTango It wasn't the teleport class.
    setExecutor(instance1)
    setExecutor(instance2)
    setExecutor(instance3)

    That is what you had.
    You had 3 instances of the same class.
     
Thread Status:
Not open for further replies.

Share This Page