Chunks

Discussion in 'Plugin Development' started by PHILLIPS_71, Jul 21, 2013.

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

    PHILLIPS_71

    I'm wanting to make a command that will get a chunk and save chunks with a command (/chunk <name>) so it will save the chunk and you name it do if a player walks in The chunk it would say the name of the chunk as you walk out win ill say Wilderness or something. I know how to make the command I just don't know how to get the chunk the player is standing on and naming it.
     
  2. Offline

    caseif

    Get the chunk with player.getLocation().getChunk().
    As for naming it, that would require a database of some sort. You would need to map the chunk name to its location. However, this could prove inefficient, and so you may just want to store regions instead. You would just need to store a minimum and maximum value for each axis. You could obtain these by getting the minimum and maximum values of the chunk on each axis (except y), then subtract and add 16 times the radius of the region, respectively.
     
  3. Offline

    PHILLIPS_71

    AngryNerd
    Okay, Thanks for that i have made a start is this how i get the min and the max of a chunk?
     
  4. Offline

    caseif

    There should be some methods in the Chunk class that can help you.
     
  5. Offline

    PHILLIPS_71

    AngryNerd
    I think it's getBlock but I don't know how to select all the blocks how do you do that? Thanks for your help mix appreciated.
     
  6. Offline

    JazzaG

  7. Offline

    PHILLIPS_71

    JazzaG
    Yeah thats what i was looking at so am i right with the getBlock or would it best to use getX and getZ?
     
  8. Offline

    PHILLIPS_71

    Still a bit confused with is but I had an idea would I be able to do a player mice event and if a chunk is saved into a hash set display the message but would would I save the chunk into a hash set.
     
  9. Offline

    JazzaG

    PHILLIPS_71

    You can do what you want with a yaml file looking like this..
    Code:
    x;z: chunkName
    x;z: anotherChunkName
    
    alongside this event handler..
    Code:
    Map<String, Integer[]> oldChunk = new HashMap<String, Integer[]>();
     
    @EventHandler
    void onPlayerMove(PlayerMoveEvent e) {
        Player player = e.getPlayer();
        Chunk chunk = player.getLocation().getChunk();
        
        // No spam for you >:D
        Integer[] old = oldChunk.get(player.getName());
        if(old == null || (old[0] == chunk.getX() && old[1] == chunk.getZ()))
            return;
        oldChunk.put(player.getName(), new Integer[] {chunk.getX(), chunk.getZ()});
       
        String chunkName = getConfig().getString(chunk.getX() + ";" + chunk.getZ());
        if(chunkName != null) 
            player.sendMessage("You entered + " chunkName); 
        else // chunk that hasn't been named?
            player.sendMessage("You're entering no man's land!");     
    }
    
     
    PHILLIPS_71 likes this.
  10. Offline

    PHILLIPS_71

    JazzaG
    Thanks for the help i really appreciate it, but how would i get it working with a command like /chunk then it would add the chunk they are in to the hashmap and set the chunk name?
     
  11. Offline

    JazzaG

    PHILLIPS_71
    I'm using the config file to store the chunk names instead of a map.

    Code:
    public boolean onCommand(...) {
        // TODO: pre-command checks?
     
        String chunkName = args[0];
        Chunk chunk = ((Player)sender)).getLocation().getChunk();
     
        getConfig().set(chunk.getX() + ";" + chunk.getZ(), chunkName);
        getConfig().save(new File(getDataFolder(), "config.yml")); // TODO: handle exception
    }
     
  12. Offline

    PHILLIPS_71

    JazzaG
    This is what i have so far but its not working i'm getting errors on line 25

    Code:
                            Chunk chunk = (Chunk) player.getLocation().getChunk();
    class:
    Code:
    public class Chunk extends JavaPlugin implements Listener {
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if ((commandLabel.equalsIgnoreCase("chunk"))) {
                if(sender.hasPermission("SCPvE.chunk")) {
                    Player player = null;
                    if (sender instanceof Player) {
                        player = (Player) sender;
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "Chunk added");
                            Chunk chunk = (Chunk) player.getLocation().getChunk();
                            oldChunk.put(player.getName(), new Integer[] {chunk.getX(), chunk.getZ()});
                            return true;
                        }
                    }
                }
            }
            return false;
        }
     
        Map<String, Integer[]> oldChunk = new HashMap<String, Integer[]>();
        @EventHandler
        void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Chunk chunk = (Chunk) player.getLocation().getChunk();
     
            Integer[] old = oldChunk.get(player.getName());
            if(old == null || (old[0] == chunk.getX() && old[1] == chunk.getZ()))
                return;
     
            String chunkName = getConfig().getString(chunk.getX() + ";" + chunk.getZ());
            if(chunkName != null)
                player.sendMessage("You entered");
            else
                player.sendMessage("You're entering no man's land!");   
        }
     
        private Integer getZ() {
            // TODO Auto-generated method stub
            return null;
        }
     
        private Integer getX() {
            // TODO Auto-generated method stub
            return null;
        }
    }
     
  13. Offline

    JazzaG

    PHILLIPS_71

    Are you importing Bukkit's chunk or Minecraft's chunk?
     
  14. Offline

    PHILLIPS_71

    JazzaG

    These are my imports

    Code:
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
     
  15. Offline

    JazzaG

    PHILLIPS_71

    Oh I see it now, your class is called Chunk.
     
  16. Offline

    PHILLIPS_71

    JazzaG
    Yes is that the issue?
     
  17. Offline

    Blir

    That's definitely the issue. Anytime you write "Chunk", it's going to think you mean your class instead of org.bukkit.Chunk. You can either rename your class or every time you want to reference Bukkit's chunk, you can write out
    Code:java
    1. org.bukkit.Chunk chunk = player.getLocation().getChunk();
     
    PHILLIPS_71 likes this.
  18. Offline

    PHILLIPS_71

    Blir JazzaG
    Okay thanks fir the help guys, i have changed the name to this and now its doing nothing here is the new class

    Code:
    public class ClaimChunk extends JavaPlugin implements Listener {
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if ((commandLabel.equalsIgnoreCase("chunk"))) {
                if(sender.hasPermission("SCPvE.chunk")) {
                    Player player = null;
                    if (sender instanceof Player) {
                        player = (Player) sender;
                       
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "Chunk added");
                            org.bukkit.Chunk chunk = player.getLocation().getChunk();
                            oldChunk.put(player.getName(), new Integer[] {chunk.getX(), chunk.getZ()});
                            return true;
                        }
                    }
                }
            }
            return false;
        }
     
        Map<String, Integer[]> oldChunk = new HashMap<String, Integer[]>();
        @EventHandler
        void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            org.bukkit.Chunk chunk = player.getLocation().getChunk();
     
            Integer[] old = oldChunk.get(player.getName());
            if(old == null || (old[0] == chunk.getX() && old[1] == chunk.getZ()))
                return;
     
            String chunkName = getConfig().getString(chunk.getX() + ";" + chunk.getZ());
            if(chunkName != null)
                player.sendMessage("You entered");
            else
                player.sendMessage("You're entering no man's land!");   
        }
    }
     
  19. Offline

    PHILLIPS_71

    Anyone know what is going wrong
     
  20. Offline

    JazzaG

    You haven't registered the events
     
  21. Offline

    PHILLIPS_71

    JazzaG
    Thanks for that i when i changed the class name i forgot to register the events, the command is working but the player move event is not sending the player a message that you have walked out of the chunk.
     
  22. Offline

    JazzaG

    PHILLIPS_71
    You aren't updating the player's old chunk.
    Code:
    oldChunk.put(player.getName(), new Integer[] {chunk.getX(), chunk.getZ()});
     
  23. Offline

    PHILLIPS_71

    JazzaG
    I already had that here is the class:

    Code:
    public class ClaimChunk extends JavaPlugin implements Listener {
       
        Map<String, Integer[]> oldChunk = new HashMap<String, Integer[]>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
     
            if (sender instanceof Player) {
                Player player = (Player) sender;
     
                if ((commandLabel.equalsIgnoreCase("chunk") && player.hasPermission("SCPvE.chunk"))) {   
     
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "Chunk added");
                        org.bukkit.Chunk chunk = player.getLocation().getChunk();
                        oldChunk.put(player.getName(), new Integer[] {chunk.getX(), chunk.getZ()});
                        return true;
                    }
                }
            }
            return false;
        }
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            org.bukkit.Chunk chunk = player.getLocation().getChunk();
     
            Integer[] old = oldChunk.get(player.getName());
            if(old == null || (old[0] == chunk.getX() && old[1] == chunk.getZ()))
                return;
     
            String chunkName = getConfig().getString(chunk.getX() + ";" + chunk.getZ());
            if(chunkName != null)
                player.sendMessage("You entered");
            else
                player.sendMessage("You're entering no man's land!");   
        }
    }
     
  24. Offline

    JazzaG

    PHILLIPS_71
    Think about where you have it, and where it should be...
     
  25. Offline

    PHILLIPS_71

    JazzaG
    Okay i have fixed it up a bit just how to i set out the config.yml file?
     
  26. Offline

    PHILLIPS_71

  27. Offline

    newboyhun

  28. Offline

    PHILLIPS_71

    newboyhun
    I don't know how to set out the config file to save the chunks in.
     
  29. Offline

    xTrollxDudex

    PHILLIPS_71
    The chunk object? You need to store information about that and put the info in the config
     
  30. Offline

    PHILLIPS_71

    xTrollxDudex
    So I would just make a config file and put x;z:
     
Thread Status:
Not open for further replies.

Share This Page