From 177e5f62f73a967195a722437ccef8e87d66d75e Mon Sep 17 00:00:00 2001 From: md678685 Date: Fri, 12 Oct 2018 17:26:22 +0100 Subject: [PATCH] Remove providers; move JSON support into ItemDb --- .../com/earth2me/essentials/Essentials.java | 22 +- .../src/com/earth2me/essentials/ItemDb.java | 182 ++++++++-- .../com/earth2me/essentials/api/IItemDb.java | 4 + Essentials/src/net/ess3/api/IEssentials.java | 3 - .../nms/flattened/FlatItemDbProvider.java | 209 ----------- nms/IdProvider/pom.xml | 32 -- .../ess3/nms/ids/LegacyItemDbProvider.java | 331 ------------------ .../src/net/ess3/nms/ItemDbProvider.java | 236 ------------- pom.xml | 1 - 9 files changed, 168 insertions(+), 852 deletions(-) delete mode 100644 nms/FlattenedProvider/src/net/ess3/nms/flattened/FlatItemDbProvider.java delete mode 100644 nms/IdProvider/pom.xml delete mode 100644 nms/IdProvider/src/net/ess3/nms/ids/LegacyItemDbProvider.java delete mode 100644 nms/NMSProvider/src/net/ess3/nms/ItemDbProvider.java diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index fa6245677..9f254e447 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -34,24 +34,20 @@ import com.google.common.collect.Iterables; import net.ess3.api.*; import net.ess3.api.IEssentials; import net.ess3.api.ISettings; -import net.ess3.nms.ItemDbProvider; import net.ess3.nms.PotionMetaProvider; import net.ess3.nms.SpawnEggProvider; import net.ess3.nms.SpawnerProvider; -import net.ess3.nms.flattened.FlatItemDbProvider; import net.ess3.nms.flattened.FlatSpawnEggProvider; -import net.ess3.nms.ids.LegacyItemDbProvider; import net.ess3.nms.legacy.LegacyPotionMetaProvider; +import net.ess3.nms.legacy.LegacySpawnEggProvider; +import net.ess3.nms.legacy.LegacySpawnerProvider; import net.ess3.nms.refl.ReflSpawnEggProvider; import net.ess3.nms.updatedmeta.BasePotionDataProvider; import net.ess3.nms.updatedmeta.BlockMetaSpawnerProvider; -import net.ess3.nms.legacy.LegacySpawnEggProvider; -import net.ess3.nms.legacy.LegacySpawnerProvider; import net.ess3.nms.v1_8_R1.v1_8_R1SpawnerProvider; import net.ess3.nms.v1_8_R2.v1_8_R2SpawnerProvider; import net.ess3.providers.ProviderFactory; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.Block; @@ -107,7 +103,6 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { private transient EssentialsTimer timer; private final transient Set vanishedPlayers = new LinkedHashSet<>(); private transient Method oldGetOnlinePlayers; - private transient ItemDbProvider itemDbProvider; private transient SpawnerProvider spawnerProvider; private transient SpawnEggProvider spawnEggProvider; private transient PotionMetaProvider potionMetaProvider; @@ -218,11 +213,6 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { confList.add(jails); execTimer.mark("Init(Jails)"); - itemDbProvider = new ProviderFactory<>(getLogger(), - Arrays.asList( - FlatItemDbProvider.class, - LegacyItemDbProvider.class - ), "item database").getProvider(); spawnerProvider = new ProviderFactory<>(getLogger(), Arrays.asList( BlockMetaSpawnerProvider.class, @@ -241,9 +231,6 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { BasePotionDataProvider.class, LegacyPotionMetaProvider.class ), "potion meta").getProvider(); - itemDbProvider.setSpawnerProvider(spawnerProvider); - itemDbProvider.setSpawnEggProvider(spawnEggProvider); - itemDbProvider.setPotionMetaProvider(potionMetaProvider); execTimer.mark("Init(Providers)"); reload(); } catch (YAMLException exception) { @@ -889,11 +876,6 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { return potionMetaProvider; } - @Override - public ItemDbProvider getItemDbProvider() { - return itemDbProvider; - } - private static void addDefaultBackPermissionsToWorld(World w) { String permName = "essentials.back.into." + w.getName(); diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index e8daf693e..c9ef5554e 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -1,9 +1,11 @@ package com.earth2me.essentials; import com.earth2me.essentials.utils.StringUtil; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import net.ess3.api.IEssentials; -import net.ess3.nms.ItemDbProvider; -import net.ess3.nms.ids.LegacyItemDbProvider; import org.bukkit.Color; import org.bukkit.FireworkEffect; import org.bukkit.Material; @@ -13,6 +15,7 @@ import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.*; import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffect; import java.util.*; @@ -23,8 +26,16 @@ import static com.earth2me.essentials.I18n.tl; public class ItemDb implements IConf, net.ess3.api.IItemDb { protected static final Logger LOGGER = Logger.getLogger("Essentials"); - private final transient IEssentials ess; - private transient ItemDbProvider provider = null; + private final transient IEssentials ess;private static Gson gson = new Gson(); + + // Maps primary name to ItemData + private final transient Map items = new HashMap<>(); + + // Maps alias to primary name + private final transient Map itemAliases = new HashMap<>(); + + // Every known alias + private final transient Set allAliases = new HashSet<>(); private transient ManagedFile file = null; @@ -34,30 +45,86 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb { @Override public void reloadConfig() { - if (provider == null) { - this.provider = ess.getItemDbProvider(); + if (file == null) { + new ManagedFile("items.json", ess); } - if (file == null) { - if (provider instanceof LegacyItemDbProvider) { - file = new ManagedFile("items.csv", ess); + this.rebuild(); + LOGGER.info(String.format("Loaded %s items.", listNames().size())); + } + + private void rebuild() { + this.reset(); + this.loadJSON(String.join("\n", file.getLines())); + } + + private void reset() { + items.clear(); + itemAliases.clear(); + allAliases.clear(); + } + + public void loadJSON(String source) { + JsonObject map = (new JsonParser()).parse(source).getAsJsonObject(); + + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + JsonElement element = entry.getValue(); + boolean valid = false; + + if (element.isJsonObject()) { + ItemData data = gson.fromJson(element, ItemData.class); + items.put(key, data); + valid = true; } else { - file = new ManagedFile("items.json", ess); + try { + String target = element.getAsString(); + itemAliases.put(key, target); + valid = true; + } catch (Exception e) { + // TODO: log invalid entry + } + } + + if (valid) { + allAliases.add(key); } } - - provider.rebuild(file.getLines()); - LOGGER.info(String.format("Loaded %s items.", provider.listNames().size())); } @Override public ItemStack get(final String id, final int quantity) throws Exception { - return provider.getStack(id, quantity); + ItemStack is = get(id); + is.setAmount(quantity); + return is; } @Override public ItemStack get(final String id) throws Exception { - return provider.getStack(id); + ItemData data = Objects.requireNonNull(getByName(id)); + PotionData potionData = data.getPotionData(); + Material material = data.getMaterial(); + + ItemStack stack = new ItemStack(material); + + if (potionData != null) { + PotionMeta meta = (PotionMeta) stack.getItemMeta(); + meta.setBasePotionData(potionData); + stack.setItemMeta(meta); + } + + return stack; + } + + private ItemData getByName(String name) { + name = name.toLowerCase(); + if (items.containsKey(name)) { + return items.get(name); + } else if (itemAliases.containsKey(name)) { + return items.get(itemAliases.get(name)); + } + + return null; } @Override @@ -95,7 +162,7 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb { @Override public String names(ItemStack item) { - List nameList = provider.getNames(item); + List nameList = nameList(item); if (nameList.size() > 15) { nameList = nameList.subList(0, 14); @@ -103,9 +170,37 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb { return StringUtil.joinList(", ", nameList); } + @Override + public List nameList(ItemStack item) { + List names = new ArrayList<>(); + String primaryName = name(item); + names.add(primaryName); + + for (Map.Entry entry : itemAliases.entrySet()) { + if (entry.getValue().equalsIgnoreCase(primaryName)) { + names.add(entry.getKey()); + } + } + + return names; + } + @Override public String name(ItemStack item) { - return provider.getPrimaryName(item); + Material type = item.getType(); + PotionData potion = null; + + if ((type.name().contains("POTION") || type.name().equals("TIPPED_ARROW")) && item.getItemMeta() instanceof PotionMeta) { + potion = ((PotionMeta) item.getItemMeta()).getBasePotionData(); + } + + for (Map.Entry entry : items.entrySet()) { + if (entry.getValue().getMaterial().equals(type) && entry.getValue().getPotionData().equals(potion)) { + return entry.getKey(); + } + } + + return null; } @Override @@ -283,17 +378,64 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb { } @Override + @Deprecated public Material getFromLegacyId(int id) { - return provider.getFromLegacyId(id); + throw new UnsupportedOperationException("Legacy IDs aren't supported on this version of EssentialsX."); } @Override + @Deprecated public int getLegacyId(Material material) throws Exception { - return provider.getLegacyId(material); + throw new UnsupportedOperationException("Legacy IDs aren't supported on this version of EssentialsX."); } @Override public Collection listNames() { - return provider.listNames(); + return Collections.unmodifiableSet(allAliases); + } + + public static class ItemData { + private String itemName; + private Material material; + private PotionData potionData; + + @Override + public int hashCode() { + return (31 * material.hashCode()) ^ potionData.hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o == null) { + return false; + } + if (!(o instanceof ItemData)) { + return false; + } + ItemData pairo = (ItemData) o; + return this.material == pairo.getMaterial() && potionDataEquals(pairo); + } + + public String getItemName() { + return itemName; + } + + public Material getMaterial() { + return material; + } + + public PotionData getPotionData() { + return this.potionData; + } + + private boolean potionDataEquals(ItemData o) { + if (this.potionData == null && o.getPotionData() == null) { + return true; + } else if (this.potionData != null && o.getPotionData() != null) { + return this.potionData.equals(o.getPotionData()); + } else { + return false; + } + } } } diff --git a/Essentials/src/com/earth2me/essentials/api/IItemDb.java b/Essentials/src/com/earth2me/essentials/api/IItemDb.java index 00f073d5f..5a64069e4 100644 --- a/Essentials/src/com/earth2me/essentials/api/IItemDb.java +++ b/Essentials/src/com/earth2me/essentials/api/IItemDb.java @@ -15,6 +15,8 @@ public interface IItemDb { String names(ItemStack item); + List nameList(ItemStack item); + String name(ItemStack item); List getMatching(User user, String[] args) throws Exception; @@ -23,7 +25,9 @@ public interface IItemDb { Collection listNames(); + @Deprecated Material getFromLegacyId(int id); + @Deprecated int getLegacyId(Material material) throws Exception; } diff --git a/Essentials/src/net/ess3/api/IEssentials.java b/Essentials/src/net/ess3/api/IEssentials.java index dc418b512..1e76dda17 100644 --- a/Essentials/src/net/ess3/api/IEssentials.java +++ b/Essentials/src/net/ess3/api/IEssentials.java @@ -1,6 +1,5 @@ package net.ess3.api; -import net.ess3.nms.ItemDbProvider; import net.ess3.nms.PotionMetaProvider; import net.ess3.nms.SpawnEggProvider; @@ -13,6 +12,4 @@ public interface IEssentials extends com.earth2me.essentials.IEssentials { SpawnEggProvider getSpawnEggProvider(); PotionMetaProvider getPotionMetaProvider(); - - ItemDbProvider getItemDbProvider(); } diff --git a/nms/FlattenedProvider/src/net/ess3/nms/flattened/FlatItemDbProvider.java b/nms/FlattenedProvider/src/net/ess3/nms/flattened/FlatItemDbProvider.java deleted file mode 100644 index 3a58463f0..000000000 --- a/nms/FlattenedProvider/src/net/ess3/nms/flattened/FlatItemDbProvider.java +++ /dev/null @@ -1,209 +0,0 @@ -package net.ess3.nms.flattened; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import net.ess3.nms.ItemDbProvider; -import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.potion.PotionData; - -import java.util.*; -import java.util.stream.Collectors; - -public class FlatItemDbProvider extends ItemDbProvider { - private static Gson gson = new Gson(); - - private final transient Map primaryNames = new HashMap<>(); - private final transient Map> names = new HashMap<>(); - - @Override - public ItemData resolve(String name) { - return getByName(name); - } - - @Override - public boolean supportsLegacyIds() { - return false; - } - - @Override - public int getLegacyId(Material material) { - return -1; - } - - @Override - public Material getFromLegacyId(int id) { - return null; - } - - @Override - public String getPrimaryName(ItemStack item) { - ItemData itemData = new FlatItemData(null, item.getType(), getPotionData(item)); - - for (Map.Entry entry : primaryNames.entrySet()) { - if (entry.getValue().equals(itemData)) { - return entry.getKey(); - } - } - - return null; - } - - private PotionData getPotionData(ItemStack item) { - ItemMeta meta = item.getItemMeta(); - if (meta instanceof PotionMeta) { - return ((PotionMeta) meta).getBasePotionData(); - } - - return null; - } - - @Override - public List getNames(ItemStack item) { - String primaryName = getPrimaryName(item); - - for (Map.Entry> entry : names.entrySet()) { - if (entry.getKey().equals(primaryName)) { - return Collections.unmodifiableList(entry.getValue()); - } - } - return null; - } - - @Override - public ItemStack getStack(String name) throws Exception { - ItemData data = Objects.requireNonNull(getByName(name)); - PotionData potionData = data.getPotionData(); - Material material = data.getMaterial(); - - ItemStack stack = new ItemStack(material); - - if (potionData != null) { - PotionMeta meta = (PotionMeta) stack.getItemMeta(); - meta.setBasePotionData(potionData); - stack.setItemMeta(meta); - } - - return stack; - } - - @Override - public Collection listNames() { - return Collections.unmodifiableSet(primaryNames.keySet()); - } - - private ItemData getByName(String name) { - if (primaryNames.containsKey(name.toLowerCase())) { - return primaryNames.get(name); - } else { - for (Map.Entry> entry : names.entrySet()) { - if (entry.getValue().contains(name.toLowerCase())) { - return primaryNames.get(entry.getKey()); - } - } - } - - return null; - } - - private void resetDb() { - primaryNames.clear(); - names.clear(); - } - - @Override - public void addFrom(List lines) { - String json = lines.stream() - .filter(line -> !line.startsWith("#")) - .collect(Collectors.joining("\n")); - - JsonObject map = (new JsonParser()).parse(json).getAsJsonObject(); - - for (Map.Entry entry : map.entrySet()) { - String key = entry.getKey(); - JsonElement element = entry.getValue(); - - if (element.isJsonObject()) { - FlatItemData data = gson.fromJson(element, FlatItemData.class); - primaryNames.put(key, data); - } else { - try { - String target = element.getAsString(); - addAlias(target, key); - } catch (Exception e) { - // TODO: log invalid entry - } - } - } - } - - @Override - public void rebuild(List lines) { - resetDb(); - addFrom(lines); - } - - private void addAlias(String primaryName, String alias) { - List aliasList; - - if (names.containsKey(primaryName)) { - aliasList = names.get(primaryName); - } else { - aliasList = new ArrayList<>(); - names.put(primaryName, aliasList); - } - - aliasList.add(alias); - } - - @Override - public boolean tryProvider() { - // Build the database initially so that we can actually test the provider - this.rebuild(this.loadResource("/items.json")); - return super.tryProvider(); - } - - @Override - public String getHumanName() { - return "Post-1.13 item database provider"; - } - - public static class FlatItemData extends ItemData { - private FlatItemData(String itemName, Material material, PotionData potionData) { - this.itemName = itemName; - this.material = material; - this.potionData = potionData; - } - - @Override - public int hashCode() { - return (31 * material.hashCode()) ^ potionData.hashCode(); - } - - @Override - public boolean equals(Object o) { - if (o == null) { - return false; - } - if (!(o instanceof ItemData)) { - return false; - } - ItemData pairo = (ItemData) o; - return this.material == pairo.getMaterial() && potionDataEquals(pairo); - } - - private boolean potionDataEquals(ItemData o) { - if (this.potionData == null && o.getPotionData() == null) { - return true; - } else if (this.potionData != null && o.getPotionData() != null) { - return this.potionData.equals(o.getPotionData()); - } else { - return false; - } - } - } -} diff --git a/nms/IdProvider/pom.xml b/nms/IdProvider/pom.xml deleted file mode 100644 index 3756f2e7f..000000000 --- a/nms/IdProvider/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - EssentialsXParent - net.ess3 - 2.15.0 - ../../pom.xml - - 4.0.0 - - IdProvider - - - - net.ess3 - NMSProvider - 2.15.0 - - - net.ess3 - ReflectionProvider - 2.15.0 - - - org.bukkit - bukkit - 1.12.2-R0.1-SNAPSHOT - - - \ No newline at end of file diff --git a/nms/IdProvider/src/net/ess3/nms/ids/LegacyItemDbProvider.java b/nms/IdProvider/src/net/ess3/nms/ids/LegacyItemDbProvider.java deleted file mode 100644 index 1d10106f1..000000000 --- a/nms/IdProvider/src/net/ess3/nms/ids/LegacyItemDbProvider.java +++ /dev/null @@ -1,331 +0,0 @@ -package net.ess3.nms.ids; - -import net.ess3.nms.ItemDbProvider; -import net.ess3.nms.refl.ReflUtil; -import org.apache.commons.lang.StringUtils; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.entity.EntityType; -import org.bukkit.inventory.ItemStack; - -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class LegacyItemDbProvider extends ItemDbProvider { - // Maps all item names to legacy IDs - private final transient Map items = new HashMap<>(); - - // Maps item data to all known names for an item - private final transient Map> names = new HashMap<>(); - - // Maps item data to the primary name for an item - private final transient Map primaryNames = new HashMap<>(); - - // Maps legacy IDs to item data - private final transient Map legacyIds = new HashMap<>(); - - // Maps names for an item to durability/meta values - private final transient Map durabilities = new HashMap<>(); - - // Maps names for an item to NBT data - private final transient Map nbtData = new HashMap<>(); - - private final transient Pattern splitPattern = Pattern.compile("((.*)[:+',;.](\\d+))"); - private final transient Pattern csvSplitPattern = Pattern.compile("(\"([^\"]*)\"|[^,]*)(,|$)"); - - @Override - public ItemData resolve(String name) { - // TODO: refactor getStack into here - return null; - } - - @Override - public ItemStack getStack(String name) throws Exception { - int itemid = 0; - String itemname; - short metaData = 0; - - // If the user provided id:metadata, split it - Matcher parts = splitPattern.matcher(name); - if (parts.matches()) { - itemname = parts.group(2); - metaData = Short.parseShort(parts.group(3)); - } else { - itemname = name; - } - - // Check whether the user provided an ID - if (isInt(itemname)) { - itemid = Integer.parseInt(itemname); - } else if (isInt(name)) { - itemid = Integer.parseInt(name); - } else { - itemname = itemname.toLowerCase(Locale.ENGLISH); - } - - if (itemid < 1) { - if (items.containsKey(itemname)) { - itemid = items.get(itemname); - if (durabilities.containsKey(itemname) && metaData == 0) { - metaData = durabilities.get(itemname); - } - } - } - - if (itemid < 1) { - throw new Exception("Unknown item name " + itemname); - } - - ItemData data = legacyIds.get(itemid); - if (data == null) { - throw new Exception("Unknown item ID " + itemid); - } - - Material mat = data.getMaterial(); - ItemStack retval = new ItemStack(mat); - if (nbtData.containsKey(itemname)) { - String nbt = nbtData.get(itemname); - if (nbt.startsWith("*")) { - nbt = nbtData.get(nbt.substring(1)); - } - retval = Bukkit.getServer().getUnsafe().modifyItemStack(retval, nbt); - } - - - Material MOB_SPAWNER; - try { - MOB_SPAWNER = Material.valueOf("SPAWNER"); - } catch (Exception e) { - MOB_SPAWNER = Material.valueOf("MOB_SPAWNER"); - } - if (mat == MOB_SPAWNER) { - if (metaData == 0) metaData = EntityType.PIG.getTypeId(); - try { - retval = getSpawnerProvider().setEntityType(retval, EntityType.fromId(metaData)); - } catch (IllegalArgumentException e) { - throw new Exception("Can't spawn entity ID " + metaData + " from mob spawners."); - } - } else if (mat == Material.MONSTER_EGG) { - EntityType type; - try { - type = EntityType.fromId(metaData); - } catch (IllegalArgumentException e) { - throw new Exception("Can't spawn entity ID " + metaData + " from spawn eggs."); - } - retval = getSpawnEggProvider().createEggItem(type); - } else if ((mat.name().endsWith("POTION") || mat.name().equals("TIPPED_ARROW")) - && ReflUtil.getNmsVersionObject().isLowerThan(ReflUtil.V1_11_R1)) { // Only apply this to pre-1.11 as items.csv might only work in 1.11 - retval = getPotionMetaProvider().createPotionItem(mat, metaData); - } else { - retval.setDurability(metaData); - } - retval.setAmount(mat.getMaxStackSize()); - return retval; - } - - @Override - public Collection listNames() { - return primaryNames.values(); - } - - @Override - public boolean supportsLegacyIds() { - return true; - } - - @Override - public int getLegacyId(Material material) throws Exception { - for (Map.Entry entry : items.entrySet()) { - if (material.name().toLowerCase(Locale.ENGLISH).equalsIgnoreCase(entry.getKey())) { - return entry.getValue(); - } - } - - throw new Exception("Item ID missing for material " + material.name()); - } - - @Override - public Material getFromLegacyId(int id) { - ItemData data = this.legacyIds.get(id); - if (data == null) { - return null; - } - - return data.getMaterial(); - } - - @Override - public String getPrimaryName(ItemStack item) { - ItemData itemData = new LegacyItemData(null, item.getType(), item.getTypeId(), item.getDurability(), null); - String name = primaryNames.get(itemData); - if (name == null) { - itemData = new LegacyItemData(null, item.getType(), item.getTypeId(), (short) 0, null); - name = primaryNames.get(itemData); - if (name == null) { - return null; - } - } - return name; - } - - @Override - public List getNames(ItemStack item) { - ItemData itemData = new LegacyItemData(null, item.getType(), item.getTypeId(), item.getDurability(), null); - List nameList = names.get(itemData); - if (nameList == null) { - itemData = new LegacyItemData(null, item.getType(), item.getTypeId(), (short) 0, null); - nameList = names.get(itemData); - if (nameList == null) { - return null; - } - } - - return nameList; - } - - private void resetDb() { - durabilities.clear(); - items.clear(); - names.clear(); - primaryNames.clear(); - } - - @Override - public void addFrom(List lines) { - lines.stream() - .filter(line -> line.length() > 0 && !(line.charAt(0) == '#')) - .map(this::parseLine) - .filter(Objects::nonNull) - .forEach(this::addItem); - - for (List nameList : names.values()) { - nameList.sort(LengthCompare.INSTANCE); - } - } - - @Override - public void rebuild(List lines) { - resetDb(); - addFrom(lines); - } - - private LegacyItemData parseLine(String line) { - String itemName = null; - int numeric = -1; - short data = 0; - String nbt = null; - - int col = 0; - Matcher matcher = csvSplitPattern.matcher(line); - while (matcher.find()) { - String match = matcher.group(1); - if (StringUtils.stripToNull(match) == null) { - continue; - } - match = StringUtils.strip(match.trim(), "\""); - switch (col) { - case 0: - itemName = match.toLowerCase(Locale.ENGLISH); - break; - case 1: - numeric = Integer.parseInt(match); - break; - case 2: - data = Short.parseShort(match); - break; - case 3: - nbt = StringUtils.stripToNull(match); - break; - default: - continue; - } - col++; - } - // Invalid row - if (itemName == null || numeric < 0) { - return null; - } - - Material material = Material.matchMaterial(itemName); - if (material == null) { - return null; - } - - return new LegacyItemData(itemName, material, numeric, data, nbt); - } - - private void addItem(LegacyItemData itemData) { - final String name = itemData.getItemName(); - final int numeric = itemData.getItemNo(); - final short data = itemData.getItemData(); - final String nbt = itemData.getNbt(); - - durabilities.put(name, data); - items.put(name, numeric); - - if (nbt != null) { - nbtData.put(itemData.getItemName(), nbt); - } - - if (names.containsKey(itemData)) { - List nameList = names.get(itemData); - nameList.add(name); - } else { - List nameList = new ArrayList<>(); - nameList.add(name); - names.put(itemData, nameList); - primaryNames.put(itemData, name); - } - - legacyIds.put(numeric, itemData); - } - - @Override - public boolean tryProvider() { - // Build the database initially so that we can actually test the provider - this.rebuild(this.loadResource("/items.csv")); - return super.tryProvider(); - } - - @Override - public String getHumanName() { - return "Pre-1.13 item database provider"; - } - - private boolean isInt(String integer) { - try { - Integer.parseInt(integer); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static class LegacyItemData extends ItemData { - private LegacyItemData(String itemName, Material material, final int legacyId, final short itemData, String nbt) { - this.itemName = itemName; - this.material = material; - this.legacyId = legacyId; - this.itemData = itemData; - this.nbt = nbt; - } - - @Override - public int hashCode() { - return (31 * legacyId) ^ itemData; - } - - @Override - public boolean equals(Object o) { - if (o == null) { - return false; - } - if (!(o instanceof ItemData)) { - return false; - } - ItemData pairo = (ItemData) o; - return this.legacyId == pairo.getItemNo() && this.itemData == pairo.getItemData(); - } - } -} diff --git a/nms/NMSProvider/src/net/ess3/nms/ItemDbProvider.java b/nms/NMSProvider/src/net/ess3/nms/ItemDbProvider.java deleted file mode 100644 index e4a3278cc..000000000 --- a/nms/NMSProvider/src/net/ess3/nms/ItemDbProvider.java +++ /dev/null @@ -1,236 +0,0 @@ -package net.ess3.nms; - -import net.ess3.providers.Provider; -import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionData; -import org.bukkit.potion.PotionType; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; - -public abstract class ItemDbProvider implements Provider { - - private SpawnerProvider spawnerProvider; - private SpawnEggProvider spawnEggProvider; - private PotionMetaProvider potionMetaProvider; - - /** - * Populate the item database using the given lines of data. - * - * @param lines The lines of data from which the database should be populated - */ - public abstract void addFrom(List lines); - - /** - * Reset the database and rebuild it from the given lines of data. - * - * @param lines The lines of the file from which the database should be built - */ - public abstract void rebuild(List lines); - - /** - * Read a resource from the jar. - * Used to build the database before data from a ManagedFile is available. - * - * @param name The name of the resource to load. - * @return The lines of the resource. - */ - protected List loadResource(final String name) { - try (InputStreamReader isr = new InputStreamReader(ItemDbProvider.class.getResourceAsStream(name))) { - BufferedReader br = new BufferedReader(isr); - return br.lines().collect(Collectors.toList()); - } catch (IOException e) { - return null; - } - } - - /** - * Resolves a material name to the corresponding ItemData. - * - * @param name The item name to look up - * @return The corresponding ItemData for the given name - */ - public abstract ItemData resolve(String name); - - /** - * Whether the provider supports legacy ID values or not. - * - * @return True if the provider supports legacy IDs, otherwise false - */ - public abstract boolean supportsLegacyIds(); - - /** - * Get the legacy ID for the material. - * - * @param material The material to look up - * @return The ID corresponding to the material, or null if not supported - */ - public abstract int getLegacyId(Material material) throws Exception; - - /** - * Get the material for the legacy ID. - * - * @param id The ID to look up - * @return The material corresponding to the ID, or -1 if not supported - */ - public abstract Material getFromLegacyId(int id); - - /** - * Get the primary name for the item in the given stack. - * - * @param item The ItemStack to check - * @return The primary name for the item - */ - public abstract String getPrimaryName(ItemStack item); - - /** - * Get all names for the item in the given stack. - * - * @param item The ItemStack to check - * @return The names for the item - */ - public abstract List getNames(ItemStack item); - - /** - * Creates a stack of a given item by its name. - * - * @param name The material name to look up - * @return An ItemStack of size 1 of the given item - */ - public abstract ItemStack getStack(String name) throws Exception; - - /** - * Creates a stack with the given amount of a given item by its name. - * - * @param name The material name to look up - * @param amount The amount of items in the returned ItemStack - * @return An ItemStack with the given amount of the given item - */ - public ItemStack getStack(String name, int amount) throws Exception { - ItemStack is = getStack(name); - is.setAmount(amount); - return is; - } - - /** - * Get all registered primary names for materials. - * This does not include any additional aliases. - * - * @return A collection of primary names - */ - public abstract Collection listNames(); - - @Override - public boolean tryProvider() { - try { - getStack("cobblestone"); - getStack("dstone"); - getStack("steelbar", 5); - getStack("splbreathlevel2pot"); - getStack("skeletonhorsespawnegg", 12); - return true; - } catch (Exception e) { - return false; - } - } - - /** - * Get the current spawner provider. - * - * @return The current spawner provider - */ - protected SpawnerProvider getSpawnerProvider() { - return spawnerProvider; - } - - /** - * Set the current spawner provider. - */ - public void setSpawnerProvider(SpawnerProvider spawnerProvider) { - this.spawnerProvider = spawnerProvider; - } - - /** - * Get the current spawn egg provider. - * - * @return The current spawn egg provider - */ - protected SpawnEggProvider getSpawnEggProvider() { - return spawnEggProvider; - } - - /** - * Set the current spawn egg provider. - */ - public void setSpawnEggProvider(SpawnEggProvider spawnEggProvider) { - this.spawnEggProvider = spawnEggProvider; - } - - /** - * Get the current potion provider. - * - * @return The current potion provider - */ - protected PotionMetaProvider getPotionMetaProvider() { - return potionMetaProvider; - } - - /** - * Set the current potion provider. - */ - public void setPotionMetaProvider(PotionMetaProvider potionMetaProvider) { - this.potionMetaProvider = potionMetaProvider; - } - - public abstract static class ItemData { - protected String itemName; - protected Material material; - protected int legacyId; - protected short itemData; - protected String nbt; - protected PotionData potionData; - - public String getItemName() { - return itemName; - } - - public Material getMaterial() { - return material; - } - - public int getItemNo() { - return legacyId; - } - - public short getItemData() { - return itemData; - } - - public String getNbt() { - return nbt; - } - - public PotionData getPotionData() { - return this.potionData; - } - } - - protected static class LengthCompare implements java.util.Comparator { - - public static final LengthCompare INSTANCE = new LengthCompare(); - - public LengthCompare() { - super(); - } - - @Override - public int compare(String s1, String s2) { - return s1.length() - s2.length(); - } - } -} diff --git a/pom.xml b/pom.xml index 4682d642f..f6b67fb0b 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,6 @@ nms/1_8_R2Provider nms/LegacyProvider nms/ReflectionProvider - nms/IdProvider nms/FlattenedProvider