From ae3ba64f5414513d1503bf7fb28864a1c6d854bd Mon Sep 17 00:00:00 2001 From: creator3 Date: Fri, 15 Jun 2018 19:12:09 -0400 Subject: [PATCH] Parse block info at startup --- .../Slimefun/api/BlockInfoConfig.java | 132 ++++++++++++++++++ .../Slimefun/api/BlockStorage.java | 108 +++++++------- 2 files changed, 181 insertions(+), 59 deletions(-) create mode 100644 src/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java diff --git a/src/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java b/src/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java new file mode 100644 index 000000000..8cffcb7c2 --- /dev/null +++ b/src/me/mrCookieSlime/Slimefun/api/BlockInfoConfig.java @@ -0,0 +1,132 @@ +package me.mrCookieSlime.Slimefun.api; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.bukkit.configuration.file.FileConfiguration; + +import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; + +public class BlockInfoConfig extends Config { + private Map data; + public BlockInfoConfig() { + this(new HashMap<>()); + } + public BlockInfoConfig(Map data) { + super((File)null,(FileConfiguration)null); + this.data=data; + } + public Map getMap(){ + return data; + } + @Override + protected void store(String path, Object value) { + if (value != null && !(value instanceof String)) { + throw new UnsupportedOperationException("Can't set \""+path+"\" to \""+value+"\" (type: "+value.getClass().getSimpleName()+") because BlockInfoConfig only supports Strings"); + } + checkPath(path); + if (value == null) { + data.remove(path); + } else { + data.put(path, (String) value); + } + } + + + private void checkPath(String path) { + if (path.contains(".")) { + throw new UnsupportedOperationException("BlockInfoConfig only supports Map (path: " + path+")"); + } + } + + @Override + public boolean contains(String path) { + checkPath(path); + return data.containsKey(path); + } + + @Override + public Object getValue(String path) { + checkPath(path); + return data.get(path); + } + + @Override + public String getString(String path) { + checkPath(path); + return data.get(path); + } + + @Override + public Set getKeys() { + return data.keySet(); + } + + private UnsupportedOperationException invalidType(String path) { + return new UnsupportedOperationException("Can't get \""+path+"\" because BlockInfoConfig only supports String values"); + } + + @Override + public int getInt(String path) { + throw invalidType(path); + } + + @Override + public boolean getBoolean(String path) { + throw invalidType(path); + } + + @Override + public List getStringList(String path) { + throw invalidType(path); + } + + @Override + public List getIntList(String path) { + throw invalidType(path); + } + + @Override + public Double getDouble(String path) { + throw invalidType(path); + } + + @Override + public Set getKeys(String path) { + throw invalidType(path); + } + + @Override + public File getFile() { + throw new UnsupportedOperationException(); + } + + @Override + public FileConfiguration getConfiguration() { + throw new UnsupportedOperationException(); + } + + @Override + public void save() { + throw new UnsupportedOperationException(); + } + + @Override + public void save(File file) { + throw new UnsupportedOperationException(); + } + + @Override + public void createFile() { + throw new UnsupportedOperationException(); + } + + @Override + public void reload() { + throw new UnsupportedOperationException(); + } + +} diff --git a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java index c1d9f4157..ed7a95bc6 100644 --- a/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -39,7 +39,7 @@ public class BlockStorage { private World world; - private Map storage = new HashMap(); + private Map storage = new HashMap<>(); private static Map map_chunks = new HashMap(); private Map inventories = new HashMap(); @@ -103,7 +103,10 @@ public class BlockStorage { String chunk_string = locationToChunkString(l); try { totalBlocks++; - storage.put(l, cfg.getString(key)); + String json = cfg.getString(key); + Config blockInfo = parseBlockInfo(l, json); + if (blockInfo == null) continue; + storage.put(l, blockInfo); if (SlimefunItem.isTicking(file.getName().replace(".sfb", ""))) { Set locations = ticking_chunks.containsKey(chunk_string) ? ticking_chunks.get(chunk_string): new HashSet(); @@ -282,30 +285,9 @@ public class BlockStorage { } public static Config getLocationInfo(Location l) { - try { BlockStorage storage = getStorage(l.getWorld()); - Config cfg = new Config("data-storage/Slimefun/temp.yml"); - if (!storage.storage.containsKey(l)) return cfg; - - for (Map.Entry entry: parseJSON(getJSONData(l)).entrySet()) { - cfg.setValue(entry.getKey(), entry.getValue()); - } - - return cfg; - } catch (Exception x) { - System.err.println(x.getClass().getName()); - System.err.println("[Slimefun] Failed to parse BlockInfo for Block @ " + l.getBlockX() + ", " + l.getBlockY() + ", " + l.getBlockZ()); - try { - System.err.println(getJSONData(l)); - } catch (Exception x2) { - System.err.println("No Metadata found!"); - } - System.err.println("[Slimefun] "); - System.err.println("[Slimefun] IGNORE THIS ERROR UNLESS IT IS SPAMMING"); - System.err.println("[Slimefun] "); - x.printStackTrace(); - return new Config("data-storage/Slimefun/temp.yml"); - } + Config cfg = storage.storage.get(l); + return cfg == null ? new BlockInfoConfig() : cfg; } private static Map parseJSON(String json) { @@ -328,11 +310,29 @@ public class BlockStorage { return map; } - private static String getJSONData(Location l) { - BlockStorage storage = getStorage(l.getWorld()); - return storage.storage.get(l); + private static BlockInfoConfig parseBlockInfo(Location l, String json){ + try { + return new BlockInfoConfig(parseJSON(json)); + } catch(Exception x) { + System.err.println(x.getClass().getName()); + System.err.println("[Slimefun] Failed to parse BlockInfo for Block @ " + l.getBlockX() + ", " + l.getBlockY() + ", " + l.getBlockZ()); + System.err.println(json); + System.err.println("[Slimefun] "); + System.err.println("[Slimefun] IGNORE THIS ERROR UNLESS IT IS SPAMMING"); + System.err.println("[Slimefun] "); + x.printStackTrace(); + return null; + } + } + + @SuppressWarnings("unchecked") + private static String serializeBlockInfo(Config cfg) { + JSONObject json = new JSONObject(); + for (String key: cfg.getKeys()) { + json.put(key, cfg.getString(key)); + } + return json.toJSONString(); } - private static String getJSONData(Chunk chunk) { return map_chunks.get(serializeChunk(chunk)); } @@ -348,7 +348,7 @@ public class BlockStorage { } public static String getLocationInfo(Location l, String key) { - return parseJSON(getJSONData(l)).get(key); + return getBlockInfo(l).getString(key); } public static void addBlockInfo(Location l, String key, String value) { @@ -364,8 +364,7 @@ public class BlockStorage { } public static void addBlockInfo(Location l, String key, String value, boolean updateTicker) { - Config cfg = new Config("data-storage/Slimefun/temp.yml"); - if (hasBlockInfo(l)) cfg = getLocationInfo(l); + Config cfg = hasBlockInfo(l) ? getLocationInfo(l) : new BlockInfoConfig(); cfg.setValue(key, value); setBlockInfo(l, cfg, updateTicker); } @@ -378,43 +377,34 @@ public class BlockStorage { BlockStorage storage = getStorage(l.getWorld()); return storage != null && storage.storage.containsKey(l) && getLocationInfo(l, "id") != null; } - + public static void setBlockInfo(Block block, Config cfg, boolean updateTicker) { setBlockInfo(block.getLocation(), cfg, updateTicker); } - - @SuppressWarnings("unchecked") + public static void setBlockInfo(Location l, Config cfg, boolean updateTicker) { - _integrated_removeBlockInfo(l, false); - - JSONObject json = new JSONObject(); - for (String key: cfg.getKeys()) { - json.put(key, cfg.getString(key)); - } - - setBlockInfo(l, json.toJSONString(), updateTicker); - } - - public static void setBlockInfo(Block b, String json, boolean updateTicker) { - setBlockInfo(b.getLocation(), json, updateTicker); - } - - public static void setBlockInfo(Location l, String json, boolean updateTicker) { BlockStorage storage = getStorage(l.getWorld()); - storage.storage.put(l, json); - Map parsed = parseJSON(json); - if (BlockMenuPreset.isInventory(parsed.get("id"))) { - if (BlockMenuPreset.isUniversalInventory(parsed.get("id"))) { - if (!universal_inventories.containsKey(parsed.get("id"))) storage.loadUniversalInventory(BlockMenuPreset.getPreset(parsed.get("id"))); + storage.storage.put(l, cfg); + if (BlockMenuPreset.isInventory(cfg.getString("id"))) { + if (BlockMenuPreset.isUniversalInventory(cfg.getString("id"))) { + if (!universal_inventories.containsKey(cfg.getString("id"))) storage.loadUniversalInventory(BlockMenuPreset.getPreset(cfg.getString("id"))); } else if (!storage.hasInventory(l)) { File file = new File("data-storage/Slimefun/stored-inventories/" + serializeLocation(l) + ".sfi"); - if (file.exists()) storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(parsed.get("id")), l, new Config(file))); - else storage.loadInventory(l, BlockMenuPreset.getPreset(parsed.get("id"))); + if (file.exists()) storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(cfg.getString("id")), l, new Config(file))); + else storage.loadInventory(l, BlockMenuPreset.getPreset(cfg.getString("id"))); } } - refreshCache(getStorage(l.getWorld()), l, parsed.get("id"), json, updateTicker); + refreshCache(getStorage(l.getWorld()), l, cfg.getString("id"), serializeBlockInfo(cfg), updateTicker); + } + public static void setBlockInfo(Block b, String json, boolean updateTicker) { + setBlockInfo(b.getLocation(), json, updateTicker); + } + public static void setBlockInfo(Location l, String json, boolean updateTicker) { + Config blockInfo = json == null ? new BlockInfoConfig() : parseBlockInfo(l, json); + if (blockInfo == null) return; + setBlockInfo(l, blockInfo, updateTicker); } public static void clearBlockInfo(Block block) { @@ -699,7 +689,7 @@ public class BlockStorage { } public static String getBlockInfoAsJson(Location l) { - return getJSONData(l); + return serializeBlockInfo(getLocationInfo(l)); } public boolean hasUniversalInventory(Block block) {