use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.
the class GiveCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element engrave = scriptEntry.getElement("engrave");
Element unlimit_stack_size = scriptEntry.getElement("unlimit_stack_size");
dInventory inventory = (dInventory) scriptEntry.getObject("inventory");
Element qty = scriptEntry.getElement("qty");
Type type = (Type) scriptEntry.getObject("type");
Element slot = scriptEntry.getElement("slot");
Object items_object = scriptEntry.getObject("items");
List<dItem> items = null;
if (items_object != null) {
items = (List<dItem>) items_object;
}
dB.report(scriptEntry, getName(), aH.debugObj("Type", type.name()) + (inventory != null ? inventory.debug() : "") + aH.debugObj("Quantity", qty.asDouble()) + engrave.debug() + unlimit_stack_size.debug() + (items != null ? aH.debugObj("Items", items) : "") + slot.debug());
switch(type) {
case MONEY:
if (Depends.economy != null) {
Depends.economy.depositPlayer(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getOfflinePlayer(), qty.asDouble());
} else {
dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
break;
case EXP:
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().giveExp(qty.asInt());
break;
case ITEM:
boolean set_quantity = scriptEntry.hasObject("set_quantity");
boolean limited = !unlimit_stack_size.asBoolean();
for (dItem item : items) {
ItemStack is = item.getItemStack();
if (is.getType() == Material.AIR) {
dB.echoError("Cannot give air!");
continue;
}
if (set_quantity) {
is.setAmount(qty.asInt());
}
// TODO: Should engrave be kept?
if (engrave.asBoolean()) {
is = CustomNBT.addCustomNBT(item.getItemStack(), "owner", ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getName(), CustomNBT.KEY_DENIZEN);
}
List<ItemStack> leftovers = inventory.addWithLeftovers(slot.asInt() - 1, limited, is);
if (!leftovers.isEmpty()) {
dB.echoDebug(scriptEntry, "The inventory didn't have enough space, the rest of the items have been placed on the floor.");
for (ItemStack leftoverItem : leftovers) {
inventory.getLocation().getWorld().dropItem(inventory.getLocation(), leftoverItem);
}
}
}
break;
}
}
use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.
the class ActionHandler method doAction.
public String doAction(String actionName, dNPC npc, dPlayer player, AssignmentScriptContainer assignment, Map<String, dObject> context) {
if (context == null) {
context = new HashMap<String, dObject>();
}
String determination = "none";
if (assignment == null) {
// dB.echoDebug("Tried to do 'on " + actionName + ":' but couldn't find a matching script.");
return determination;
}
if (!assignment.contains("actions.on " + actionName)) {
return determination;
}
dB.report(assignment, "Action", aH.debugObj("Type", "On " + actionName) + aH.debugObj("NPC", npc.toString()) + assignment.getAsScriptArg().debug() + (player != null ? aH.debugObj("Player", player.getName()) : ""));
// Fetch script from Actions
List<ScriptEntry> script = assignment.getEntries(new BukkitScriptEntryData(player, npc), "actions.on " + actionName);
if (script.isEmpty()) {
return determination;
}
// Create new ID -- this is what we will look for when determining an outcome
long id = DetermineCommand.getNewId();
// Add the reqId to each of the entries for the determine command
ScriptBuilder.addObjectToEntries(script, "ReqId", id);
dB.echoDebug(assignment, DebugElement.Header, "Building action 'On " + actionName.toUpperCase() + "' for " + npc.toString());
// Add entries and context to the queue
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(assignment.getName())).addEntries(script);
OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
oecs.contexts = context;
oecs.contexts.put("event_header", new Element(actionName));
queue.setContextSource(oecs);
// Start the queue!
queue.start();
// Check the determination by asking the DetermineCommand
if (DetermineCommand.hasOutcome(id)) {
determination = DetermineCommand.getOutcome(id).get(0);
}
// TODO: Multiple determination system
return determination;
}
use of net.aufdemrand.denizen.BukkitScriptEntryData 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.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.
the class dB method echoError.
public static void echoError(ScriptQueue source, String message) {
dScript script = null;
if (source != null && source.getEntries().size() > 0 && source.getEntries().get(0).getScript() != null) {
script = source.getEntries().get(0).getScript();
} else if (source != null && source.getLastEntryExecuted() != null && source.getLastEntryExecuted().getScript() != null) {
script = source.getLastEntryExecuted().getScript();
}
if (ThrowErrorEvent) {
ThrowErrorEvent = false;
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(message));
if (source != null) {
context.put("queue", source);
}
if (script != null) {
context.put("script", script);
}
List<String> events = new ArrayList<String>();
events.add("script generates error");
if (script != null) {
events.add(script.identifySimple() + " generates error");
}
ScriptEntry entry = (source != null ? source.getLastEntryExecuted() : null);
List<String> Determinations = OldEventManager.doEvents(events, entry != null ? entry.entryData : new BukkitScriptEntryData(null, null), context, true);
ThrowErrorEvent = true;
for (String Determination : Determinations) {
if (Determination.equalsIgnoreCase("CANCELLED")) {
return;
}
}
}
if (!showDebug) {
return;
}
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR" + (script != null ? " in script '" + script.getName() + "'" : "") + "! " + ChatColor.WHITE + trimMessage(message));
}
use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.
the class NPCTags method navCancel.
// <--[action]
// @Actions
// cancel navigation
// cancel navigation due to <reason>
//
// @Triggers when a plugin or script cancels an NPC's navigation.
//
// @Context
// None
//
// -->
@EventHandler
public void navCancel(NavigationCancelEvent event) {
dNPC npc = DenizenAPI.getDenizenNPC(event.getNPC());
if (NPCNavigationSmartEvent.IsActive()) {
OldEventManager.doEvents(Arrays.asList("npc cancels navigation"), new BukkitScriptEntryData(null, npc), null);
}
if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
return;
}
npc.action("cancel navigation", null);
npc.action("cancel navigation due to " + event.getCancelReason().toString(), null);
}
Aggregations