Skip to content

Commit

Permalink
Support legacy IDs, but add warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Jul 18, 2018
1 parent c5d9cf1 commit d0f263b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 77 deletions.
59 changes: 21 additions & 38 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -141,17 +141,19 @@ else if (ScriptRegistry.containsScript(m.group(1), BookScriptContainer.class)) {
try {
String material = m.group(1).toUpperCase();

// TODO: 1.13 - should we rework this?
// if (aH.matchesInteger(material)) {
// stack = new dItem(Integer.valueOf(material));
// }
// else {
if (aH.matchesInteger(material)) {
if (!nope) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
}
stack = new dItem(Integer.valueOf(material));
}
else {
dMaterial mat = dMaterial.valueOf(material);
stack = new dItem(mat.getMaterial());
if (mat.hasData()) {
stack.setDurability(mat.getData());
}
// }
}

if (m.group(2) != null) {
stack.setDurability(Short.valueOf(m.group(2)));
Expand Down Expand Up @@ -221,14 +223,15 @@ public dItem(Material material) {
this(new ItemStack(material));
}

// TODO: 1.13 - should we rework these?
// public dItem(int itemId) {
// this(new ItemStack(itemId));
// }
@Deprecated
public dItem(int itemId) {
this(new ItemStack(dMaterial.getLegacyMaterial(itemId)));
}

// public dItem(int type, int qty) {
// this(new ItemStack(type, qty));
// }
@Deprecated
public dItem(int itemId, int qty) {
this(new ItemStack(dMaterial.getLegacyMaterial(itemId), qty));
}

public dItem(Material material, int qty) {
this(new ItemStack(material, qty));
Expand Down Expand Up @@ -540,8 +543,7 @@ public String identifySimple() {
return "null";
}

// TODO: 1.13 - what is this check???
if (item.getType().getId() != 0) {
if (item.getType() != Material.AIR) {

// If saved item, return that
if (isUnique()) {
Expand Down Expand Up @@ -576,8 +578,7 @@ public String identifyNoIdentifier() {
return "null";
}

// TODO: 1.13 - what is this check???
if (item.getType().getId() != 0) {
if (item.getType() != Material.AIR) {

// If saved item, return that
if (isUnique()) {
Expand All @@ -602,8 +603,7 @@ public String identifySimpleNoIdentifier() {
return "null";
}

// TODO: 1.13 - what is this check???
if (item.getType().getId() != 0) {
if (item.getType() != Material.AIR) {

// If saved item, return that
if (isUnique()) {
Expand Down Expand Up @@ -657,36 +657,19 @@ public void forget() {

public static void registerTags() {

// <--[tag]
// @attribute <i@item.id>
// @returns Element(Number)
// @group deprecated info
// @description
// Returns the item ID number of the item.
// EG, a stone item will return 1.
// Note that ID numbers are considered deprecated - you should use the names instead!
// -->
registerTag("id", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
// TODO: 1.13 - should we keep this
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
return new Element(((dItem) object).getItemStack().getType().getId())
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <i@item.data>
// @returns Element(Number)
// @group deprecated info
// @description
// Returns the data value of the material of the item.
// EG, white wool will return 0, while red wool will return 14.
// Note that data values are considered deprecated - you should use the names instead!
// -->
registerTag("data", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
return new Element(((dItem) object).getItemStack().getData().getData())
.getAttribute(attribute.fulfill(1));
}
Expand Down
100 changes: 62 additions & 38 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.util.ReflectionHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Fetchable;
Expand All @@ -13,6 +14,7 @@
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.material.MaterialData;
Expand Down Expand Up @@ -93,6 +95,30 @@ public static enum dMaterials {
// dMaterials are just made and disposed of for standard 'Materials', but these we will keep around since
// they are special :)

private static final Map<Integer, Material> MATERIAL_BY_LEGACY_ID;

static {
MATERIAL_BY_LEGACY_ID = new HashMap<Integer, Material>();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R1)) {
Map<String, Material> map = ReflectionHelper.getFieldValue(Material.class, "BY_NAME", null);
for (Material material : map.values()) {
if (material.isLegacy()) {
MATERIAL_BY_LEGACY_ID.put(material.getId(), Bukkit.getUnsafe().fromLegacy(material));
}
}
}
else {
for (Material material : Material.values()) {
MATERIAL_BY_LEGACY_ID.put(material.getId(), material);
}
}
}

@Deprecated
public static Material getLegacyMaterial(int id) {
return MATERIAL_BY_LEGACY_ID.get(id);
}

public static dMaterial getMaterialPre1_13(String material, int data, String name) {
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1)) {
return new dMaterial(Material.valueOf(material), data).forceIdentifyAs(name);
Expand Down Expand Up @@ -624,14 +650,16 @@ public static dMaterial valueOf(String string, TagContext context) {
}
return getMaterialFrom(mat.material, data);
}
// TODO: 1.13 - re-implement?
// int matid = aH.getIntegerFrom(string);
// if (matid != 0) {
// m = Material.getMaterial(matid);
// if (m != null) {
// return getMaterialFrom(m, data);
// }
// }
int matid = aH.getIntegerFrom(string);
if (matid != 0) {
if (!nope) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
}
m = getLegacyMaterial(matid);
if (m != null) {
return getMaterialFrom(m, data);
}
}
return null;
}

Expand Down Expand Up @@ -660,6 +688,10 @@ public static dMaterial quickOfNamed(String string) {
return null;
}

// :( boolean for technicality, can be fixed
// by making matches() method better.
public static boolean nope = false;

/**
* Determine whether a string is a valid material.
*
Expand All @@ -671,10 +703,14 @@ public static boolean matches(String arg) {
if (arg.startsWith("m@")) {
return true;
}
// TODO: Make this better. Probably creating some unnecessary
// objects by doing this :(
nope = true;
if (valueOf(arg) != null) {
nope = false;
return true;
}

nope = false;
return false;
}

Expand Down Expand Up @@ -877,30 +913,34 @@ public dObject setPrefix(String prefix) {

public static void registerTags() {

// <--[tag]
// @attribute <m@material.has_gravity>
// @returns Element(Boolean)
// @description
// Returns whether the material is affected by gravity.
// -->
registerTag("has_gravity", new TagRunnable() {
registerTag("id", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.hasGravity())
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
return new Element(((dMaterial) object).material.getId())
.getAttribute(attribute.fulfill(1));
}
});

registerTag("data", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release.");
return new Element(((dMaterial) object).getData())
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <m@material.id>
// @returns Element(Number)
// @attribute <m@material.has_gravity>
// @returns Element(Boolean)
// @description
// Returns the material's ID.
// Returns whether the material is affected by gravity.
// -->
registerTag("id", new TagRunnable() {
registerTag("has_gravity", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.getId())
return new Element(((dMaterial) object).material.hasGravity())
.getAttribute(attribute.fulfill(1));
}
});
Expand Down Expand Up @@ -1116,22 +1156,6 @@ public String run(Attribute attribute, dObject object) {
}
});

// <--[tag]
// @attribute <m@material.data>
// @returns Element(Number)
// @description
// Returns the bukkit Material data value. For example: <m@red_clay.data>
// will return '14'. Note: This kind of 'material identification' has been deprecated
// by bukkit and should be used sparingly.
// -->
registerTag("data", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).getData())
.getAttribute(attribute.fulfill(1));
}
});

// <--[tag]
// @attribute <m@material.item>
// @returns dItem
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/resources/plugin.yml
Expand Up @@ -4,6 +4,8 @@ version: ${project.version} (build ${BUILD_NUMBER})
main: net.aufdemrand.denizen.Denizen
softdepend: [Citizens, Vault]

api-version: 1.13

commands:
denizen:
description: Lists denizen commands.
Expand Down Expand Up @@ -61,4 +63,4 @@ permissions:
denizen.npc.assign: true
denizen.npc.constants: true
denizen.npc.pushable: true

0 comments on commit d0f263b

Please sign in to comment.