Custom Object Overwrites Itself

Discussion in 'Plugin Development' started by The_Roycester, May 15, 2019.

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

    The_Roycester

    Hi, I am currently making a plugin that takes blocks in a cuboid and finds their location and data. I then use that data in a method to test if that same sequence of blocks is built somewhere else in the world. (Kind of like a schematic type of thing) Anyway I got it to record the blocks data and that all works fine. However when I save multiple "schematics" which are the sequences of blocks, the 2nd one I make overwrites the 1st ones data. I was hoping someone could just look over my code and see if I'm just missing something obvious. Thank you in advance!

    Here are my classes and methods I think are the problem:
    Code:
        HashMap<String, ArrayList<blockinfo>> snbi = new HashMap<String, ArrayList<blockinfo>>();
        ArrayList<blockinfo> bi = new ArrayList<blockinfo>();
        static ArrayList<String> materials = new ArrayList<String>();
    
        ArrayList<String> materialsinorder = new ArrayList<String>();
        HashMap<String, ArrayList<String>> materialsused = new HashMap<String, ArrayList<String>>();
       
        public void getBlockInfo(Player player, String schematicname, Integer length, Integer width, Integer height, Integer posx, Integer posy, Integer posz) {
           
            player.sendMessage(posx.toString());
            player.sendMessage(posy.toString());
            player.sendMessage(posz.toString());
           
            Integer volume = length*width*height;
            Integer lengthcounter = 0;
            Integer widthcounter = 1;
            Integer heightcounter = 1;
            Integer posxc = posx;
            Integer posyc = posy;
            Integer poszc = posz;
            Integer blockx;
            bi.clear();
            materialsinorder.clear();
            materialsused.clear();
            for(Integer i = 1; i <= volume; i++) {
                if(lengthcounter < length) {
                    blockx = posxc + lengthcounter;
                    Location blockloc = new Location(Bukkit.getWorld("world"), blockx, posyc + (heightcounter - 1), poszc + (widthcounter - 1));
                    materials.add(blockloc.getBlock().getType().toString());
                    ArrayList<String> s = new ArrayList<String>();
                    s.add(blockloc.getBlock().getType().toString());
                    materialsused.put(schematicname, s);
                    /*if(!materialsused.get(schematicname).contains(blockloc.getBlock().getType().toString())) {
                        materialsused.get(schematicname).add(blockloc.getBlock().getType().toString());
                       
                        player.sendMessage("Added " + blockloc.getBlock().getType().toString());
                    }*/
                   
                    //player.sendMessage(blockloc.getBlock().getType().toString());
                    createBlockInfo(player, schematicname, i, blockloc.getBlock().getType().toString(), blockx - lengthcounter, posy - (heightcounter - 1), posz - (widthcounter - 1), lengthcounter, (heightcounter - 1), (widthcounter - 1));
                    blockinfo bl = snbi.get(schematicname).get(i - 1);
                    String material = bl.getMaterial();
                    materialsinorder.add(material);
                    for(blockinfo b2 : snbi.get(schematicname)) {
                        player.sendMessage(b2.getID());
                    }
                    lengthcounter++;
                }
                if((lengthcounter == length) && (widthcounter < width)) {
                    widthcounter++;
                    lengthcounter = 0;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter < height)) {
                    widthcounter = 1;
                    lengthcounter = 0;
                    heightcounter++;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter == height)) {
                    /*for(String mat : materialsinorder) {
                        System.out.print(mat);
                    }*/
                    player.sendMessage("schematiccreated");
                }
    
            }
    
           
            //mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(snbi.get(schematicname));
            mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsUsed(materialsused.get(schematicname));
            mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsInOrder(materialsinorder);
           
            /*for(blockinfo b2 : snbi.get(schematicname)) {
                player.sendMessage(b2.getID());
            }*/
        }
    
        public void createBlockInfo(Player player, String schematicname, Integer i, String material, Integer relativex, Integer relativey, Integer relativez, Integer x, Integer y, Integer z) {
            blockinfo b = new blockinfo(schematicname + "." + i, material, relativex, relativey, relativez, x, y, z);
            bi.add(b);
            mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(bi);
            snbi.put(schematicname, bi);
            /*blockinfo.createBlockID(schematicname + "." + i);
            blockinfo b = blockinfo.getBlockID(schematicname + "." + i);
            b.setMaterial(material);
            b.setRelativeX(relativex);
            b.setRelativeY(relativey);
            b.setRelativeZ(relativez);
            b.setX(x);
            b.setY(y);
            b.setZ(z);*/
        }
       
        public void listSchematics(Player player) {
            for(mobschematic m : mobschematicmanager.getManager().mobschematics) {
                player.sendMessage(m.getSchematicName());
                if(m.getBlockInfo() == null) {
                    player.sendMessage("blockinfo is null");
                }
                System.out.print("Materials in order that should be in the schematic.");
                ArrayList<blockinfo> b = m.getBlockInfo();
                for(blockinfo bi : b) {
                    System.out.print(bi.getMaterial().toString());
                }
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage("Must be a player.");
            }
            if(sender instanceof Player) {
                if(label.equalsIgnoreCase("createschematic")) {
                    mobschematicmanager.getManager().createSchematic(args[0]);
                    createSchematicLength(args[0], x1, x2);
                    createSchematicWidth(args[0], z1, z2);
                    createSchematicHeight(args[0], y1, y2);
                    setCornerX(x1, x2);
                    setCornerY(y1, y2);
                    setCornerZ(z1, z2);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicX(bottomleftx);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicY(bottomy);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicZ(bottomfrontz);
                    sender.sendMessage(mobschematicmanager.getManager().getMobSchematic(args[0]).getSchematicX().toString());
                    bi.clear();
                    getBlockInfo(((Player) sender).getPlayer(), args[0], length, width, height, bottomleftx, bottomy, bottomfrontz);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setMaterials(materials);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setBlockInfo(bi);
                    sender.sendMessage("Createdschematic!");
                }
                if(label.equalsIgnoreCase("listschematics")) {
                    listSchematics((Player) sender);
                }
            }
            return false;
        }
    }
    If you need anything else just let me know!
     
  2. From the given code I'm not able to figure out the problem. Anyways I guess the overwriting takes place in the HashMap snbi. Since you only insert something into snbi once, in the method createBlockInfo, you probably give the schematic somehow the same name. Try debugging at this point.
     
  3. Offline

    The_Roycester

    So I attempted to debug it with the sbni hashmap and here are my changes, but the bug still remains with the object overwriting the other objects.

    Changes to previous code:
    Code:
    package me.akroyce25.mobshematics.read_write;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import me.akroyce25.mobshematics.blockinfo;
    import me.akroyce25.mobshematics.main;
    import me.akroyce25.mobshematics.mobschematic;
    import me.akroyce25.mobshematics.mobschematicmanager;
    
    public class write implements Listener, CommandExecutor{
       
        private main plugin;
       
        public write(main plugin) {
            this.plugin = plugin;
        }
      
        public main getPlugin() {
            return plugin;
        }
       
        static Integer x1;
        static Integer y1;
        static Integer z1;
        static Integer x2;
        static Integer y2;
        static Integer z2;
       
        public Integer length = 0;
        public Integer width = 0;
        public Integer height = 0;
       
        public Integer bottomleftx = 0;
        public Integer bottomy = 0;
        public Integer bottomfrontz = 0;
       
        public Integer upperrightx = 0;
        public Integer uppery = 0;
        public Integer upperbackz = 0;
       
        ArrayList<blockinfo> bi = new ArrayList<blockinfo>();
        static ArrayList<String> materials = new ArrayList<String>();
       
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                x1 = e.getClickedBlock().getX();
                y1 = e.getClickedBlock().getY();
                z1 = e.getClickedBlock().getZ();
                //e.getPlayer().sendMessage(x1.toString());
    
            }
            if(e.getAction() == Action.LEFT_CLICK_BLOCK) {
                x2 = e.getClickedBlock().getX();
                y2 = e.getClickedBlock().getY();
                z2 = e.getClickedBlock().getZ();
                //e.getPlayer().sendMessage(x2.toString());
            }
        }
       
        public void createSchematicLength(String schematicname, Integer pos1x, Integer pos2x) {
            if(pos1x == pos2x) {
                length = 1;
            }
            if(pos1x != pos2x) {
                if((pos1x >= 0) && (pos2x >= 0)) {
                    if(pos1x > pos2x) {
                        length = (pos1x - pos2x) + 1;
                    }
                    if(pos2x > pos1x) {
                        length = (pos2x - pos1x) + 1;
                    }
                }
               
                if((pos1x >= 0) && (pos2x < 0)) {
                    if(pos1x > Math.abs(pos2x)) {
                        length = (pos1x - Math.abs(pos2x)) + 1;
                    }
                    if(pos1x < Math.abs(pos2x)) {
                        length = (Math.abs(pos2x) - pos1x) + 1;
                    }
                }
               
                if((pos1x < 0) && (pos2x >= 0)) {
                    if(Math.abs(pos1x) > pos2x) {
                        length = (Math.abs(pos1x) - pos2x) + 1;
                    }
                    if(pos2x > Math.abs(pos1x)) {
                        length = (pos2x - Math.abs(pos1x)) + 1;
                    }
                }
               
                if((pos1x < 0) && (pos2x < 0)) {
                    if(Math.abs(pos1x) > Math.abs(pos2x)) {
                         length = (Math.abs(pos1x) - Math.abs(pos2x)) + 1;
                    }
                    if(Math.abs(pos2x) > Math.abs(pos1x)) {
                        length = (Math.abs(pos2x) - Math.abs(pos1x)) + 1;
                    }
                }
            }
    
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicLength(length);;
        }
    
        public void createSchematicWidth(String schematicname, Integer pos1z, Integer pos2z) {
            if(pos1z == pos2z) {
                width = 1;
            }
            if(pos1z != pos2z) {
                if((pos1z >= 0) && (pos2z >= 0)) {
                    if(pos1z > pos2z) {
                        width = (pos1z - pos2z) + 1;
                    }
                    if(pos2z > pos1z) {
                        width = (pos2z - pos1z) + 1;
                    }
                }
               
                if((pos1z >= 0) && (pos2z < 0)) {
                    if(pos1z > Math.abs(pos2z)) {
                        width = (pos1z - Math.abs(pos2z)) + 1;
                    }
                    if(pos1z < Math.abs(pos2z)) {
                        width = (Math.abs(pos2z) - pos1z) + 1;
                    }
                }
               
                if((pos1z < 0) && (pos2z >= 0)) {
                    if(Math.abs(pos1z) > pos2z) {
                        width = (Math.abs(pos1z) - pos2z) + 1;
                    }
                    if(pos2z > Math.abs(pos1z)) {
                        width = (pos2z - Math.abs(pos1z)) + 1;
                    }
                }
               
                if((pos1z < 0) && (pos2z < 0)) {
                    if(Math.abs(pos1z) > Math.abs(pos2z)) {
                         width = (Math.abs(pos1z) - Math.abs(pos2z)) + 1;
                    }
                    if(Math.abs(pos2z) > Math.abs(pos1z)) {
                        width = (Math.abs(pos2z) - Math.abs(pos1z)) + 1;
                    }
                }
            }
           
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicWidth(width);;
        }
    
        public void createSchematicHeight(String schematicname, Integer pos1y, Integer pos2y) {
            if(pos1y == pos2y) {
                height = 1;
            }
            if(pos1y != pos2y) {
                if((pos1y >= 0) && (pos2y >= 0)) {
                    if(pos1y > pos2y) {
                        height = (pos1y - pos2y) + 1;
                    }
                    if(pos2y > pos1y) {
                        height = (pos2y - pos1y) + 1;
                    }
                }
               
                if((pos1y >= 0) && (pos2y < 0)) {
                    if(pos1y > Math.abs(pos2y)) {
                        height = (pos1y - Math.abs(pos2y)) + 1;
                    }
                    if(pos1y < Math.abs(pos2y)) {
                        height = (Math.abs(pos2y) - pos1y) + 1;
                    }
                }
               
                if((pos1y < 0) && (pos2y >= 0)) {
                    if(Math.abs(pos1y) > pos2y) {
                        height = (Math.abs(pos1y) - pos2y) + 1;
                    }
                    if(pos2y > Math.abs(pos1y)) {
                        height = (pos2y - Math.abs(pos1y)) + 1;
                    }
                }
               
                if((pos1y < 0) && (pos2y < 0)) {
                    if(Math.abs(pos1y) > Math.abs(pos2y)) {
                         height = (Math.abs(pos1y) - Math.abs(pos2y)) + 1;
                    }
                    if(Math.abs(pos2y) > Math.abs(pos1y)) {
                        height = (Math.abs(pos2y) - Math.abs(pos1y)) + 1;
                    }
                }
            }
           
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicHeight(height);;
        }
       
        public void setCornerX(Integer pos1x, Integer pos2x) {
            if(pos1x == pos2x) {
                bottomleftx = pos1x;
                upperrightx = pos2x;
            }
            if(pos1x != pos2x) {
                if((pos1x >= 0) && (pos2x >= 0)) {
                    if(pos1x > pos2x) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(pos2x > pos1x) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
               
                if((pos1x >= 0) && (pos2x < 0)) {
                    if(pos1x > Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(pos1x < Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                }
               
                if((pos1x < 0) && (pos2x >= 0)) {
                    if(Math.abs(pos1x) > pos2x) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                    if(pos2x > Math.abs(pos1x)) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
               
                if((pos1x < 0) && (pos2x < 0)) {
                    if(Math.abs(pos1x) > Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(Math.abs(pos2x) > Math.abs(pos1x)) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
            }
    
        }
       
        public void setCornerY(Integer pos1y, Integer pos2y) {
            if(pos1y == pos2y) {
                bottomy = pos1y;
                uppery = pos2y;
            }
            if(pos1y != pos2y) {
                if((pos1y >= 0) && (pos2y >= 0)) {
                    if(pos1y > pos2y) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(pos2y > pos1y) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
               
                if((pos1y >= 0) && (pos2y < 0)) {
                    if(pos1y > Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(pos1y < Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                }
               
                if((pos1y < 0) && (pos2y >= 0)) {
                    if(Math.abs(pos1y) > pos2y) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                    if(pos2y > Math.abs(pos1y)) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
               
                if((pos1y < 0) && (pos2y < 0)) {
                    if(Math.abs(pos1y) > Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(Math.abs(pos2y) > Math.abs(pos1y)) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
            }
    
        }
       
        public void setCornerZ(Integer pos1z, Integer pos2z) {
            if(pos1z == pos2z) {
                bottomfrontz = pos1z;
                upperbackz = pos2z;
            }
            if(pos1z != pos2z) {
                if((pos1z >= 0) && (pos2z >= 0)) {
                    if(pos1z > pos2z) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(pos2z > pos1z) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
               
                if((pos1z >= 0) && (pos2z < 0)) {
                    if(pos1z > Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(pos1z < Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                }
               
                if((pos1z < 0) && (pos2z >= 0)) {
                    if(Math.abs(pos1z) > pos2z) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                    if(pos2z > Math.abs(pos1z)) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
               
                if((pos1z < 0) && (pos2z < 0)) {
                    if(Math.abs(pos1z) > Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(Math.abs(pos2z) > Math.abs(pos1z)) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
            }
    
        }
       
        ArrayList<String> materialsinorder = new ArrayList<String>();
        HashMap<String, ArrayList<String>> materialsused = new HashMap<String, ArrayList<String>>();
       
        public void getBlockInfo(Player player, String schematicname, Integer length, Integer width, Integer height, Integer posx, Integer posy, Integer posz) {
           
            player.sendMessage(posx.toString());
            player.sendMessage(posy.toString());
            player.sendMessage(posz.toString());
           
            Integer volume = length*width*height;
            Integer lengthcounter = 0;
            Integer widthcounter = 1;
            Integer heightcounter = 1;
            Integer posxc = posx;
            Integer posyc = posy;
            Integer poszc = posz;
            Integer blockx;
            bi.clear();
            materialsinorder.clear();
            materialsused.clear();
            for(Integer i = 1; i <= volume; i++) {
                if(lengthcounter < length) {
                    blockx = posxc + lengthcounter;
                    Location blockloc = new Location(Bukkit.getWorld("world"), blockx, posyc + (heightcounter - 1), poszc + (widthcounter - 1));
                    materials.add(blockloc.getBlock().getType().toString());
                    ArrayList<String> s = new ArrayList<String>();
                    s.add(blockloc.getBlock().getType().toString());
                    materialsused.put(schematicname, s);
                    /*if(!materialsused.get(schematicname).contains(blockloc.getBlock().getType().toString())) {
                        materialsused.get(schematicname).add(blockloc.getBlock().getType().toString());
                       
                        player.sendMessage("Added " + blockloc.getBlock().getType().toString());
                    }*/
                   
                    //player.sendMessage(blockloc.getBlock().getType().toString());
                    createBlockInfo(player, schematicname, i, blockloc.getBlock().getType().toString(), blockx - lengthcounter, posy - (heightcounter - 1), posz - (widthcounter - 1), lengthcounter, (heightcounter - 1), (widthcounter - 1));
                    lengthcounter++;
                }
                if((lengthcounter == length) && (widthcounter < width)) {
                    widthcounter++;
                    lengthcounter = 0;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter < height)) {
                    widthcounter = 1;
                    lengthcounter = 0;
                    heightcounter++;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter == height)) {
                    /*for(String mat : materialsinorder) {
                        System.out.print(mat);
                    }*/
                    player.sendMessage("schematiccreated");
                }
    
            }
    
           
            //mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(snbi.get(schematicname));
            mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsUsed(materialsused.get(schematicname));
           
            /*for(blockinfo b2 : snbi.get(schematicname)) {
                player.sendMessage(b2.getID());
            }*/
        }
    
        public void createBlockInfo(Player player, String schematicname, Integer i, String material, Integer relativex, Integer relativey, Integer relativez, Integer x, Integer y, Integer z) {
            blockinfo b = new blockinfo(schematicname + "." + i, material, relativex, relativey, relativez, x, y, z);
            bi.add(b);
            mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(bi);
            materialsinorder.add(material);
            mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsInOrder(materialsinorder);
        }
       
        public void listSchematics(Player player) {
            for(mobschematic m : mobschematicmanager.getManager().mobschematics) {
                player.sendMessage(m.getSchematicName());
                if(m.getBlockInfo() == null) {
                    player.sendMessage("blockinfo is null");
                }
                System.out.print("Materials in order that should be in the schematic.");
                ArrayList<blockinfo> b = m.getBlockInfo();
                for(blockinfo bi : b) {
                    System.out.print(bi.getMaterial().toString());
                }
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage("Must be a player.");
            }
            if(sender instanceof Player) {
                if(label.equalsIgnoreCase("createschematic")) {
                    mobschematicmanager.getManager().createSchematic(args[0]);
                    createSchematicLength(args[0], x1, x2);
                    createSchematicWidth(args[0], z1, z2);
                    createSchematicHeight(args[0], y1, y2);
                    setCornerX(x1, x2);
                    setCornerY(y1, y2);
                    setCornerZ(z1, z2);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicX(bottomleftx);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicY(bottomy);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicZ(bottomfrontz);
                    sender.sendMessage(mobschematicmanager.getManager().getMobSchematic(args[0]).getSchematicX().toString());
                    bi.clear();
                    getBlockInfo(((Player) sender).getPlayer(), args[0], length, width, height, bottomleftx, bottomy, bottomfrontz);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setMaterials(materials);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setBlockInfo(bi);
                    sender.sendMessage("Createdschematic!");
                }
                if(label.equalsIgnoreCase("listschematics")) {
                    listSchematics((Player) sender);
                }
            }
            return false;
        }
    }
     
  4. Which object overwrites the others?
    I thought your problem is the sbni HashMap but I realized that you commented it in the first post. So please explain again where something gets overwritten
     
    Last edited: May 17, 2019
  5. Offline

    The_Roycester

    Ok so when I use the “createschematic” command for the first time it works perfectly. It creates my schematic object which contains an ArrayList of blockinfo schematics. When I run the “listschematics” command it prints the correct materials in the order that it is supposed to be in for that specific schematic object. Now when I go to run the “createschematic” command a second time it works perfect creating the schematic object and the blockinfo object ArrayList, however, it takes the blockinfo object ArrayList and applies it to both schematic objects. Therefore it overwrites the materials in order for the first schematic but the second schematic is perfect. This is apparent when I run the “listschematics” command for a second time and it prints out each schematics materials in order which are identical.

    I just can’t figure out where the code overwrites itself. I’m thinking it has to be in a for loop because it’s cycling through each schematic or something but I’m not positive.
     
  6. Okay, so I couldn't really figure out what the issue is, I barely understand your code. In my opinion it's really weird that you work with the schematic names every time and calculate everything in this one class instead of creating and working with an e.g. Schematic Object instead. The only thing that seems to be a lot more confusing than the other parts of the code is in getBlockInfo: In the loop which goes over the whole volume, you create an ArrayList every run, you add exactly one string to this list and put it into the 'materialsused' HashMap, each time with the same key (the schematic name). I'm not sure if later in the loop this is important, otherwise this is the only thing that in my opinion seems to be kind of incorrect.
    If not I unfortunately can't help you along.
     
  7. Offline

    The_Roycester

    Ok thank you DerDonut for your help. Unfortunately, I have tried everything you told me and have been attempting to debug it all today and I still cannot figure it out. If anyone else could help that would be greatly appreciated. The problem is listed in the previous parts of the thread. Here are my classes with code changed and some comments added to make it a little bit easier to understand.

    Code:
        static Integer x1;
        static Integer y1;
        static Integer z1;
        static Integer x2;
        static Integer y2;
        static Integer z2;
       
        public Integer length = 0;
        public Integer width = 0;
        public Integer height = 0;
       
        public Integer bottomleftx = 0;
        public Integer bottomy = 0;
        public Integer bottomfrontz = 0;
       
        public Integer upperrightx = 0;
        public Integer uppery = 0;
        public Integer upperbackz = 0;
       
        ArrayList<blockinfo> bi = new ArrayList<blockinfo>();
        ArrayList<String> materials = new ArrayList<String>();
        ArrayList<String> materialsinorder = new ArrayList<String>();
       
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                x1 = e.getClickedBlock().getX();
                y1 = e.getClickedBlock().getY();
                z1 = e.getClickedBlock().getZ();
                //e.getPlayer().sendMessage(x1.toString());
    
            }
            if(e.getAction() == Action.LEFT_CLICK_BLOCK) {
                x2 = e.getClickedBlock().getX();
                y2 = e.getClickedBlock().getY();
                z2 = e.getClickedBlock().getZ();
                //e.getPlayer().sendMessage(x2.toString());
            }
        }
       
        public void createSchematicLength(String schematicname, Integer pos1x, Integer pos2x) {
            if(pos1x == pos2x) {
                length = 1;
            }
            if(pos1x != pos2x) {
                if((pos1x >= 0) && (pos2x >= 0)) {
                    if(pos1x > pos2x) {
                        length = (pos1x - pos2x) + 1;
                    }
                    if(pos2x > pos1x) {
                        length = (pos2x - pos1x) + 1;
                    }
                }
               
                if((pos1x >= 0) && (pos2x < 0)) {
                    if(pos1x > Math.abs(pos2x)) {
                        length = (pos1x - Math.abs(pos2x)) + 1;
                    }
                    if(pos1x < Math.abs(pos2x)) {
                        length = (Math.abs(pos2x) - pos1x) + 1;
                    }
                }
               
                if((pos1x < 0) && (pos2x >= 0)) {
                    if(Math.abs(pos1x) > pos2x) {
                        length = (Math.abs(pos1x) - pos2x) + 1;
                    }
                    if(pos2x > Math.abs(pos1x)) {
                        length = (pos2x - Math.abs(pos1x)) + 1;
                    }
                }
               
                if((pos1x < 0) && (pos2x < 0)) {
                    if(Math.abs(pos1x) > Math.abs(pos2x)) {
                         length = (Math.abs(pos1x) - Math.abs(pos2x)) + 1;
                    }
                    if(Math.abs(pos2x) > Math.abs(pos1x)) {
                        length = (Math.abs(pos2x) - Math.abs(pos1x)) + 1;
                    }
                }
            }
    
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicLength(length);;
        }
    
        public void createSchematicWidth(String schematicname, Integer pos1z, Integer pos2z) {
            if(pos1z == pos2z) {
                width = 1;
            }
            if(pos1z != pos2z) {
                if((pos1z >= 0) && (pos2z >= 0)) {
                    if(pos1z > pos2z) {
                        width = (pos1z - pos2z) + 1;
                    }
                    if(pos2z > pos1z) {
                        width = (pos2z - pos1z) + 1;
                    }
                }
               
                if((pos1z >= 0) && (pos2z < 0)) {
                    if(pos1z > Math.abs(pos2z)) {
                        width = (pos1z - Math.abs(pos2z)) + 1;
                    }
                    if(pos1z < Math.abs(pos2z)) {
                        width = (Math.abs(pos2z) - pos1z) + 1;
                    }
                }
               
                if((pos1z < 0) && (pos2z >= 0)) {
                    if(Math.abs(pos1z) > pos2z) {
                        width = (Math.abs(pos1z) - pos2z) + 1;
                    }
                    if(pos2z > Math.abs(pos1z)) {
                        width = (pos2z - Math.abs(pos1z)) + 1;
                    }
                }
               
                if((pos1z < 0) && (pos2z < 0)) {
                    if(Math.abs(pos1z) > Math.abs(pos2z)) {
                         width = (Math.abs(pos1z) - Math.abs(pos2z)) + 1;
                    }
                    if(Math.abs(pos2z) > Math.abs(pos1z)) {
                        width = (Math.abs(pos2z) - Math.abs(pos1z)) + 1;
                    }
                }
            }
           
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicWidth(width);;
        }
    
        public void createSchematicHeight(String schematicname, Integer pos1y, Integer pos2y) {
            if(pos1y == pos2y) {
                height = 1;
            }
            if(pos1y != pos2y) {
                if((pos1y >= 0) && (pos2y >= 0)) {
                    if(pos1y > pos2y) {
                        height = (pos1y - pos2y) + 1;
                    }
                    if(pos2y > pos1y) {
                        height = (pos2y - pos1y) + 1;
                    }
                }
               
                if((pos1y >= 0) && (pos2y < 0)) {
                    if(pos1y > Math.abs(pos2y)) {
                        height = (pos1y - Math.abs(pos2y)) + 1;
                    }
                    if(pos1y < Math.abs(pos2y)) {
                        height = (Math.abs(pos2y) - pos1y) + 1;
                    }
                }
               
                if((pos1y < 0) && (pos2y >= 0)) {
                    if(Math.abs(pos1y) > pos2y) {
                        height = (Math.abs(pos1y) - pos2y) + 1;
                    }
                    if(pos2y > Math.abs(pos1y)) {
                        height = (pos2y - Math.abs(pos1y)) + 1;
                    }
                }
               
                if((pos1y < 0) && (pos2y < 0)) {
                    if(Math.abs(pos1y) > Math.abs(pos2y)) {
                         height = (Math.abs(pos1y) - Math.abs(pos2y)) + 1;
                    }
                    if(Math.abs(pos2y) > Math.abs(pos1y)) {
                        height = (Math.abs(pos2y) - Math.abs(pos1y)) + 1;
                    }
                }
            }
           
            mobschematicmanager.getManager().getMobSchematic(schematicname).setSchematicHeight(height);;
        }
       
        public void setCornerX(Integer pos1x, Integer pos2x) {
            if(pos1x == pos2x) {
                bottomleftx = pos1x;
                upperrightx = pos2x;
            }
            if(pos1x != pos2x) {
                if((pos1x >= 0) && (pos2x >= 0)) {
                    if(pos1x > pos2x) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(pos2x > pos1x) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
               
                if((pos1x >= 0) && (pos2x < 0)) {
                    if(pos1x > Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(pos1x < Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                }
               
                if((pos1x < 0) && (pos2x >= 0)) {
                    if(Math.abs(pos1x) > pos2x) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                    if(pos2x > Math.abs(pos1x)) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
               
                if((pos1x < 0) && (pos2x < 0)) {
                    if(Math.abs(pos1x) > Math.abs(pos2x)) {
                        bottomleftx = pos2x;
                        upperrightx = pos1x;
                    }
                    if(Math.abs(pos2x) > Math.abs(pos1x)) {
                        bottomleftx = pos1x;
                        upperrightx = pos2x;
                    }
                }
            }
    
        }
       
        public void setCornerY(Integer pos1y, Integer pos2y) {
            if(pos1y == pos2y) {
                bottomy = pos1y;
                uppery = pos2y;
            }
            if(pos1y != pos2y) {
                if((pos1y >= 0) && (pos2y >= 0)) {
                    if(pos1y > pos2y) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(pos2y > pos1y) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
               
                if((pos1y >= 0) && (pos2y < 0)) {
                    if(pos1y > Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(pos1y < Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                }
               
                if((pos1y < 0) && (pos2y >= 0)) {
                    if(Math.abs(pos1y) > pos2y) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                    if(pos2y > Math.abs(pos1y)) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
               
                if((pos1y < 0) && (pos2y < 0)) {
                    if(Math.abs(pos1y) > Math.abs(pos2y)) {
                        bottomy = pos2y;
                        uppery = pos1y;
                    }
                    if(Math.abs(pos2y) > Math.abs(pos1y)) {
                        bottomy = pos1y;
                        uppery = pos2y;
                    }
                }
            }
    
        }
       
        public void setCornerZ(Integer pos1z, Integer pos2z) {
            if(pos1z == pos2z) {
                bottomfrontz = pos1z;
                upperbackz = pos2z;
            }
            if(pos1z != pos2z) {
                if((pos1z >= 0) && (pos2z >= 0)) {
                    if(pos1z > pos2z) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(pos2z > pos1z) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
               
                if((pos1z >= 0) && (pos2z < 0)) {
                    if(pos1z > Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(pos1z < Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                }
               
                if((pos1z < 0) && (pos2z >= 0)) {
                    if(Math.abs(pos1z) > pos2z) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                    if(pos2z > Math.abs(pos1z)) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
               
                if((pos1z < 0) && (pos2z < 0)) {
                    if(Math.abs(pos1z) > Math.abs(pos2z)) {
                        bottomfrontz = pos2z;
                        upperbackz = pos1z;
                    }
                    if(Math.abs(pos2z) > Math.abs(pos1z)) {
                        bottomfrontz = pos1z;
                        upperbackz = pos2z;
                    }
                }
            }
    
        }
    
       
        public void getBlockInfo(Player player, String schematicname, Integer length, Integer width, Integer height, Integer posx, Integer posy, Integer posz) {
           
            player.sendMessage(posx.toString());
            player.sendMessage(posy.toString());
            player.sendMessage(posz.toString());
           
            Integer volume = length*width*height;
            Integer lengthcounter = 0;
            Integer widthcounter = 1;
            Integer heightcounter = 1;
            Integer posxc = posx;
            Integer posyc = posy;
            Integer poszc = posz;
            Integer blockx;
           
            bi.clear();
            materialsinorder.clear();
            materials.clear();
           
            for(Integer i = 1; i <= volume; i++) {
                if(lengthcounter < length) {
                    blockx = posxc + lengthcounter;
                    Location blockloc = new Location(Bukkit.getWorld("world"), blockx, posyc + (heightcounter - 1), poszc + (widthcounter - 1));
                   
                    //Get the block material at blockloc.
                    String blockmaterial = blockloc.getBlock().getBlockData().getMaterial().toString();
                   
                    //Check if the schematic has that material in the materials arraylist.
                    if(mobschematicmanager.getManager().getMobSchematic(schematicname).getMaterials() == null) {
                        materials.add(blockmaterial);
                        mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterials(materials);
                    }
                    if(mobschematicmanager.getManager().getMobSchematic(schematicname).getMaterials().contains(blockmaterial)) {
                        System.out.println(schematicname + " already has that material.");
                    }
                    if(!mobschematicmanager.getManager().getMobSchematic(schematicname).getMaterials().contains(blockmaterial)) {
                        materials.add(blockmaterial);
                        mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterials(materials);
                    }
                   
                    //At the material to a materials in order list.
                    materialsinorder.add(blockmaterial);
                   
                    //Set blockinfo object to ArrayList and then to mobschematic object.
                    mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(bi);
                   
                    //Create a block info object for every single block in the cuboid.
                    createBlockInfo(player, schematicname, i, blockloc.getBlock().getType().toString(), blockx - lengthcounter, posy - (heightcounter - 1), posz - (widthcounter - 1), lengthcounter, (heightcounter - 1), (widthcounter - 1));
                    lengthcounter++;
                }
                if((lengthcounter == length) && (widthcounter < width)) {
                    widthcounter++;
                    lengthcounter = 0;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter < height)) {
                    widthcounter = 1;
                    lengthcounter = 0;
                    heightcounter++;
                }
                if((lengthcounter == length) && (widthcounter == width) && (heightcounter == height)) {
                    player.sendMessage("schematiccreated");
                    mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterials(materials);
                    mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsInOrder(materialsinorder);
                    mobschematicmanager.getManager().getMobSchematic(schematicname).setBlockInfo(bi);
                }
    
            }
    
        }
    
        public void createBlockInfo(Player player, String schematicname, Integer i, String material, Integer relativex, Integer relativey, Integer relativez, Integer x, Integer y, Integer z) {
           
            blockinfo b = new blockinfo(schematicname + "." + i, material, relativex, relativey, relativez, x, y, z);
           
            System.out.print(schematicname);
            bi.add(b);
            materialsinorder.add(material);
            mobschematicmanager.getManager().getMobSchematic(schematicname).setMaterialsInOrder(materialsinorder);
        }
       
        public void listSchematics(Player player) {
            for(mobschematic m : mobschematicmanager.getManager().mobschematics) {
                System.out.print(m.getSchematicName());
                if(m.getBlockInfo() == null) {
                    player.sendMessage("blockinfo is null");
                }
                System.out.print("Materials in order that should be in the schematic.");
                ArrayList<blockinfo> b = m.getBlockInfo();
                for(blockinfo bi : b) {
                    System.out.print(bi.getMaterial().toString());
                }
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage("Must be a player.");
            }
            if(sender instanceof Player) {
                if(label.equalsIgnoreCase("createschematic")) {
                   
                    mobschematicmanager.getManager().createSchematic(args[0]);
                   
                    createSchematicLength(args[0], x1, x2);
                    createSchematicWidth(args[0], z1, z2);
                    createSchematicHeight(args[0], y1, y2);
                    setCornerX(x1, x2);
                    setCornerY(y1, y2);
                    setCornerZ(z1, z2);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicX(bottomleftx);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicY(bottomy);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setSchematicZ(bottomfrontz);
    
                    sender.sendMessage(mobschematicmanager.getManager().getMobSchematic(args[0]).getSchematicX().toString());
                   
                    getBlockInfo(((Player) sender).getPlayer(), args[0], length, width, height, bottomleftx, bottomy, bottomfrontz);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setMaterials(materials);
                    mobschematicmanager.getManager().getMobSchematic(args[0]).setBlockInfo(bi);
                    sender.sendMessage("Createdschematic!");
                }
                if(label.equalsIgnoreCase("listschematics")) {
                    listSchematics((Player) sender);
                }
            }
            return false;
        }
    
     
  8. Offline

    The_Roycester

    Just in case anyone else runs into this same situation, the only solution I have found to my problem was to save my objects to a yml file.
     
Thread Status:
Not open for further replies.

Share This Page