use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class ConstantTags method constantTags.
@TagManager.TagEvents
public void constantTags(ReplaceableTagEvent event) {
if (!event.matches("cons")) {
return;
}
if (!event.hasValue()) {
dB.echoError("Constant tag '" + event.raw_tag + " does not contain a valid constant! " + "Replacement has been aborted...");
return;
}
dB.echoError(event.getAttributes().getScriptEntry().getResidingQueue(), "constant: tags are deprecated! Use <npc.constant[]>!");
NPC npc = null;
if (event.getType() != null && event.getType().matches("\\d+")) {
npc = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(event.getType()));
} else if (((BukkitTagContext) event.getContext()).npc != null) {
npc = ((BukkitTagContext) event.getContext()).npc.getCitizen();
}
if (npc == null) {
dB.echoError("Constant tag '" + event.raw_tag + " does not contain a valid NPC! " + "Has the NPC been removed, or is there no NPC list available? " + "Replacement has been aborted...");
return;
}
Attribute attribute = event.getAttributes();
if (npc.hasTrait(ConstantsTrait.class) && npc.getTrait(ConstantsTrait.class).getConstant(event.getValue()) != null) {
event.setReplaced(new Element(npc.getTrait(ConstantsTrait.class).getConstant(event.getValue())).getAttribute(attribute.fulfill(1)));
}
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class FlagTags method flagTag.
/**
* Replaces FLAG TAGs. Called automatically by the dScript ScriptBuilder and Executer.
*
* @param event ReplaceableTagEvent
*/
@TagManager.TagEvents
public void flagTag(ReplaceableTagEvent event) {
if (!event.matches("FLAG")) {
return;
}
if (!event.hasValue()) {
return;
}
dB.echoError(event.getAttributes().getScriptEntry().getResidingQueue(), "flag.x: tags are deprecated! Use <x.flag[]>, EG, <global.flag[name]>!");
// Replace <FLAG...> TAGs.
String flagName = event.getValue().split(":").length > 1 ? event.getValue().split(":")[0].toUpperCase() : event.getValue().toUpperCase();
String flagFallback = event.hasAlternative() ? event.getAlternative() : "EMPTY";
int index = -1;
ReplaceType replaceType = ReplaceType.ASSTRING;
// Get format, if specified
if (flagName.contains(".")) {
try {
int replaceTypeIndex = flagName.split("\\.").length - 1;
replaceType = ReplaceType.valueOf(flagName.split("\\.")[replaceTypeIndex].replace("_", "").toUpperCase());
flagName = flagName.replace("." + flagName.split("\\.")[replaceTypeIndex], "");
} catch (Exception e) {
replaceType = ReplaceType.ASSTRING;
}
}
// Check flag replacement type
if (event.getType().toUpperCase().startsWith("G")) {
if (denizen.flagManager().getGlobalFlag(flagName).get(index).isEmpty()) {
// dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' flag not found, using fallback!", flagName);
} else {
FlagManager.Flag flag = denizen.flagManager().getGlobalFlag(flagName);
event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
// dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
}
} else if (event.getType().toUpperCase().startsWith("D") || event.getType().toUpperCase().startsWith("N")) {
if (denizen.flagManager().getNPCFlag(((BukkitTagContext) event.getContext()).npc.getId(), flagName).get(index).isEmpty()) {
// dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' flag not found, using fallback!", flagName);
} else {
FlagManager.Flag flag = denizen.flagManager().getNPCFlag(((BukkitTagContext) event.getContext()).npc.getId(), flagName);
event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
// dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
}
} else if (event.getType().toUpperCase().startsWith("P")) {
// Separate name since subType context may specify a different (or offline) player
dPlayer player = ((BukkitTagContext) event.getContext()).player;
// No name? No flag replacement!
if (player == null) {
return;
}
if (denizen.flagManager().getPlayerFlag(player, flagName).get(index).isEmpty()) {
if (replaceType.toString().equals("ISEXPIRED")) {
event.setReplaced("true");
}
} else {
FlagManager.Flag flag = denizen.flagManager().getPlayerFlag(player, flagName);
event.setReplaced(getReplaceable(flag, flag.get(index), replaceType));
// dB.echoDebug(ChatColor.YELLOW + "//REPLACED//" + ChatColor.WHITE + " '%s' with flag value '" + event.getReplaced() + "'.", flagName);
}
}
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class BookScriptContainer method writeBookTo.
public dItem writeBookTo(dItem book, dPlayer player, dNPC npc) {
// Get current ItemMeta from the book
BookMeta bookInfo = (BookMeta) book.getItemStack().getItemMeta();
if (contains("TITLE")) {
String title = getString("TITLE");
title = TagManager.tag(title, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.setTitle(title);
}
if (contains("SIGNED")) {
if (getString("SIGNED").equalsIgnoreCase("false")) {
book.getItemStack().setType(Material.BOOK_AND_QUILL);
}
}
if (contains("AUTHOR")) {
String author = getString("AUTHOR");
author = TagManager.tag(author, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.setAuthor(author);
}
if (contains("TEXT")) {
List<String> pages = getStringList("TEXT");
for (String page : pages) {
page = TagManager.tag(page, new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
bookInfo.addPage(page);
}
}
book.getItemStack().setItemMeta(bookInfo);
return book;
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class EntityScriptContainer method getEntityFrom.
public dEntity getEntityFrom(dPlayer player, dNPC npc) {
dEntity entity = null;
try {
if (contains("ENTITY_TYPE")) {
String entityType = TagManager.tag((getString("ENTITY_TYPE", "")), new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
entity = dEntity.valueOf(entityType);
} else {
throw new Exception("Missing entity_type argument!");
}
Set<StringHolder> strings = getConfigurationSection("").getKeys(false);
for (StringHolder string : strings) {
if (!string.low.equals("entity_type") && !string.low.equals("type")) {
String value = TagManager.tag((getString(string.low, "")), new BukkitTagContext(player, npc, false, null, shouldDebug(), new dScript(this)));
entity.adjust(new Mechanism(new Element(string.low), new Element(value)));
}
}
if (entity == null || entity.isUnique()) {
return null;
}
entity.setEntityScript(getName());
} catch (Exception e) {
dB.echoError("Woah! An exception has been called with this entity script!");
dB.echoError(e);
entity = null;
}
return entity;
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class dItem method valueOf.
/**
* Gets a Item Object from a string form.
*
* @param string The string or dScript argument String
* @param player The dPlayer to be used for player contexts
* where applicable.
* @param npc The dNPC to be used for NPC contexts
* where applicable.
* @return an Item, or null if incorrectly formatted
*/
public static dItem valueOf(String string, dPlayer player, dNPC npc) {
if (string == null || string.equals("")) {
return null;
}
Matcher m;
dItem stack = null;
///////
// Handle objects with properties through the object fetcher
m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
return ObjectFetcher.getObjectFrom(dItem.class, string, new BukkitTagContext(player, npc, false, null, true, null));
}
////////
// Match @object format for saved dItems
m = item_by_saved.matcher(string);
if (m.matches() && NotableManager.isSaved(m.group(2)) && NotableManager.isType(m.group(2), dItem.class)) {
stack = (dItem) NotableManager.getSavedObject(m.group(2));
if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}
return stack;
}
string = string.replace("i@", "");
m = ITEM_PATTERN.matcher(string);
if (m.matches()) {
try {
if (ScriptRegistry.containsScript(m.group(1), ItemScriptContainer.class)) {
// Get item from script
stack = ScriptRegistry.getScriptContainerAs(m.group(1), ItemScriptContainer.class).getItemFrom(player, npc);
} else if (ScriptRegistry.containsScript(m.group(1), BookScriptContainer.class)) {
// Get book from script
stack = ScriptRegistry.getScriptContainerAs(m.group(1), BookScriptContainer.class).getBookFrom(player, npc);
}
if (stack != null) {
if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}
return stack;
}
} catch (Exception e) {
// Just a catch, might be a regular item...
}
try {
String material = m.group(1).toUpperCase();
if (aH.matchesInteger(material)) {
stack = new dItem(Integer.valueOf(material));
} else {
dMaterial mat = dMaterial.valueOf(material);
stack = new dItem(mat.getMaterial());
if (mat.hasData()) {
stack.setDurability(mat.getData());
}
}
if (m.group(2) != null) {
stack.setDurability(Short.valueOf(m.group(2)));
}
if (m.group(3) != null) {
stack.setAmount(Integer.valueOf(m.group(3)));
}
return stack;
} catch (Exception e) {
if (!string.equalsIgnoreCase("none") && !nope) {
dB.log("Does not match a valid item ID or material: " + string);
}
}
}
if (!nope) {
dB.log("valueOf dItem returning null: " + string);
}
// No match! Return null.
return null;
}
Aggregations