Skip to content

Commit

Permalink
Add dItem.unbreakable property
Browse files Browse the repository at this point in the history
  • Loading branch information
Fortifier42 committed Nov 26, 2015
1 parent 6346723 commit 7d436b3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -791,6 +791,7 @@ public void onEnable() {
propertyParser.registerProperty(ItemQuantity.class, dItem.class);
propertyParser.registerProperty(ItemSkullskin.class, dItem.class);
propertyParser.registerProperty(ItemSpawnEgg.class, dItem.class);
propertyParser.registerProperty(ItemUnbreakable.class, dItem.class);
} catch (Exception e) {
dB.echoError(e);
}
Expand Down
@@ -0,0 +1,84 @@
package net.aufdemrand.denizen.objects.properties.item;

import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;

public class ItemUnbreakable implements Property {

public static boolean describes(dObject item) {
return item instanceof dItem;
}

public static ItemUnbreakable getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
return new ItemUnbreakable((dItem) _item);
}

private ItemUnbreakable(dItem _item) {
item = _item;
}

dItem item;

public String getAttribute(Attribute attribute) {
if (attribute == null) {
return "null";
}

// <--[tag]
// @attribute <i@item.unbreakable>
// @returns Element(Boolean)
// @group properties
// @mechanism dItem.unbreakable
// @description
// Returns whether an item has the unbreakable flag.
// -->
if (attribute.startsWith("unbreakable")) {
return new Element(Boolean.valueOf(getPropertyString() != null)).getAttribute(attribute.fulfill(1));
}

return null;
}

public String getPropertyString() {
ItemStack itemStack = CraftItemStack.asNMSCopy(item.getItemStack());
if ((itemStack != null) && (itemStack.hasTag()) &&
(itemStack.getTag().getBoolean("Unbreakable"))) {
return "true";
}
return null;
}

public String getPropertyId() {
return "unbreakable";
}

public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object dItem
// @name unbreakable
// @input Element(Boolean)
// @description
// Changes whether an item has the unbreakable item flag.
// @tags
// <i@item.unbreakable>
// -->
if ((mechanism.matches("unbreakable")) && (mechanism.requireBoolean())) {
ItemStack itemStack = CraftItemStack.asNMSCopy(item.getItemStack());
NBTTagCompound tag = itemStack.hasTag() ? itemStack.getTag() : new NBTTagCompound();
tag.setInt("Unbreakable", mechanism.getValue().asBoolean() ? 1 : 0);
itemStack.setTag(tag);
item.setItemStack(CraftItemStack.asBukkitCopy(itemStack));
}
}
}

0 comments on commit 7d436b3

Please sign in to comment.