More Container fixes

This commit is contained in:
Daniel Saukel 2020-03-18 16:13:29 +01:00
parent ccf185e105
commit 293dc75f0e
5 changed files with 76 additions and 54 deletions

View File

@ -17,11 +17,11 @@
package de.erethon.dungeonsxl.reward;
import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.compatibility.Version;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.player.GlobalPlayer;
import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.util.ContainerAdapter;
import de.erethon.dungeonsxl.world.DGameWorld;
import de.erethon.dungeonsxl.world.block.RewardChest;
import de.erethon.vignette.api.PaginatedInventoryGUI;
@ -30,16 +30,12 @@ import de.erethon.vignette.api.layout.PaginatedFlowInventoryLayout;
import de.erethon.vignette.api.layout.PaginatedInventoryLayout.PaginationButtonPosition;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
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.ItemStack;
@ -93,12 +89,12 @@ public class RewardListener implements Listener {
return;
}
if (!(isValidContainer(inventory.getTopInventory()))) {
if (!(ContainerAdapter.isValidContainer(inventory.getTopInventory()))) {
return;
}
for (RewardChest rewardChest : gameWorld.getRewardChests()) {
if (!rewardChest.getBlock().equals(getHolderBlock(inventory.getTopInventory().getHolder()))) {
if (!rewardChest.getBlock().equals(ContainerAdapter.getHolderBlock(inventory.getTopInventory().getHolder()))) {
continue;
}
@ -143,20 +139,4 @@ 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();
}
}
}

View File

@ -20,8 +20,8 @@ import de.erethon.caliburn.loottable.LootTable;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.sign.Passive;
import de.erethon.dungeonsxl.api.world.InstanceWorld;
import de.erethon.dungeonsxl.util.ContainerAdapter;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.inventory.ItemStack;
@ -88,21 +88,21 @@ public abstract class ChestSign extends Passive {
Block yRelative = sign.getRelative(0, i, 0);
Block zRelative = sign.getRelative(0, 0, i);
if (xRelative.getState() instanceof Container) {
if (ContainerAdapter.isValidContainer(xRelative)) {
if (chestContent == null) {
chestContent = ((Container) xRelative.getState()).getInventory().getContents();
chestContent = ContainerAdapter.getBlockInventory(xRelative).getContents();
}
chest = xRelative;
} else if (yRelative.getState() instanceof Container) {
} else if (ContainerAdapter.isValidContainer(yRelative)) {
if (chestContent == null) {
chestContent = ((Container) yRelative.getState()).getInventory().getContents();
chestContent = ContainerAdapter.getBlockInventory(yRelative).getContents();
}
chest = yRelative;
} else if (zRelative.getState() instanceof Container) {
} else if (ContainerAdapter.isValidContainer(zRelative)) {
if (chestContent == null) {
chestContent = ((Container) zRelative.getState()).getInventory().getContents();
chestContent = ContainerAdapter.getBlockInventory(zRelative).getContents();
}
chest = zRelative;
}

View File

@ -20,9 +20,9 @@ import de.erethon.caliburn.item.VanillaItem;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.world.InstanceWorld;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.util.ContainerAdapter;
import java.util.Arrays;
import java.util.List;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.inventory.ItemStack;
@ -79,7 +79,7 @@ public class DungeonChestSign extends ChestSign {
}
chestContent = Arrays.copyOfRange(list.toArray(new ItemStack[list.size()]), 0, 26);
((Container) chest.getState()).getInventory().setContents(chestContent);
ContainerAdapter.getBlockInventory(chest).setContents(chestContent);
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2012-2020 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.util;
import de.erethon.commons.compatibility.Version;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
/**
* @author Daniel Saukel
*/
public class ContainerAdapter {
public static boolean isValidContainer(Block block) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return block.getState() instanceof Container;
} else {
return block.getState() instanceof Chest;
}
}
public static boolean isValidContainer(Inventory inventory) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return inventory.getHolder() instanceof Container;
} else {
return inventory.getHolder() instanceof Chest;
}
}
public static Block getHolderBlock(InventoryHolder holder) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return ((Container) holder).getBlock();
} else {
return ((Chest) holder).getBlock();
}
}
public static Inventory getBlockInventory(Block block) {
if (Version.isAtLeast(Version.MC1_12_1)) {
return ((Container) block.getState()).getInventory();
} else {
return ((Chest) block.getState()).getInventory();
}
}
}

View File

@ -32,7 +32,6 @@ import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
@ -45,7 +44,6 @@ public class RewardChest extends GameBlock {
private Economy econ;
private boolean used = false;
private Container container;
private double moneyReward;
private int levelReward;
private ItemStack[] itemReward;
@ -54,11 +52,6 @@ public class RewardChest extends GameBlock {
super(plugin, container);
econ = plugin.getEconomyProvider();
if (!(container.getState() instanceof Container)) {
return;
}
this.container = (Container) container.getState();
this.moneyReward = moneyReward;
this.levelReward = levelReward;
this.itemReward = itemReward;
@ -78,20 +71,6 @@ public class RewardChest extends GameBlock {
this.used = used;
}
/**
* @return the chest
*/
public Container getContainer() {
return container;
}
/**
* @param container the container to set
*/
public void setContainer(Container container) {
this.container = container;
}
/**
* @return the moneyReward
*/
@ -135,7 +114,7 @@ public class RewardChest extends GameBlock {
return;
}
if (container.getLocation().distance(container.getLocation()) < 1) {
if (block.getLocation().distance(block.getLocation()) < 1) {
addTreasure(api.getPlayerGroup(opener));
used = true;
}