Remove providers; move JSON support into ItemDb

This commit is contained in:
md678685 2018-10-12 17:26:22 +01:00
parent b70b67fa1f
commit 177e5f62f7
9 changed files with 168 additions and 852 deletions

View File

@ -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<String> 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();

View File

@ -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<String, ItemData> items = new HashMap<>();
// Maps alias to primary name
private final transient Map<String, String> itemAliases = new HashMap<>();
// Every known alias
private final transient Set<String> 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<String, JsonElement> 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<String> nameList = provider.getNames(item);
List<String> 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<String> nameList(ItemStack item) {
List<String> names = new ArrayList<>();
String primaryName = name(item);
names.add(primaryName);
for (Map.Entry<String, String> 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<String, ItemData> 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<String> 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;
}
}
}
}

View File

@ -15,6 +15,8 @@ public interface IItemDb {
String names(ItemStack item);
List<String> nameList(ItemStack item);
String name(ItemStack item);
List<ItemStack> getMatching(User user, String[] args) throws Exception;
@ -23,7 +25,9 @@ public interface IItemDb {
Collection<String> listNames();
@Deprecated
Material getFromLegacyId(int id);
@Deprecated
int getLegacyId(Material material) throws Exception;
}

View File

@ -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();
}

View File

@ -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<String, FlatItemData> primaryNames = new HashMap<>();
private final transient Map<String, List<String>> 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<String, FlatItemData> 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<String> getNames(ItemStack item) {
String primaryName = getPrimaryName(item);
for (Map.Entry<String, List<String>> 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<String> listNames() {
return Collections.unmodifiableSet(primaryNames.keySet());
}
private ItemData getByName(String name) {
if (primaryNames.containsKey(name.toLowerCase())) {
return primaryNames.get(name);
} else {
for (Map.Entry<String, List<String>> 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<String> lines) {
String json = lines.stream()
.filter(line -> !line.startsWith("#"))
.collect(Collectors.joining("\n"));
JsonObject map = (new JsonParser()).parse(json).getAsJsonObject();
for (Map.Entry<String, JsonElement> 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<String> lines) {
resetDb();
addFrom(lines);
}
private void addAlias(String primaryName, String alias) {
List<String> 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;
}
}
}
}

View File

@ -1,32 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EssentialsXParent</artifactId>
<groupId>net.ess3</groupId>
<version>2.15.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>IdProvider</artifactId>
<dependencies>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>ReflectionProvider</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -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<String, Integer> items = new HashMap<>();
// Maps item data to all known names for an item
private final transient Map<ItemData, List<String>> names = new HashMap<>();
// Maps item data to the primary name for an item
private final transient Map<ItemData, String> primaryNames = new HashMap<>();
// Maps legacy IDs to item data
private final transient Map<Integer, LegacyItemData> legacyIds = new HashMap<>();
// Maps names for an item to durability/meta values
private final transient Map<String, Short> durabilities = new HashMap<>();
// Maps names for an item to NBT data
private final transient Map<String, String> 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<String> listNames() {
return primaryNames.values();
}
@Override
public boolean supportsLegacyIds() {
return true;
}
@Override
public int getLegacyId(Material material) throws Exception {
for (Map.Entry<String, Integer> 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<String> getNames(ItemStack item) {
ItemData itemData = new LegacyItemData(null, item.getType(), item.getTypeId(), item.getDurability(), null);
List<String> 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<String> lines) {
lines.stream()
.filter(line -> line.length() > 0 && !(line.charAt(0) == '#'))
.map(this::parseLine)
.filter(Objects::nonNull)
.forEach(this::addItem);
for (List<String> nameList : names.values()) {
nameList.sort(LengthCompare.INSTANCE);
}
}
@Override
public void rebuild(List<String> 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<String> nameList = names.get(itemData);
nameList.add(name);
} else {
List<String> 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();
}
}
}

View File

@ -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<String> 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<String> 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<String> 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<String> 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<String> 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<String> {
public static final LengthCompare INSTANCE = new LengthCompare();
public LengthCompare() {
super();
}
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
}
}

View File

@ -47,7 +47,6 @@
<module>nms/1_8_R2Provider</module>
<module>nms/LegacyProvider</module>
<module>nms/ReflectionProvider</module>
<module>nms/IdProvider</module>
<module>nms/FlattenedProvider</module>
</modules>