Solved /undo method help

Discussion in 'Plugin Development' started by Meatiex, Jul 7, 2017.

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

    Meatiex

    Hey, I made a /set command, just like the WorldEdit one, but, without the annoying chat spam, but i'm stumped on how to code a /undo command with multiple undo(s)...

    I'm thinking I could make a Hashmap for each player, inside that, a HashMap for the undo #, then inside that a HashMap Location, and inside that a HashMap Matrial, and block damage(for colored wool)

    HashMap<Player, HashMap<Integer, HashMap<Location, HashMap<Material, Integer>

    but I feel like theirs a better way to do this, any ideas?

    Here's my /set command in case you were wondering:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] arg3) {
          
            if (sender instanceof Player) {
                Player player = (Player)sender;
              
                Location pos1 = clickEvent.pos1.get(player);
                Location pos2 = clickEvent.pos2.get(player);
    
                    Material block = player.getItemInHand().getType();
                    int damage = player.getItemInHand().getDurability();
                    fill(pos1, pos2, block, damage);
            }
            return true;
        }
      
        @SuppressWarnings("deprecation")
        public void fill(Location l1, Location l2, Material block, int damage) {
          
            if (l1.getX() > l2.getX()) {
                double temp = l1.getX();
                l1.setX(l2.getX());
                l2.setX(temp);
            }
          
            if (l1.getY() > l2.getY()) {
                double temp = l1.getY();
                l1.setY(l2.getY());
                l2.setY(temp);
            }
          
            if (l1.getZ() > l2.getZ()) {
                double temp = l1.getZ();
                l1.setZ(l2.getZ());
                l2.setZ(temp);
            }
          
            for(double x = l1.getX(); x <= l2.getX(); x++)
            for(double y = l1.getY(); y <= l2.getY(); y++)
            for(double z = l1.getZ(); z <= l2.getZ(); z++) {
                new Location(Bukkit.getWorld("plotworld"), x, y, z).getBlock().setType(block);
                new Location(Bukkit.getWorld("plotworld"), x, y, z).getBlock().setData((byte)damage);
            }
        }
     
  2. Offline

    Horsey

    Do not use public static on variables please, use getters and setters.

    Why are you using the type and the damage of the item in the player's hand rather than just using it as an ItemStack?

    Also, why aren't you making sure that the material is actually a block?

    To answer your question: Use a custom object in a HashMap!
     
  3. Offline

    Meatiex

    I'll do some research on getters / setters vs public static
    I'm using block and damage because you can't world.getBlock().setType(itemStack);
    Now checking if item in hand is a block, thanks!
    off to learn about Custom objects, once I learn, it looks simpler than my HashMap inception xD Thank you!
     
  4. Offline

    Horsey

    Public static is bad practice. Just don't use it.

    I didn't know there wasn't a setblock method though...
     
  5. Offline

    Meatiex

    This works for setting Material, or id, not itemStack :) world.getBlock().setType(Material, or id);

    I know this is sort of hypocritical, but I figured out a way to do this, probably not the best way, but it sure works!
    A ArrayList of Strings inside a HashMap of Players, where the string represent the location and block data needed for the undo!

    Thank you for you're recommendation, it helped me brainstorm to come up with this idea xD

    I didn't know using public static was bad, I will look in to that, thank you!

    Here is my code:
    Code:
    static HashMap<Player, ArrayList<String>> undo = new HashMap<Player, ArrayList<String>>();
    
    //Inserted in the code in my first post
            String temp = l1.getX() + "," + l1.getY() + "," + l1.getZ() + "," + l2.getX() + "," + l2.getY() + "," + l2.getZ();
        
            for(double x = l1.getX(); x <= l2.getX(); x++)
            for(double y = l1.getY(); y <= l2.getY(); y++)
            for(double z = l1.getZ(); z <= l2.getZ(); z++) {
                Block b = new Location(Bukkit.getWorld("plotworld"), x, y, z).getBlock();
                temp += "," + b.getTypeId() + ":" + b.getData();
                b.setType(block);
                b.setData((byte)damage);
            }
            if (!undo.containsKey(player)) undo.put(player, new ArrayList<String>());
            undo.get(player).add(temp);
    
    //
    
        @SuppressWarnings("deprecation")
        public void undo(Player player) {
            if (!setCommand.undo.containsKey(player)) return;
            ArrayList<String> list = setCommand.undo.get(player);
            if (list.size() != 0) {
            String data = list.get(list.size()-1);
            String[] args = data.split(",");
            Location l1 = new Location(Bukkit.getWorld("plotworld"), Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2]));
            Location l2 = new Location(Bukkit.getWorld("plotworld"), Double.parseDouble(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]));
           
            int i = 6;
           
            for(double x = l1.getX(); x <= l2.getX(); x++)
            for(double y = l1.getY(); y <= l2.getY(); y++)
            for(double z = l1.getZ(); z <= l2.getZ(); z++) {
           
                System.out.println("Setting Block: " + x + " " + y + " " + z + " To: " + args[i]);
               
                String[] a = args[i++].split(":");
                Material block = Material.getMaterial(Integer.parseInt(a[0]));
                int damage = Integer.parseInt(a[1]);
               
                Block b = new Location(Bukkit.getWorld("plotworld"), x, y, z).getBlock();
                b.setType(block);
                b.setData((byte)damage);
               
            }
           
            list.remove(list.size()-1);
            }
        }
    
     
    Last edited: Jul 8, 2017
  6. Offline

    Horsey

    That's a unique idea :) I never thought of it!
     
Thread Status:
Not open for further replies.

Share This Page