diff --git a/src/com/massivecraft/massivecore/MassiveCore.java b/src/com/massivecraft/massivecore/MassiveCore.java index 565657a5..a6d0d736 100644 --- a/src/com/massivecraft/massivecore/MassiveCore.java +++ b/src/com/massivecraft/massivecore/MassiveCore.java @@ -229,5 +229,5 @@ public class MassiveCore extends MassivePlugin MassiveCoreTaskDeleteFiles.get().run(); IdUtil.saveCachefileDatas(); } - + } diff --git a/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java b/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java index ac0ae260..f7fa4073 100644 --- a/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java +++ b/src/com/massivecraft/massivecore/adapter/ItemStackAdapterInnerV1_8.java @@ -16,7 +16,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.SkullMeta; import com.massivecraft.massivecore.Couple; -import com.massivecraft.massivecore.util.HeadUtil; +import com.massivecraft.massivecore.nms.NmsHead; import com.massivecraft.massivecore.xlib.gson.JsonArray; import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonObject; @@ -170,7 +170,7 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7 if ( ! meta.hasOwner()) return; // Resolve to avoid MStore sync bouncing. - Couple resolved = HeadUtil.resolve(meta); + Couple resolved = NmsHead.resolve(meta); String name = resolved.getFirst(); UUID id = resolved.getSecond(); @@ -190,13 +190,13 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7 if (element != null) id = UUID.fromString(element.getAsString()); // Resolve to avoid MStore sync bouncing. - Couple resolved = HeadUtil.resolve(name, id); + Couple resolved = NmsHead.resolve(name, id); name = resolved.getFirst(); id = resolved.getSecond(); if (name != null || id != null) { - HeadUtil.set(meta, name, id); + NmsHead.set(meta, name, id); } } } diff --git a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java index 7f58e62d..877151ab 100644 --- a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java +++ b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java @@ -1,18 +1,7 @@ package com.massivecraft.massivecore.cmd.massivecore; -import org.bukkit.Location; -import org.bukkit.entity.Player; - -import com.massivecraft.massivecore.MassiveCorePerm; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.cmd.MassiveCommand; -import com.massivecraft.massivecore.cmd.VisibilityMode; -import com.massivecraft.massivecore.cmd.arg.AREnum; -import com.massivecraft.massivecore.cmd.arg.ARFloat; -import com.massivecraft.massivecore.cmd.arg.ARInteger; -import com.massivecraft.massivecore.cmd.req.ReqHasPerm; -import com.massivecraft.massivecore.cmd.req.ReqIsPlayer; -import com.massivecraft.massivecore.particleeffect.ParticleEffect; public class CmdMassiveCoreTest extends MassiveCommand { @@ -25,8 +14,10 @@ public class CmdMassiveCoreTest extends MassiveCommand // Aliases this.addAliases("test"); + + // OLD STUFF // Arg - this.addArg(AREnum.get(ParticleEffect.class), "particleEffect").setDesc("the particle effect type to show"); + /*this.addArg(AREnum.get(ParticleEffect.class), "particleEffect").setDesc("the particle effect type to show"); this.addArg(ARFloat.get(), "offsetX").setDesc("the maximum offset on x-axis for this particle"); this.addArg(ARFloat.get(), "offsetY").setDesc("the maximum offset on y-axis for this particle"); this.addArg(ARFloat.get(), "offsetZ").setDesc("the maximum offset on z-axis for this particle"); @@ -38,7 +29,7 @@ public class CmdMassiveCoreTest extends MassiveCommand this.addRequirements(ReqIsPlayer.get()); // VisibilityMode - this.setVisibilityMode(VisibilityMode.SECRET); + this.setVisibilityMode(VisibilityMode.SECRET);*/ } // -------------------------------------------- // @@ -48,20 +39,23 @@ public class CmdMassiveCoreTest extends MassiveCommand @Override public void perform() throws MassiveException { - // Args + + sendRaw(mson("This is your ", mson("item").tooltip(me.getItemInHand()))); + // OLD STUFF + /*// Args ParticleEffect particleEffect = this.readArg(); Location center = me.getEyeLocation().add(0, 0, 0); - float offsetX = (Float) this.readArg(); - float offsetY = (Float) this.readArg(); - float offsetZ = (Float) this.readArg(); - float speed = (Float) this.readArg(); - int amount = (Integer) this.readArg(); + float offsetX = this.readArg(); + float offsetY = this.readArg(); + float offsetZ = this.readArg(); + float speed = this.readArg(); + int amount = this.readArg(); Player player = me; - particleEffect.display(center, offsetX, offsetY, offsetZ, speed, amount, player); + particleEffect.display(center, offsetX, offsetY, offsetZ, speed, amount, player);*/ } } diff --git a/src/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java b/src/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java index 879c2fb0..cac7d5ca 100644 --- a/src/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/InventoryMixinDefault.java @@ -1,13 +1,11 @@ package com.massivecraft.massivecore.mixin; -import java.lang.reflect.Constructor; - import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.PlayerInventory; -import com.massivecraft.massivecore.particleeffect.ReflectionUtils; +import com.massivecraft.massivecore.nms.NmsInventory; public class InventoryMixinDefault extends InventoryMixinAbstract { @@ -18,32 +16,6 @@ public class InventoryMixinDefault extends InventoryMixinAbstract private static InventoryMixinDefault i = new InventoryMixinDefault(); public static InventoryMixinDefault get() { return i; } - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - public static Class playerInventoryClass; - public static Class entityHumanClass; - public static Constructor playerInventoryConstructor; - public static Class craftInventoryPlayerClass; - public static Constructor craftInventoryPlayerConstructor; - - static - { - try - { - playerInventoryClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("PlayerInventory"); - entityHumanClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("EntityHuman"); - playerInventoryConstructor = ReflectionUtils.getConstructor(playerInventoryClass, entityHumanClass); - craftInventoryPlayerClass = ReflectionUtils.PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftInventoryPlayer"); - craftInventoryPlayerConstructor = ReflectionUtils.getConstructor(craftInventoryPlayerClass, playerInventoryClass); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // @@ -51,17 +23,7 @@ public class InventoryMixinDefault extends InventoryMixinAbstract @Override public PlayerInventory createPlayerInventory() { - try - { - Object playerInventory = playerInventoryConstructor.newInstance(new Object[]{null}); - Object craftInventoryPlayer = craftInventoryPlayerConstructor.newInstance(playerInventory); - return (PlayerInventory)craftInventoryPlayer; - } - catch (Exception e) - { - e.printStackTrace(); - return null; - } + return NmsInventory.createPlayerInventory(); } @Override diff --git a/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java b/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java index 89f6f05c..a3f22274 100644 --- a/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java @@ -7,8 +7,8 @@ import org.bukkit.entity.Player; import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.nms.NmsPacket; import com.massivecraft.massivecore.util.IdUtil; -import com.massivecraft.massivecore.util.PacketUtil; public class MessageMixinDefault extends MessageMixinAbstract { @@ -90,12 +90,12 @@ public class MessageMixinDefault extends MessageMixinAbstract CommandSender sender = IdUtil.getSender(sendeeObject); if (sender == null) return false; - if (sender instanceof Player && PacketUtil.isRawAvailable()) + if (sender instanceof Player && NmsPacket.get().isAvailable()) { Player player = (Player) sender; for (Mson mson : msons) { - PacketUtil.sendRaw(player, mson.toRaw()); + NmsPacket.sendRaw(player, mson.toRaw()); } } else diff --git a/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java b/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java index ba8c395c..02c6478c 100644 --- a/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java @@ -2,8 +2,8 @@ package com.massivecraft.massivecore.mixin; import org.bukkit.entity.Player; +import com.massivecraft.massivecore.nms.NmsPacket; import com.massivecraft.massivecore.util.IdUtil; -import com.massivecraft.massivecore.util.PacketUtil; public class TitleMixinDefault extends TitleMixinAbstract { @@ -29,13 +29,13 @@ public class TitleMixinDefault extends TitleMixinAbstract if (titleSub == null) titleSub = ""; if (titleMain == null) titleMain = ""; - return PacketUtil.sendTitle(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub); + return NmsPacket.sendTitle(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub); } @Override public boolean isTitlesAvailable() { - return PacketUtil.isTitleAvailable(); + return NmsPacket.get().isAvailable(); } } diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index b90968c3..dab024c7 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -11,6 +11,7 @@ import java.util.regex.Pattern; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import org.bukkit.inventory.ItemStack; import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.adapter.LowercaseEnumAdapter; @@ -186,6 +187,8 @@ public class Mson implements Serializable public Mson tooltipParse(String... tooltip) { this.hoverEvent(MsonEvent.hoverTextParse(text)); return this; } public Mson tooltipParse(Collection tooltip) { this.hoverEvent(MsonEvent.hoverTextParse(text)); return this; } + public Mson tooltip(ItemStack item) { this.hoverEvent(MsonEvent.item(item)); return this; } + // -------------------------------------------- // // CONVENIENCE STYLE // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/mson/MsonEvent.java b/src/com/massivecraft/massivecore/mson/MsonEvent.java index e8486a58..45b3d001 100644 --- a/src/com/massivecraft/massivecore/mson/MsonEvent.java +++ b/src/com/massivecraft/massivecore/mson/MsonEvent.java @@ -4,6 +4,9 @@ import java.io.Serializable; import java.util.Collection; import java.util.Objects; +import org.bukkit.inventory.ItemStack; + +import com.massivecraft.massivecore.nms.NmsItem; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; @@ -60,7 +63,7 @@ public class MsonEvent implements Serializable return MsonEvent.valueOf(MsonEventAction.RUN_COMMAND, cmd); } - // hoverEvents + // showText public static MsonEvent hoverText(String hoverText) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, hoverText); @@ -76,7 +79,7 @@ public class MsonEvent implements Serializable return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n")); } - // hoverEventsParsed + // showTextParsed public static MsonEvent hoverTextParse(String hoverText) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(hoverText)); @@ -91,6 +94,13 @@ public class MsonEvent implements Serializable { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n"))); } + + // showItem + public static MsonEvent item(ItemStack item) + { + if (item == null) throw new NullPointerException("item"); + return MsonEvent.valueOf(MsonEventAction.SHOW_ITEM, NmsItem.itemToString(item)); + } // -------------------------------------------- // // CONVENIENCE diff --git a/src/com/massivecraft/massivecore/mson/MsonEventAction.java b/src/com/massivecraft/massivecore/mson/MsonEventAction.java index c0bfcf39..cd2e242c 100644 --- a/src/com/massivecraft/massivecore/mson/MsonEventAction.java +++ b/src/com/massivecraft/massivecore/mson/MsonEventAction.java @@ -9,11 +9,12 @@ public enum MsonEventAction SUGGEST_COMMAND(), RUN_COMMAND(), OPEN_URL(), - SHOW_TEXT(true) - + SHOW_TEXT(true), + SHOW_ITEM(true), + // End of list ; - + // -------------------------------------------- // // FIELD // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/nms/NmsAbstract.java b/src/com/massivecraft/massivecore/nms/NmsAbstract.java new file mode 100644 index 00000000..37abca18 --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsAbstract.java @@ -0,0 +1,76 @@ +package com.massivecraft.massivecore.nms; + +import java.util.logging.Level; + +import com.massivecraft.massivecore.MassiveCore; +import com.massivecraft.massivecore.particleeffect.ParticleEffect.ParticlePacket; + +public abstract class NmsAbstract +{ + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final int DEFAULT_REQUIRED_VERSION = 7; + + public static final String BUG_TRACKER_URL = "https://github.com/MassiveCraft/MassiveCore/issues"; + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private boolean available = false; + public boolean isAvailable() { return this.available; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public NmsAbstract() + { + try + { + if (this.isAvailableForCurrentVersion()) + { + setup(); + available = true; + + /*String name = this.getClass().getSimpleName(); + name = name.substring(3); + name = name + "s"; + String msg = String.format("The NMS util for %s was enabled.", name); + MassiveCore.get().log(msg);*/ + } + } + catch (Throwable t) + { + String errorMsg = String.format("If you use 1.%s.X or above, please report this error at %s", this.getRequiredVersion(), BUG_TRACKER_URL); + MassiveCore.get().log(Level.INFO, errorMsg); + t.printStackTrace(); + + available = false; + } + } + + protected abstract void setup() throws Throwable; + + // -------------------------------------------- // + // VERSIONING + // -------------------------------------------- // + + public static int getCurrentVersion() + { + return ParticlePacket.getVersion(); + } + + public int getRequiredVersion() + { + return DEFAULT_REQUIRED_VERSION; + } + + public boolean isAvailableForCurrentVersion() + { + return getCurrentVersion() >= this.getRequiredVersion(); + } + +} diff --git a/src/com/massivecraft/massivecore/util/HeadUtil.java b/src/com/massivecraft/massivecore/nms/NmsHead.java similarity index 75% rename from src/com/massivecraft/massivecore/util/HeadUtil.java rename to src/com/massivecraft/massivecore/nms/NmsHead.java index dfbb145d..d4af82bc 100644 --- a/src/com/massivecraft/massivecore/util/HeadUtil.java +++ b/src/com/massivecraft/massivecore/nms/NmsHead.java @@ -1,4 +1,4 @@ -package com.massivecraft.massivecore.util; +package com.massivecraft.massivecore.nms; import java.lang.reflect.Field; import java.util.UUID; @@ -7,9 +7,20 @@ import org.bukkit.inventory.meta.SkullMeta; import com.massivecraft.massivecore.Couple; import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; +import com.massivecraft.massivecore.util.IdData; +import com.massivecraft.massivecore.util.IdUtil; +import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.ReflectionUtil; -public class HeadUtil +public class NmsHead extends NmsAbstract { + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsHead i = new NmsHead(); + public static NmsHead get () { return i; } + // -------------------------------------------- // // REFLECTION CACHE // -------------------------------------------- // @@ -21,23 +32,27 @@ public class HeadUtil public static Field fieldGameProfileDotId; public static Field fieldGameProfileDotName; - static + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public int getRequiredVersion() { - try - { - classCraftMetaSkull = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftMetaSkull"); - fieldCraftMetaSkullDotProfile = ReflectionUtil.getField(classCraftMetaSkull, "profile"); - - classGameProfile = Class.forName("com.mojang.authlib.GameProfile"); - fieldGameProfileDotId = ReflectionUtil.getField(classGameProfile, "id"); - fieldGameProfileDotName = ReflectionUtil.getField(classGameProfile, "name"); - } - catch (Exception e) - { - e.printStackTrace(); - } + return 8; } + @Override + protected void setup() throws Throwable + { + classCraftMetaSkull = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftMetaSkull"); + fieldCraftMetaSkullDotProfile = ReflectionUtil.getField(classCraftMetaSkull, "profile"); + + classGameProfile = Class.forName("com.mojang.authlib.GameProfile"); + fieldGameProfileDotId = ReflectionUtil.getField(classGameProfile, "id"); + fieldGameProfileDotName = ReflectionUtil.getField(classGameProfile, "name"); + } + // -------------------------------------------- // // GAMEPROFILE: SIMPLE // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/nms/NmsInventory.java b/src/com/massivecraft/massivecore/nms/NmsInventory.java new file mode 100644 index 00000000..16c3431f --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsInventory.java @@ -0,0 +1,63 @@ +package com.massivecraft.massivecore.nms; + +import java.lang.reflect.Constructor; + +import org.bukkit.inventory.PlayerInventory; + +import com.massivecraft.massivecore.particleeffect.ReflectionUtils; + +public class NmsInventory extends NmsAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsInventory i = new NmsInventory(); + public static NmsInventory get () { return i; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private static Class playerInventoryClass; + private static Class entityHumanClass; + private static Constructor playerInventoryConstructor; + private static Class craftInventoryPlayerClass; + private static Constructor craftInventoryPlayerConstructor; + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + protected void setup() throws Throwable + { + playerInventoryClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("PlayerInventory"); + entityHumanClass = ReflectionUtils.PackageType.MINECRAFT_SERVER.getClass("EntityHuman"); + playerInventoryConstructor = ReflectionUtils.getConstructor(playerInventoryClass, entityHumanClass); + craftInventoryPlayerClass = ReflectionUtils.PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftInventoryPlayer"); + craftInventoryPlayerConstructor = ReflectionUtils.getConstructor(craftInventoryPlayerClass, playerInventoryClass); + } + + // -------------------------------------------- // + // CREATE PLAYERINVENTORY + // -------------------------------------------- // + + public static PlayerInventory createPlayerInventory() + { + if ( ! get().isAvailable()) return null; + + try + { + Object playerInventory = playerInventoryConstructor.newInstance(new Object[]{null}); + Object craftInventoryPlayer = craftInventoryPlayerConstructor.newInstance(playerInventory); + return (PlayerInventory)craftInventoryPlayer; + } + catch (Exception e) + { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/com/massivecraft/massivecore/nms/NmsItem.java b/src/com/massivecraft/massivecore/nms/NmsItem.java new file mode 100644 index 00000000..f8731c99 --- /dev/null +++ b/src/com/massivecraft/massivecore/nms/NmsItem.java @@ -0,0 +1,73 @@ +package com.massivecraft.massivecore.nms; + +import java.lang.reflect.Method; + +import org.bukkit.inventory.ItemStack; + +import com.massivecraft.massivecore.particleeffect.ReflectionUtils; +import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; + +public class NmsItem extends NmsAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsItem i = new NmsItem(); + public static NmsItem get () { return i; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + // Handling tooltips + private static Method toNms; + private static Method toJson; + private static Class cbItem; + private static Class nmsItem; + private static Class nbtTag; + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public int getRequiredVersion() + { + return 8; + } + + @Override + protected void setup() throws Throwable + { + cbItem = PackageType.CRAFTBUKKIT_INVENTORY.getClass("CraftItemStack"); + nmsItem = PackageType.MINECRAFT_SERVER.getClass("ItemStack"); + nbtTag = PackageType.MINECRAFT_SERVER.getClass("NBTTagCompound"); + + toNms = ReflectionUtils.getMethod(cbItem, "asNMSCopy", ItemStack.class); + toJson = ReflectionUtils.getMethod(nmsItem, "save", nbtTag); + + // Set accessible + toNms.setAccessible(true); + toJson.setAccessible(true); + } + + + public static String itemToString(ItemStack item) + { + if ( ! get().isAvailable()) return null; + try + { + Object nmsItem = toNms.invoke(null, item); + if (nmsItem == null) throw new RuntimeException(); + String str = toJson.invoke(nmsItem, nbtTag.newInstance()).toString(); + return str; + } + catch (Exception ex) + { + ex.printStackTrace(); + return null; + } + } + +} diff --git a/src/com/massivecraft/massivecore/util/PacketUtil.java b/src/com/massivecraft/massivecore/nms/NmsPacket.java similarity index 65% rename from src/com/massivecraft/massivecore/util/PacketUtil.java rename to src/com/massivecraft/massivecore/nms/NmsPacket.java index 488ff7fe..391ebcd2 100644 --- a/src/com/massivecraft/massivecore/util/PacketUtil.java +++ b/src/com/massivecraft/massivecore/nms/NmsPacket.java @@ -1,9 +1,8 @@ -package com.massivecraft.massivecore.util; +package com.massivecraft.massivecore.nms; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.logging.Level; import org.bukkit.entity.Player; import org.json.simple.JSONObject; @@ -12,16 +11,19 @@ import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.particleeffect.ReflectionUtils; import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; -public final class PacketUtil +public final class NmsPacket extends NmsAbstract { + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static NmsPacket i = new NmsPacket(); + public static NmsPacket get () { return i; } + // -------------------------------------------- // // FIELDS // -------------------------------------------- // - - // Is using - private static boolean useTitles = false; - private static boolean useRaw = false; - + // The enums used to tell which packet it is. // They correspond to the commands with the same name. private static Class titleEnumClass; @@ -47,64 +49,51 @@ public final class PacketUtil private static Constructor chatPacketConstructor; // -------------------------------------------- // - // SETUP + // OVERRIDE // -------------------------------------------- // - - static + + @Override + public int getRequiredVersion() { - try + return 8; + } + + @Override + protected void setup() throws Throwable + { + // The enum used for titles + titleEnumClass = getTitleEnumClass(); + + // Get the title enum values. + for (Object o : titleEnumClass.getEnumConstants()) { - // The enum used for titles - titleEnumClass = getTitleEnumClass(); - - // Get the title enum values. - for (Object o : titleEnumClass.getEnumConstants()) - { - Enum e = (Enum) o; - if (e.name().equalsIgnoreCase("TITLE")) titleMainEnum = e; - else if (e.name().equalsIgnoreCase("SUBTITLE")) titleSubEnum = e; - else if (e.name().equalsIgnoreCase("TIMES")) titleTimesEnum = e; - } - - // Get chatserializer and chat component. - iChatBaseComponent = PackageType.MINECRAFT_SERVER.getClass("IChatBaseComponent"); - - chatSerializer = getChatSerializer(); - - // Get title packet and it's constructor - Class titlePacketClass = PackageType.MINECRAFT_SERVER.getClass("PacketPlayOutTitle"); - titlePacketConstructor = ReflectionUtils.getConstructor(titlePacketClass, titleEnumClass, iChatBaseComponent); - titlePacketConstructorTimes = ReflectionUtils.getConstructor(titlePacketClass, titleEnumClass, iChatBaseComponent, Integer.class, Integer.class, Integer.class); - - // Get Chat packet and it's constructor - Class chatPacketClass = PackageType.MINECRAFT_SERVER.getClass("PacketPlayOutChat"); - chatPacketConstructor = ReflectionUtils.getConstructor(chatPacketClass, iChatBaseComponent); - - // Player connection - getHandle = ReflectionUtils.getMethod("CraftPlayer", PackageType.CRAFTBUKKIT_ENTITY, "getHandle"); - playerConnection = ReflectionUtils.getField("EntityPlayer", PackageType.MINECRAFT_SERVER, false, "playerConnection"); - sendPacket = ReflectionUtils.getMethod(playerConnection.getType(), "sendPacket", PackageType.MINECRAFT_SERVER.getClass("Packet")); - - // Set accessible - setAllAccessible(); - - // This succeeded, we use titles and the chat. - useTitles = true; - useRaw = true; - } - catch (Exception ex) - { - MassiveCore.get().log(Level.INFO, "If you use 1.7.X or below, disregard this error"); - MassiveCore.get().log(Level.INFO, "If you use 1.8.X or above, please report at https://github.com/MassiveCraft/MassiveCore/issues"); - ex.printStackTrace(); - MassiveCore.get().log(Level.INFO, "If you use 1.7.X or below, disregard this error"); - MassiveCore.get().log(Level.INFO, "If you use 1.8.X or above, please report at https://github.com/MassiveCraft/MassiveCore/issues"); - - // It didn't succeed, we will not use titles. - useTitles = false; - useRaw = false; + Enum e = (Enum) o; + if (e.name().equalsIgnoreCase("TITLE")) titleMainEnum = e; + else if (e.name().equalsIgnoreCase("SUBTITLE")) titleSubEnum = e; + else if (e.name().equalsIgnoreCase("TIMES")) titleTimesEnum = e; } + // Get chatserializer and chat component. + iChatBaseComponent = PackageType.MINECRAFT_SERVER.getClass("IChatBaseComponent"); + + chatSerializer = getChatSerializer(); + + // Get title packet and it's constructor + Class titlePacketClass = PackageType.MINECRAFT_SERVER.getClass("PacketPlayOutTitle"); + titlePacketConstructor = ReflectionUtils.getConstructor(titlePacketClass, titleEnumClass, iChatBaseComponent); + titlePacketConstructorTimes = ReflectionUtils.getConstructor(titlePacketClass, titleEnumClass, iChatBaseComponent, Integer.class, Integer.class, Integer.class); + + // Get Chat packet and it's constructor + Class chatPacketClass = PackageType.MINECRAFT_SERVER.getClass("PacketPlayOutChat"); + chatPacketConstructor = ReflectionUtils.getConstructor(chatPacketClass, iChatBaseComponent); + + // Player connection + getHandle = ReflectionUtils.getMethod("CraftPlayer", PackageType.CRAFTBUKKIT_ENTITY, "getHandle"); + playerConnection = ReflectionUtils.getField("EntityPlayer", PackageType.MINECRAFT_SERVER, false, "playerConnection"); + sendPacket = ReflectionUtils.getMethod(playerConnection.getType(), "sendPacket", PackageType.MINECRAFT_SERVER.getClass("Packet")); + + // Set accessible + setAllAccessible(); } public static Class getTitleEnumClass() throws ClassNotFoundException @@ -150,27 +139,13 @@ public final class PacketUtil sendPacket.setAccessible(true); } - // -------------------------------------------- // - // AVAILABLE - // -------------------------------------------- // - - public static boolean isTitleAvailable() - { - return useTitles; - } - - public static boolean isRawAvailable() - { - return useRaw; - } - // -------------------------------------------- // // SEND TITLES // -------------------------------------------- // public static boolean sendTitle(Player player, int ticksIn, int ticksStay, int ticksOut, String titleMain, String titleSub) { - if ( ! useTitles) return false; + if ( ! get().isAvailable()) return false; try { @@ -217,7 +192,7 @@ public final class PacketUtil public static boolean sendRaw(Player player, String string) { - if ( ! useRaw) return false; + if ( ! get().isAvailable()) return false; try {