Skip to content

Commit

Permalink
Add ItemLore prop, add <&sc> tag, adjust symbols
Browse files Browse the repository at this point in the history
- Added ItemLore property
- added <&sc> tag to return semicolons magically
- adjusted how special/control symbols are handled to avoid errors /
hack risks
  • Loading branch information
mcmonkey4eva committed Dec 15, 2013
1 parent c292930 commit cb928e7
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -9,6 +9,7 @@
import net.aufdemrand.denizen.scripts.commands.core.*;
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 @@ -872,7 +873,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
Expand Up @@ -112,7 +112,8 @@ public static <T extends dObject> T getObjectFrom(Class<T> dClass, String value,
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])));
((Adjustable) gotten).adjust(new Mechanism(new Element(data[0]),
new Element(data[1].replace((char)0x2011, ';'))));
}
}
return gotten;
Expand Down
35 changes: 5 additions & 30 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -3,6 +3,7 @@
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.Item.ItemDisplayname;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.objects.properties.PropertyParser;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
Expand Down Expand Up @@ -662,14 +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));
}
if (ItemDisplayname.describes(this))
return Element.TRUE.getAttribute(attribute.fulfill(1));
else
return Element.FALSE.getAttribute(attribute.fulfill(1));
}

// <--[tag]
Expand Down Expand Up @@ -763,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 Down
Expand Up @@ -42,7 +42,6 @@ public String getAttribute(Attribute attribute) {
return new Element(item.getItemStack().getItemMeta().getDisplayName())
.getAttribute(attribute.fulfill(1));


return null;
}

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


import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.tags.Attribute;

import java.util.ArrayList;
import java.util.List;

public class ItemLore implements Property {

public static boolean describes(dObject item) {
return item instanceof dItem
&& ((dItem) item).getItemStack().hasItemMeta()
&& ((dItem) item).getItemStack().getItemMeta().hasLore();
}

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


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

dItem item;

@Override
public String getAttribute(Attribute attribute) {

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

// <--[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")) {
List<String> loreList = new ArrayList<String>();
for (String itemLore : item.getItemStack().getItemMeta().getLore()) {
if (!itemLore.startsWith(dItem.itemscriptIdentifier)) {
loreList.add(itemLore);
}
}
return new dList(loreList).getAttribute(attribute.fulfill(1));
}

return null;
}


@Override
public String getPropertyString() {
StringBuilder output = new StringBuilder();
for (String itemLore : item.getItemStack().getItemMeta().getLore()) {
if (!itemLore.startsWith(dItem.itemscriptIdentifier)) {
output.append(itemLore).append("|");
}
}
return (output.length() == 0) ? "": output.substring(0, output.length() - 1);
}

@Override
public String getPropertyId() {
return "lore";
}
}
Expand Up @@ -36,6 +36,7 @@ public PropertyParser() {
// register core dItem properties
registerProperty(ItemEnchantments.class, dItem.class);
registerProperty(ItemDisplayname.class, dItem.class);
registerProperty(ItemLore.class, dItem.class);

}

