Solved is this a problem in my plugin?

Discussion in 'Plugin Development' started by xize, Jun 7, 2014.

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

    xize

    Hello,

    so ive made a schematic builder based on schedulers.
    but however it seems that it just skips specific blocks such as block of quart.

    my scheduler is:

    Code:
    public class SchematicBuilder {
        private final Schematic schematic;
        private final Location loc;
        private final Location playerLoc;
        private final String player;
        private boolean warning = false;
        private LinkedHashMap<Block, Integer> data = new LinkedHashMap<Block, Integer>();
        public SchematicBuilder(Schematic schematic, Location base, String player) {
            this.schematic = schematic;
            this.loc = base;
            this.playerLoc = loc.clone();
            this.loc.setX(loc.getX()-(schematic.getWidth()/2));
            this.loc.setZ(loc.getZ()-(schematic.getLength()/2));
            this.player = player;
        }
        /**
        * @author xize
        * @param returns the schematic
        * @return Schematic
        */
        public Schematic getSchematic() {
            return schematic;
        }
        /**
        * @author xize
        * @param slowly generates the schematic from bottom to highest.
        */
        public void startGeneration(final EntityType type) {
            new BukkitRunnable() {
                private Iterator<Entry<Block, Integer>> it;
                private LivingEntity entity;
                public void setData() {
                    for (int y = 0; y < schematic.getHeight(); y++){
                        for(int x = 0; x < schematic.getWidth(); x++){
                            for (int z = 0; z < schematic.getLength(); ++z){
                               
                                Location temp = loc.clone().add(x, y, z);
                               
                                //Location temp = loc.clone().add(x/2, y, z/2); <- pastes the schematic but 1/² smaller this is per accident but pretty cool.
                                //Location loc = new Location(temp.getWorld(), temp.getBlockX()/2, temp.getBlockY(), temp.getBlockZ()/2);
                                Block block = temp.getBlock();
                                int index = y * schematic.getWidth() * schematic.getLength() + z * schematic.getWidth() + x;
                                //if(getMaterial(schematic.getBlocks()[index]) != Material.AIR) {
                                    data.put(block, index);   
                                //}
                            }
                        }
                    }
                }
                public void setupBuilderEntity() {
                        if(this.entity instanceof LivingEntity) {
                            return;
                        } else {
                            this.entity = (LivingEntity) playerLoc.getWorld().spawnEntity(playerLoc, type);
                            this.entity.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 1));
                            this.entity.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Integer.MAX_VALUE, 1));
                            this.entity.setCustomName(ChatColor.GOLD + "[ManCo]"+ChatColor.WHITE + "builder!");
                            this.entity.setCustomNameVisible(true);
                        }
                }
                @SuppressWarnings("deprecation")
                @Override
                public void run() {
                    if(data.isEmpty()) {
                        setData();
                    }
                    setupBuilderEntity();
                    if(!(it instanceof Iterator)) {
                        this.it = data.entrySet().iterator();
                    }
                    if(it instanceof Iterator) {
                        if(it.hasNext()) {
                            Entry<Block, Integer> entry = it.next();
                            Block block = entry.getKey();
                            int index = entry.getValue();
                            Material dataValue = getMaterial(schematic.getBlocks()[index]);
                            byte subValue = schematic.getData()[index];
                            if(entity instanceof Enderman) {
                                Enderman enderman = (Enderman) entity;
                                enderman.setCarriedMaterial(new MaterialData(dataValue, subValue));
                            }
                            if(block.getType() == Material.AIR) {
                                try {
                                    if(!block.getChunk().isLoaded()) {
                                        block.getChunk().load();
                                    }
                                    block.setTypeIdAndData(dataValue.getId(), subValue, true);
                                    saveRollback(block);
                                    block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, dataValue);
                                    entity.teleport(block.getRelative(BlockFace.UP).getRelative(BlockFace.UP).getLocation());
                                    entity.setHealth(entity.getMaxHealth());
                                }catch(NullPointerException e) {e.printStackTrace();}
                            }
                            it.remove();
                            data.remove(block);
                        } else {
                            entity.remove();
                            data.clear();
                            cancel();
                        }
                    }
                }
            }.runTaskTimer(ManCo.getPlugin(), 0L, 1L);
        }
        @SuppressWarnings("deprecation")
        public Material getMaterial(int id) {
            try {
                Material mat = Material.getMaterial(id);
                return mat;
            } catch(NullPointerException e) {
                e.printStackTrace();
                if(!warning) {
                    this.warning = true;
                    ManCo.log(LogType.SEVERE, "warning the schematic " + schematic.getName() + " has invalid blocks maybe they belong to modpacks or minecraft snapshots?");
                    ManCo.log(LogType.SEVERE, "changing block with id: "+id+" into air.");
                }
                return Material.AIR;
            }
        }
        @SuppressWarnings("deprecation")
        private void saveRollback(Block block) {
            if(Hook.isCoreProtectEnabled()) {
                CoreProtectHook.log(player, block);
            } else if(Hook.isLogBlockEnabled()) {
                LogBlockHook.log(player, block);
            } else if(Hook.isPrismEnabled()) {
                PrismHook.log(player, block);
            }
        }
    }
    
    and my schematic utils which aren't fully coded by me credits to chasechocolate though:

    Schematic.java
    Code:
    public class Schematic {
       
            private short width;
            private short height;
            private short length;
           
            private String name;
            private String materials;
           
            private byte[] blocks;
            private byte[] data;
           
            public Schematic(String name, short width, short height, short length, String materials, byte[] blocks, byte[] data){
                super();
                this.name = name;
                this.width = width;
                this.height = height;
                this.length = length;
                this.materials = materials;
                this.blocks = blocks;
                this.data = data;
            }
           
            public String getName(){
                return this.name;
            }
           
            public short getWidth(){
                return this.width;
            }
           
            public void setWidth(short width){
                this.width = width;
            }
           
            public short getHeight(){
                return this.height;
            }
           
            public void setHeight(short height){
                this.height = height;
            }
           
            public short getLength(){
                return this.length;
            }
           
            public void setLength(short length){
                this.length = length;
            }
           
            public String getMaterials(){
                return this.materials;
            }
           
            public void setMaterials(String materials){
                this.materials = materials;
            }
           
            public byte[] getBlocks(){
                return this.blocks;
            }
           
            public void setBlocks(byte[] blocks){
                this.blocks = blocks;
            }
           
            public byte[] getData(){
                return this.data;
            }
           
            public void setData(byte[] data){
                this.data = data;
            }
    }
    
    and SchematicUtils.java:
    Code:
    /**
    *
    * @author chasechocolate
    * @param found this inside his github, but I have slightly modified.
    *
    */
    public class SchematicUtils {
        private static File baseSchematicsFile = new File(ManCo.getPlugin().getDataFolder() + File.separator + "schematics");
        private static List<Schematic> allSchematics = new ArrayList<Schematic>();
        public static void initSchematics(){
            allSchematics.clear();
            for(File schematicFile : baseSchematicsFile.listFiles()){
                if(!(schematicFile.getName().startsWith("."))){
                    Schematic schematic = loadSchematic(schematicFile);
                    if(schematic != null){
                        allSchematics.add(schematic);
                    }
                }
            }
        }
       
        public static boolean doesExist(String name) {
            for(Schematic schem : getAllSchematics()) {
                if(schem.getName().equalsIgnoreCase(name)) return true;
            }
            return false;
        }
       
        public static Schematic getByName(String name) {
            for(Schematic schem : getAllSchematics()) {
                if(schem.getName().equalsIgnoreCase(name)) return schem;
            }
            return null;
        }
        public static List<Schematic> getAllSchematics(){
            return allSchematics;
        }
        public static Schematic loadSchematic(File file){
            try{
                if(file.exists()){
                    NBTInputStream nbtStream =  new NBTInputStream(new FileInputStream(file));
                    CompoundTag compound = (CompoundTag) nbtStream.readTag();
                    Map<String, Tag> tags = compound.getValue();
                    Short width = ((ShortTag) tags.get("Width")).getValue();
                    Short height = ((ShortTag) tags.get("Height")).getValue();
                    Short length = ((ShortTag) tags.get("Length")).getValue();
                    String materials = ((StringTag) tags.get("Materials")).getValue();
                    byte[] blocks = ((ByteArrayTag) tags.get("Blocks")).getValue();
                    byte[] data = ((ByteArrayTag) tags.get("Data")).getValue();
                    nbtStream.close();
                    Schematic schematic = new Schematic(file.getName().replace(".schematic", ""), width, height, length, materials, blocks, data);
                    return schematic;
                }
            } catch(Exception e){
                ManCo.log(LogType.SEVERE, "could not load this file: " + file.getName());
                e.printStackTrace();
            }
            return null;
        }
    }
    
    so what is causing this problem, ive tried everything already debug it by seeing what values are printend but most schematics work but when I'm using a schematic with for example block quarts its just changed to AIR, could this be because I'm using a outdated jnbt?, I'm using 1.1

    thanks for the help:)

    -edit-
    oh ive just found what the issue seems to be by looking what the schematic structure is at the wiki at mojang I just found this link:
    http://minecraft.gamepedia.com/Data_values#Data

    which is probably exactly the kind of blocks I'm missing!
     
Thread Status:
Not open for further replies.

Share This Page