Skip to content

Commit

Permalink
fix 1.21 InventoryView changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfachBeez committed Jul 8, 2024
1 parent 585e36f commit eb9aa63
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.anweisen.utilities.bukkit.utils.menu;

import net.anweisen.utilities.bukkit.utils.misc.CompatibilityUtils;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -26,7 +27,7 @@ public void onClick(@Nonnull InventoryClickEvent event) {
Inventory inventory = event.getClickedInventory();
if (inventory == null) return;

if (inventory == event.getView().getTopInventory()) {
if (inventory == CompatibilityUtils.getTopInventory(event)) {

if (inventory.getHolder() != MenuPosition.HOLDER) return; // No menu inventory

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.anweisen.utilities.bukkit.utils.misc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.Nonnull;
import net.anweisen.utilities.common.logging.ILogger;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
* In API version 1.20.6 and earlier, InventoryView is an abstract class
* In API version 1.21, InventoryView is an interface
*/
public class CompatibilityUtils {

protected static final ILogger logger = ILogger.forThisClass();
private static final Logger log = LoggerFactory.getLogger(CompatibilityUtils.class);

private CompatibilityUtils() {
}

public static Inventory getTopInventory(@Nonnull Player player) {
InventoryView view = player.getOpenInventory();

try {
Method getTopInventory = InventoryView.class.getMethod("getTopInventory");
return (Inventory) getTopInventory.invoke(view);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
logger.error("Failed to get top inventory", ex);
return null;
}
}

public static Inventory getTopInventory(@Nonnull InventoryClickEvent event) {
InventoryView view = event.getView();

try {
Method getTopInventory = InventoryView.class.getMethod("getTopInventory");
return (Inventory) getTopInventory.invoke(view);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
logger.error("Failed to get top inventory", ex);
return null;
}
}
}

0 comments on commit eb9aa63

Please sign in to comment.