Skip to content

Commit

Permalink
Fix plugin not handling interactions while holding deactivated items (#…
Browse files Browse the repository at this point in the history
…351)

* Fix interacting with disabled items not being handled, close #328

* Bump pl3xmap

* Also disable using blocks during spawn egg handling
  • Loading branch information
WiIIiam278 authored Nov 2, 2023
1 parent a9b1b62 commit 37245e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand All @@ -49,12 +50,12 @@ default void onPlayerInteract(@NotNull PlayerInteractEvent e) {
switch (e.getAction()) {
case RIGHT_CLICK_AIR -> {
if (e.getHand() == EquipmentSlot.HAND) {
handleRightClick(e);
handleItemInteraction(e);
}
}
case RIGHT_CLICK_BLOCK -> {
if (e.getHand() == EquipmentSlot.HAND) {
if (handleRightClick(e)) {
if (handleItemInteraction(e)) {
return;
}
}
Expand Down Expand Up @@ -110,11 +111,18 @@ default void onPlayerInteract(@NotNull PlayerInteractEvent e) {
}

// Handle inspecting chunks and using spawn eggs
private boolean handleRightClick(@NotNull PlayerInteractEvent e) {
if (e.useItemInHand() == Event.Result.DENY) {
return true;
private boolean handleItemInteraction(@NotNull PlayerInteractEvent e) {
// Check if the user was allowed to perform an action using an item in their main hand
if (e.useItemInHand() != Event.Result.DENY) {
return handleInspectorTool(e) || handleSpawnEggs(e);
}

// Otherwise, the event was handled provided the user didn't right-click a block
return e.getAction() != Action.RIGHT_CLICK_BLOCK;
}

// Returns true if an inspector tool operation was handled
private boolean handleInspectorTool(@NotNull PlayerInteractEvent e) {
final Material item = e.getPlayer().getInventory().getItemInMainHand().getType();
if (item == Material.matchMaterial(getPlugin().getSettings().getInspectorTool())) {
e.setUseInteractedBlock(Event.Result.DENY);
Expand All @@ -134,14 +142,20 @@ private boolean handleRightClick(@NotNull PlayerInteractEvent e) {
}
return true;
}
return false;
}

// Returns true if a spawn egg operation was handled
private boolean handleSpawnEggs(@NotNull PlayerInteractEvent e) {
final Material item = e.getPlayer().getInventory().getItemInMainHand().getType();
if (item.getKey().toString().toLowerCase().contains(SPAWN_EGG_NAME)) {
if (getListener().handler().cancelOperation(Operation.of(
BukkitUser.adapt(e.getPlayer()),
Operation.Type.USE_SPAWN_EGG,
getPosition(e.getPlayer().getLocation())
))) {
e.setUseItemInHand(Event.Result.DENY);
e.setUseInteractedBlock(Event.Result.DENY);
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
compileOnly "redis.clients:jedis:$jedis_version"
compileOnly 'com.github.BlueMap-Minecraft:BlueMapAPI:2.6.1'
compileOnly 'us.dynmap:DynmapCoreAPI:3.4'
compileOnly 'maven.modrinth:pl3xmap:1.20.2-472'
compileOnly 'maven.modrinth:pl3xmap:1.20.2-SNAPSHOT'
compileOnly 'com.github.plan-player-analytics:Plan:5.5.2150'

testImplementation 'com.github.plan-player-analytics:Plan:5.5.2150'
Expand Down

0 comments on commit 37245e2

Please sign in to comment.