Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/aufdemrand/Denizen
Browse files Browse the repository at this point in the history
Conflicts:

src/main/java/net/aufdemrand/denizen/scripts/commands/entity/AgeCommand.
java

src/main/java/net/aufdemrand/denizen/scripts/commands/player/ChatCommand
.java
  • Loading branch information
aufdemrand committed Dec 15, 2013
2 parents 32f6dff + 7c3cdd3 commit 03e7911
Show file tree
Hide file tree
Showing 26 changed files with 431 additions and 216 deletions.
Expand Up @@ -146,7 +146,9 @@ private static CommandMap getCommandMap() {
}


public static List<String> trimEvents(List<String> event) {
public static List<String> trimEvents(List<String> original) {
List<String> event = new ArrayList<String>();
event.addAll(original);
List<String> parsed = new ArrayList<String>();

if (dB.showEventsTrimming) dB.echoApproval("Trimming world events '" + event.toString() + '\'');
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -8,6 +8,7 @@
import net.aufdemrand.denizen.objects.aH.Argument;
import net.aufdemrand.denizen.scripts.commands.core.Comparable;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -871,7 +872,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
return "null";
} else {
dB.echoDebug(attribute.getScriptEntry(), "Filled tag <" + attribute.getOrigin() + "> with '" +
element.replace((char)0x01, '<').replace((char)0x02, '>').replace(dList.internal_escape, "|") + "'.");
element + "'.");
return element;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Mechanism.java
Expand Up @@ -62,7 +62,8 @@ public boolean requireInteger() {
}

public <T extends dObject> boolean requireObject(Class<T> type) {
return requireObject("Invalid " + type.getName() + " specified.", type);
// TODO: Remove getSimpleName(), or simplify somehow.
return requireObject("Invalid " + type.getSimpleName() + " specified.", type);
}

public boolean requireBoolean(String error) {
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/ObjectFetcher.java
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
Expand Down Expand Up @@ -82,9 +84,12 @@ public static Class getObjectClass(String id) {
return null;
}

final static Pattern PROPERTIES_PATTERN = Pattern.compile("(.+)\\[(.+=.+)\\]", Pattern.CASE_INSENSITIVE);

public static boolean checkMatch(Class<? extends dObject> dClass, String value) {
try {
return (Boolean) matches.get(dClass).invoke(null, value);
Matcher m = PROPERTIES_PATTERN.matcher(value);
return (Boolean) matches.get(dClass).invoke(null, m.matches() ? m.group(1): value);
} catch (Exception e) {
dB.echoError(e);
}
Expand All @@ -94,8 +99,24 @@ public static boolean checkMatch(Class<? extends dObject> dClass, String value)
}

public static <T extends dObject> T getObjectFrom(Class<T> dClass, String value) {
return getObjectFrom(dClass, value, null, null);
}

public static <T extends dObject> T getObjectFrom(Class<T> dClass, String value, dPlayer player, dNPC npc) {
try {
return (T) valueof.get(dClass).invoke(null, value);
Matcher m = PROPERTIES_PATTERN.matcher(value);
boolean matched = m.matches();
T gotten = (T) ((dClass.equals(dItem.class)) ? dItem.valueOf(matched ? m.group(1): value):
valueof.get(dClass).invoke(null, matched ? m.group(1): value));
if (gotten != null && gotten instanceof Adjustable && matched) {
String[] properties = m.group(2).split(";");
for (String property: properties) {
String[] data = property.split("=", 2);
((Adjustable) gotten).adjust(new Mechanism(new Element(data[0]),
new Element(data[1].replace((char)0x2011, ';'))));
}
}
return gotten;
} catch (Exception e) {
dB.echoError(e);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dColor.java
Expand Up @@ -9,6 +9,7 @@

import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.DyeColor;

public class dColor implements dObject {

Expand Down Expand Up @@ -108,6 +109,10 @@ public dColor(Color color) {
this.color = color;
}

public dColor(DyeColor dyeColor) {
this.color = dyeColor.getColor();
}


/////////////////////
// INSTANCE FIELDS/METHODS
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -1753,7 +1753,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
// -->
if (attribute.startsWith("describe"))
return new Element("e@" + getEntityType().name().toLowerCase()
+ '[' + PropertyParser.getPropertiesString(this) + ']')
+ PropertyParser.getPropertiesString(this))
.getAttribute(attribute.fulfill(1));

// Iterate through this object's properties' attributes
Expand Down
161 changes: 53 additions & 108 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
@@ -1,8 +1,11 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.properties.ItemColor;
import net.aufdemrand.denizen.objects.properties.Item.ItemDisplayname;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.BookScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer;
Expand Down Expand Up @@ -45,13 +48,6 @@ public class dItem implements dObject, Notable, Properties, Adjustable {
final public static String itemscriptIdentifier = "§0id:";


// List of classes to check for properties

final static Class[] PROPERTIES = {
ItemColor.class
};


//////////////////
// OBJECT FETCHER
////////////////
Expand Down Expand Up @@ -489,7 +485,7 @@ else if (isItemscript()) {
}

// Else, return the material name
return "i@" + identifyMaterial().replace("m@", "");
return "i@" + identifyMaterial().replace("m@", "") + PropertyParser.getPropertiesString(this);
}

public String identifyMaterial() {
Expand Down Expand Up @@ -667,74 +663,10 @@ public String getAttribute(Attribute attribute) {
// Returns whether the item has a custom set display name.
// -->
if (attribute.startsWith("has_display")) {
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasDisplayName()) {
return Element.TRUE
.getAttribute(attribute.fulfill(1));
}
else {
return Element.FALSE
.getAttribute(attribute.fulfill(1));
}
}

// <--[tag]
// @attribute <i@item.display>
// @returns Element
// @description
// Returns the display name of the item, as set by plugin or an anvil.
// -->
if (attribute.startsWith("display"))
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasDisplayName())
return new Element(getItemStack().getItemMeta().getDisplayName())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.enchantments.with_levels>
// @returns dList
// @description
// Returns a list of enchantments on the item, with their levels listed too.
// In the format of ENCHANTMENT,LEVEL - EG: DAMAGE_ALL,3
// -->
if (attribute.startsWith("enchantments.with_levels")) {
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasEnchants()) {
List<String> enchants = new ArrayList<String>();
for (Map.Entry<Enchantment, Integer> enchantment : getItemStack().getEnchantments().entrySet())
enchants.add(enchantment.getKey().getName() + "," + enchantment.getValue());
return new dList(enchants)
.getAttribute(attribute.fulfill(2));
}
}

// <--[tag]
// @attribute <i@item.enchantments.levels>
// @returns dList
// @description
// Returns a list of enchantments on the item, showing only the level.
// -->
if (attribute.startsWith("enchantments.levels")) {
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasEnchants()) {
List<String> enchants = new ArrayList<String>();
for (Map.Entry<Enchantment, Integer> enchantment : getItemStack().getEnchantments().entrySet())
enchants.add(String.valueOf(enchantment.getValue()));
return new dList(enchants)
.getAttribute(attribute.fulfill(2));
}
}

// <--[tag]
// @attribute <i@item.enchantments>
// @returns dList
// @description
// Returns a list of enchantments on the item.
// -->
if (attribute.startsWith("enchantments")) {
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasEnchants()) {
List<String> enchants = new ArrayList<String>();
for (Map.Entry<Enchantment, Integer> enchantment : getItemStack().getEnchantments().entrySet())
enchants.add(enchantment.getKey().getName());
return new dList(enchants)
.getAttribute(attribute.fulfill(1));
}
if (ItemDisplayname.describes(this))
return Element.TRUE.getAttribute(attribute.fulfill(1));
else
return Element.FALSE.getAttribute(attribute.fulfill(1));
}

// <--[tag]
Expand Down Expand Up @@ -828,28 +760,6 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.lore>
// @returns dList
// @description
// Returns lore as a dList. Excludes the custom-script-id lore.
// To get that information, use <i@item.scriptname>.
// -->
if (attribute.startsWith("lore")) {
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasLore()) {

List<String> loreList = new ArrayList<String>();

for (String itemLore : getItemStack().getItemMeta().getLore()) {
if (!itemLore.startsWith(itemscriptIdentifier)) {
loreList.add(itemLore);
}
}
return new dList(loreList).getAttribute(attribute.fulfill(1));
}
else return new dList("").getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
Expand All @@ -870,6 +780,11 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));
}

