Nice work.

This commit is contained in:
Brianna 2019-05-22 02:14:24 -04:00
parent 164e4e995d
commit 9d9af620ef
7 changed files with 13 additions and 425 deletions

View File

@ -26,7 +26,6 @@ import com.songoda.epicanchors.utils.Methods;
import com.songoda.epicanchors.utils.ServerVersion;
import com.songoda.epicanchors.utils.SettingsManager;
import com.songoda.epicanchors.utils.updateModules.LocaleModule;
import com.songoda.epicanchors.utils.version.NMSUtil;
import com.songoda.update.Plugin;
import com.songoda.update.SongodaUpdate;
import org.apache.commons.lang.ArrayUtils;
@ -249,10 +248,10 @@ public class EpicAnchorsPlugin extends JavaPlugin implements EpicAnchors {
}
location.getBlock().setType(Material.AIR);
if (NMSUtil.getVersionNumber() > 8)
if (isServerVersionAtLeast(ServerVersion.V1_9))
location.getWorld().spawnParticle(Particle.LAVA, location.clone().add(.5, .5, .5), 5, 0, 0, 0, 5);
location.getWorld().playSound(location, this.isServerVersionAtLeast(ServerVersion.V1_13)
location.getWorld().playSound(location, this.isServerVersionAtLeast(ServerVersion.V1_9)
? Sound.ENTITY_GENERIC_EXPLODE : Sound.valueOf("EXPLODE"), 10, 10);
getAnchorManager().removeAnchor(location);

View File

@ -3,7 +3,7 @@ package com.songoda.epicanchors.anchor;
import com.songoda.epicanchors.EpicAnchorsPlugin;
import com.songoda.epicanchors.api.anchor.Anchor;
import com.songoda.epicanchors.gui.GUIOverview;
import com.songoda.epicanchors.utils.version.NMSUtil;
import com.songoda.epicanchors.utils.ServerVersion;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -57,10 +57,10 @@ public class EAnchor implements Anchor {
}
ticksLeft = ticksLeft + 20 * 60 * 30;
Sound sound = NMSUtil.getVersionNumber() > 8 ? Sound.ENTITY_PLAYER_LEVELUP : Sound.valueOf("LEVEL_UP");
Sound sound = EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_8) ? Sound.ENTITY_PLAYER_LEVELUP : Sound.valueOf("LEVEL_UP");
player.playSound(player.getLocation(), sound, 0.6F, 15.0F);
if (NMSUtil.getVersionNumber() > 8)
if (EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_8))
player.getWorld().spawnParticle(Particle.SPELL_WITCH, getLocation().add(.5, .5, .5), 100, .5, .5, .5);
}

View File

@ -5,7 +5,6 @@ import com.songoda.epicanchors.anchor.EAnchor;
import com.songoda.epicanchors.api.anchor.Anchor;
import com.songoda.epicanchors.utils.Methods;
import com.songoda.epicanchors.utils.ServerVersion;
import com.songoda.epicanchors.utils.version.NMSUtil;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Particle;
@ -64,10 +63,10 @@ public class InteractListeners implements Listener {
if (player.getGameMode() != GameMode.CREATIVE)
Methods.takeItem(player, 1);
Sound sound = NMSUtil.getVersionNumber() > 8 ? Sound.ENTITY_PLAYER_LEVELUP : Sound.valueOf("LEVEL_UP");
Sound sound = EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_8) ? Sound.ENTITY_PLAYER_LEVELUP : Sound.valueOf("LEVEL_UP");
player.playSound(player.getLocation(), sound, 0.6F, 15.0F);
if (NMSUtil.getVersionNumber() > 8)
if (EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_8))
player.getWorld().spawnParticle(Particle.SPELL_WITCH, anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5);
event.setCancelled(true);

View File

