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.
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.
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."); }
@r0306 Thank you, also one more thing, Do you know how to i can use Plugitn Manager to put that class in
I'm not sure but on onEnable when i "learned" how to do listeners it said something about a PluginManager
jamw Oh. You mean register the listener. You would add this line to the onEnable() method. Code: getServer().getPluginManager().registerEvents(new Listeners(), this);
@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
jamw Yes, you should make the Hashmap public AND static. Also, for the p, you should replace it with player.
@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?
@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?