Anvil GUI will no longer ruin your life (Blame Eso)
This commit is contained in:
parent
54fa20cdb5
commit
d4c5f9f2be
|
@ -4,7 +4,7 @@ stages:
|
||||||
variables:
|
variables:
|
||||||
name: "UltimateModeration"
|
name: "UltimateModeration"
|
||||||
path: "/builds/$CI_PROJECT_PATH"
|
path: "/builds/$CI_PROJECT_PATH"
|
||||||
version: "1.1.2"
|
version: "1.1.3"
|
||||||
|
|
||||||
build:
|
build:
|
||||||
stage: build
|
stage: build
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
package com.songoda.ultimatemoderation.utils.gui;
|
package com.songoda.ultimatemoderation.utils.gui;
|
||||||
|
|
||||||
import com.songoda.ultimatemoderation.UltimateModeration;
|
import com.songoda.ultimatemoderation.UltimateModeration;
|
||||||
import com.songoda.ultimatemoderation.utils.ServerVersion;
|
|
||||||
import com.songoda.ultimatemoderation.utils.version.NMSUtil;
|
import com.songoda.ultimatemoderation.utils.version.NMSUtil;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
|
@ -24,7 +23,6 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class AbstractAnvilGUI {
|
public class AbstractAnvilGUI {
|
||||||
|
|
||||||
private static Class<?> BlockPositionClass;
|
private static Class<?> BlockPositionClass;
|
||||||
private static Class<?> PacketPlayOutOpenWindowClass;
|
private static Class<?> PacketPlayOutOpenWindowClass;
|
||||||
private static Class<?> IChatBaseComponentClass;
|
private static Class<?> IChatBaseComponentClass;
|
||||||
|
@ -37,6 +35,7 @@ public class AbstractAnvilGUI {
|
||||||
private static Class<?> WorldClass;
|
private static Class<?> WorldClass;
|
||||||
private static Class<?> PlayerInventoryClass;
|
private static Class<?> PlayerInventoryClass;
|
||||||
private static Class<?> ContainersClass;
|
private static Class<?> ContainersClass;
|
||||||
|
private static Class<?> CraftPlayerClass;
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
private Map<AnvilSlot, ItemStack> items = new HashMap<>();
|
private Map<AnvilSlot, ItemStack> items = new HashMap<>();
|
||||||
|
@ -44,8 +43,6 @@ public class AbstractAnvilGUI {
|
||||||
private Inventory inv;
|
private Inventory inv;
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
|
|
||||||
private Sound closeSound = Sound.ENTITY_PLAYER_LEVELUP;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
BlockPositionClass = NMSUtil.getNMSClass("BlockPosition");
|
BlockPositionClass = NMSUtil.getNMSClass("BlockPosition");
|
||||||
PacketPlayOutOpenWindowClass = NMSUtil.getNMSClass("PacketPlayOutOpenWindow");
|
PacketPlayOutOpenWindowClass = NMSUtil.getNMSClass("PacketPlayOutOpenWindow");
|
||||||
|
@ -57,6 +54,7 @@ public class AbstractAnvilGUI {
|
||||||
ContainerClass = NMSUtil.getNMSClass("Container");
|
ContainerClass = NMSUtil.getNMSClass("Container");
|
||||||
WorldClass = NMSUtil.getNMSClass("World");
|
WorldClass = NMSUtil.getNMSClass("World");
|
||||||
PlayerInventoryClass = NMSUtil.getNMSClass("PlayerInventory");
|
PlayerInventoryClass = NMSUtil.getNMSClass("PlayerInventory");
|
||||||
|
CraftPlayerClass = NMSUtil.getCraftClass("entity.CraftPlayer");
|
||||||
|
|
||||||
if (NMSUtil.getVersionNumber() > 13) {
|
if (NMSUtil.getVersionNumber() > 13) {
|
||||||
ContainerAccessClass = NMSUtil.getNMSClass("ContainerAccess");
|
ContainerAccessClass = NMSUtil.getNMSClass("ContainerAccess");
|
||||||
|
@ -64,90 +62,79 @@ public class AbstractAnvilGUI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractAnvilGUI(final Player player, final AnvilClickEventHandler handler) {
|
public AbstractAnvilGUI(Player player, AnvilClickEventHandler handler) {
|
||||||
|
UltimateModeration instance = UltimateModeration.getInstance();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
|
|
||||||
this.listener = new Listener() {
|
this.listener = new Listener() {
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
if (event.getWhoClicked() instanceof Player) {
|
if (event.getWhoClicked() instanceof Player && event.getInventory().equals(AbstractAnvilGUI.this.inv)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
if (event.getInventory().equals(inv)) {
|
ItemStack item = event.getCurrentItem();
|
||||||
event.setCancelled(true);
|
int slot = event.getRawSlot();
|
||||||
|
|
||||||
ItemStack item = event.getCurrentItem();
|
if (item == null || item.getType().equals(Material.AIR) || slot != 2)
|
||||||
int slot = event.getRawSlot();
|
return;
|
||||||
String name = "";
|
|
||||||
|
|
||||||
if (item != null) {
|
String name = "";
|
||||||
if (item.hasItemMeta()) {
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
|
|
||||||
if (meta != null && meta.hasDisplayName()) {
|
ItemMeta meta = item.getItemMeta();
|
||||||
name = meta.getDisplayName();
|
if (meta != null && meta.hasDisplayName())
|
||||||
}
|
name = meta.getDisplayName();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AnvilClickEvent clickEvent = new AnvilClickEvent(AnvilSlot.bySlot(slot), name);
|
AnvilClickEvent clickEvent = new AnvilClickEvent(AnvilSlot.bySlot(slot), name);
|
||||||
|
handler.onAnvilClick(clickEvent);
|
||||||
|
|
||||||
handler.onAnvilClick(clickEvent);
|
if (clickEvent.getWillClose())
|
||||||
|
event.getWhoClicked().closeInventory();
|
||||||
|
|
||||||
if (clickEvent.getWillClose()) {
|
if (clickEvent.getWillDestroy())
|
||||||
event.getWhoClicked().closeInventory();
|
AbstractAnvilGUI.this.destroy();
|
||||||
}
|
|
||||||
|
|
||||||
if (clickEvent.getWillDestroy()) {
|
|
||||||
destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onInventoryClose(InventoryCloseEvent event) {
|
public void onInventoryClose(InventoryCloseEvent event) {
|
||||||
if (event.getPlayer() instanceof Player) {
|
if (event.getPlayer() instanceof Player && AbstractAnvilGUI.this.inv.equals(event.getInventory())) {
|
||||||
Inventory inv = event.getInventory();
|
Inventory inv = event.getInventory();
|
||||||
player.setLevel(player.getLevel() - 1);
|
player.setLevel(player.getLevel() - 1);
|
||||||
if (inv.equals(inv)) {
|
inv.clear();
|
||||||
inv.clear();
|
Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> {
|
||||||
if (UltimateModeration.getInstance().isServerVersionAtLeast(ServerVersion.V1_9))
|
if (AbstractAnvilGUI.this.onClose != null)
|
||||||
player.playSound(player.getLocation(), closeSound, 1F, 1F);
|
AbstractAnvilGUI.this.onClose.onClose(player, inv);
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(UltimateModeration.getInstance(), () -> {
|
AbstractAnvilGUI.this.destroy();
|
||||||
if (onClose != null) onClose.OnClose(player, inv);
|
}, 1L);
|
||||||
destroy();
|
|
||||||
}, 1L);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||||
if (event.getPlayer().equals(getPlayer())) {
|
if (event.getPlayer().equals(AbstractAnvilGUI.this.player)) {
|
||||||
player.setLevel(player.getLevel() - 1);
|
player.setLevel(player.getLevel() - 1);
|
||||||
destroy();
|
AbstractAnvilGUI.this.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(listener, UltimateModeration.getInstance());
|
Bukkit.getPluginManager().registerEvents(this.listener, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayer() {
|
public Player getPlayer() {
|
||||||
return player;
|
return this.player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSlot(AnvilSlot slot, ItemStack item) {
|
public void setSlot(AnvilSlot slot, ItemStack item) {
|
||||||
items.put(slot, item);
|
this.items.put(slot, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void open() {
|
public void open() {
|
||||||
player.setLevel(player.getLevel() + 1);
|
this.player.setLevel(this.player.getLevel() + 1);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Object craftPlayer = NMSUtil.getCraftClass("entity.CraftPlayer").cast(player);
|
Object craftPlayer = CraftPlayerClass.cast(this.player);
|
||||||
Method getHandleMethod = craftPlayer.getClass().getMethod("getHandle");
|
Method getHandleMethod = CraftPlayerClass.getMethod("getHandle");
|
||||||
Object entityPlayer = getHandleMethod.invoke(craftPlayer);
|
Object entityPlayer = getHandleMethod.invoke(craftPlayer);
|
||||||
Object playerInventory = NMSUtil.getFieldObject(entityPlayer, NMSUtil.getField(entityPlayer.getClass(), "inventory", false));
|
Object playerInventory = NMSUtil.getFieldObject(entityPlayer, NMSUtil.getField(entityPlayer.getClass(), "inventory", false));
|
||||||
Object world = NMSUtil.getFieldObject(entityPlayer, NMSUtil.getField(entityPlayer.getClass(), "world", false));
|
Object world = NMSUtil.getFieldObject(entityPlayer, NMSUtil.getField(entityPlayer.getClass(), "world", false));
|
||||||
|
@ -170,10 +157,10 @@ public class AbstractAnvilGUI {
|
||||||
Method getBukkitViewMethod = container.getClass().getMethod("getBukkitView");
|
Method getBukkitViewMethod = container.getClass().getMethod("getBukkitView");
|
||||||
Object bukkitView = getBukkitViewMethod.invoke(container);
|
Object bukkitView = getBukkitViewMethod.invoke(container);
|
||||||
Method getTopInventoryMethod = bukkitView.getClass().getMethod("getTopInventory");
|
Method getTopInventoryMethod = bukkitView.getClass().getMethod("getTopInventory");
|
||||||
inv = (Inventory) getTopInventoryMethod.invoke(bukkitView);
|
this.inv = (Inventory) getTopInventoryMethod.invoke(bukkitView);
|
||||||
|
|
||||||
for (AnvilSlot slot : items.keySet()) {
|
for (AnvilSlot slot : this.items.keySet()) {
|
||||||
inv.setItem(slot.getSlot(), items.get(slot));
|
this.inv.setItem(slot.getSlot(), this.items.get(slot));
|
||||||
}
|
}
|
||||||
|
|
||||||
Method nextContainerCounterMethod = entityPlayer.getClass().getMethod("nextContainerCounter");
|
Method nextContainerCounterMethod = entityPlayer.getClass().getMethod("nextContainerCounter");
|
||||||
|
@ -194,7 +181,7 @@ public class AbstractAnvilGUI {
|
||||||
.newInstance(c, "minecraft:anvil", inventoryTitle, 0);
|
.newInstance(c, "minecraft:anvil", inventoryTitle, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
NMSUtil.sendPacket(player, packet);
|
NMSUtil.sendPacket(this.player, packet);
|
||||||
|
|
||||||
Field activeContainerField = NMSUtil.getField(EntityHumanClass, "activeContainer", true);
|
Field activeContainerField = NMSUtil.getField(EntityHumanClass, "activeContainer", true);
|
||||||
|
|
||||||
|
@ -214,26 +201,22 @@ public class AbstractAnvilGUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
player = null;
|
this.player = null;
|
||||||
items = null;
|
this.items = null;
|
||||||
|
|
||||||
HandlerList.unregisterAll(listener);
|
HandlerList.unregisterAll(this.listener);
|
||||||
|
|
||||||
listener = null;
|
this.listener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OnClose getOnClose() {
|
private OnClose getOnClose() {
|
||||||
return onClose;
|
return this.onClose;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOnClose(OnClose onClose) {
|
public void setOnClose(OnClose onClose) {
|
||||||
this.onClose = onClose;
|
this.onClose = onClose;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCloseSound(Sound sound) {
|
|
||||||
closeSound = sound;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum AnvilSlot {
|
public enum AnvilSlot {
|
||||||
INPUT_LEFT(0),
|
INPUT_LEFT(0),
|
||||||
INPUT_RIGHT(1),
|
INPUT_RIGHT(1),
|
||||||
|
@ -256,7 +239,7 @@ public class AbstractAnvilGUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSlot() {
|
public int getSlot() {
|
||||||
return slot;
|
return this.slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,15 +262,15 @@ public class AbstractAnvilGUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public AnvilSlot getSlot() {
|
public AnvilSlot getSlot() {
|
||||||
return slot;
|
return this.slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getWillClose() {
|
public boolean getWillClose() {
|
||||||
return close;
|
return this.close;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWillClose(boolean close) {
|
public void setWillClose(boolean close) {
|
||||||
|
@ -295,7 +278,7 @@ public class AbstractAnvilGUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getWillDestroy() {
|
public boolean getWillDestroy() {
|
||||||
return destroy;
|
return this.destroy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWillDestroy(boolean destroy) {
|
public void setWillDestroy(boolean destroy) {
|
||||||
|
|
|
@ -98,7 +98,7 @@ public abstract class AbstractGUI implements Listener {
|
||||||
if (gui == null || gui.inventory == null) return;
|
if (gui == null || gui.inventory == null) return;
|
||||||
|
|
||||||
for (OnClose onClose : gui.onCloses) {
|
for (OnClose onClose : gui.onCloses) {
|
||||||
onClose.OnClose((Player) event.getPlayer(), inventory);
|
onClose.onClose((Player) event.getPlayer(), inventory);
|
||||||
}
|
}
|
||||||
gui.destory();
|
gui.destory();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,6 @@ import org.bukkit.inventory.Inventory;
|
||||||
|
|
||||||
public interface OnClose {
|
public interface OnClose {
|
||||||
|
|
||||||
void OnClose(Player player, Inventory inventory);
|
void onClose(Player player, Inventory inventory);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class NMSUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getCraftClass(String className) throws ClassNotFoundException {
|
public static Class<?> getCraftClass(String className) {
|
||||||
try {
|
try {
|
||||||
String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
|
String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
|
||||||
Class<?> clazz = Class.forName(fullName);
|
Class<?> clazz = Class.forName(fullName);
|
||||||
|
|
Loading…
Reference in New Issue