Skip to content

Commit

Permalink
Merge pull request #568 from mcmonkey4eva/master
Browse files Browse the repository at this point in the history
More fixes
  • Loading branch information
aufdemrand committed Dec 19, 2013
2 parents be056fc + 50e2f92 commit 5880766
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 152 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.

202 changes: 122 additions & 80 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -10,23 +10,24 @@
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.ArrayList;
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 +215,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 +539,38 @@ 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.has_display>
// @returns Element(Boolean)
// @description
// Returns whether the item has a custom set display name.
// -->
if (attribute.startsWith("has_display")) {
return new Element(ItemDisplayname.describes(this))
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.is_book>
// @returns Element(Boolean)
// @description
// Returns whether the item is considered an editable book.
// -->
if (attribute.startsWith("is_book")) {
return new Element(ItemBook.describes(this))
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.material.formatted>
// @returns Element
Expand Down Expand Up @@ -593,79 +626,6 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("material"))
return getMaterial().getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.has_display>
// @returns Element(Boolean)
// @description
// Returns whether the item has a custom set display name.
// -->
if (attribute.startsWith("has_display")) {
return new Element(ItemDisplayname.describes(this))
.getAttribute(attribute.fulfill(1));
}

// TODO: Property for Book info
if (attribute.startsWith("book")) {
if (getItemStack().getType() == Material.WRITTEN_BOOK) {
attribute.fulfill(1);
BookMeta bookInfo = (BookMeta) getItemStack().getItemMeta();

// <--[tag]
// @attribute <i@item.book.author>
// @returns Element
// @description
// Returns the author of the book. Note: Item must be a 'written_book'.
// -->
if (attribute.startsWith("author"))
return new Element(bookInfo.getAuthor())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.book.title>
// @returns Element
// @description
// Returns the title of the book. Note: Item must be a 'written_book'.
// -->
if (attribute.startsWith("title"))
return new Element(bookInfo.getTitle())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.book.page_count>
// @returns Element(Number)
// @description
// Returns the number of pages in the book. Note: Item must be a 'written_book'.
// -->
if (attribute.startsWith("page_count"))
return new Element(bookInfo.getPageCount())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.book.get_page[<#>]>
// @returns Element
// @description
// Returns the page specified from the book as an element. Note: Item must be a 'written_book'.
// -->
if (attribute.startsWith("get_page") && aH.matchesInteger(attribute.getContext(1)))
return new Element(bookInfo.getPage(attribute.getIntContext(1)))
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.book.pages>
// @returns dList
// @description
// Returns the pages of the book as a dList. Note: Item must be a 'written_book'.
// -->
if (attribute.startsWith("pages"))
return new dList(bookInfo.getPages())
.getAttribute(attribute.fulfill(1));

} else {
dB.echoError("Item referenced is not a written book!");
return "null";
}
}

// <--[tag]
// @attribute <i@item.scriptname>
// @returns Element
Expand Down Expand Up @@ -764,7 +724,7 @@ public void adjust(Mechanism mechanism) {
dB.echoError("Cannot apply enchantment '" + data[0] +"': '" + data[1] + "' is not a valid integer!");
else {
try {
item.addEnchantment(Enchantment.getByName(data[0].toUpperCase()), Integer.valueOf(data[1]));
item.addUnsafeEnchantment(Enchantment.getByName(data[0].toUpperCase()), Integer.valueOf(data[1]));
}
catch (NullPointerException e) {
dB.echoError("Unknown enchantment '" + data[0] + "'");
Expand Down Expand Up @@ -826,6 +786,88 @@ 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.");
}

// <--[mechanism]
// @object dItem
// @name book
// @input Element
// @description
// Changes the information on a book item.
// Accepts simple symbol escaping as follows:
// | = &pipe; < = &lt; > = &gt; newline = &nl; & = &amp;
// @tags
// <i@item.is_book>
// <i@item.book.author>
// <i@item.book.title>
// <i@item.book.page_count>
// <i@item.book.get_page[<#>]>
// <i@item.book.pages>
// <i@item.book>
// -->
if (mechanism.matches("book")) {
if (ItemBook.describes(this)) {
BookMeta meta = (BookMeta) item.getItemMeta();
dList data = value.asType(dList.class);
if (data.size() < 2) {
dB.echoError("Invalid book input!");
}
else {
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author")
&& data.get(2).equalsIgnoreCase("title")) {
if (!item.getType().equals(Material.WRITTEN_BOOK)) {
dB.echoError("That type of book cannot have title or author!");
}
else {
meta.setAuthor(ItemBook.unEscape(data.get(1)));
meta.setTitle(ItemBook.unEscape(data.get(3)));
for (int i = 0; i < 4; i++)
data.remove(0); // No .removeRange?
}
}
if (!data.get(0).equalsIgnoreCase("pages")) {
dB.echoError("Invalid book input!");
}
else {
ArrayList<String> newPages = new ArrayList<String>();
for (int i = 1; i < data.size(); i++) {
newPages.add(ItemBook.unEscape(data.get(i)));
}
meta.setPages(newPages);
}
item.setItemMeta(meta);
}
}
else
dB.echoError("Material '" + getMaterial().identify().replace("m@", "") + "' is not a book.");
}



if (!mechanism.fulfilled())
mechanism.reportInvalid();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -627,7 +627,7 @@ else if (getYaw() < 315)
attribute.fulfill(1);

// <--[tag]
// @attribute <l@location.find.blocks[<block>|...].within[X]>
// @attribute <l@location.find.blocks[<block>|...].within[<#>]>
// @returns dList
// @description
// Returns a list of matching blocks within a radius.
Expand Down Expand Up @@ -666,7 +666,7 @@ public int compare(dLocation loc1, dLocation loc2) {
}

// <--[tag]
// @attribute <l@location.find.surface_blocks[<block>|...].within[X]>
// @attribute <l@location.find.surface_blocks[<block>|...].within[<#>]>
// @returns dList
// @description
// Returns a list of matching surface blocks within a radius.
Expand Down Expand Up @@ -713,7 +713,7 @@ public int compare(dLocation loc1, dLocation loc2) {
}

// <--[tag]
// @attribute <l@location.find.players.within[X]>
// @attribute <l@location.find.players.within[<#>]>
// @returns dList
// @description
// Returns a list of players within a radius.
Expand All @@ -739,7 +739,7 @@ public int compare(dPlayer pl1, dPlayer pl2) {
}

// <--[tag]
// @attribute <l@location.find.npcs.within[X]>
// @attribute <l@location.find.npcs.within[<#>]>
// @returns dList
// @description
// Returns a list of NPCs within a radius.
Expand All @@ -765,7 +765,7 @@ public int compare(dNPC npc1, dNPC npc2) {
}

// <--[tag]
// @attribute <l@location.find.entities[<entity>|...].within[X]>
// @attribute <l@location.find.entities[<entity>|...].within[<#>]>
// @returns dList
// @description
// Returns a list of entities within a radius, with an optional search parameter
Expand Down Expand Up @@ -811,7 +811,7 @@ public int compare(dEntity ent1, dEntity ent2) {
}

// <--[tag]
// @attribute <l@location.find.living_entities.within[X]>
// @attribute <l@location.find.living_entities.within[<#>]>
// @returns dList
// @description
// Returns a list of living entities within a radius.
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
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dWorld.java
Expand Up @@ -481,6 +481,7 @@ public String getAttribute(Attribute attribute) {
// @returns Duration
// @description
// Returns the in-game time of this world.
// -->
if (attribute.startsWith("time.full"))
return new Element(getWorld().getFullTime())
.getAttribute(attribute.fulfill(1));
Expand Down

0 comments on commit 5880766

Please sign in to comment.