@ -1,7 +1,6 @@
package com.songoda.epicanchors.utils;
import com.songoda.epicanchors.EpicAnchorsPlugin;
import com.songoda.epicanchors.utils.version.NMSUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -176,14 +175,14 @@ public class SettingsManager implements Listener {
public enum Setting {
o1("Main.Name-Tag", "&eAnchor &8(&7{REMAINING}&8)"),
o2("Main.Anchor-Lore", "&7Place down to keep that chunk|&7loaded until the time runs out."),
o3("Main.Anchor Block Material", NMSUtil.getVersionNumber() > 12 ? "END_PORTAL_FRAME" : "ENDER_PORTAL_FRAME"),
o3("Main.Anchor Block Material", EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "END_PORTAL_FRAME" : "ENDER_PORTAL_FRAME"),
o4("Main.Add Time With Economy", true),
o5("Main.Economy Cost", 5000.0),
o6("Main.Add Time With XP", true),
o7("Main.XP Cost", 10),
o8("Main.Allow Anchor Breaking", false),
o9("Interfaces.Economy Icon", NMSUtil.getVersionNumber() > 12 ? "SUNFLOWER" : "GOLD_INGOT"),
o10("Interfaces.XP Icon", NMSUtil.getVersionNumber() > 12 ? "EXPERIENCE_BOTTLE" : "EXP_BOTTLE"),
o9("Interfaces.Economy Icon", EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "SUNFLOWER" : "GOLD_INGOT"),
o10("Interfaces.XP Icon", EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "EXPERIENCE_BOTTLE" : "EXP_BOTTLE"),
o11("Interfaces.Glass Type 1", 7),
o12("Interfaces.Glass Type 2", 11),
o13("Interfaces.Glass Type 3", 3),

View File

@ -1,310 +0,0 @@
package com.songoda.epicanchors.utils.gui;
import com.songoda.epicanchors.EpicAnchorsPlugin;
import com.songoda.epicanchors.utils.version.NMSUtil;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class AbstractAnvilGUI {
private static Class<?> BlockPositionClass;
private static Class<?> PacketPlayOutOpenWindowClass;
private static Class<?> IChatBaseComponentClass;
private static Class<?> ICraftingClass;
private static Class<?> ContainerAnvilClass;
private static Class<?> ChatMessageClass;
private static Class<?> EntityHumanClass;
private static Class<?> ContainerClass;
private static Class<?> ContainerAccessClass;
private static Class<?> WorldClass;
private static Class<?> PlayerInventoryClass;
private static Class<?> ContainersClass;
private Player player;
private Map<AnvilSlot, ItemStack> items = new HashMap<>();
private OnClose onClose = null;
private Inventory inv;
private Listener listener;
private Sound closeSound;
static {
BlockPositionClass = NMSUtil.getNMSClass("BlockPosition");
PacketPlayOutOpenWindowClass = NMSUtil.getNMSClass("PacketPlayOutOpenWindow");
IChatBaseComponentClass = NMSUtil.getNMSClass("IChatBaseComponent");
ICraftingClass = NMSUtil.getNMSClass("ICrafting");
ContainerAnvilClass = NMSUtil.getNMSClass("ContainerAnvil");
EntityHumanClass = NMSUtil.getNMSClass("EntityHuman");
ChatMessageClass = NMSUtil.getNMSClass("ChatMessage");
ContainerClass = NMSUtil.getNMSClass("Container");
WorldClass = NMSUtil.getNMSClass("World");
PlayerInventoryClass = NMSUtil.getNMSClass("PlayerInventory");
if (NMSUtil.getVersionNumber() > 13) {
ContainerAccessClass = NMSUtil.getNMSClass("ContainerAccess");
ContainersClass = NMSUtil.getNMSClass("Containers");
}
}
public AbstractAnvilGUI(final Player player, final AnvilClickEventHandler handler) {
this.player = player;
if (NMSUtil.getVersionNumber() > 8) {
this.closeSound = Sound.ENTITY_PLAYER_LEVELUP;
} else {
this.closeSound = Sound.valueOf("LEVEL_UP");
}
this.listener = new Listener() {
@EventHandler(priority = EventPriority.LOWEST)
public void onInventoryClick(InventoryClickEvent event) {
if (event.getWhoClicked() instanceof Player) {
if (event.getInventory().equals(inv)) {
event.setCancelled(true);
ItemStack item = event.getCurrentItem();
int slot = event.getRawSlot();
String name = "";
if (item != null) {
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
name = meta.getDisplayName();
}
}
}
AnvilClickEvent clickEvent = new AnvilClickEvent(AnvilSlot.bySlot(slot), name);
handler.onAnvilClick(clickEvent);
if (clickEvent.getWillClose()) {
event.getWhoClicked().closeInventory();
}
if (clickEvent.getWillDestroy()) {
destroy();
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInventoryClose(InventoryCloseEvent event) {
if (event.getPlayer() instanceof Player) {
Inventory inv = event.getInventory();
player.setLevel(player.getLevel() - 1);
if (inv.equals(inv)) {
inv.clear();
player.playSound(player.getLocation(), closeSound, 1F, 1F);
Bukkit.getScheduler().scheduleSyncDelayedTask(EpicAnchorsPlugin.getInstance(), () -> {
if (onClose != null) onClose.OnClose(player, inv);
destroy();
}, 1L);
}
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerQuit(PlayerQuitEvent event) {
if (event.getPlayer().equals(getPlayer())) {
player.setLevel(player.getLevel() - 1);
destroy();
}
}
};
Bukkit.getPluginManager().registerEvents(listener, EpicAnchorsPlugin.getInstance());
}
public Player getPlayer() {
return player;
}
public void setSlot(AnvilSlot slot, ItemStack item) {
items.put(slot, item);
}
public void open() {
player.setLevel(player.getLevel() + 1);
try {
Object craftPlayer = NMSUtil.getCraftClass("entity.CraftPlayer").cast(player);
Method getHandleMethod = craftPlayer.getClass().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));
Object blockPosition = BlockPositionClass.getConstructor(int.class, int.class, int.class).newInstance(0, 0, 0);
Object container;
if (NMSUtil.getVersionNumber() > 13) {
container = ContainerAnvilClass
.getConstructor(int.class, PlayerInventoryClass, ContainerAccessClass)
.newInstance(7, playerInventory, ContainerAccessClass.getMethod("at", WorldClass, BlockPositionClass).invoke(null, world, blockPosition));
} else {
container = ContainerAnvilClass
.getConstructor(PlayerInventoryClass, WorldClass, BlockPositionClass, EntityHumanClass)
.newInstance(playerInventory, world, blockPosition, entityPlayer);
}
NMSUtil.getField(ContainerClass, "checkReachable", true).set(container, false);
Method getBukkitViewMethod = container.getClass().getMethod("getBukkitView");
Object bukkitView = getBukkitViewMethod.invoke(container);
Method getTopInventoryMethod = bukkitView.getClass().getMethod("getTopInventory");
inv = (Inventory) getTopInventoryMethod.invoke(bukkitView);
for (AnvilSlot slot : items.keySet()) {
inv.setItem(slot.getSlot(), items.get(slot));
}
Method nextContainerCounterMethod = entityPlayer.getClass().getMethod("nextContainerCounter");
int c = (int) nextContainerCounterMethod.invoke(entityPlayer);
Constructor<?> chatMessageConstructor = ChatMessageClass.getConstructor(String.class, Object[].class);
Object inventoryTitle = chatMessageConstructor.newInstance("Repairing", new Object[]{});
Object packet;
if (NMSUtil.getVersionNumber() > 13) {
packet = PacketPlayOutOpenWindowClass
.getConstructor(int.class, ContainersClass, IChatBaseComponentClass)
.newInstance(c, ContainersClass.getField("ANVIL").get(null), inventoryTitle);
} else {
packet = PacketPlayOutOpenWindowClass
.getConstructor(int.class, String.class, IChatBaseComponentClass, int.class)
.newInstance(c, "minecraft:anvil", inventoryTitle, 0);
}
NMSUtil.sendPacket(player, packet);
Field activeContainerField = NMSUtil.getField(EntityHumanClass, "activeContainer", true);
if (activeContainerField != null) {
activeContainerField.set(entityPlayer, container);
NMSUtil.getField(ContainerClass, "windowId", true).set(activeContainerField.get(entityPlayer), c);
Method addSlotListenerMethod = activeContainerField.get(entityPlayer).getClass().getMethod("addSlotListener", ICraftingClass);
addSlotListenerMethod.invoke(activeContainerField.get(entityPlayer), entityPlayer);
if (NMSUtil.getVersionNumber() > 13) {
ContainerClass.getMethod("setTitle", IChatBaseComponentClass).invoke(container, inventoryTitle);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void destroy() {
player = null;
items = null;
HandlerList.unregisterAll(listener);
listener = null;
}
private OnClose getOnClose() {
return 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),
OUTPUT(2);
private int slot;
AnvilSlot(int slot) {
this.slot = slot;
}
public static AnvilSlot bySlot(int slot) {
for (AnvilSlot anvilSlot : values()) {
if (anvilSlot.getSlot() == slot) {
return anvilSlot;
}
}
return null;
}
public int getSlot() {
return slot;
}
}
@FunctionalInterface
public interface AnvilClickEventHandler {
void onAnvilClick(AnvilClickEvent event);
}
public class AnvilClickEvent {
private AnvilSlot slot;
private String name;
private boolean close = true;
private boolean destroy = true;
public AnvilClickEvent(AnvilSlot slot, String name) {
this.slot = slot;
this.name = name;
}
public AnvilSlot getSlot() {
return slot;
}
public String getName() {
return name;
}
public boolean getWillClose() {
return close;
}
public void setWillClose(boolean close) {
this.close = close;
}
public boolean getWillDestroy() {
return destroy;
}
public void setWillDestroy(boolean destroy) {
this.destroy = destroy;
}
}
}

View File

@ -1,6 +1,7 @@
package com.songoda.epicanchors.utils.gui;
import com.songoda.epicanchors.utils.version.NMSUtil;
import com.songoda.epicanchors.EpicAnchorsPlugin;
import com.songoda.epicanchors.utils.ServerVersion;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
@ -23,7 +24,7 @@ public class Range {
this.bottom = bottom;
if (onClickSound == null) {
if (NMSUtil.getVersionNumber() > 8) {
if (EpicAnchorsPlugin.getInstance().isServerVersionAtLeast(ServerVersion.V1_8)) {
this.onClickSound = Sound.UI_BUTTON_CLICK;
} else {
this.onClickSound = Sound.valueOf("CLICK");

View File

@ -1,100 +0,0 @@
package com.songoda.epicanchors.utils.version;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
public class NMSUtil {
public static String getVersion() {
String name = Bukkit.getServer().getClass().getPackage().getName();
return name.substring(name.lastIndexOf('.') + 1) + ".";
}
public static int getVersionNumber() {
String name = getVersion().substring(3);
return Integer.valueOf(name.substring(0, name.length() - 4));
}
public static int getVersionReleaseNumber() {
String NMSVersion = getVersion();
return Integer.valueOf(NMSVersion.substring(NMSVersion.length() - 2).replace(".", ""));
}
public static Class<?> getNMSClass(String className) {
try {
String fullName = "net.minecraft.server." + getVersion() + className;
Class<?> clazz = Class.forName(fullName);
return clazz;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Class<?> getCraftClass(String className) throws ClassNotFoundException {
try {
String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
Class<?> clazz = Class.forName(fullName);
return clazz;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Field getField(Class<?> clazz, String name, boolean declared) {
try {
Field field;
if (declared) {
field = clazz.getDeclaredField(name);
} else {
field = clazz.getField(name);
}
field.setAccessible(true);
return field;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Object getFieldObject(Object object, Field field) {
try {
return field.get(object);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void setField(Object object, String fieldName, Object fieldValue, boolean declared) {
try {
Field field;
if (declared) {
field = object.getClass().getDeclaredField(fieldName);
} else {
field = object.getClass().getField(fieldName);
}
field.setAccessible(true);
field.set(object, fieldValue);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void sendPacket(Player player, Object packet) {
try {
Object handle = player.getClass().getMethod("getHandle").invoke(player);
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}
}