Skip to content

Commit

Permalink
Add ItemPlantgrowth property
Browse files Browse the repository at this point in the history
Plant growth is currently handled rather poorly by Bukkit...
nonetheless, here's a working property for it!
  • Loading branch information
mcmonkey4eva committed Dec 17, 2013
1 parent 914420d commit 0c5cac5
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -193,6 +193,14 @@ public <T extends dObject> T asType(Class<T> dClass) {
return ObjectFetcher.getObjectFrom(dClass, element);
}

public boolean matchesEnum(Enum[] values) {
for (Enum value : values)
if (value.name().replace("_", "").equalsIgnoreCase(element.replace("_", "")))
return true;

return false;
}

private String prefix;

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/Mechanism.java
Expand Up @@ -49,8 +49,9 @@ public boolean requireDouble() {
}

public boolean requireEnum(boolean allowInt, Enum<?>... values) {
return requireEnum("Invalid " + values[0].getDeclaringClass().getName() + "."
+ " Must specify valid name" + (allowInt ? " or number" : "") + ".", allowInt, values);
// TODO: Remove getSimpleName(), or simplify somehow.
return requireEnum("Invalid " + values[0].getDeclaringClass().getSimpleName() + "."
+ " Must specify a valid name" + (allowInt ? " or number" : "") + ".", allowInt, values);
}

