Skip to content

Commit

Permalink
slightly smarter toggles item event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 19, 2021
1 parent 4a74366 commit 0854e12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
@@ -1,8 +1,10 @@
package com.denizenscript.denizen.events.player;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
Expand Down Expand Up @@ -32,8 +34,11 @@ public class PlayerHoldsItemEvent extends BukkitScriptEvent implements Listener
//
// @Triggers when a player starts or stops holding up an item, such as a shield, spyglass, or crossbow.
//
// @Warning For 'lowers', the item may be tracked incorrectly. Prefer 'player lowers item' (the generic item form) for a 'lowers' event (similar for 'toggles').
// Also be aware this event may misfire in some cases.
//
// @Context
// <context.state> returns an ElementTag(Boolean) with a value of "true" if the player is now holding a shield and "false" otherwise.
// <context.state> returns an ElementTag(Boolean) with a value of "true" if the player is now holding up a raisable item and "false" otherwise.
//
// @Player Always.
//
Expand All @@ -47,6 +52,7 @@ public PlayerHoldsItemEvent() {
public static PlayerHoldsItemEvent instance;
public PlayerTag player;
public boolean state;
public ItemTag raised;

@Override
public boolean couldMatch(ScriptPath path) {
Expand Down Expand Up @@ -76,7 +82,7 @@ public boolean matches(ScriptPath path) {
return false;
}
String item = path.eventArgLowerAt(2);
if (!item.equals("item") && !tryItem(player.getHeldItem(), item)) {
if (!item.equals("item") && !tryItem(raised, item)) {
return false;
}
return super.matches(path);
Expand Down Expand Up @@ -115,25 +121,34 @@ public void destroy() {
enabled = false;
}

public void run(Player pl) {
cancelled = false;
player = new PlayerTag(pl);
if (DenizenPacketHandler.raisableItems.contains(player.getHeldItem().getBukkitMaterial())
|| !DenizenPacketHandler.raisableItems.contains(player.getOffhandItem().getBukkitMaterial())) {
raised = player.getHeldItem();
}
else {
raised = player.getOffhandItem();
}
fire();
}

public static void signalDidRaise(Player player) {
if (raisedShields.contains(player.getUniqueId())) {
return;
}
raisedShields.add(player.getUniqueId());
instance.state = true;
instance.player = new PlayerTag(player);
instance.cancelled = false;
instance.fire();
instance.run(player);
}

public static void signalDidLower(Player player) {
if (!raisedShields.remove(player.getUniqueId())) {
return;
}
instance.state = false;
instance.player = new PlayerTag(player);
instance.cancelled = false;
instance.fire();
instance.run(player);
}

@EventHandler
Expand Down
Expand Up @@ -1880,6 +1880,7 @@ else if (mtr.angle == BlockFace.EAST) {
// @group attributes
// @description
// Returns whether the entity is supported by a block.
// This can be inaccurate for players.
// -->
registerSpawnedOnlyTag(ElementTag.class, "is_on_ground", (attribute, object) -> {
return new ElementTag(object.getBukkitEntity().isOnGround());
Expand Down
Expand Up @@ -415,6 +415,10 @@ public ItemTag getHeldItem() {
return new ItemTag(getPlayerEntity().getEquipment().getItemInMainHand());
}

public ItemTag getOffhandItem() {
return new ItemTag(getPlayerEntity().getEquipment().getItemInOffHand());
}

public void decrementStatistic(Statistic statistic, int amount) {
getOfflinePlayer().decrementStatistic(statistic, amount);
}
Expand Down

0 comments on commit 0854e12

Please sign in to comment.