Skip to content

Commit

Permalink
add ItemSkullskin property
Browse files Browse the repository at this point in the history
For human_skull (skull_item)'s
Now they won't lose their owner
  • Loading branch information
mcmonkey4eva committed Dec 16, 2013
1 parent 9a9a672 commit 29b2425
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 23 deletions.
54 changes: 33 additions & 21 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -2,8 +2,7 @@

import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.Item.ItemDisplayname;
import net.aufdemrand.denizen.objects.properties.Item.ItemDurability;
import net.aufdemrand.denizen.objects.properties.Item.*;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
Expand Down Expand Up @@ -529,6 +528,16 @@ public String getAttribute(Attribute attribute) {
return new Element(ItemDurability.describes(this))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.has_lore>
// @returns Element(Boolean)
// @description
// Returns whether the item has lore set on it.
// -->
if (attribute.startsWith("has_lore"))
return new Element(ItemLore.describes(this))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.material.formatted>
// @returns Element
Expand Down Expand Up @@ -595,24 +604,7 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.skin>
// @returns Element
// @description
// Returns the name of the player whose skin a skull item uses.
// Note: Item must be a 'skull_item' with a skin.
// -->
if (attribute.startsWith("skin")) {
if (getItemStack().getType() == Material.SKULL_ITEM) {
SkullMeta skullInfo = (SkullMeta) getItemStack().getItemMeta();

if (skullInfo.hasOwner()) {
return new Element(skullInfo.getOwner())
.getAttribute(attribute.fulfill(1));
}
}
}

// TODO: Property for Book info
if (attribute.startsWith("book")) {
if (getItemStack().getType() == Material.WRITTEN_BOOK) {
attribute.fulfill(1);
Expand Down Expand Up @@ -808,12 +800,32 @@ public void adjust(Mechanism mechanism) {
// <i@item.repairable>
// -->
if (mechanism.matches("durability") && mechanism.requireInteger()) {
if (isRepairable())
if (ItemDurability.describes(this))
item.setDurability((short)value.asInt());
else
dB.echoError("Material '" + getMaterial().identify().replace("m@", "") + "' is not repairable.");
}

// <--[mechanism]
// @object dItem
// @name skull_skin
// @input Element
// @description
// Changes the durability of damageable items.
// @tags
// <i@item.skin>
// <i@item.has_skin>
// -->
if (mechanism.matches("skull_skin")) {
if (ItemSkullskin.describes(this)) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
meta.setOwner(value.asString());
item.setItemMeta(meta);
}
else
dB.echoError("Material '" + getMaterial().identify().replace("m@", "") + "' cannot hold a skin.");
}


if (!mechanism.fulfilled())
mechanism.reportInvalid();
Expand Down
Expand Up @@ -285,7 +285,6 @@ public static enum dMaterials { WHITE_WOOL, ORANGE_WOOL, MAGENTA_WOOL, LIGHT_BLU
public final static dMaterial SKELETON_SKULL = new dMaterial(Material.SKULL_ITEM, 0).forceIdentifyAs("SKELETON_SKULL");
public final static dMaterial WITHERSKELETON_SKULL = new dMaterial(Material.SKULL_ITEM, 1).forceIdentifyAs("WITHERSKELETON_SKULL");
public final static dMaterial ZOMBIE_SKULL = new dMaterial(Material.SKULL_ITEM, 2).forceIdentifyAs("ZOMBIE_SKULL");
// TODO: Human skull skin property
public final static dMaterial HUMAN_SKULL = new dMaterial(Material.SKULL_ITEM, 3).forceIdentifyAs("HUMAN_SKULL");
public final static dMaterial CREEPER_SKULL = new dMaterial(Material.SKULL_ITEM, 4).forceIdentifyAs("CREEPER_SKULL");

Expand Down
Expand Up @@ -6,7 +6,6 @@
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.tags.Attribute;
import org.bukkit.inventory.meta.Repairable;

public class ItemDurability implements Property {

Expand Down
@@ -0,0 +1,87 @@
package net.aufdemrand.denizen.objects.properties.Item;


import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Material;
import org.bukkit.inventory.meta.SkullMeta;

public class ItemSkullskin implements Property {

public static boolean describes(dObject item) {
return item instanceof dItem
&& ((dItem) item).getItemStack().getType() == Material.SKULL_ITEM
&& ((dItem) item).getItemStack().getData().getData() == 3;
}

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


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

dItem item;

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) return "null";

// <--[tag]
// @attribute <i@item.skin>
// @returns Element
// @description
// Returns the name of the player whose skin a skull item uses.
// Note: Item must be a 'skull_item' with a skin.
// -->
if (attribute.startsWith("skin")) {
if (item.getItemStack().hasItemMeta()
&& item.getItemStack().getItemMeta() instanceof SkullMeta
&& ((SkullMeta)item.getItemStack().getItemMeta()).hasOwner())
return new Element(((SkullMeta)item.getItemStack().getItemMeta()).getOwner())
.getAttribute(attribute.fulfill(1));
else
dB.echoError("This skull_item does not have a skin set!");
}

// <--[tag]
// @attribute <i@item.has_skin>
// @returns Element(Boolean)
// @description
// Returns whether the item has a custom skin set.
// (Only for human 'skull_item's)
// -->
if (attribute.startsWith("has_skin"))
return new Element(item.getItemStack().hasItemMeta()
&& item.getItemStack().getItemMeta() instanceof SkullMeta
&& ((SkullMeta)item.getItemStack().getItemMeta()).hasOwner())
.getAttribute(attribute.fulfill(1));


return null;
}


@Override
public String getPropertyString() {
if (item.getItemStack().hasItemMeta()
&& item.getItemStack().getItemMeta() instanceof SkullMeta
&& ((SkullMeta)item.getItemStack().getItemMeta()).hasOwner())
return ((SkullMeta)item.getItemStack().getItemMeta()).getOwner();
else
return null;
}

@Override
public String getPropertyId() {
return "skull_skin";
}
}
Expand Up @@ -39,6 +39,7 @@ public PropertyParser() {
registerProperty(ItemLore.class, dItem.class);
registerProperty(ItemQuantity.class, dItem.class);
registerProperty(ItemDurability.class, dItem.class);
registerProperty(ItemSkullskin.class, dItem.class);

}

Expand Down

0 comments on commit 29b2425

Please sign in to comment.