Skip to content

Commit

Permalink
add 'on player raises <item>' option for shield event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 4, 2020
1 parent 702156c commit 6cc1d76
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
Expand Up @@ -24,6 +24,7 @@ public class PlayerHoldsShieldEvent extends BukkitScriptEvent implements Listene
// player raises shield
// player lowers shield
// player toggles shield
// player (raises/lowers/toggles) <shield-item>
//
// @Regex ^on player (toggles|raises|lowers) shield$
//
Expand Down Expand Up @@ -52,7 +53,10 @@ public boolean couldMatch(ScriptPath path) {
if (!(middleWord.equals("raises") || middleWord.equals("lowers") || middleWord.equals("toggles"))) {
return false;
}
return path.eventArgLowerAt(0).equals("player") && path.eventArgLowerAt(2).equals("shield");
if (!path.eventArgLowerAt(0).equals("player")) {
return false;
}
return (path.eventArgLowerAt(2).equals("shield") || couldMatchItem(path.eventArgLowerAt(2)));
}

@Override
Expand All @@ -67,6 +71,10 @@ public boolean matches(ScriptPath path) {
if (!runInCheck(path, player.getLocation())) {
return false;
}
String shieldItem = path.eventArgLowerAt(2);
if (!shieldItem.equals("shield") && !tryItem(player.getHeldItem(), shieldItem)) {
return false;
}
return super.matches(path);
}

Expand Down
Expand Up @@ -1255,14 +1255,19 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object NPCTag
// @name owner
// @input Element
// @input PlayerTag
// @description
// Sets the owner of the NPC.
// @tags
// <NPCTag.owner>
// -->
if (mechanism.matches("owner")) {
getCitizen().getTrait(Owner.class).setOwner(mechanism.getValue().asString());
if (PlayerTag.matches(mechanism.getValue().asString())) {
getCitizen().getTrait(Owner.class).setOwner(mechanism.valueAsType(PlayerTag.class).getPlayerEntity());
}
else {
getCitizen().getTrait(Owner.class).setOwner(mechanism.getValue().asString());
}
}

// <--[mechanism]
Expand Down
Expand Up @@ -418,6 +418,10 @@ public World getWorld() {
}
}

public ItemTag getHeldItem() {
return new ItemTag(getPlayerEntity().getEquipment().getItemInMainHand());
}

public void decrementStatistic(Statistic statistic, int amount) {
if (isOnline()) {
getPlayerEntity().decrementStatistic(statistic, amount);
Expand Down Expand Up @@ -1665,7 +1669,7 @@ else if (Depends.permissions != null) {
attribute.fulfill(1);
return new ElementTag(object.getPlayerEntity().getInventory().getHeldItemSlot() + 1);
}
return new ItemTag(object.getPlayerEntity().getEquipment().getItemInMainHand());
return object.getHeldItem();
});

// <--[tag]
Expand Down
Expand Up @@ -115,7 +115,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// @group properties
// @mechanism ItemTag.patterns
// @description
// Lists a banner's patterns in the form "OLOR/PATTERN|COLOR/PATTERN" etc.
// Lists a banner's patterns in the form "COLOR/PATTERN|COLOR/PATTERN" etc.
// For the list of possible colors, see <@link url http://bit.ly/1dydq12>.
// For the list of possible patterns, see <@link url http://bit.ly/1MqRn7T>.
// -->
Expand Down
Expand Up @@ -58,23 +58,14 @@ public void run(ReplaceableTagEvent event) {
}

public void npcTags(ReplaceableTagEvent event) {

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

// Build a new attribute out of the raw_tag supplied in the script to be fulfilled
Attribute attribute = event.getAttributes();

// NPCTags require a... NPCTag!
NPCTag n = ((BukkitTagContext) event.getContext()).npc;

// Player tag may specify a new player in the <player[context]...> portion of the tag.
if (attribute.hasContext(1))
// Check if this is a valid player and update the PlayerTag object reference.
{
NPCTag npc = ((BukkitTagContext) event.getContext()).npc;
if (attribute.hasContext(1)) {
if (NPCTag.matches(attribute.getContext(1))) {
n = NPCTag.valueOf(attribute.getContext(1), attribute.context);
npc = NPCTag.valueOf(attribute.getContext(1), attribute.context);
}
else {
if (!event.hasAlternative()) {
Expand All @@ -83,16 +74,13 @@ public void npcTags(ReplaceableTagEvent event) {
return;
}
}


if (n == null || !n.isValid()) {
if (npc == null || !npc.isValid()) {
if (!event.hasAlternative()) {
Debug.echoError("Invalid or missing NPC for tag <" + event.raw_tag + ">!");
}
return;
}

event.setReplacedObject(CoreUtilities.autoAttrib(n, attribute.fulfill(1)));
event.setReplacedObject(CoreUtilities.autoAttrib(npc, attribute.fulfill(1)));

}

Expand Down

0 comments on commit 6cc1d76

Please sign in to comment.