-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
585e36f
commit eb9aa63
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
bukkit-utils/src/main/java/net/anweisen/utilities/bukkit/utils/misc/CompatibilityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |