Fix reward chest backwards compatibility

This commit is contained in:
Daniel Saukel 2020-03-18 01:15:32 +01:00
parent bd91373a03
commit 9a10bea264

View File

@ -17,6 +17,7 @@
package de.erethon.dungeonsxl.reward; package de.erethon.dungeonsxl.reward;
import de.erethon.caliburn.item.VanillaItem; import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.compatibility.Version;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.player.GlobalPlayer; import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.config.DMessage;
@ -29,6 +30,7 @@ import de.erethon.vignette.api.layout.PaginatedFlowInventoryLayout;
import de.erethon.vignette.api.layout.PaginatedInventoryLayout.PaginationButtonPosition; import de.erethon.vignette.api.layout.PaginatedInventoryLayout.PaginationButtonPosition;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container; import org.bukkit.block.Container;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -36,6 +38,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -89,14 +93,12 @@ public class RewardListener implements Listener {
return; return;
} }
if (!(inventory.getTopInventory().getHolder() instanceof Container)) { if (!(isValidContainer(inventory.getTopInventory()))) {
return; return;
} }
Container container = (Container) inventory.getTopInventory().getHolder();
for (RewardChest rewardChest : gameWorld.getRewardChests()) { for (RewardChest rewardChest : gameWorld.getRewardChests()) {
if (!rewardChest.getBlock().equals(container.getBlock())) { if (!rewardChest.getBlock().equals(getHolderBlock(inventory.getTopInventory().getHolder()))) {
continue; continue;
} }
@ -141,4 +143,20 @@ public class RewardListener implements Listener {
} }
} }
private static boolean isValidContainer(Inventory inventory) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return inventory.getHolder() instanceof Container;
} else {
return inventory.getHolder() instanceof Chest;
}
}
private static Block getHolderBlock(InventoryHolder holder) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return ((Container) holder).getBlock();
} else {
return ((Chest) holder).getBlock();
}
}
} }