diff --git a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java index 778855e0cc1..e2a2ff13ebd 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java @@ -64,6 +64,7 @@ import net.ess3.provider.ContainerProvider; import net.ess3.provider.DamageEventProvider; import net.ess3.provider.FormattedCommandAliasProvider; +import net.ess3.provider.InventoryViewProvider; import net.ess3.provider.ItemUnbreakableProvider; import net.ess3.provider.KnownCommandsProvider; import net.ess3.provider.MaterialTagProvider; @@ -80,6 +81,7 @@ import net.ess3.provider.SyncCommandsProvider; import net.ess3.provider.WorldInfoProvider; import net.ess3.provider.providers.BaseBannerDataProvider; +import net.ess3.provider.providers.BaseInventoryViewProvider; import net.ess3.provider.providers.BaseLoggerProvider; import net.ess3.provider.providers.BlockMetaSpawnerItemProvider; import net.ess3.provider.providers.BukkitMaterialTagProvider; @@ -88,6 +90,7 @@ import net.ess3.provider.providers.FlatSpawnEggProvider; import net.ess3.provider.providers.LegacyBannerDataProvider; import net.ess3.provider.providers.LegacyDamageEventProvider; +import net.ess3.provider.providers.LegacyInventoryViewProvider; import net.ess3.provider.providers.LegacyItemUnbreakableProvider; import net.ess3.provider.providers.LegacyPlayerLocaleProvider; import net.ess3.provider.providers.LegacyPotionMetaProvider; @@ -130,6 +133,7 @@ import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.inventory.InventoryView; import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -206,6 +210,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { private transient SignDataProvider signDataProvider; private transient DamageEventProvider damageEventProvider; private transient BiomeKeyProvider biomeKeyProvider; + private transient InventoryViewProvider inventoryViewProvider; private transient Kits kits; private transient RandomTeleport randomTeleport; private transient UpdateChecker updateChecker; @@ -504,6 +509,12 @@ public void onEnable() { damageEventProvider = new LegacyDamageEventProvider(); } + if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_21_R01)) { + inventoryViewProvider = new BaseInventoryViewProvider(); + } else { + inventoryViewProvider = new LegacyInventoryViewProvider(); + } + if (PaperLib.isPaper() && VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_19_4_R01)) { biomeKeyProvider = new PaperBiomeKeyProvider(); } @@ -928,12 +939,14 @@ private boolean isEssentialsPlugin(Plugin plugin) { public void cleanupOpenInventories() { for (final User user : getOnlineUsers()) { if (user.isRecipeSee()) { - user.getBase().getOpenInventory().getTopInventory().clear(); - user.getBase().getOpenInventory().close(); + final InventoryView view = user.getBase().getOpenInventory(); + + inventoryViewProvider.getTopInventory(view).clear(); + inventoryViewProvider.close(view); user.setRecipeSee(false); } if (user.isInvSee() || user.isEnderSee()) { - user.getBase().getOpenInventory().close(); + inventoryViewProvider.close(user.getBase().getOpenInventory()); user.setInvSee(false); user.setEnderSee(false); } @@ -1385,6 +1398,11 @@ public BannerDataProvider getBannerDataProvider() { return bannerDataProvider; } + @Override + public InventoryViewProvider getInventoryViewProvider() { + return inventoryViewProvider; + } + @Override public CustomItemResolver getCustomItemResolver() { return customItemResolver; diff --git a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java index 02be19c1a63..0b3ad90b861 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java @@ -299,7 +299,7 @@ public void onPlayerQuit(final PlayerQuitEvent event) { } user.setLogoutLocation(); if (user.isRecipeSee()) { - user.getBase().getOpenInventory().getTopInventory().clear(); + ess.getInventoryViewProvider().getTopInventory(user.getBase().getOpenInventory()).clear(); } final ArrayList viewers = new ArrayList<>(user.getBase().getInventory().getViewers()); @@ -897,14 +897,14 @@ public void run() { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onInventoryClickEvent(final InventoryClickEvent event) { Player refreshPlayer = null; - final Inventory top = event.getView().getTopInventory(); + final Inventory top = ess.getInventoryViewProvider().getTopInventory(event.getView()); final InventoryType type = top.getType(); final Inventory clickedInventory; if (event.getRawSlot() < 0) { clickedInventory = null; } else { - clickedInventory = event.getRawSlot() < top.getSize() ? top : event.getView().getBottomInventory(); + clickedInventory = event.getRawSlot() < top.getSize() ? top : ess.getInventoryViewProvider().getBottomInventory(event.getView()); } final User user = ess.getUser((Player) event.getWhoClicked()); @@ -963,7 +963,7 @@ private boolean isPreventBindingHat(User user, PlayerInventory inventory) { @EventHandler(priority = EventPriority.MONITOR) public void onInventoryCloseEvent(final InventoryCloseEvent event) { Player refreshPlayer = null; - final Inventory top = event.getView().getTopInventory(); + final Inventory top = ess.getInventoryViewProvider().getTopInventory(event.getView()); final InventoryType type = top.getType(); if (type == InventoryType.PLAYER) { final User user = ess.getUser((Player) event.getPlayer()); @@ -977,7 +977,7 @@ public void onInventoryCloseEvent(final InventoryCloseEvent event) { final User user = ess.getUser((Player) event.getPlayer()); if (user.isRecipeSee()) { user.setRecipeSee(false); - event.getView().getTopInventory().clear(); + ess.getInventoryViewProvider().getTopInventory(event.getView()).clear(); refreshPlayer = user.getBase(); } } else if (type == InventoryType.CHEST && top.getSize() == 9) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java index 5fbb1bf8777..598b1abe51e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java @@ -14,6 +14,7 @@ import net.ess3.provider.ContainerProvider; import net.ess3.provider.DamageEventProvider; import net.ess3.provider.FormattedCommandAliasProvider; +import net.ess3.provider.InventoryViewProvider; import net.ess3.provider.ItemUnbreakableProvider; import net.ess3.provider.KnownCommandsProvider; import net.ess3.provider.MaterialTagProvider; @@ -192,5 +193,7 @@ public interface IEssentials extends Plugin { BannerDataProvider getBannerDataProvider(); + InventoryViewProvider getInventoryViewProvider(); + PluginCommand getPluginCommand(String cmd); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrecipe.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrecipe.java index b08a673a024..448d2476ef9 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrecipe.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrecipe.java @@ -131,7 +131,7 @@ public void shapedRecipe(final CommandSource sender, final ShapedRecipe recipe, if (VersionUtil.PRE_FLATTENING && item.getDurability() == Short.MAX_VALUE) { item.setDurability((short) 0); } - view.getTopInventory().setItem(j * 3 + k + 1, item); + ess.getInventoryViewProvider().getTopInventory(view).setItem(j * 3 + k + 1, item); } } } else { @@ -174,7 +174,7 @@ public void shapelessRecipe(final CommandSource sender, final ShapelessRecipe re if (VersionUtil.PRE_FLATTENING && item.getDurability() == Short.MAX_VALUE) { item.setDurability((short) 0); } - view.setItem(i + 1, item); + ess.getInventoryViewProvider().setItem(view, i + 1, item); } } else { diff --git a/providers/1_12Provider/src/main/java/net/ess3/provider/providers/LegacyInventoryViewProvider.java b/providers/1_12Provider/src/main/java/net/ess3/provider/providers/LegacyInventoryViewProvider.java new file mode 100644 index 00000000000..1fd5ca68f35 --- /dev/null +++ b/providers/1_12Provider/src/main/java/net/ess3/provider/providers/LegacyInventoryViewProvider.java @@ -0,0 +1,33 @@ +package net.ess3.provider.providers; + +import net.ess3.provider.InventoryViewProvider; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; + +public class LegacyInventoryViewProvider implements InventoryViewProvider { + @Override + public Inventory getTopInventory(InventoryView view) { + return view.getTopInventory(); + } + + @Override + public Inventory getBottomInventory(InventoryView view) { + return view.getBottomInventory(); + } + + @Override + public void setItem(InventoryView view, int slot, ItemStack item) { + view.setItem(slot, item); + } + + @Override + public void close(InventoryView view) { + view.close(); + } + + @Override + public String getDescription() { + return "Legacy InventoryView Abstract Class ABI Provider"; + } +} diff --git a/providers/BaseProviders/src/main/java/net/ess3/provider/InventoryViewProvider.java b/providers/BaseProviders/src/main/java/net/ess3/provider/InventoryViewProvider.java new file mode 100644 index 00000000000..49ad346120f --- /dev/null +++ b/providers/BaseProviders/src/main/java/net/ess3/provider/InventoryViewProvider.java @@ -0,0 +1,19 @@ +package net.ess3.provider; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; + +/** + * Bukkit changed InventoryView to an interface in 1.21. We need to use providers + * to avoid breaking ABI compatibility with earlier versions of Bukkit. + */ +public interface InventoryViewProvider extends Provider { + Inventory getTopInventory(InventoryView view); + + void close(InventoryView view); + + Inventory getBottomInventory(InventoryView view); + + void setItem(InventoryView view, int slot, ItemStack item); +} diff --git a/providers/BaseProviders/src/main/java/net/ess3/provider/providers/BaseInventoryViewProvider.java b/providers/BaseProviders/src/main/java/net/ess3/provider/providers/BaseInventoryViewProvider.java new file mode 100644 index 00000000000..d6b8194e218 --- /dev/null +++ b/providers/BaseProviders/src/main/java/net/ess3/provider/providers/BaseInventoryViewProvider.java @@ -0,0 +1,33 @@ +package net.ess3.provider.providers; + +import net.ess3.provider.InventoryViewProvider; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; + +public class BaseInventoryViewProvider implements InventoryViewProvider { + @Override + public Inventory getTopInventory(InventoryView view) { + return view.getTopInventory(); + } + + @Override + public Inventory getBottomInventory(InventoryView view) { + return view.getBottomInventory(); + } + + @Override + public void setItem(InventoryView view, int slot, ItemStack item) { + view.setItem(slot, item); + } + + @Override + public void close(InventoryView view) { + view.close(); + } + + @Override + public String getDescription() { + return "1.21+ InventoryView Interface ABI Provider"; + } +}