1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Parse block info at startup

This commit is contained in:
creator3 2018-06-15 19:12:09 -04:00
parent 634c306342
commit ae3ba64f54
2 changed files with 181 additions and 59 deletions

View File

@ -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<String, String> data;
public BlockInfoConfig() {
this(new HashMap<>());
}
public BlockInfoConfig(Map<String, String> data) {
super((File)null,(FileConfiguration)null);
this.data=data;
}
public Map<String, String> 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<String,String> (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<String> 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<String> getStringList(String path) {
throw invalidType(path);
}
@Override
public List<Integer> getIntList(String path) {
throw invalidType(path);
}
@Override
public Double getDouble(String path) {
throw invalidType(path);
}
@Override
public Set<String> 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();
}
}

View File

@ -39,7 +39,7 @@ public class BlockStorage {
private World world;
private Map<Location, String> storage = new HashMap<Location, String>();
private Map<Location, Config> storage = new HashMap<>();
private static Map<String, String> map_chunks = new HashMap<String, String>();
private Map<Location, BlockMenu> inventories = new HashMap<Location, BlockMenu>();
@ -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<Location> locations = ticking_chunks.containsKey(chunk_string) ? ticking_chunks.get(chunk_string): new HashSet<Location>();
@ -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<String, String> 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<String, String> 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<String, String> 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) {