Skip to content
Permalink
Browse files

Initial removal of item IDs.

We do not rely on Bukkit's item ids anymore, though we still support them in commands via a mapping built off of the items.csv.
  • Loading branch information...
drtshock committed Dec 13, 2017
1 parent dcbc106 commit 1a820ad9b7d2564d836f3044f770aec56c0b04a1
Showing with 225 additions and 141 deletions.
  1. +3 −3 Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
  2. +3 −2 Essentials/src/com/earth2me/essentials/ISettings.java
  3. +2 −1 Essentials/src/com/earth2me/essentials/IUser.java
  4. +60 −23 Essentials/src/com/earth2me/essentials/ItemDb.java
  5. +9 −8 Essentials/src/com/earth2me/essentials/Settings.java
  6. +3 −2 Essentials/src/com/earth2me/essentials/User.java
  7. +24 −14 Essentials/src/com/earth2me/essentials/UserData.java
  8. +35 −11 Essentials/src/com/earth2me/essentials/Worth.java
  9. +5 −0 Essentials/src/com/earth2me/essentials/api/IItemDb.java
  10. +2 −1 Essentials/src/com/earth2me/essentials/commands/Commandgive.java
  11. +4 −1 Essentials/src/com/earth2me/essentials/commands/Commanditem.java
  12. +5 −3 Essentials/src/com/earth2me/essentials/commands/Commanditemdb.java
  13. +5 −2 Essentials/src/com/earth2me/essentials/commands/Commandmore.java
  14. +10 −5 Essentials/src/com/earth2me/essentials/commands/Commandrepair.java
  15. +1 −1 Essentials/src/com/earth2me/essentials/commands/Commandsell.java
  16. +2 −2 Essentials/src/com/earth2me/essentials/commands/Commandsetworth.java
  17. +6 −6 Essentials/src/com/earth2me/essentials/commands/Commandunlimited.java
  18. +1 −1 Essentials/src/com/earth2me/essentials/commands/Commandworth.java
  19. +8 −22 Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java
  20. +7 −6 EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuild.java
  21. +20 −20 EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java
  22. +3 −2 EssentialsAntiBuild/src/com/earth2me/essentials/antibuild/IAntiBuild.java
  23. +5 −4 EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java
  24. +2 −1 EssentialsProtect/src/com/earth2me/essentials/protect/IProtect.java
