Skip to content

Commit

Permalink
fix: check for enderchest blocks (#34)
Browse files Browse the repository at this point in the history
* fix: check for enderchest blocks

* refactor: inline if case

* fix: take top inventory when shifting items inside
  • Loading branch information
rexlManu authored Jan 1, 2024
1 parent 0c1edad commit 9f6104e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ public void logPlayerItemSent(Player player, ItemStack itemStack,
}
}

public void logInventoryCreation(Player player, InventoryType inventoryType,
public void logInventoryCreation(Player player, String inventoryType,
Location location) {
if (!inventoryCreationLogText.isEmpty()) {
this.getLogger()
.log(
Level.INFO,
() ->
inventoryCreationLogText
.replace("[inventory_type]", inventoryType.name())
.replace("[inventory_type]", inventoryType)
.replace(PLAYER_NAME_PLACEHOLDER, player.getName())
.replace("[x]", String.valueOf(location.getBlockX()))
.replace("[y]", String.valueOf(location.getBlockY()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.simplyvanilla.noillegals.check;

import net.simplyvanilla.noillegals.NoIllegalsPlugin;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.block.EnderChest;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
Expand All @@ -16,7 +19,7 @@ public InventoryCreationCheck(NoIllegalsPlugin plugin) {
@EventHandler(ignoreCancelled = true)
public void handleInventoryCreation(BlockPlaceEvent event) {
// block isn't a container, so we don't care
if (!(event.getBlock().getState() instanceof Container container)) {
if (!this.isContainer(event.getBlock())) {
return;
}

Expand All @@ -25,7 +28,30 @@ public void handleInventoryCreation(BlockPlaceEvent event) {
return;
}

this.plugin.logInventoryCreation(event.getPlayer(), container.getInventory().getType(),
String containerType = this.getContainerType(event.getBlock());

this.plugin.logInventoryCreation(event.getPlayer(), containerType,
event.getBlock().getLocation());
}

private String getContainerType(Block block) {
if (block.getState() instanceof EnderChest) {
return "ENDER_CHEST";
}

if (block.getState() instanceof Container container) {
return container.getInventory().getType().name();
}

// should not happen
return "unknown";
}

private boolean isContainer(Block block) {
if (block.getState() instanceof Container) {
return true;
}

return block.getType().equals(Material.ENDER_CHEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ private void handleItemManipulation(InventoryClickEvent event, Player player) {
this.plugin.logPlayerItemReceive(player, item);
} else {
this.plugin.logPlayerItemSent(player, item,
event.getClickedInventory()
.getType());
event.getView().getTopInventory().getType());
}
}
if ((event.getAction().equals(InventoryAction.HOTBAR_MOVE_AND_READD)
Expand All @@ -82,7 +81,7 @@ private void handleItemPickups(Player player, InventoryClickEvent event) {
item.setAmount(0);
return;
}

if (event.getView().getTopInventory().getType().equals(InventoryType.CRAFTING)) {
return;
}
Expand Down

0 comments on commit 9f6104e

Please sign in to comment.