Problems with saving player location

Discussion in 'Plugin Development' started by Nerdfuryz, Jul 7, 2013.

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

    Nerdfuryz

    Code:
    public class wildWestBukkit extends JavaPlugin implements Listener{
        public wildWestBukkit plugin;
        public Map<String, Location> points = new HashMap<String, Location>();
        int ty;
        int tx;
        int tz;
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this,this);
     
        }
             
        public boolean oncommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("setbandit")){
                Player target = Bukkit.getPlayerExact(sender.getName());
                int hx = target.getLocation().getBlockX();
                int hy = target.getLocation().getBlockY();
                int hz = target.getLocation().getBlockZ();
                float pitch = target.getLocation().getPitch();
                float yaw = target.getLocation().getYaw();
           
                if (sender.hasPermission("bandit.setpoint"))
                {
                player.sendMessage(ChatColor.RED + "You have successfully set a point for bandits!");
                Location point = new Location(target.getWorld(), hx, hy, hz, pitch, yaw);
                points.put(null, point);
         
            }
         
            }
            return false;
     
        }
        @EventHandler
        public void onSignClick(PlayerInteractEvent p) {
            Material t = p.getClickedBlock().getType();
              if(t == null){
                  p.setCancelled(true);
                }else if ((t == Material.WALL_SIGN || t == Material.SIGN
                    || t == Material.SIGN_POST)
                    && p.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Sign sign = (Sign) p.getClickedBlock().getState();
                if (sign.getLine(0).contains("[Bandit]")) {
                    Player player = p.getPlayer();
                    player.teleport((Location) points);
                }
            }
               
          }
     
    @Override
        public void onDisable() {
        }
    }
                    
    Working on having /setbandit set the point and when you click sign it will bring you to that point. Created hashmap but the command doesn't seem to be responding?
     
  2. Offline

    LinearLogic

    Capitalize the 'c' in 'onCommand' (and while you're at it, capitalize your class name ;)).
     
  3. Offline

    Nerdfuryz

    Haha, just did both and the command works but it isn't teleporting when I right click a sign now?
     
  4. Offline

    SnipsRevival

    Nerdfuryz What happens if(t != null) and is not one of the other Materials you checked for?
     
  5. Offline

    Nerdfuryz

    ?
     
  6. Offline

    SnipsRevival

    Nerdfuryz does getType() even return null?
     
  7. Offline

    Nerdfuryz

    Probably not, I was going to modify it later because I was getting null pointer exception
     
  8. Offline

    SnipsRevival

    You can try adding some debugging messages to see where you are getting in your code and work from there. Nerdfuryz
     
  9. Offline

    Nerdfuryz

    I got it fixed guys no worries
    Code:java
    1. import org.bukkit.Material;
    2. import org.bukkit.block.Sign;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Location;
    13. import org.bukkit.World;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class WildWestBukkit extends JavaPlugin implements Listener{
    17. @Override
    18. public void onEnable() {
    19. Bukkit.getPluginManager().registerEvents(this,this);
    20.  
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    24. Player p = (Player) sender;
    25. if (commandLabel.equalsIgnoreCase("setbandit")){
    26. getConfig().set("bandit.world", p.getLocation().getWorld().getName());
    27. getConfig().set("bandit.x", p.getLocation().getX());
    28. getConfig().set("bandit.y", p.getLocation().getY());
    29. getConfig().set("bandit.z", p.getLocation().getZ());
    30. saveConfig();
    31. p.sendMessage(ChatColor.GREEN + "bandit set!");
    32. return true;
    33.  
    34.  
    35.  
    36.  
    37.  
    38. }
    39. return false;
    40.  
    41. }
    42. @EventHandler
    43. public void onSignClick(PlayerInteractEvent p) {
    44. Material t = p.getClickedBlock().getType();
    45. if ((t == Material.WALL_SIGN || t == Material.SIGN
    46. || t == Material.SIGN_POST)
    47. && p.getAction() == Action.RIGHT_CLICK_BLOCK) {
    48. Sign sign = (Sign) p.getClickedBlock().getState();
    49. if (sign.getLine(0).contains("[Bandit]")) {
    50. Player p2 = (Player) p.getPlayer();
    51. World w = Bukkit.getServer().getWorld(getConfig().getString("bandit.world"));
    52. double x = getConfig().getDouble("bandit.x");
    53. double y = getConfig().getDouble("bandit.y");
    54. double z = getConfig().getDouble("bandit.z");
    55. p2.teleport(new Location(w, x, y, z));
    56. p2.sendMessage(ChatColor.GREEN + "Teleport bandit");
    57. }
    58. }
    59.  
    60. }
    61.  
    62. @Override
    63. public void onDisable() {
    64. saveConfig();
    65. }
    66. }
    67.  
     
  10. Offline

    SnipsRevival

    Nerdfuryz Good and I just realized my thinking was completely off when I said
     
  11. Offline

    LinearLogic

    By the way, I wouldn't recommend having your onCommand method return false if the command isn't /setbandit, because returning false automatically messages the command sender the value of the "usage" node in the plugin.yml, if you have the command listed there. It might confuse players who are running different commands.
     
Thread Status:
Not open for further replies.

Share This Page