Retrieving all itemstacks

Discussion in 'Plugin Development' started by Jnorr44, Jun 2, 2013.

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

    Jnorr44

    Okay, so I have the need to retrieve the itemstacks from the inventoryholder here:
    Basically I am just storing all data for all blocks, serializing it, and loading it whenever required.

    Save method:
    Code:
        @SuppressWarnings("unused") public void save(Rectangle rect) {
            try {
                Location anchor = rect.getCorner(1);
                List<Map<String, Object>> serialized = new ArrayList<Map<String, Object>>();
                gameID: {
                    Map<String, Object> serialGameID = new HashMap<String, Object>();
                    serialGameID.put("gameName", game.getName());
                    serialized.add(serialGameID);
                }
                for (Location loc : rect.getLocations()) {
                    Map<String, Object> thisSerialBlock = new HashMap<String, Object>();
                    Block block = loc.getBlock();
                    BlockState state = block.getState();
                    thisSerialBlock.put("id", block.getTypeId());
                    thisSerialBlock.put("data", block.getData());
                    thisSerialBlock.put("worldName", loc.getWorld());
                    thisSerialBlock.put("xDif", loc.getX() - anchor.getX());
                    thisSerialBlock.put("yDif", loc.getY() - anchor.getY());
                    thisSerialBlock.put("zDif", loc.getZ() - anchor.getZ());
                    thisSerialBlock.put("yaw", loc.getYaw());
                    thisSerialBlock.put("pitch", loc.getPitch());
                    thisSerialBlock.put("biome", block.getBiome());// unused
                    specificData: {
                        if (state instanceof InventoryHolder) {
                            thisSerialBlock.put("INVENTORY_HOLDER", "NULL");
                            for (ItemStack stack : ((InventoryHolder) state).getInventory().getContents()) {
                                Map<String, Object> serialStack = stack.serialize();
                                for (String name : serialStack.keySet()) {
                                    Object obj = serialStack.get(name);
                                    serialStack.remove(name);
                                    serialStack.put("MAPDATA_ITEM=" + name, obj);
                                }
                                thisSerialBlock.putAll(serialStack);
                            }
                        } else if (state instanceof ContainerBlock) {
                            thisSerialBlock.put("CONTAINER_BLOCK", "NULL");
                            for (ItemStack stack : ((ContainerBlock) state).getInventory().getContents()) {
                                Map<String, Object> serialStack = stack.serialize();
                                for (String name : serialStack.keySet()) {
                                    Object obj = serialStack.get(name);
                                    serialStack.remove(name);
                                    serialStack.put("MAPDATA_ITEM=" + name, obj);
                                }
                                thisSerialBlock.putAll(serialStack);
                            }
                        }
                        if (state instanceof CommandBlock) {
                            thisSerialBlock.put("command", ((CommandBlock) state).getCommand());
                        }
                        if (state instanceof CreatureSpawner) {
                            CreatureSpawner spawner = (CreatureSpawner) state;
                            thisSerialBlock.put("spawnType", spawner.getSpawnedType().toString());
                            thisSerialBlock.put("spawnDelay", spawner.getDelay());
                        }
                        if (state instanceof Jukebox) {
                            thisSerialBlock.put("playingRecord", ((Jukebox) state).getPlaying().toString());
                        }
                        if (state instanceof NoteBlock) {
                            thisSerialBlock.put("rawNote", ((NoteBlock) state).getRawNote());
                        }
                        if (state instanceof Sign) {
                            for (int i = 0; i <= 3; i++) {
                                String line = ((Sign) state).getLine(i);
                                thisSerialBlock.put("signLine" + i, line);
                            }
                        }
                        if (state instanceof Skull) {
                            Skull skull = (Skull) state;
                            thisSerialBlock.put("skullOwner", skull.getOwner());
                            thisSerialBlock.put("skullRotation", skull.getRotation().toString());
                            thisSerialBlock.put("skullType", skull.getSkullType().toString());
                        }
                    }
                    serialized.add(thisSerialBlock);
                }
                External.save(serialized, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    Load method:
    Code:
        @SuppressWarnings("unused") public void load(Location anchor) {
            try {
                List<Map<String, Object>> serialized = External.load(file);
                for (Map<String, Object> thisSerialBlock : serialized) {
                    int id = (Integer) thisSerialBlock.get("id");
                    byte data = (Byte) thisSerialBlock.get("data");
                    World world = Bukkit.getWorld((String) thisSerialBlock.get("worldName"));
                    double x = anchor.getX() + (Double) thisSerialBlock.get("xDif");
                    double y = anchor.getY() + (Double) thisSerialBlock.get("yDif");
                    double z = anchor.getZ() + (Double) thisSerialBlock.get("zDif");
                    float yaw = (Float) thisSerialBlock.get("yaw");
                    float pitch = (Float) thisSerialBlock.get("pitch");
                    Location loc = new Location(world, x, y, z, yaw, pitch);
                    Block block = loc.getBlock();
                    block.setTypeId(id);
                    block.setData(data);
                    BlockState state = block.getState();
                    specificData: {
                        // TODO inventory holder and container block
                        if (thisSerialBlock.containsKey("INVENTORY_HOLDER")) {
                           
                        } else if (thisSerialBlock.containsKey("CONTAINER_BLOCK")) {
                           
                        }
                        if (thisSerialBlock.containsKey("command")) {
                            ((CommandBlock) state).setCommand((String) thisSerialBlock.get("command"));
                        }
                        if (thisSerialBlock.containsKey("spawnType")) {
                            CreatureSpawner spawner = (CreatureSpawner) state;
                            spawner.setSpawnedType(EntityType.valueOf((String) thisSerialBlock.get("spawnType")));
                            spawner.setDelay((Integer) thisSerialBlock.get("spawnDelay"));
                        }
                        if (thisSerialBlock.containsKey("playingRecord")) {
                            ((Jukebox) state).setPlaying(Material.valueOf((String) thisSerialBlock.get("playingRecord")));
                        }
                        if (thisSerialBlock.containsKey("rawNote")) {
                            ((NoteBlock) state).setRawNote((Byte) thisSerialBlock.get("rawNote"));
                        }
                        if (thisSerialBlock.containsKey("signLine0")) {
                            for (int i = 0; i <= 3; i++) {
                                ((Sign) state).setLine(i, (String) thisSerialBlock.get("signLine" + i));
                            }
                        }
                        if (thisSerialBlock.containsKey("skullOwner")) {
                            Skull skull = (Skull) state;
                            skull.setOwner((String) thisSerialBlock.get("skullOwner"));
                            skull.setRotation(BlockFace.valueOf((String) thisSerialBlock.get("skullRotation")));
                            skull.setSkullType(SkullType.valueOf((String) thisSerialBlock.get("skullType")));
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    Could someone help me out here? It's a bit confusing. You can change ALMOST anything, except for the List<Map<String, Object>> that is saved.
     
Thread Status:
Not open for further replies.

Share This Page