// Iterate through this object's properties' attributes
for (Property property : PropertyParser.getProperties(this)) {
String returned = property.getAttribute(attribute);
if (returned != null) return returned;
}

return new Element(identify()).getAttribute(attribute);
}
Expand All @@ -879,39 +794,69 @@ public String getAttribute(Attribute attribute) {
public void adjust(Mechanism mechanism) {

Element value = mechanism.getValue();
ItemMeta meta = item.getItemMeta();

// <--[mechanism]
// @object dItem
// @name set_display
// @name display_name
// @input Element
// @description
// Changes the items display name.
// @tags
// <i@item.display>
// -->
if (mechanism.matches("set_display")) {
if (mechanism.matches("display_name")) {
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(value.asString());
item.setItemMeta(meta);
}

// <--[mechanism]
// @object dItem
// @name set_lore
// @name lore
// @input dList
// @description
// Sets the item's lore.
// @tags
// <i@item.lore>
// -->
if (mechanism.matches("set_lore") && mechanism.requireObject(dList.class)) {
meta.setLore(dList.valueOf(value.asString()));
if (mechanism.matches("lore")) {
ItemMeta meta = item.getItemMeta();
meta.setLore(value.asType(dList.class));
item.setItemMeta(meta);
}

if (mechanism.matches("add_enchant")) {
// meta.addEnchant(Enchantment.getByName(value.asString()), TODO:(int) level, TODO:(boolean) ignoreLevelRestriction);
// <--[mechanism]
// @object dItem
// @name enchantments
// @input dList
// @description
// Sets the item's enchantments.
// @tags
// <i@item.enchantments>
// <i@item.enchantments.levels>
// <i@item.enchantments.with_levels>
// -->
if (mechanism.matches("enchantments")) {
dList enchants = value.asType(dList.class);
for (String enchant: value.asType(dList.class)) {
if (!enchant.contains(","))
dB.echoError("Invalid enchantment format, use name,level|...");
else {
String[] data = enchant.split(",", 2);
if (Integer.valueOf(data[1]) == null)
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]));
}
catch (NullPointerException e) {
dB.echoError("Unknown enchantment '" + data[0] + "'");
}
}
}
}
}