@@ -596,7 +596,7 @@ public void onPlayerInteract(final PlayerInteractEvent event) {
case LEFT_CLICK_BLOCK:
if (event.getItem() != null && event.getItem().getType() != Material.AIR) {
final User user = ess.getUser(event.getPlayer());
if (user.hasPowerTools() && user.arePowerToolsEnabled() && usePowertools(user, event.getItem().getTypeId())) {
if (user.hasPowerTools() && user.arePowerToolsEnabled() && usePowertools(user, event.getItem().getType())) {
event.setCancelled(true);
}
}
@@ -630,8 +630,8 @@ public void run() {
}
}

private boolean usePowertools(final User user, final int id) {
final List<String> commandList = user.getPowertool(id);
private boolean usePowertools(final User user, final Material material) {
final List<String> commandList = user.getPowertool(material);
if (commandList == null || commandList.isEmpty()) {
return false;
}
@@ -5,6 +5,7 @@
import com.earth2me.essentials.textreader.IText;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.EventPriority;

@@ -80,7 +81,7 @@

int getProtectCreeperMaxHeight();

List<Integer> getProtectList(final String configName);
List<Material> getProtectList(final String configName);

boolean getProtectPreventSpawn(final String creatureName);

@@ -120,7 +121,7 @@

boolean isTradeInStacks(int id);

List<Integer> itemSpawnBlacklist();
List<Material> itemSpawnBlacklist();

List<EssentialsSign> enabledSigns();

@@ -4,6 +4,7 @@
import net.ess3.api.ITeleport;
import net.ess3.api.MaxMoneyException;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.math.BigDecimal;
@@ -35,7 +36,7 @@

boolean canAfford(BigDecimal value);

Boolean canSpawnItem(final int itemId);
Boolean canSpawnItem(final Material material);

void setLastLocation();

@@ -20,17 +20,20 @@
import org.bukkit.potion.PotionEffect;

import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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 final transient Map<String, Integer> items = new HashMap<>();
private final transient Map<ItemData, List<String>> names = new HashMap<>();
private final transient Map<ItemData, String> primaryName = new HashMap<>();
private final transient Map<Integer, ItemData> legacyIds = new HashMap<>();
private final transient Map<String, Short> durabilities = new HashMap<>();
private final transient Map<String, String> nbtData = new HashMap<>();
private final transient ManagedFile file;
@@ -95,13 +98,19 @@ public void reloadConfig() {
if (itemName == null || numeric < 0) {
continue;
}

Material material = Material.matchMaterial(itemName);
if (material == null) {
LOGGER.warning(String.format("Failed to find material for %s", itemName));
continue;
}
durabilities.put(itemName, data);
items.put(itemName, numeric);
if (nbt != null) {
nbtData.put(itemName, nbt);
}

ItemData itemData = new ItemData(numeric, data);
ItemData itemData = new ItemData(material, numeric, data);
if (names.containsKey(itemData)) {
List<String> nameList = names.get(itemData);
nameList.add(itemName);
@@ -111,6 +120,8 @@ public void reloadConfig() {
names.put(itemData, nameList);
primaryName.put(itemData, itemName);
}

legacyIds.put(numeric, itemData);
}

for (List<String> nameList : names.values()) {
@@ -152,27 +163,19 @@ public ItemStack get(final String id) throws Exception {
if (durabilities.containsKey(itemname) && metaData == 0) {
metaData = durabilities.get(itemname);
}
} else if (Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH)) != null) {
Material bMaterial = Material.getMaterial(itemname.toUpperCase(Locale.ENGLISH));
itemid = bMaterial.getId();
} else {
try {
Material bMaterial = Bukkit.getUnsafe().getMaterialFromInternalName(itemname.toLowerCase(Locale.ENGLISH));
itemid = bMaterial.getId();
} catch (Throwable throwable) {
throw new Exception(tl("unknownItemName", itemname), throwable);
}
}
}

if (itemid < 1) {
throw new Exception(tl("unknownItemName", itemname));
}

final Material mat = Material.getMaterial(itemid);
if (mat == null) {
ItemData data = legacyIds.get(itemid);
if (data == null) {
throw new Exception(tl("unknownItemId", itemid));
}

Material mat = data.getMaterial();
ItemStack retval = new ItemStack(mat);
if (nbtData.containsKey(itemname)) {
String nbt = nbtData.get(itemname);
@@ -241,10 +244,10 @@ public ItemStack get(final String id) throws Exception {

@Override
public String names(ItemStack item) {
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
ItemData itemData = new ItemData(item.getType(), item.getDurability());
List<String> nameList = names.get(itemData);
if (nameList == null) {
itemData = new ItemData(item.getTypeId(), (short) 0);
itemData = new ItemData(item.getType(), (short) 0);
nameList = names.get(itemData);
if (nameList == null) {
return null;
@@ -259,10 +262,10 @@ public String names(ItemStack item) {

@Override
public String name(ItemStack item) {
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());
ItemData itemData = new ItemData(item.getType(), item.getDurability());
String name = primaryName.get(itemData);
if (name == null) {
itemData = new ItemData(item.getTypeId(), (short) 0);
itemData = new ItemData(item.getType(), (short) 0);
name = primaryName.get(itemData);
if (name == null) {
return null;
@@ -415,22 +418,56 @@ public String serialize(ItemStack is) {
return sb.toString().trim().replaceAll("§", "&");
}

@Override
public Material getFromLegacyId(int id) {
ItemData data = this.legacyIds.get(id);
if(data == null) {
return null;
}

return data.getMaterial();
}

@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("Itemid not found for material: " + material.name());
}

@Override
public Collection<String> listNames() {
return primaryName.values();
}

static class ItemData {
final private int itemNo;
final private Material material;
private int legacyId;
final private short itemData;

ItemData(final int itemNo, final short itemData) {
this.itemNo = itemNo;
ItemData(Material material, short itemData) {
this.material = material;
this.itemData = itemData;
}

@Deprecated
ItemData(Material material, final int legacyId, final short itemData) {
this.material = material;
this.legacyId = legacyId;
this.itemData = itemData;
}

public Material getMaterial() {
return material;
}

@Deprecated
public int getItemNo() {
return itemNo;
return legacyId;
}

public short getItemData() {
@@ -439,7 +476,7 @@ public short getItemData() {

@Override
public int hashCode() {
return (31 * itemNo) ^ itemData;
return (31 * material.hashCode()) ^ itemData;
}

@Override
@@ -451,7 +488,7 @@ public boolean equals(Object o) {
return false;
}
ItemData pairo = (ItemData) o;
return this.itemNo == pairo.getItemNo() && this.itemData == pairo.getItemData();
return this.material == pairo.getMaterial() && this.itemData == pairo.getItemData();
}
}

@@ -10,6 +10,7 @@

import net.ess3.api.IEssentials;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.event.EventPriority;
@@ -555,15 +556,15 @@ public void reloadConfig() {
unprotectedSigns = _getUnprotectedSign();
}

private List<Integer> itemSpawnBl = new ArrayList<Integer>();
private List<Material> itemSpawnBl = new ArrayList<Material>();

@Override
public List<Integer> itemSpawnBlacklist() {
public List<Material> itemSpawnBlacklist() {
return itemSpawnBl;
}

private List<Integer> _getItemSpawnBlacklist() {
final List<Integer> epItemSpwn = new ArrayList<Integer>();
private List<Material> _getItemSpawnBlacklist() {
final List<Material> epItemSpwn = new ArrayList<>();
if (ess.getItemDb() == null) {
logger.log(Level.FINE, "Aborting ItemSpawnBL read, itemDB not yet loaded.");
return epItemSpwn;
@@ -575,7 +576,7 @@ public void reloadConfig() {
}
try {
final ItemStack iStack = ess.getItemDb().get(itemName);
epItemSpwn.add(iStack.getTypeId());
epItemSpwn.add(iStack.getType());
} catch (Exception ex) {
logger.log(Level.SEVERE, tl("unknownItemInList", itemName, "item-spawn-blacklist"));
}
@@ -685,8 +686,8 @@ public boolean getProtectPreventSpawn(final String creatureName) {
}

@Override
public List<Integer> getProtectList(final String configName) {
final List<Integer> list = new ArrayList<Integer>();
public List<Material> getProtectList(final String configName) {
final List<Material> list = new ArrayList<>();
for (String itemName : config.getString(configName, "").split(",")) {
itemName = itemName.trim();
if (itemName.isEmpty()) {
@@ -695,7 +696,7 @@ public boolean getProtectPreventSpawn(final String creatureName) {
ItemStack itemStack;
try {
itemStack = ess.getItemDb().get(itemName);
list.add(itemStack.getTypeId());
list.add(itemStack.getType());
} catch (Exception ex) {
logger.log(Level.SEVERE, tl("unknownItemInList", itemName, configName));
}
@@ -18,6 +18,7 @@

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;
@@ -222,8 +223,8 @@ private void _dispose() {
}

@Override
public Boolean canSpawnItem(final int itemId) {
return !ess.getSettings().itemSpawnBlacklist().contains(itemId);
public Boolean canSpawnItem(final Material material) {
return !ess.getSettings().itemSpawnBlacklist().contains(material);
}

@Override

1 comment on commit 1a820ad

@drtshock

This comment has been minimized.

Copy link
Member Author

commented on 1a820ad Dec 13, 2017

I deprecated the wrong item data. Oops.

Please sign in to comment.
You can’t perform that action at this time.