public boolean requireFloat() {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/net/aufdemrand/denizen/objects/Properties.java

This file was deleted.

49 changes: 43 additions & 6 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -10,23 +10,23 @@
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.material.CocoaPlant;
import org.bukkit.material.Crops;
import org.bukkit.material.NetherWarts;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class dItem implements dObject, Notable, Properties, Adjustable {
public class dItem implements dObject, Notable, Adjustable {

// An item pattern with the following groups:
//
Expand Down Expand Up @@ -214,7 +214,7 @@ public dItem(Material material, int qty) {
}

public dItem(dMaterial material, int qty) {
item = new ItemStack(material.getMaterial(), qty);
item = new ItemStack(material.getMaterial(), qty, (short)0, material.getData());
}

public dItem(int type, int qty) {
Expand Down Expand Up @@ -538,6 +538,16 @@ public String getAttribute(Attribute attribute) {
return new Element(ItemLore.describes(this))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.is_crop>
// @returns Element(Boolean)
// @description
// Returns whether the item is a growable crop.
// -->
if (attribute.startsWith("is_crop"))
return new Element(ItemPlantgrowth.describes(this))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.material.formatted>
// @returns Element
Expand Down Expand Up @@ -826,6 +836,33 @@ public void adjust(Mechanism mechanism) {
dB.echoError("Material '" + getMaterial().identify().replace("m@", "") + "' cannot hold a skin.");
}

// <--[mechanism]
// @object dItem
// @name plant_growth
// @input Element
// @description
// Changes the growth level of plant items.
// See <@link tag i@item.plant_growth> for valid inputs.
// @tags
// <i@item.is_crop>
// <i@item.plant_growth>
// -->
if (mechanism.matches("plant_growth")) {
if (ItemPlantgrowth.describes(this)) {
Element inputValue = new Element(value.asString().toUpperCase());
if (item.getData() instanceof Crops && inputValue.matchesEnum(CropState.values()))
((Crops)item.getData()).setState(CropState.valueOf(value.asString().toUpperCase()));
else if (item.getData() instanceof NetherWarts && inputValue.matchesEnum(NetherWartsState.values()))
((NetherWarts)item.getData()).setState(NetherWartsState.valueOf(value.asString().toUpperCase()));
else if (item.getData() instanceof CocoaPlant && inputValue.matchesEnum(CocoaPlant.CocoaPlantSize.values()))
((CocoaPlant)item.getData()).setSize(CocoaPlant.CocoaPlantSize.valueOf(value.asString().toUpperCase()));
else if (mechanism.requireInteger())
item.getData().setData((byte) value.asInt());
}
else
dB.echoError("Material '" + getMaterial().identify().replace("m@", "") + "' is not a plant.");
}


if (!mechanism.fulfilled())
mechanism.reportInvalid();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -357,9 +357,8 @@ public static enum dMaterials { WHITE_WOOL, ORANGE_WOOL, MAGENTA_WOOL, LIGHT_BLU

// TODO: The following would be walls of useless materials, make properties for these instead of custom mats
// Step rotations [rotation=(north/west/south/east)(up/down)] for each of the step blocks
// Rotations for chests/furnaces/pumpkins/etc [rotation=(north/south/east/west)] for each of those types
// Rotations for chests/furnaces/pumpkins/cocoa/etc [rotation=(north/south/east/west)] for each of those types
// Rotations and open/closedness for doors/gates/etc. [Unknown?]
// Plant growths [growth=(0-15)]
// Potions [potion_effect=(<name>)(<level>)(splash?)]
// Leather colors [leather_color=(<dColor>)] or [leather_color=(dye_one)(dye_two)]

Expand Down Expand Up @@ -835,6 +834,16 @@ else return new Element(material.name().equalsIgnoreCase(compared.getMaterial().
return new Element(getData())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <m@material.item>
// @returns dItem
// @description
// Returns an item of the material.
// -->
if (attribute.startsWith("item"))
return new dItem(this, 1)
.getAttribute(attribute.fulfill(1));

// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
Expand Down
@@ -0,0 +1,94 @@
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 org.bukkit.Material;
import org.bukkit.material.CocoaPlant;
import org.bukkit.material.Crops;
import org.bukkit.material.NetherWarts;

public class ItemPlantgrowth implements Property {

public static boolean describes(dObject item) {
return item instanceof dItem
&& (
((dItem) item).getItemStack().getData() instanceof Crops
|| ((dItem) item).getItemStack().getData() instanceof NetherWarts
|| ((dItem) item).getItemStack().getData() instanceof CocoaPlant
|| ((dItem) item).getItemStack().getType().equals(Material.PUMPKIN_STEM)
|| ((dItem) item).getItemStack().getType().equals(Material.MELON_STEM)
|| ((dItem) item).getItemStack().getType().equals(Material.CARROT)
|| ((dItem) item).getItemStack().getType().equals(Material.POTATO)
);
}

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


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

dItem item;

@Override
public String getAttribute(Attribute attribute) {

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

// <--[tag]
// @attribute <i@item.plant_growth>
// @returns Element
// @description
// Returns the growth level of a plant item as one of the following:
// Wheat: SEEDED, GERMINATED, VERY_SMALL, SMALL, MEDIUM, TALL, VERY_TALL, RIPE
// Nether Warts: SEEDED, STAGE_ONE, STAGE_TWO, RIPE
// Cocoa Plants: SMALL, MEDIUM, LARGE
// Pumpkin stem, melon stem, carrot, potato: 0-7
// -->
if (attribute.startsWith("plant_growth")) {
if (item.getItemStack().getData() instanceof Crops)
return new Element(((Crops)item.getItemStack().getData()).getState().name())
.getAttribute(attribute.fulfill(1));
else if (item.getItemStack().getData() instanceof NetherWarts)
return new Element(((NetherWarts)item.getItemStack().getData()).getState().name())
.getAttribute(attribute.fulfill(1));
else if (item.getItemStack().getData() instanceof CocoaPlant)
return new Element(((CocoaPlant)item.getItemStack().getData()).getSize().name())
.getAttribute(attribute.fulfill(1));
}

return null;
}


@Override
public String getPropertyString() {
String state;
if (item.getItemStack().getData() instanceof Crops)
state = ((Crops)item.getItemStack().getData()).getState().name();
else if (item.getItemStack().getData() instanceof NetherWarts)
state = ((NetherWarts)item.getItemStack().getData()).getState().name();
else if (item.getItemStack().getData() instanceof CocoaPlant)
state = ((CocoaPlant)item.getItemStack().getData()).getSize().name();
else
state = String.valueOf(item.getItemStack().getData().getData());

if (!state.equalsIgnoreCase("SEEDED") && !state.equalsIgnoreCase("0"))
return state;
else
return null;
}

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

}

Expand Down
Expand Up @@ -631,7 +631,7 @@ public void registerCoreMembers() {
// - narrate 'You invoke your power of notice...'
// - define range '<player.flag[range_level].mul[3]>'
// - define blocks '<player.flag[noticeable_blocks]>'
// - narrate '[NOTICE] You have noticed <player.location.find.blocks[%blocks%].within[%range].size>
// - narrate '[NOTICE] You have noticed <player.location.find.blocks[%blocks%].within[%range%].size>
// blocks in the area that may be of interest.'

// @Usage
Expand Down

0 comments on commit 0c5cac5

Please sign in to comment.