Skip to content

Commit

Permalink
Fix legacy compatibility (Fixes EssentialsX#5851)
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed Jul 2, 2024
1 parent 9e57dd6 commit fcf6e64
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 10 deletions.
24 changes: 21 additions & 3 deletions Essentials/src/main/java/com/earth2me/essentials/Essentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1385,6 +1398,11 @@ public BannerDataProvider getBannerDataProvider() {
return bannerDataProvider;
}

@Override
public InventoryViewProvider getInventoryViewProvider() {
return inventoryViewProvider;
}

@Override
public CustomItemResolver getCustomItemResolver() {
return customItemResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HumanEntity> viewers = new ArrayList<>(user.getBase().getInventory().getViewers());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,5 +193,7 @@ public interface IEssentials extends Plugin {

BannerDataProvider getBannerDataProvider();

InventoryViewProvider getInventoryViewProvider();

PluginCommand getPluginCommand(String cmd);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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";
}
}

0 comments on commit fcf6e64

Please sign in to comment.