/back Command

Discussion in 'Plugin Development' started by jamw, May 19, 2012.

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

    jamw

    So I'm just going to say it - i almost suck at making plugins lol. Now i know how to set locations and everything but i'm just wondering how i could make a /back command. So when you die you just do /back. Would i need a playerlistener for a death event? Please help as i feel like im just making it more difficult then it needs to be.
     
  2. Offline

    captainz0r

    Yes use a player listener to check if your dead and then save your position to a variable then make the back command teleport to that position.
     
  3. Offline

    jamw

    Like i said im fairly new so can you help me with that i have not made very many player listeners
     
  4. Offline

    r0306

    jamw
    You can add the player and their location to a hashmap on death. Then in the command executor, check if the hashmap contains their name. If it does, teleport them according to their location stored in the hashmap and remove them from the hashmap. If not, send them a message saying that they were not dead. Here's an example:

    Listener:
    Code:
    public class Listeners implements Listener {
    HashMap<Player, Location> back = new HashMap<Player, Location>();
    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
    Player p = event.getEntity();
    back.put(p, p.getLocation());
    }
    
    CommandExecutor:
    Code:
    if (Listeners.back.contains(p)) {
    p.teleport(Listeners.back.get(p));
    Listeners.back.remove(p);
    } else {
    p.sendMessage("You were not dead.");
    }
     
    jamw likes this.
  5. Offline

    jamw

    Alright im going to try this ill let you know in a bit if it works
     
  6. Offline

    r0306

    jamw
    Updated with an example code.
     
    jamw likes this.
  7. Offline

    jamw

    @r0306 Thank you, also one more thing, Do you know how to i can use Plugitn Manager to put that class in
     
  8. Offline

    r0306

    jamw
    What do you mean?
     
    jamw likes this.
  9. Offline

    jamw

    I'm not sure but on onEnable when i "learned" how to do listeners it said something about a PluginManager
     
  10. Offline

    r0306

    jamw
    Oh. You mean register the listener.
    You would add this line to the onEnable() method.
    Code:
    getServer().getPluginManager().registerEvents(new Listeners(), this);
     
    jamw likes this.
  11. Offline

    jamw

    @r0306 Yes! haha thank you. Also here is my current code.. it wanted me to make the hashmap static and it doesn't recognize the "p"
    This is the main :
    Code:
    public class Back extends JavaPlugin{
        public final Logger log = Logger.getLogger("Minecraft");
        public static Back plugin;
       
        @Override
        public void onEnable(){
            this.log.info("Back Is Enabled!");
            getServer().getPluginManager().registerEvents(new PlayerListener(), this);
        }
       
        @Override
        public void onDisable(){
            this.log.info("Back Is Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd,String commandLabel, String[] args) {
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("back")){
                if(player.hasPermission("Back.back")){
                    if(args.length == 0){
                    if (PlayerListener.back.contains(p)) {
                        p.teleport(PlayerListener.back.get(p));
                        PlayerListener.back.remove(p);
                        } else {
                        p.sendMessage("You were not dead.");
                        }
                       
                       
                    }
                }
               
            }
            return false;
           
        }
     
    }
    
    PlayerListener:

    Code:
    public class PlayerListener implements Listener {
       
        static HashMap<Player, Location> back = new HashMap<Player, Location>();
       
       
        @EventHandler
        public void onDeath(PlayerDeathEvent event){
        Player p = event.getEntity();
        back.put(p, p.getLocation());
           
        }
     
    }
    
    Also, thank you for all your help and understanding :)
     
  12. Offline

    r0306

    jamw
    Yes, you should make the Hashmap public AND static. Also, for the p, you should replace it with player.
     
    jamw likes this.
  13. Offline

    jamw

    @r0306 i'm sorry you are going to be so annoyed with me but
    Code:
    if( PlayerListener.back).contains(player)) {
    gets an error should it be containsValue? or containsKey?
     
  14. Offline

    r0306

    jamw
    It should be containsKey(). And don't worry, you aren't annoying me at all. :p
     
    jamw likes this.
  15. Offline

    jamw

    @r0306 Thank you so much for all your help, if i need anything else is it ok if i just message you instead of bumping this thread that only your beautiful soul is helping me with? :p
     
  16. Offline

    r0306

    jamw
    Sure. Just give me a PM if you have any questions.
     
    jamw likes this.
Thread Status:
Not open for further replies.

Share This Page