use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class DenizenChat method talk.
@Override
public void talk(SpeechContext speechContext) {
if (!(speechContext instanceof DenizenSpeechContext)) {
return;
}
DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
Talkable talker = context.getTalker();
if (talker == null) {
return;
}
ScriptEntry entry = context.getScriptEntry();
ScriptQueue queue = entry.getResidingQueue();
String defTalker = null;
if (queue.hasDefinition("talker")) {
defTalker = queue.getDefinition("talker");
}
queue.addDefinition("talker", new dEntity(talker.getEntity()).identify());
String defMessage = null;
if (queue.hasDefinition("message")) {
defMessage = queue.getDefinition("message");
}
queue.addDefinition("message", context.getMessage());
// Chat to the world using Denizen chat settings
if (!context.hasRecipients()) {
String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, text, context);
} else // Single recipient
if (context.size() <= 1) {
// Send chat to target
String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
for (Talkable entity : context) {
entity.talkTo(context, text, this);
}
// Check if bystanders hear targeted chat
if (context.isBystandersEnabled()) {
String defTarget = null;
if (queue.hasDefinition("target")) {
defTarget = queue.getDefinition("target");
}
queue.addDefinition("target", new dEntity(context.iterator().next().getEntity()).identify());
String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, bystanderText, context);
if (defTarget != null) {
queue.addDefinition("target", defTarget);
}
}
} else // Multiple recipients
{
// Send chat to targets
String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
for (Talkable entity : context) {
entity.talkTo(context, text, this);
}
if (context.isBystandersEnabled()) {
String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
if (format.length <= 1) {
dB.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
}
StringBuilder parsed = new StringBuilder();
Iterator<Talkable> iter = context.iterator();
int i = 0;
while (iter.hasNext()) {
if (i == format.length) {
parsed.append(format[i]);
break;
}
parsed.append(format[i]).append(new dEntity(iter.next().getEntity()).getName());
i++;
}
String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry, false));
String defTargets = null;
if (queue.hasDefinition("targets")) {
defTargets = queue.getDefinition("targets");
}
queue.addDefinition("targets", targets);
String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, bystanderText, context);
if (defTargets != null) {
queue.addDefinition("targets", defTargets);
}
}
}
if (defMessage != null) {
queue.addDefinition("message", defMessage);
}
if (defTalker != null) {
queue.addDefinition("talker", defTalker);
}
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class BukkitElementProperties method getAttribute.
@Override
public String getAttribute(Attribute attribute) {
// -->
if (attribute.startsWith("is") && attribute.hasContext(1) && (attribute.startsWith("to", 2) || attribute.startsWith("than", 2)) && attribute.hasContext(2)) {
// Use the Comparable object as implemented for the IF command. First, a new Comparable!
Comparable com = new Comparable();
// Check for negative logic
String operator;
if (attribute.getContext(1).startsWith("!")) {
operator = attribute.getContext(1).substring(1);
com.setNegativeLogic();
} else {
operator = attribute.getContext(1);
}
// Operator is the value of the .is[] context. Valid are Comparable.Operators, same
// as used by the IF command.
Comparable.Operator comparableOperator = null;
try {
comparableOperator = Comparable.Operator.valueOf(operator.replace("==", "EQUALS").replace(">=", "OR_MORE").replace("<=", "OR_LESS").replace("<", "LESS").replace(">", "MORE").replace("=", "EQUALS").toUpperCase());
} catch (IllegalArgumentException e) {
}
if (comparableOperator != null) {
com.setOperator(comparableOperator);
// Comparable is the value of this element
com.setComparable(element.asString());
// Compared_to is the value of the .to[] context.
com.setComparedto(attribute.getContext(2));
return new Element(com.determineOutcome()).getAttribute(attribute.fulfill(2));
} else {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError("Unknown operator '" + operator + "'.");
}
}
// -->
if (attribute.startsWith("aschunk") || attribute.startsWith("as_chunk")) {
dObject object = Element.handleNull(element.asString(), dChunk.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dChunk", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("ascolor") || attribute.startsWith("as_color")) {
dObject object = Element.handleNull(element.asString(), dColor.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dColor", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("ascuboid") || attribute.startsWith("as_cuboid")) {
dObject object = Element.handleNull(element.asString(), dCuboid.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dCuboid", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asentity") || attribute.startsWith("as_entity")) {
dObject object = Element.handleNull(element.asString(), dEntity.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dEntity", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asinventory") || attribute.startsWith("as_inventory")) {
dObject object = Element.handleNull(element.asString(), dInventory.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dInventory", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asitem") || attribute.startsWith("as_item")) {
dObject object = Element.handleNull(element.asString(), dItem.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dItem", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("aslocation") || attribute.startsWith("as_location")) {
dObject object = Element.handleNull(element.asString(), dLocation.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dLocation", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asmaterial") || attribute.startsWith("as_material")) {
dObject object = Element.handleNull(element.asString(), dMaterial.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dMaterial", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asnpc") || attribute.startsWith("as_npc")) {
dObject object = Element.handleNull(element.asString(), dNPC.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dNPC", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asplayer") || attribute.startsWith("as_player")) {
dObject object = Element.handleNull(element.asString(), dPlayer.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dPlayer", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asworld") || attribute.startsWith("as_world")) {
dObject object = Element.handleNull(element.asString(), dWorld.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dWorld", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("asplugin") || attribute.startsWith("as_plugin")) {
dObject object = Element.handleNull(element.asString(), dPlugin.valueOf(element.asString(), new BukkitTagContext(attribute.getScriptEntry(), false)), "dPlugin", attribute.hasAlternative());
if (object != null) {
return object.getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(element.debug())).getAttribute(attribute.fulfill(2));
}
// -->
if (attribute.startsWith("last_color")) {
return new Element(ChatColor.getLastColors(element.asString())).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("format") && attribute.hasContext(1)) {
FormatScriptContainer format = ScriptRegistry.getScriptContainer(attribute.getContext(1));
if (format == null) {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError("Could not find format script matching '" + attribute.getContext(1) + "'");
return null;
} else {
return new Element(format.getFormattedText(element.asString(), attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getNPC() : null, attribute.getScriptEntry() != null ? ((BukkitScriptEntryData) attribute.getScriptEntry().entryData).getPlayer() : null)).getAttribute(attribute.fulfill(1));
}
}
// -->
if (attribute.startsWith("strip_color")) {
return new Element(ChatColor.stripColor(element.asString())).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("parse_color")) {
char prefix = '&';
if (attribute.hasContext(1)) {
prefix = attribute.getContext(1).charAt(0);
}
return new Element(ChatColor.translateAlternateColorCodes(prefix, element.asString())).getAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("to_itemscript_hash")) {
return new Element(ItemScriptHelper.createItemScriptID(element.asString())).getAttribute(attribute.fulfill(1));
}
return null;
}
use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.
the class dInventory method valueOf.
/**
* Gets a dInventory from a string format.
*
* @param string The inventory in string form. (in@player[playerName], in@scriptName, etc.)
* @return The dInventory value. If the string is incorrectly formatted or
* the specified inventory is invalid, this is null.
*/
public static dInventory valueOf(String string, dPlayer player, dNPC npc) {
if (string == null) {
return null;
}
///////
// Handle objects with properties through the object fetcher
Matcher m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
return ObjectFetcher.getObjectFrom(dInventory.class, string, new BukkitTagContext(player, npc, false, null, false, null));
}
// Match in@scriptName for Inventory Scripts, as well as in@notableName
m = inventory_by_script.matcher(string);
if (m.matches()) {
if (ScriptRegistry.containsScript(m.group(2), InventoryScriptContainer.class)) {
return ScriptRegistry.getScriptContainerAs(m.group(2), InventoryScriptContainer.class).getInventoryFrom(player, npc);
}
if (NotableManager.isSaved(m.group(2)) && NotableManager.isType(m.group(2), dInventory.class)) {
return (dInventory) NotableManager.getSavedObject(m.group(2));
}
for (String idType : idTypes) {
if (m.group(2).equalsIgnoreCase(idType)) {
return new dInventory(m.group(2));
}
}
}
m = inventory_by_type.matcher(string);
if (m.matches()) {
// Set the type of the inventory holder
String type = CoreUtilities.toLowerCase(m.group(2));
// Set the name/id/location of the inventory holder
String holder = m.group(3);
if (type.equals("generic")) {
Argument arg = Argument.valueOf(holder);
if (arg.matchesEnum(InventoryType.values())) {
return new dInventory(InventoryType.valueOf(holder.toUpperCase()));
} else if (arg.matchesPrimitive(PrimitiveType.Integer)) {
return new dInventory(arg.asElement().asInt());
} else {
dB.echoError("That type of inventory does not exist!");
}
} else if (type.equals("npc")) {
if (dNPC.matches(holder)) {
return dNPC.valueOf(holder).getDenizenInventory();
}
} else if (type.equals("player")) {
if (dPlayer.matches(holder)) {
return dPlayer.valueOf(holder).getInventory();
}
} else if (type.equals("workbench")) {
if (dPlayer.matches(holder)) {
dInventory workbench = dPlayer.valueOf(holder).getWorkbench();
if (workbench != null) {
dB.echoError("Value of dInventory returning null (" + string + ")." + " Specified player does not have an open workbench.");
} else {
return workbench;
}
}
} else if (type.equals("enderchest")) {
if (dPlayer.matches(holder)) {
return dPlayer.valueOf(holder).getEnderChest();
}
} else if (type.equals("entity")) {
if (dEntity.matches(holder)) {
return dEntity.valueOf(holder).getInventory();
}
} else if (type.equals("location")) {
if (dLocation.matches(holder)) {
return dLocation.valueOf(holder).getInventory();
}
}
// If the dInventory is invalid, alert the user and return null
dB.echoError("Value of dInventory returning null. Invalid " + type + " specified: " + holder);
return null;
}
dB.echoError("Value of dInventory returning null. Invalid dInventory specified: " + string);
return null;
}
Aggregations