Skip to content

Commit

Permalink
fix(): InventoryView ex and revert part of f51bf7e
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Aug 1, 2024
1 parent cf476ad commit a19da27
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.ExplosionResult;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.inventory.Inventory;

Expand All @@ -19,6 +20,7 @@ public class VersionedEvent {
private Constructor<BlockExplodeEvent> BLOCK_EXPLODE_EVENT_CONSTRUCTOR;

private Method GET_TOP_INVENTORY;
private Method GET_CLICKED_INVENTORY;

public void init() {
if (Slimefun.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_21)) {
Expand All @@ -29,6 +31,11 @@ public void init() {
GET_TOP_INVENTORY =
Class.forName("org.bukkit.inventory.InventoryView").getMethod("getTopInventory");
GET_TOP_INVENTORY.setAccessible(true);

GET_CLICKED_INVENTORY = Class.forName("org.bukkit.event.inventory.InventoryClickEvent")
.getMethod("getClickedInventory");
GET_CLICKED_INVENTORY.setAccessible(true);

} catch (NoSuchMethodException | ClassNotFoundException e) {
Slimefun.logger().log(Level.WARNING, "无法初始化事件版本兼容模块, 部分功能可能无法正常使用", e);
}
Expand Down Expand Up @@ -62,4 +69,17 @@ public Inventory getTopInventory(InventoryEvent event) {
return (Inventory) GET_TOP_INVENTORY.invoke(event.getView());
}
}

@SneakyThrows
public Inventory getClickedInventory(InventoryClickEvent event) {
if (Slimefun.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_21)) {
return event.getClickedInventory();
} else {
if (GET_CLICKED_INVENTORY == null) {
throw new IllegalStateException("Unable to get clicked inventory: missing method");
}

return (Inventory) GET_CLICKED_INVENTORY.invoke(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ public ToolUseHandler getItemHandler() {

List<Block> blocks = findBlocks(b);

if (!StorageCacheUtils.hasBlock(b.getLocation())) {
blocks.add(b);
}

breakBlocks(e, p, tool, b, blocks, drops);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public void onBlockBreak(BlockBreakEvent e) {
dropItems(e, drops);

// Checks for vanilla sensitive blocks everywhere
checkForSensitiveBlocks(e.getBlock(), 0, e.isDropItems());
// checkForSensitiveBlocks(e.getBlock(), 0, e.isDropItems());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public VillagerTradingListener(@Nonnull Slimefun plugin) {

@EventHandler(ignoreCancelled = true)
public void onPreTrade(InventoryClickEvent e) {
Inventory clickedInventory = e.getClickedInventory();
Inventory clickedInventory = VersionedEvent.getClickedInventory(e);
Inventory topInventory = VersionedEvent.getTopInventory(e);

if (clickedInventory != null && topInventory.getType() == InventoryType.MERCHANT) {
Expand Down

0 comments on commit a19da27

Please sign in to comment.