Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional code for a OO tag system.
- Loading branch information
1 parent
a59d17b
commit d62b310
Showing
3 changed files
with
240 additions
and
28 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/java/net/aufdemrand/denizen/tags/Attribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package net.aufdemrand.denizen.tags; | ||
|
||
import net.aufdemrand.denizen.Denizen; | ||
import net.aufdemrand.denizen.events.ReplaceableTagEvent; | ||
import net.aufdemrand.denizen.npc.dNPC; | ||
import net.aufdemrand.denizen.scripts.ScriptEntry; | ||
import net.aufdemrand.denizen.tags.core.*; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Calls a bukkit event for replaceable tags. | ||
* | ||
* @author Jeremy Schroeder | ||
* | ||
*/ | ||
|
||
public class Attribute { | ||
|
||
public Denizen denizen; | ||
|
||
public Attribute(Denizen denizen) { | ||
this.denizen = denizen; | ||
} | ||
|
||
public void registerCoreTags() { | ||
new PlayerTags(denizen); | ||
new UtilTags(denizen); | ||
new OfflinePlayerTags(denizen); | ||
new FlagTags(denizen); | ||
new BookmarkTags(denizen); | ||
new ConstantTags(denizen); | ||
new ProcedureScriptTag(denizen); | ||
new NPCTags(denizen); | ||
new AnchorTags(denizen); | ||
new ContextTags(denizen); | ||
new LocationTags(denizen); | ||
new SpecialCharacterTags(denizen); | ||
new TextTags(denizen); | ||
new ForeignCharacterTags(denizen); | ||
} | ||
|
||
public static String tag(OfflinePlayer player, dNPC npc, String arg) { | ||
return tag(player, npc, arg, false, null); | ||
} | ||
|
||
public static String tag(OfflinePlayer player, dNPC npc, String arg, boolean instant) { | ||
return tag(player, npc, arg, instant, null); | ||
} | ||
|
||
public static String tag(OfflinePlayer player, dNPC npc, String arg, boolean instant, ScriptEntry scriptEntry) { | ||
if (arg == null) return null; | ||
// confirm there are/is a replaceable TAG(s), if not, return the arg. | ||
if (arg.indexOf('>') == -1 || arg.length() < 3) return arg; | ||
|
||
// Find location of the first tag | ||
int[] positions = locateTag(arg); | ||
if (positions == null) return arg; | ||
|
||
boolean changeBack = false; | ||
int failsafe = 0; | ||
do { | ||
// Just in case, do-loops make me nervous, but does implement a limit of 25 tags per argument. | ||
failsafe++; | ||
ReplaceableTagEvent event; | ||
if (positions == null) break; | ||
else { | ||
event = new ReplaceableTagEvent(player, npc, arg.substring(positions[0] + 1, positions[1]), scriptEntry); | ||
if (event.isInstant() != instant) { | ||
changeBack = true; | ||
// Not the right type of tag, change out brackets so it doesn't get parsed again | ||
arg = arg.substring(0, positions[0]) + "{" + event.getReplaced() + "}" + arg.substring(positions[1] + 1, arg.length()); | ||
} else { | ||
// Call Event | ||
Bukkit.getServer().getPluginManager().callEvent(event); | ||
if (!event.replaced() && event.getAlternative() != null) event.setReplaced(event.getAlternative()); | ||
arg = arg.substring(0, positions[0]) + event.getReplaced() + arg.substring(positions[1] + 1, arg.length()); | ||
} | ||
} | ||
// Find new TAG | ||
positions = locateTag(arg); | ||
} while (positions != null || failsafe < 25); | ||
// Change brackets back | ||
if (changeBack) arg = arg.replace("{", "<").replace("}", ">"); | ||
// Return argument with replacements | ||
return arg; | ||
} | ||
// Match all < > brackets that don't contain < > inside them | ||
private static Pattern tagRegex = Pattern.compile("<([^<>]+)>"); | ||
|
||
private static int[] locateTag(String arg) { | ||
// find tag brackets pattern | ||
Matcher tagMatcher = tagRegex.matcher(arg); | ||
if (tagMatcher.find()) | ||
return new int[]{tagMatcher.start(), tagMatcher.end() - 1}; | ||
// no matching brackets pattern, return null | ||
else return null; | ||
} | ||
|
||
public static List<String> fillArguments(List<String> args, ScriptEntry scriptEntry) { | ||
return fillArguments(args, scriptEntry, false); | ||
} | ||
|
||
public static List<String> fillArguments(List<String> args, ScriptEntry scriptEntry, boolean instant) { | ||
List<String> filledArgs = new ArrayList<String>(); | ||
if (args != null) { | ||
for (String argument : args) { | ||
if (scriptEntry.getPlayer() == null && scriptEntry.getOfflinePlayer() != null) | ||
filledArgs.add(tag(scriptEntry.getOfflinePlayer(), scriptEntry.getNPC(), argument, instant, scriptEntry)); | ||
else | ||
filledArgs.add(tag(scriptEntry.getPlayer(), scriptEntry.getNPC(), argument, instant, scriptEntry)); | ||
} | ||
} | ||
return filledArgs; | ||
} | ||
|
||
public static List<String> fillArguments(String[] args, Player player, dNPC npc) { | ||
List<String> filledArgs = new ArrayList<String>(); | ||
if (args != null) { | ||
for (String argument : args) { | ||
filledArgs.add(tag(player, npc, argument, false)); | ||
} | ||
} | ||
return filledArgs; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/main/java/net/aufdemrand/denizen/utilities/arguments/Element.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package net.aufdemrand.denizen.utilities.arguments; | ||
|
||
import net.aufdemrand.denizen.interfaces.dScriptArgument; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
public class Element extends ArrayList<String> implements dScriptArgument { | ||
|
||
/** | ||
* | ||
* @param string the string or dScript argument String | ||
* @return a dScript List | ||
* | ||
*/ | ||
public static Element valueOf(String string) { | ||
if (string == null) return null; | ||
|
||
String prefix = null; | ||
// Strip prefix (ie. targets:...) | ||
if (string.split(":").length > 1) { | ||
prefix = string.split(":", 2)[0]; | ||
string = string.split(":", 2)[1]; | ||
} | ||
|
||
return new Element(prefix, string); | ||
} | ||
|
||
private String prefix; | ||
|
||
public Element(String prefix, String items) { | ||
if (prefix == null) this.prefix = "list"; | ||
else this.prefix = prefix; | ||
addAll(Arrays.asList(items.split("\\|"))); | ||
} | ||
|
||
public Element(String prefix, java.util.List<String> items) { | ||
if (prefix == null) this.prefix = "list"; | ||
else this.prefix = prefix; | ||
addAll(items); | ||
} | ||
|
||
@Override | ||
public String getDefaultPrefix() { | ||
return prefix; | ||
} | ||
|
||
@Override | ||
public String debug() { | ||
return (prefix + "='<A>" + this.toString() + "<G>' "); | ||
} | ||
|
||
@Override | ||
public String as_dScriptArg() { | ||
if (isEmpty()) return null; | ||
StringBuilder dScriptArg = new StringBuilder(); | ||
dScriptArg.append(prefix + ":"); | ||
for (String item : this) | ||
dScriptArg.append(item + "|"); | ||
|
||
return dScriptArg.toString().substring(0, dScriptArg.length() - 1); | ||
} | ||
|
||
@Override | ||
public dScriptArgument setPrefix(String prefix) { | ||
this.prefix = prefix; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String getAttribute(String attribute) { | ||
|
||
if (attribute == null) return null; | ||
|
||
// Desensitize the attribute for comparison | ||
attribute = attribute.toLowerCase(); | ||
|
||
if (attribute.startsWith(".aslist")) { | ||
StringBuilder dScriptArg = new StringBuilder(); | ||
for (String item : this) | ||
dScriptArg.append(item + "|"); | ||
return dScriptArg.toString().substring(0, dScriptArg.length() - 1); | ||
} | ||
|
||
if (attribute.startsWith(".ascslist")) { | ||
StringBuilder dScriptArg = new StringBuilder(); | ||
for (String item : this) | ||
dScriptArg.append(item + ", "); | ||
return dScriptArg.toString().substring(0, dScriptArg.length() - 2); | ||
} | ||
|
||
if (attribute.startsWith(".get[")) { | ||
int index = Integer.valueOf(attribute.split("\\[")[1].split("\\]")[0]); | ||
if (index > size()) return null; | ||
|
||
String item = get(index - 1); | ||
|
||
attribute = "." + attribute.split("\\.", 2)[1]; | ||
|
||
|
||
|
||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
} |
28 changes: 0 additions & 28 deletions
28
src/main/java/net/aufdemrand/denizen/utilities/nbt/ListOfLore.java
This file was deleted.
Oops, something went wrong.