Expand Down Expand Up @@ -73,7 +74,8 @@ public static String getPropertiesString(dObject object) {
for (Property property: getProperties(object)) {
String description = property.getPropertyString();
if (description != null) {
prop_string.append(property.getPropertyId()).append('=').append(description).append(';');
prop_string.append(property.getPropertyId()).append('=')
.append(description.replace(';', (char)0x2011)).append(';');
}
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dB.report(scriptEntry, getName(), talkers.debug() + targets.debug() + message.debug());

// Create new speech context
SpeechContext context = new SpeechContext(message.asString());
SpeechContext context = new SpeechContext(TagManager.CleanOutputFully(message.asString()));

if (!targets.equals(Element.FALSE)) {

Expand Down
Expand Up @@ -11,6 +11,7 @@
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ else if ((arg.matchesPrefix("target") || arg.matchesPrefix(TARGET_ARG))) {
// Use raw_value as to not accidentally strip a value before any :'s.
else {
if (!scriptEntry.hasObject("text"))
scriptEntry.addObject("text", new Element(arg.raw_value));
scriptEntry.addObject("text", new Element(TagManager.CleanOutputFully(arg.raw_value)));
}
}

Expand Down
26 changes: 19 additions & 7 deletions src/main/java/net/aufdemrand/denizen/tags/TagManager.java
Expand Up @@ -52,6 +52,23 @@ public void registerCoreTags() {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}

// INTERNAL MAPPING NOTE:
// 0x01: <
// 0x02: >
// 0x04: Exclusively For Utilities.talkToNPC()
// 0x05: |
// 0x2011: ;

public static String CleanOutput(String input) {
return input.replace((char)0x01, '<').replace((char)0x02, '>')
/*.replace((char)0x2011, ';')*/.replace(dList.internal_escape_char, '|');
}

public static String CleanOutputFully(String input) {
return input.replace((char)0x01, '<').replace((char)0x02, '>')
.replace((char)0x2011, ';').replace(dList.internal_escape_char, '|');
}

@EventHandler
public void fetchObject(ReplaceableTagEvent event) {
if (!event.getName().contains("@")) return;
Expand Down Expand Up @@ -114,9 +131,7 @@ public static String tag(dPlayer player, dNPC npc, String arg, boolean instant,
// Find location of the first tag
int[] positions = locateTag(arg);
if (positions == null) {
// Unescape internal escape codes.
arg = arg.replace((char)0x01, '<').replace((char)0x02, '>').replace(dList.internal_escape, "|");
return arg;
return CleanOutput(arg);
}

int failsafe = 0;
Expand Down Expand Up @@ -144,10 +159,7 @@ public static String tag(dPlayer player, dNPC npc, String arg, boolean instant,
positions = locateTag(arg);
} while (positions != null || failsafe < 50);

// Unescape internal escape codes.
arg = arg.replace((char)0x01, '<').replace((char)0x02, '>').replace(dList.internal_escape, "|");

return arg;
return CleanOutput(arg);

This comment has been minimized.

Copy link
@aufdemrand

aufdemrand Dec 15, 2013

Contributor

What is up with this, when do we need to call it, and why two versions?

This comment has been minimized.

Copy link
@Morphan1

Morphan1 Dec 15, 2013

Contributor

It looks like CleanOutput() just transfers all the internal characters to their readable string formats.
But... CleanOutput() doesn't do semicolons? Only CleanOutputFully() does

for some reason

This comment has been minimized.

Copy link
@mcmonkey4eva

mcmonkey4eva Dec 15, 2013

Author Member

@aufdemrand - CleanOutputFully is just a function to show internal escapes as the characters they really are, for use when directly outputting potentially escaped information to the console / to players / whatever.
CleanOutput just does ... exactly what I replaced with it. Deals with the internal escapes like < that should already be valid at the end of TagManager (and can be used for future parsing)
It doesn't clean the semicolon because that has to stay escaped either until being outputted or read by a property (It's used purely as an escape for property stuff, there's no need to use the escape anywhere else, a regular semicolon is fine)

}

// Match all < > brackets that don't contain < > inside them
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/aufdemrand/denizen/tags/core/TextTags.java
Expand Up @@ -468,6 +468,15 @@ else if (event.getName().equalsIgnoreCase("&dq"))
else if (event.getName().equalsIgnoreCase("&co"))
event.setReplaced(new Element(":").getAttribute(attribute.fulfill(1)));

// <--[tag]
// @attribute <&sc>
// @returns Element
// @description
// Returns a semicolon symbol: ;
// -->
else if (event.getName().equalsIgnoreCase("&sc"))
event.setReplaced(new Element(String.valueOf((char)0x2011)).getAttribute(attribute.fulfill(1)));

// <--[tag]
// @attribute <&rb>
// @returns Element
Expand Down
Expand Up @@ -108,7 +108,7 @@ public static String[] wrapWords(String text, int width) {
*
*/
public static void talkToNPC(String message, dPlayer player, dNPC npc, double range) {
String replacer = String.valueOf((char)0x03);
String replacer = String.valueOf((char)0x04);
// Get formats from Settings, and fill in <TEXT>
String talkFormat = Settings.ChatToNpcFormat()
.replace("<TEXT>", replacer).replace("<text>", replacer).replace("<Text>", replacer);
Expand Down
Expand Up @@ -10,6 +10,7 @@

import net.aufdemrand.denizen.Settings;

import net.aufdemrand.denizen.tags.TagManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -297,10 +298,10 @@ public static void sendMessage(String string) {

// These colors are used a lot in the debugging of commands/etc, so having a few shortcuts is nicer
// than having a bunch of ChatColor.XXXX
string = string
string = TagManager.CleanOutputFully(string
.replace("<Y>", ChatColor.YELLOW.toString())
.replace("<G>", ChatColor.DARK_GRAY.toString())
.replace("<A>", ChatColor.AQUA.toString());
.replace("<A>", ChatColor.AQUA.toString()));

// 'Hack-fix' for disallowing multiple 'footers' to print in a row
if (string.equals(ChatColor.LIGHT_PURPLE + "+---------------------+")) {
Expand Down

0 comments on commit cb928e7

Please sign in to comment.