Skip to content

Commit

Permalink
Add .color for potion item color support (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
MusicScore authored and mcmonkey4eva committed Jun 8, 2018
1 parent c3d8803 commit 7c99a3e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 95 deletions.
2 changes: 1 addition & 1 deletion plugin/src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -856,7 +856,7 @@ public void onEnable() {
propertyParser.registerProperty(ItemBook.class, dItem.class);
propertyParser.registerProperty(ItemDisplayname.class, dItem.class);
propertyParser.registerProperty(ItemDurability.class, dItem.class);
propertyParser.registerProperty(ItemDye.class, dItem.class);
propertyParser.registerProperty(ItemColor.class, dItem.class);
propertyParser.registerProperty(ItemEnchantments.class, dItem.class);
propertyParser.registerProperty(ItemFirework.class, dItem.class);
propertyParser.registerProperty(ItemFlags.class, dItem.class);
Expand Down
16 changes: 12 additions & 4 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -742,17 +742,25 @@ public String run(Attribute attribute, dObject object) {
});

// <--[tag]
// @attribute <i@item.is_dyeable>
// @attribute <i@item.is_colorable>
// @returns Element(Boolean)
// @group properties
// Returns whether the item can have a dye.
// Returns whether the item can have a custom color.
// If this returns true, it will enable access to:
// <@link mechanism dItem.dye>, and <@link tag i@item.dye_color>
// <@link mechanism dItem.color>, and <@link tag i@item.color>
// -->
registerTag("is_colorable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(ItemColor.describes(object))
.getAttribute(attribute.fulfill(1));
}
});

registerTag("is_dyeable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(ItemDye.describes(object))
return new Element(ItemColor.describes(object))
.getAttribute(attribute.fulfill(1));
}
});
Expand Down
@@ -0,0 +1,124 @@
package net.aufdemrand.denizen.objects.properties.item;

import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.objects.dColor;
import net.aufdemrand.denizen.objects.dItem;
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 org.bukkit.Material;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;

public class ItemColor implements Property {

public static boolean describes(dObject item) {
// Leather armor and potions
return item instanceof dItem
&& (((dItem) item).getItemStack().getType() == Material.LEATHER_BOOTS
|| ((dItem) item).getItemStack().getType() == Material.LEATHER_CHESTPLATE
|| ((dItem) item).getItemStack().getType() == Material.LEATHER_HELMET
|| ((dItem) item).getItemStack().getType() == Material.LEATHER_LEGGINGS
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2)
&& (((dItem) item).getItemStack().getType() == Material.POTION
|| ((dItem) item).getItemStack().getType() == Material.SPLASH_POTION
|| ((dItem) item).getItemStack().getType() == Material.LINGERING_POTION)));
}

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

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

dItem item;

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) {
return null;
}

// <--[tag]
// @attribute <i@item.color>
// @returns dColor
// @mechanism dItem.color
// @group properties
// @description
// Returns the color of the leather armor item or potion item.
// -->
if (attribute.startsWith("color") || attribute.startsWith("dye_color")) {
Material mat = item.getItemStack().getType();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2)
&& (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION)) {
return new dColor(((PotionMeta) item.getItemStack().getItemMeta()).getColor()).getAttribute(attribute.fulfill((1)));
}
return new dColor(((LeatherArmorMeta) item.getItemStack().getItemMeta()).getColor()).getAttribute(attribute.fulfill(1));
}

return null;
}


@Override
public String getPropertyString() {
Material mat = item.getItemStack().getType();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2)
&& (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION)) {
return new dColor(((PotionMeta) item.getItemStack().getItemMeta()).getColor()).identify();
}
return new dColor(((LeatherArmorMeta) item.getItemStack().getItemMeta()).getColor()).identify();
}

@Override
public String getPropertyId() {
return "color";
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object dItem
// @name color
// @input dColor
// @description
// Sets the leather armor item's dye color or the potion item's color in the format RED,GREEN,BLUE.
// @tags
// <i@item.color>
// <i@item.dye_color>
// -->
if ((mechanism.matches("dye") || mechanism.matches("dye_color")
|| mechanism.matches("color")) && (mechanism.requireObject(dColor.class))) {
dColor color = mechanism.getValue().asType(dColor.class);
Material mat = item.getItemStack().getType();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2)
&& (mat == Material.POTION
|| mat == Material.LINGERING_POTION
|| mat == Material.SPLASH_POTION)) {
PotionMeta meta = (PotionMeta) item.getItemStack().getItemMeta();
meta.setColor(color.getColor());
item.getItemStack().setItemMeta(meta);
return;
}
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemStack().getItemMeta();
meta.setColor(color.getColor());
item.getItemStack().setItemMeta(meta);
}

}
}

This file was deleted.

0 comments on commit 7c99a3e

Please sign in to comment.