Anvil GUI will no longer ruin your life (Blame Eso)

This commit is contained in:
Brianna 2019-05-22 04:43:20 -04:00
parent 54fa20cdb5
commit d4c5f9f2be
5 changed files with 57 additions and 74 deletions

View File

@ -4,7 +4,7 @@ stages:
variables:
name: "UltimateModeration"
path: "/builds/$CI_PROJECT_PATH"
version: "1.1.2"
version: "1.1.3"
build:
stage: build

View File

@ -1,10 +1,9 @@
package com.songoda.ultimatemoderation.utils.gui;
import com.songoda.ultimatemoderation.UltimateModeration;
import com.songoda.ultimatemoderation.utils.ServerVersion;
import com.songoda.ultimatemoderation.utils.version.NMSUtil;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -24,7 +23,6 @@ import java.util.HashMap;
import java.util.Map;
public class AbstractAnvilGUI {
private static Class<?> BlockPositionClass;
private static Class<?> PacketPlayOutOpenWindowClass;
private static Class<?> IChatBaseComponentClass;
@ -37,6 +35,7 @@ public class AbstractAnvilGUI {
private static Class<?> WorldClass;
private static Class<?> PlayerInventoryClass;
private static Class<?> ContainersClass;
private static Class<?> CraftPlayerClass;
private Player player;
private Map<AnvilSlot, ItemStack> items = new HashMap<>();
@ -44,8 +43,6 @@ public class AbstractAnvilGUI {
private Inventory inv;
private Listener listener;
private Sound closeSound = Sound.ENTITY_PLAYER_LEVELUP;
static {
BlockPositionClass = NMSUtil.getNMSClass("BlockPosition");
PacketPlayOutOpenWindowClass = NMSUtil.getNMSClass("PacketPlayOutOpenWindow");
@ -57,6 +54,7 @@ public class AbstractAnvilGUI {
ContainerClass = NMSUtil.getNMSClass("Container");
WorldClass = NMSUtil.getNMSClass("World");
PlayerInventoryClass = NMSUtil.getNMSClass("PlayerInventory");
CraftPlayerClass = NMSUtil.getCraftClass("entity.CraftPlayer");
if (NMSUtil.getVersionNumber() > 13) {
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.listener = new Listener() {
@EventHandler(priority = EventPriority.LOWEST)
@EventHandler(priority = EventPriority.HIGHEST)
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)) {
event.setCancelled(true);
ItemStack item = event.getCurrentItem();
int slot = event.getRawSlot();
ItemStack item = event.getCurrentItem();
int slot = event.getRawSlot();
String name = "";
if (item == null || item.getType().equals(Material.AIR) || slot != 2)
return;
if (item != null) {
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
String name = "";
if (meta != null && meta.hasDisplayName()) {
name = meta.getDisplayName();
}
}
}
ItemMeta meta = item.getItemMeta();
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()) {
event.getWhoClicked().closeInventory();
}
if (clickEvent.getWillDestroy()) {
destroy();
}
}
if (clickEvent.getWillDestroy())
AbstractAnvilGUI.this.destroy();
}
}
@EventHandler(priority = EventPriority.LOWEST)
@EventHandler(priority = EventPriority.HIGHEST)
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();
player.setLevel(player.getLevel() - 1);
if (inv.equals(inv)) {
inv.clear();
if (UltimateModeration.getInstance().isServerVersionAtLeast(ServerVersion.V1_9))
player.playSound(player.getLocation(), closeSound, 1F, 1F);
Bukkit.getScheduler().scheduleSyncDelayedTask(UltimateModeration.getInstance(), () -> {
if (onClose != null) onClose.OnClose(player, inv);
destroy();
}, 1L);
}
inv.clear();
Bukkit.getScheduler().scheduleSyncDelayedTask(instance, () -> {
if (AbstractAnvilGUI.this.onClose != null)
AbstractAnvilGUI.this.onClose.onClose(player, inv);
AbstractAnvilGUI.this.destroy();
}, 1L);
}
}
@EventHandler(priority = EventPriority.LOWEST)
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerQuitEvent event) {
if (event.getPlayer().equals(getPlayer())) {
if (event.getPlayer().equals(AbstractAnvilGUI.this.player)) {
player.setLevel(player.getLevel() - 1);
destroy();
AbstractAnvilGUI.this.destroy();
}
}
};
Bukkit.getPluginManager().registerEvents(listener, UltimateModeration.getInstance());
Bukkit.getPluginManager().registerEvents(this.listener, instance);
}
public Player getPlayer() {
return player;
return this.player;
}
public void setSlot(AnvilSlot slot, ItemStack item) {
items.put(slot, item);
this.items.put(slot, item);
}
public void open() {
player.setLevel(player.getLevel() + 1);
this.player.setLevel(this.player.getLevel() + 1);
try {
Object craftPlayer = NMSUtil.getCraftClass("entity.CraftPlayer").cast(player);
Method getHandleMethod = craftPlayer.getClass().getMethod("getHandle");
Object craftPlayer = CraftPlayerClass.cast(this.player);
Method getHandleMethod = CraftPlayerClass.getMethod("getHandle");
Object entityPlayer = getHandleMethod.invoke(craftPlayer);
Object playerInventory = NMSUtil.getFieldObject(entityPlayer, NMSUtil.getField(entityPlayer.getClass(), "inventory", 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");
Object bukkitView = getBukkitViewMethod.invoke(container);
Method getTopInventoryMethod = bukkitView.getClass().getMethod("getTopInventory");
inv = (Inventory) getTopInventoryMethod.invoke(bukkitView);
this.inv = (Inventory) getTopInventoryMethod.invoke(bukkitView);
for (AnvilSlot slot : items.keySet()) {
inv.setItem(slot.getSlot(), items.get(slot));
for (AnvilSlot slot : this.items.keySet()) {
this.inv.setItem(slot.getSlot(), this.items.get(slot));
}
Method nextContainerCounterMethod = entityPlayer.getClass().getMethod("nextContainerCounter");
@ -194,7 +181,7 @@ public class AbstractAnvilGUI {
.newInstance(c, "minecraft:anvil", inventoryTitle, 0);
}
NMSUtil.sendPacket(player, packet);
NMSUtil.sendPacket(this.player, packet);
Field activeContainerField = NMSUtil.getField(EntityHumanClass, "activeContainer", true);
@ -214,26 +201,22 @@ public class AbstractAnvilGUI {
}
public void destroy() {
player = null;
items = null;
this.player = null;
this.items = null;
HandlerList.unregisterAll(listener);
HandlerList.unregisterAll(this.listener);
listener = null;
this.listener = null;
}
private OnClose getOnClose() {
return onClose;
return this.onClose;
}
public void setOnClose(OnClose onClose) {
this.onClose = onClose;
}
public void setCloseSound(Sound sound) {
closeSound = sound;
}
public enum AnvilSlot {
INPUT_LEFT(0),
INPUT_RIGHT(1),
@ -256,7 +239,7 @@ public class AbstractAnvilGUI {
}
public int getSlot() {
return slot;
return this.slot;
}
}
@ -279,15 +262,15 @@ public class AbstractAnvilGUI {
}
public AnvilSlot getSlot() {
return slot;
return this.slot;
}
public String getName() {
return name;
return this.name;
}
public boolean getWillClose() {
return close;
return this.close;
}
public void setWillClose(boolean close) {
@ -295,7 +278,7 @@ public class AbstractAnvilGUI {
}
public boolean getWillDestroy() {
return destroy;
return this.destroy;
}
public void setWillDestroy(boolean destroy) {

View File

@ -98,7 +98,7 @@ public abstract class AbstractGUI implements Listener {
if (gui == null || gui.inventory == null) return;
for (OnClose onClose : gui.onCloses) {
onClose.OnClose((Player) event.getPlayer(), inventory);
onClose.onClose((Player) event.getPlayer(), inventory);
}
gui.destory();
}

View File

@ -5,6 +5,6 @@ import org.bukkit.inventory.Inventory;
public interface OnClose {
void OnClose(Player player, Inventory inventory);
void onClose(Player player, Inventory inventory);
}

View File

@ -33,7 +33,7 @@ public class NMSUtil {
}
}
public static Class<?> getCraftClass(String className) throws ClassNotFoundException {
public static Class<?> getCraftClass(String className) {
try {
String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
Class<?> clazz = Class.forName(fullName);