item.setItemMeta(meta);
if (!mechanism.fulfilled())
mechanism.reportInvalid();

Expand Down
16 changes: 9 additions & 7 deletions src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -21,8 +21,9 @@ public class dList extends ArrayList<String> implements dObject {
Pattern.compile("(fl\\[((?:p@|n@)(.+?))\\]@|fl@)(.+)",
Pattern.CASE_INSENSITIVE);

public final static String internal_escape = String.valueOf((char)0x05);
final static Pattern split_char = Pattern.compile("[\\|" + internal_escape + "]");
public final static char internal_escape_char = (char)0x05;
public final static String internal_escape = String.valueOf(internal_escape_char);
final static Pattern split_char = Pattern.compile("(?!\\[[^\\[]*)\\b*[\\|" + internal_escape + "]\\b*(?![^\\[]*\\])");
final static Pattern identifier = Pattern.compile("li@", Pattern.CASE_INSENSITIVE);

@Fetchable("li, fl")
Expand Down Expand Up @@ -60,7 +61,7 @@ public static dList valueOf(String string) {
}
}

// Use value of string, which will seperate values by the use of a pipe '|'
// Use value of string, which will separate values by the use of a pipe '|'
return new dList(string.replaceFirst(identifier.pattern(), ""));
}

Expand Down Expand Up @@ -89,7 +90,9 @@ public dList() { }

// A string of items, split by '|'
public dList(String items) {
if (items != null) addAll(Arrays.asList(split_char.split(items)));
if (items != null) {
addAll(Arrays.asList(split_char.split(items)));
}
}

// A List<String> of items
Expand Down Expand Up @@ -206,9 +209,8 @@ public List<dObject> filter(Class<? extends dObject> dClass, ScriptEntry entry)
try {
if (ObjectFetcher.checkMatch(dClass, element)) {

dObject object = (dClass.equals(dItem.class) && entry != null ?
dItem.valueOf(element, entry.getPlayer(), entry.getNPC()):
ObjectFetcher.getObjectFrom(dClass, element));
dObject object = ObjectFetcher.getObjectFrom(dClass, element,
entry.getPlayer(), entry.getNPC());

// Only add the object if it is not null, thus filtering useless
// list items
Expand Down

0 comments on commit 03e7911

Please sign in to comment.