Anvil gui no longer ruins your server.

This commit is contained in:
Brianna 2019-05-22 04:40:50 -04:00
parent 05bfc8acb9
commit c698a33ec0
5 changed files with 56 additions and 75 deletions

View File

@ -109,8 +109,6 @@ public class GUIGeneralOptions extends AbstractGUI {
gui.setSlot(AbstractAnvilGUI.AnvilSlot.INPUT_LEFT, item);
gui.open();
gui.setCloseSound(plugin.isServerVersionAtLeast(ServerVersion.V1_9) ? Sound.ENTITY_GENERIC_EXPLODE : Sound.valueOf("EXPLODE"));
}));
registerClickable(11, ((player1, inventory1, cursor, slot, type) -> {

View File

@ -1,10 +1,9 @@
package com.songoda.ultimatekits.utils.gui;
import com.songoda.ultimatekits.UltimateKits;
import com.songoda.ultimatekits.utils.ServerVersion;
import com.songoda.ultimatekits.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) {
UltimateKits instance = UltimateKits.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 (UltimateKits.getInstance().isServerVersionAtLeast(ServerVersion.V1_9))
player.playSound(player.getLocation(), closeSound, 1F, 1F);
Bukkit.getScheduler().scheduleSyncDelayedTask(UltimateKits.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, UltimateKits.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);