2018-08-12 14:03:07 +00:00
|
|
|
package com.boydti.fawe.util;
|
|
|
|
|
2019-11-11 21:02:28 +00:00
|
|
|
import static org.slf4j.LoggerFactory.getLogger;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.boydti.fawe.FaweCache;
|
|
|
|
import com.boydti.fawe.object.brush.BrushSettings;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseItem;
|
|
|
|
import com.sk89q.worldedit.command.tool.BrushTool;
|
|
|
|
import com.sk89q.worldedit.entity.Player;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
|
|
|
public final class BrushCache {
|
|
|
|
private static final WeakHashMap<Object, BrushTool> brushCache = new WeakHashMap<>();
|
|
|
|
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
|
|
|
|
private static final CompoundTag getNBT(BaseItem item) {
|
|
|
|
return item.hasNbtData() ? item.getNbtData() : null;
|
|
|
|
}
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
private static Object getKey(BaseItem item) {
|
2018-08-12 14:03:07 +00:00
|
|
|
return item.getNativeItem();
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:45:41 +00:00
|
|
|
private static final ThreadLocal<Boolean> RECURSION = new ThreadLocal<>();
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
public static final BrushTool getTool(Player player, LocalSession session, BaseItem item) {
|
2019-06-12 13:45:41 +00:00
|
|
|
if (!item.hasNbtData()) return null;
|
2018-08-12 14:03:07 +00:00
|
|
|
Object key = getKey(item);
|
2019-06-12 13:45:41 +00:00
|
|
|
if (key == null) return null;
|
2018-08-12 14:03:07 +00:00
|
|
|
BrushTool cached = brushCache.get(key);
|
2019-06-12 13:45:41 +00:00
|
|
|
if (cached != null) return cached;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2018-08-23 21:18:14 +00:00
|
|
|
CompoundTag nbt = item.getNbtData();
|
2019-06-12 13:45:41 +00:00
|
|
|
if (nbt == null) return null;
|
2018-08-23 21:18:14 +00:00
|
|
|
StringTag json = (StringTag) nbt.getValue().get("weBrushJson");
|
2020-02-05 05:37:42 +00:00
|
|
|
// TODO: Ping @MattBDev to reimplement 2020-02-04
|
|
|
|
// if (json != null) {
|
|
|
|
// try {
|
|
|
|
// if (RECURSION.get() != null) return null;
|
|
|
|
// RECURSION.set(true);
|
|
|
|
//
|
|
|
|
// BrushTool tool = BrushTool.fromString(player, session, json.getValue());
|
|
|
|
// tool.setHolder(item);
|
|
|
|
// brushCache.put(key, tool);
|
|
|
|
// return tool;
|
|
|
|
// } catch (Exception throwable) {
|
|
|
|
// getLogger(BrushCache.class).debug("Invalid brush for " + player + " holding " + item.getType() + ": " + json.getValue(), throwable);
|
|
|
|
// item.setNbtData(null);
|
|
|
|
// brushCache.remove(key);
|
|
|
|
// } finally {
|
|
|
|
// RECURSION.remove();
|
|
|
|
// }
|
|
|
|
// }
|
2018-08-12 14:03:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BrushTool getCachedTool(BaseItem item) {
|
|
|
|
Object key = getKey(item);
|
2019-06-12 13:45:41 +00:00
|
|
|
if (key != null) return brushCache.get(key);
|
2018-08-12 14:03:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final BrushTool setTool(BaseItem item, BrushTool tool) {
|
2019-06-12 13:45:41 +00:00
|
|
|
if (item.getNativeItem() == null) return null;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
CompoundTag nbt = item.getNbtData();
|
|
|
|
Map<String, Tag> map;
|
|
|
|
if (nbt == null) {
|
|
|
|
if (tool == null) {
|
2019-05-19 18:55:45 +00:00
|
|
|
item.setNbtData(null);
|
|
|
|
return tool;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
nbt = new CompoundTag(map = new HashMap<>());
|
|
|
|
} else {
|
|
|
|
map = ReflectionUtils.getMap(nbt.getValue());
|
|
|
|
}
|
|
|
|
brushCache.remove(getKey(item));
|
|
|
|
CompoundTag display = (CompoundTag) map.get("display");
|
|
|
|
Map<String, Tag> displayMap;
|
|
|
|
return tool;
|
|
|
|
}
|
2019-06-04 15:48:30 +00:00
|
|
|
}
|