Skip to content

Commit

Permalink
partial initial implementation of modernized tag base registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 23, 2020
1 parent 8afed58 commit 09ccbd6
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 521 deletions.
Expand Up @@ -41,7 +41,7 @@ public class AreaEnterExitScriptEvent extends BukkitScriptEvent implements Liste
//
// @Context
// <context.area> returns the area object that was entered or exited.
// <context.cause> returns the cause of the event. Can be: WALK, WORLD_CHANGE, JOIN, LEAVE, TELEPORT, VEHICLE.
// <context.cause> returns the cause of the event. Can be: WALK, WORLD_CHANGE, JOIN, QUIT, TELEPORT, VEHICLE.
// <context.to> returns the location the player moved to.
// <context.from> returns the location the player moved from (when available).
//
Expand Down
Expand Up @@ -72,10 +72,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
/* Match arguments to expected variables */
for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("qty")
if (!scriptEntry.hasObject("quantity")
&& arg.matchesPrefix("q", "qty", "quantity")
&& arg.matchesFloat()) {
scriptEntry.addObject("qty", arg.asElement());
scriptEntry.addObject("quantity", arg.asElement());
scriptEntry.addObject("set_quantity", new ElementTag(true));
}
else if (!scriptEntry.hasObject("type")
Expand Down Expand Up @@ -118,7 +118,7 @@ else if (!scriptEntry.hasObject("slot")
scriptEntry.defaultObject("type", Type.ITEM)
.defaultObject("engrave", new ElementTag(false))
.defaultObject("unlimit_stack_size", new ElementTag(false))
.defaultObject("qty", new ElementTag(1))
.defaultObject("quantity", new ElementTag(1))
.defaultObject("slot", new ElementTag(1));

Type type = (Type) scriptEntry.getObject("type");
Expand All @@ -143,7 +143,7 @@ public void execute(ScriptEntry scriptEntry) {
ElementTag engrave = scriptEntry.getElement("engrave");
ElementTag unlimit_stack_size = scriptEntry.getElement("unlimit_stack_size");
InventoryTag inventory = scriptEntry.getObjectTag("inventory");
ElementTag qty = scriptEntry.getElement("qty");
ElementTag quantity = scriptEntry.getElement("quantity");
Type type = (Type) scriptEntry.getObject("type");
ElementTag slot = scriptEntry.getElement("slot");

Expand All @@ -159,7 +159,7 @@ public void execute(ScriptEntry scriptEntry) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugObj("Type", type.name())
+ (inventory != null ? inventory.debug() : "")
+ ArgumentHelper.debugObj("Quantity", qty.asDouble())
+ quantity.debug()
+ engrave.debug()
+ unlimit_stack_size.debug()
+ (items != null ? ArgumentHelper.debugObj("Items", items) : "")
Expand All @@ -171,15 +171,15 @@ public void execute(ScriptEntry scriptEntry) {

case MONEY:
if (Depends.economy != null) {
Depends.economy.depositPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), qty.asDouble());
Depends.economy.depositPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
}
else {
Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
break;

case EXP:
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(qty.asInt());
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(quantity.asInt());
break;

case ITEM:
Expand All @@ -192,7 +192,7 @@ public void execute(ScriptEntry scriptEntry) {
continue;
}
if (set_quantity) {
is.setAmount(qty.asInt());
is.setAmount(quantity.asInt());
}
// TODO: Should engrave be kept?
if (engrave.asBoolean()) {
Expand Down
Expand Up @@ -105,10 +105,10 @@ else if (!scriptEntry.hasObject("type")
&& arg.matches("item_in_hand", "iteminhand")) {
scriptEntry.addObject("type", Type.ITEMINHAND);
}
else if (!scriptEntry.hasObject("qty")
else if (!scriptEntry.hasObject("quantity")
&& arg.matchesPrefix("q", "qty", "quantity")
&& arg.matchesFloat()) {
scriptEntry.addObject("qty", arg.asElement());
scriptEntry.addObject("quantity", arg.asElement());
}
else if (!scriptEntry.hasObject("items")
&& arg.matchesPrefix("bydisplay")
Expand Down Expand Up @@ -172,7 +172,7 @@ else if (!scriptEntry.hasObject("inventory")
}

scriptEntry.defaultObject("type", Type.ITEM)
.defaultObject("qty", new ElementTag(1));
.defaultObject("quantity", new ElementTag(1));

Type type = (Type) scriptEntry.getObject("type");

Expand All @@ -194,7 +194,7 @@ else if (!scriptEntry.hasObject("inventory")
public void execute(ScriptEntry scriptEntry) {

InventoryTag inventory = scriptEntry.getObjectTag("inventory");
ElementTag qty = scriptEntry.getElement("qty");
ElementTag quantity = scriptEntry.getElement("quantity");
ElementTag displayname = scriptEntry.getElement("displayname");
ItemTag scriptitem = scriptEntry.getObjectTag("scriptitem");
ElementTag slot = scriptEntry.getElement("slot");
Expand All @@ -212,7 +212,7 @@ public void execute(ScriptEntry scriptEntry) {

if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("Type", type.name())
+ qty.debug()
+ quantity.debug()
+ (inventory != null ? inventory.debug() : "")
+ (displayname != null ? displayname.debug() : "")
+ (scriptitem != null ? scriptitem.debug() : "")
Expand All @@ -230,7 +230,7 @@ public void execute(ScriptEntry scriptEntry) {
}
case ITEMINHAND: {
int inHandAmt = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().getEquipment().getItemInMainHand().getAmount();
int theAmount = (int) qty.asDouble();
int theAmount = (int) quantity.asDouble();
ItemStack newHandItem = new ItemStack(Material.AIR);
if (theAmount > inHandAmt) {
Debug.echoDebug(scriptEntry, "...player did not have enough of the item in hand, taking all...");
Expand All @@ -256,19 +256,19 @@ public void execute(ScriptEntry scriptEntry) {
Debug.echoError(scriptEntry.getResidingQueue(), "No economy loaded! Have you installed Vault and a compatible economy plugin?");
return;
}
Depends.economy.withdrawPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), qty.asDouble());
Depends.economy.withdrawPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
break;
}
case XP: {
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(-qty.asInt());
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(-quantity.asInt());
break;
}
case ITEM: {
for (ItemTag item : items) {
ItemStack is = item.getItemStack();
is.setAmount(qty.asInt());
is.setAmount(quantity.asInt());
if (!inventory.removeItem(item, item.getAmount())) {
Debug.echoDebug(scriptEntry, "Inventory does not contain at least " + qty.asInt() + " of " + item.identify() + "... Taking all...");
Debug.echoDebug(scriptEntry, "Inventory does not contain at least " + quantity.asInt() + " of " + item.identify() + "... Taking all...");
}
}
break;
Expand All @@ -279,39 +279,39 @@ public void execute(ScriptEntry scriptEntry) {
return;
}
takeByMatcher(inventory, (item) -> item.hasItemMeta() && item.getItemMeta().hasDisplayName() &&
item.getItemMeta().getDisplayName().equalsIgnoreCase(displayname.identify()), qty.asInt());
item.getItemMeta().getDisplayName().equalsIgnoreCase(displayname.identify()), quantity.asInt());
break;
}
case BYCOVER: {
if (titleAuthor == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Must specify a cover!");
return;
}
inventory.removeBook(titleAuthor.get(0), titleAuthor.size() > 1 ? titleAuthor.get(1) : null, qty.asInt());
inventory.removeBook(titleAuthor.get(0), titleAuthor.size() > 1 ? titleAuthor.get(1) : null, quantity.asInt());
break;
}
case NBT: {
if (nbtKey == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Must specify an NBT key!");
return;
}
takeByMatcher(inventory, (item) -> CustomNBT.hasCustomNBT(item, nbtKey.asString(), CustomNBT.KEY_DENIZEN), qty.asInt());
takeByMatcher(inventory, (item) -> CustomNBT.hasCustomNBT(item, nbtKey.asString(), CustomNBT.KEY_DENIZEN), quantity.asInt());
break;
}
case SCRIPTNAME: {
if (scriptitem == null || scriptitem.getScriptName() == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Must specify a valid script name!");
return;
}
takeByMatcher(inventory, (item) -> scriptitem.getScriptName().equalsIgnoreCase(new ItemTag(item).getScriptName()), qty.asInt());
takeByMatcher(inventory, (item) -> scriptitem.getScriptName().equalsIgnoreCase(new ItemTag(item).getScriptName()), quantity.asInt());
break;
}
case MATERIAL: {
if (material == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Must specify a valid material!");
return;
}
takeByMatcher(inventory, (item) -> item.getType() == material.getMaterial() && !(new ItemTag(item).isItemscript()), qty.asInt());
takeByMatcher(inventory, (item) -> item.getType() == material.getMaterial() && !(new ItemTag(item).isItemscript()), quantity.asInt());
break;
}
case SLOT: {
Expand All @@ -322,8 +322,8 @@ public void execute(ScriptEntry scriptEntry) {
}
ItemStack original = inventory.getInventory().getItem(slotId);
if (original != null && original.getType() != Material.AIR) {
if (original.getAmount() > qty.asInt()) {
original.setAmount(original.getAmount() - qty.asInt());
if (original.getAmount() > quantity.asInt()) {
original.setAmount(original.getAmount() - quantity.asInt());
inventory.setSlots(slotId, original);
}
else {
Expand Down
@@ -1,11 +1,7 @@
package com.denizenscript.denizen.tags.core;

import com.denizenscript.denizen.objects.BiomeTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;

public class BiomeTagBase {

Expand All @@ -18,32 +14,12 @@ public BiomeTagBase() {
// Returns a biome object constructed from the input value.
// Refer to <@link language BiomeTag objects>.
// -->
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
biomeTags(event);
TagManager.registerTagHandler("biome", (attribute) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("Biome tag base must have input.");
return null;
}
}, "biome");
}

public void biomeTags(ReplaceableTagEvent event) {

if (!event.matches("biome") || event.replaced()) {
return;
}

BiomeTag biome = null;

if (event.hasNameContext()) {
biome = BiomeTag.valueOf(event.getNameContext(), event.getAttributes().context);
}

if (biome == null) {
return;
}

Attribute attribute = event.getAttributes();
event.setReplacedObject(CoreUtilities.autoAttrib(biome, attribute.fulfill(1)));

return BiomeTag.valueOf(attribute.getContext(1), attribute.context);
});
}
}
@@ -1,11 +1,7 @@
package com.denizenscript.denizen.tags.core;

import com.denizenscript.denizen.objects.ChunkTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;

public class ChunkTagBase {

Expand All @@ -18,32 +14,12 @@ public ChunkTagBase() {
// Returns a chunk object constructed from the input value.
// Refer to <@link language ChunkTag objects>.
// -->
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
chunkTags(event);
TagManager.registerTagHandler("chunk", (attribute) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("Chunk tag base must have input.");
return null;
}
}, "chunk");
}

public void chunkTags(ReplaceableTagEvent event) {

if (!event.matches("chunk") || event.replaced()) {
return;
}

ChunkTag chunk = null;

if (event.hasNameContext()) {
chunk = ChunkTag.valueOf(event.getNameContext(), event.getAttributes().context);
}

if (chunk == null) {
return;
}

Attribute attribute = event.getAttributes();
event.setReplacedObject(CoreUtilities.autoAttrib(chunk, attribute.fulfill(1)));

return ChunkTag.valueOf(attribute.getContext(1), attribute.context);
});
}
}
@@ -1,11 +1,7 @@
package com.denizenscript.denizen.tags.core;

import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;

public class ColorTagBase {

Expand All @@ -18,32 +14,12 @@ public ColorTagBase() {
// Returns a color object constructed from the input value.
// Refer to <@link language ColorTag objects>.
// -->
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
colorTags(event);
TagManager.registerTagHandler("color", (attribute) -> {
if (!attribute.hasContext(1)) {
attribute.echoError("Color tag base must have input.");
return null;
}
}, "color");
}

public void colorTags(ReplaceableTagEvent event) {

if (!event.matches("color") || event.replaced()) {
return;
}

ColorTag color = null;

if (event.hasNameContext()) {
color = ColorTag.valueOf(event.getNameContext(), event.getAttributes().context);
}

if (color == null) {
return;
}

Attribute attribute = event.getAttributes();
event.setReplacedObject(CoreUtilities.autoAttrib(color, attribute.fulfill(1)));

return ColorTag.valueOf(attribute.getContext(1), attribute.context);
});
}
}

0 comments on commit 09ccbd6

Please sign in to comment.