shulker
This commit is contained in:
@@ -2,6 +2,7 @@ package io.github.itzispyder.ogredupealias.events;
|
|||||||
|
|
||||||
import io.github.itzispyder.ogredupealias.data.PlacedStructures;
|
import io.github.itzispyder.ogredupealias.data.PlacedStructures;
|
||||||
import io.github.itzispyder.ogredupealias.plugin.InventoryPresets;
|
import io.github.itzispyder.ogredupealias.plugin.InventoryPresets;
|
||||||
|
import io.github.itzispyder.ogredupealias.utils.ShulkerUtils;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -15,7 +16,7 @@ public class InteractionListener implements Listener {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
public void onClick(PlayerInteractEvent e) {
|
public void onClick(PlayerInteractEvent e) {
|
||||||
final Player p = e.getPlayer();
|
final Player p = e.getPlayer();
|
||||||
final Action action = e.getAction();
|
final Action a = e.getAction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Block b = e.getClickedBlock();
|
final Block b = e.getClickedBlock();
|
||||||
@@ -26,6 +27,10 @@ public class InteractionListener implements Listener {
|
|||||||
p.openInventory(InventoryPresets.createCustomTable());
|
p.openInventory(InventoryPresets.createCustomTable());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
|
||||||
|
ShulkerUtils.onShulkerInteraction(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ignore) {}
|
catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package io.github.itzispyder.ogredupealias.events;
|
package io.github.itzispyder.ogredupealias.events;
|
||||||
|
|
||||||
import io.github.itzispyder.ogredupealias.plugin.custom.forging.CustomTable;
|
import io.github.itzispyder.ogredupealias.plugin.custom.forging.CustomTable;
|
||||||
|
import io.github.itzispyder.ogredupealias.utils.ShulkerUtils;
|
||||||
import io.github.itzispyder.ogredupealias.utils.Text;
|
import io.github.itzispyder.ogredupealias.utils.Text;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
@@ -21,6 +22,7 @@ public class InventoryActionListener implements Listener {
|
|||||||
if (inv.getType() == InventoryType.PLAYER) return;
|
if (inv.getType() == InventoryType.PLAYER) return;
|
||||||
|
|
||||||
if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryAction(e);
|
if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryAction(e);
|
||||||
|
else if (title.contains(Text.color("&7Viewing "))) ShulkerUtils.onInventoryClick(e);
|
||||||
}
|
}
|
||||||
catch (Exception ignore) {}
|
catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
@@ -31,6 +33,7 @@ public class InventoryActionListener implements Listener {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryClose(e);
|
if (title.equals(Text.color("&eForging Table"))) CustomTable.onInventoryClose(e);
|
||||||
|
else if (title.contains(Text.color("&7Viewing "))) ShulkerUtils.onInventoryClose(e);
|
||||||
}
|
}
|
||||||
catch (Exception ignore) {}
|
catch (Exception ignore) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package io.github.itzispyder.ogredupealias.utils;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.block.ShulkerBox;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||||
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BlockStateMeta;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public abstract class ShulkerUtils {
|
||||||
|
|
||||||
|
private static final Map<UUID,ItemStack> lastOpenedBox = new HashMap<>();
|
||||||
|
|
||||||
|
public static Inventory getOf(ShulkerBox box) {
|
||||||
|
final String display = box.getCustomName();
|
||||||
|
final Inventory inv = Bukkit.createInventory(null, box.getInventory().getSize(), Text.color("&7Viewing " + display));
|
||||||
|
|
||||||
|
inv.setContents(box.getInventory().getContents());
|
||||||
|
return inv;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onShulkerInteraction(PlayerInteractEvent e) {
|
||||||
|
final Player p = e.getPlayer();
|
||||||
|
final ItemStack item = e.getItem();
|
||||||
|
final Action a = e.getAction();
|
||||||
|
|
||||||
|
if (item == null || item.getType().isAir()) return;
|
||||||
|
if (a != Action.RIGHT_CLICK_BLOCK && a != Action.RIGHT_CLICK_AIR) return;
|
||||||
|
if (!item.getType().name().contains("SHULKER_BOX")) return;
|
||||||
|
|
||||||
|
final BlockStateMeta meta = (BlockStateMeta) item.getItemMeta();
|
||||||
|
final ShulkerBox box = (ShulkerBox) meta.getBlockState();
|
||||||
|
|
||||||
|
p.openInventory(getOf(box));
|
||||||
|
lastOpenedBox.put(p.getUniqueId(),item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onInventoryClick(InventoryClickEvent e) {
|
||||||
|
final Inventory inv = e.getClickedInventory();
|
||||||
|
final Player p = (Player) e.getWhoClicked();
|
||||||
|
|
||||||
|
final ItemStack item = lastOpenedBox.get(p.getUniqueId());
|
||||||
|
final BlockStateMeta meta = (BlockStateMeta) item.getItemMeta();
|
||||||
|
final ShulkerBox box = (ShulkerBox) meta.getBlockState();
|
||||||
|
|
||||||
|
box.getInventory().setContents(inv.getContents());
|
||||||
|
box.update();
|
||||||
|
meta.setBlockState(box);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void onInventoryClose(InventoryCloseEvent e) {
|
||||||
|
final Inventory inv = e.getInventory();
|
||||||
|
final Player p = (Player) e.getPlayer();
|
||||||
|
|
||||||
|
final ItemStack item = lastOpenedBox.get(p.getUniqueId());
|
||||||
|
final BlockStateMeta meta = (BlockStateMeta) item.getItemMeta();
|
||||||
|
final ShulkerBox box = (ShulkerBox) meta.getBlockState();
|
||||||
|
|
||||||
|
box.getInventory().setContents(inv.getContents());
|
||||||
|
box.update();
|
||||||
|
meta.setBlockState(box);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
lastOpenedBox.remove(p.getUniqueId());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user