Skip to content

Commit

Permalink
Allow <script> in item scripts
Browse files Browse the repository at this point in the history
and book scripts too! Uses a reworking of the tag engine...
  • Loading branch information
mcmonkey4eva committed Nov 1, 2014
1 parent 0690357 commit 075723d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.dScript;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.tags.Attribute;

Expand All @@ -12,13 +13,7 @@

/**
* Bukkit event that fires on the finding of a replaceable tag, as indicated by surrounding < >'s.
*
* @author Jeremy Schroeder
*
* @version 1.0
*
*/

public class ReplaceableTagEvent extends Event {

private static final HandlerList handlers = new HandlerList();
Expand All @@ -40,17 +35,20 @@ public class ReplaceableTagEvent extends Event {

public String raw_tag;

private dScript script;

////////////
// Constructors

public ReplaceableTagEvent(dPlayer player, dNPC npc, String tag) {
this(player, npc, tag, null);
this(player, npc, tag, null, null);
}

public ReplaceableTagEvent(dPlayer player, dNPC npc, String tag, ScriptEntry scriptEntry) {
public ReplaceableTagEvent(dPlayer player, dNPC npc, String tag, ScriptEntry scriptEntry, dScript script) {

// Reference ScriptEntry if available
this.scriptEntry = scriptEntry;
this.script = script;

// Reference player/npc
this.player = player;
Expand Down Expand Up @@ -302,6 +300,10 @@ public boolean isInstant() {
return instant;
}

public dScript getScript() {
return script;
}

public boolean replaced() {
return wasReplaced && replaced != null;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dScript.java
Expand Up @@ -113,6 +113,12 @@ public dScript(String scriptName) {
}
}

public dScript(ScriptContainer container) {
this.container = container;
name = container.getName().toUpperCase();
valid = true;
}

///////////////////////
// Instance fields and methods
/////////////////////
Expand Down
Expand Up @@ -2,10 +2,12 @@

import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.dScript;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.objects.dItem;

import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;
Expand Down Expand Up @@ -64,7 +66,7 @@ public dItem writeBookTo(dItem book, dPlayer player, dNPC npc) {

if (contains("TITLE")) {
String title = getString("TITLE");
title = TagManager.tag(player, npc, title, false);
title = TagManager.tag(player, npc, title, false, null, dB.shouldDebug(this), new dScript(this));
bookInfo.setTitle(title);
}

Expand All @@ -84,7 +86,7 @@ public dItem writeBookTo(dItem book, dPlayer player, dNPC npc) {
List<String> pages = getStringList("TEXT");

for (String page : pages) {
page = TagManager.tag(player, npc, page, false);
page = TagManager.tag(player, npc, page, false, null, dB.shouldDebug(this), new dScript(this));
bookInfo.addPage(page);
}
}
Expand Down
Expand Up @@ -5,13 +5,10 @@
import java.util.List;
import java.util.Map;

import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.LeatherColorer;

Expand Down Expand Up @@ -112,7 +109,7 @@ public ItemScriptContainer(YamlConfiguration configurationSection, String script

// Process all tags in list
for (int n = 0; n < recipeList.size(); n++) {
recipeList.set(n, TagManager.tag(player, npc, recipeList.get(n)));
recipeList.set(n, TagManager.tag(player, npc, recipeList.get(n), false, null, dB.shouldDebug(this), new dScript(this)));
}

// Store every ingredient in a dList
Expand Down Expand Up @@ -165,7 +162,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
}
// Check validity of material
if (contains("MATERIAL")){
String material = TagManager.tag(player, npc, getString("MATERIAL"), false, null, debug);
String material = TagManager.tag(player, npc, getString("MATERIAL"), false, null, debug, new dScript(this));
if (material.startsWith("m@"))
material = material.substring(2);
stack = dItem.valueOf(material);
Expand All @@ -187,20 +184,20 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {

// Set Display Name
if (contains("DISPLAY NAME")){
String displayName = TagManager.tag(player, npc, getString("DISPLAY NAME"), false, null, debug);
String displayName = TagManager.tag(player, npc, getString("DISPLAY NAME"), false, null, debug, new dScript(this));
meta.setDisplayName(displayName);
}

// Set if the object is bound to the player
if (contains("BOUND")) {
bound = Boolean.valueOf(TagManager.tag(player, npc, getString("BOUND"), false, null, debug));
bound = Boolean.valueOf(TagManager.tag(player, npc, getString("BOUND"), false, null, debug, new dScript(this)));
}

// Set Lore
if (contains("LORE")) {

for (String l : getStringList("LORE")){
l = TagManager.tag(player, npc, l);
l = TagManager.tag(player, npc, l, false, null, debug, new dScript(this));
lore.add(l);
}
}
Expand All @@ -212,7 +209,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
if (contains("ENCHANTMENTS")) {
for (String enchantment : getStringList("ENCHANTMENTS")) {

enchantment = TagManager.tag(player, npc, enchantment, false, null, debug);
enchantment = TagManager.tag(player, npc, enchantment, false, null, debug, new dScript(this));
try {
// Build enchantment context
int level = 1;
Expand All @@ -235,14 +232,14 @@ public dItem getItemFrom(dPlayer player, dNPC npc) {
// Set Color
if (contains("COLOR"))
{
String color = TagManager.tag(player, npc, getString("COLOR"), false, null, debug);
String color = TagManager.tag(player, npc, getString("COLOR"), false, null, debug, new dScript(this));
LeatherColorer.colorArmor(stack, color);
}

// Set Book
if (contains("BOOK")) {
BookScriptContainer book = ScriptRegistry
.getScriptContainer(TagManager.tag(player, npc, getString("BOOK"), false, null, debug).replace("s@", ""));
.getScriptContainer(TagManager.tag(player, npc, getString("BOOK"), false, null, debug, new dScript(this)).replace("s@", ""));

stack = book.writeBookTo(stack, player, npc);
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/aufdemrand/denizen/tags/TagManager.java
Expand Up @@ -30,12 +30,14 @@ public static class TagContext {
public final boolean instant;
public final ScriptEntry entry;
public final boolean debug;
public TagContext(dPlayer player, dNPC npc, boolean instant, ScriptEntry entry, boolean debug) {
public final dScript script;
public TagContext(dPlayer player, dNPC npc, boolean instant, ScriptEntry entry, boolean debug, dScript script) {
this.player = player;
this.npc = npc;
this.instant = instant;
this.entry = entry;
this.debug = debug;
this.script = script;
}
}

Expand Down Expand Up @@ -172,7 +174,7 @@ public void fetchObject(ReplaceableTagEvent event) {
}

public static String readSingleTag(String str, TagContext context) {
ReplaceableTagEvent event = new ReplaceableTagEvent(context.player, context.npc, str, context.entry);
ReplaceableTagEvent event = new ReplaceableTagEvent(context.player, context.npc, str, context.entry, context.script);
if (event.isInstant() != context.instant) {
// Not the right type of tag, escape the brackets so it doesn't get parsed again
return String.valueOf((char)0x01) + str + String.valueOf((char)0x02);
Expand Down Expand Up @@ -211,7 +213,11 @@ public static String tag(dPlayer player, dNPC npc, String arg, boolean instant,


public static String tag(dPlayer player, dNPC npc, String arg, boolean instant, ScriptEntry scriptEntry, boolean debug) {
return tag(arg, new TagContext(player, npc, instant, scriptEntry, debug));
return tag(arg, new TagContext(player, npc, instant, scriptEntry, debug, scriptEntry != null ? scriptEntry.getScript(): null));
}

public static String tag(dPlayer player, dNPC npc, String arg, boolean instant, ScriptEntry scriptEntry, boolean debug, dScript script) {
return tag(arg, new TagContext(player, npc, instant, scriptEntry, debug, script));
}

public static String tag(String arg, TagContext context) {
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dScript;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

Expand Down Expand Up @@ -32,6 +33,10 @@ public void scriptTags(ReplaceableTagEvent event) {
// the ScriptEntry for a 'script' context
if (event.hasNameContext() && dScript.matches(event.getNameContext()))
script = dScript.valueOf(event.getNameContext());
else if (event.getScript() != null)
script = event.getScript();
else if (event.getScriptEntry() == null)
return;
else if (event.getScriptEntry().getScript() != null)
script = event.getScriptEntry().getScript();
else if (event.getScriptEntry().hasObject("script"))
Expand Down

0 comments on commit 075723d

Please sign in to comment.