Item tooltips

This commit is contained in:
Magnus Ulf 2015-06-05 10:20:02 +02:00 committed by Olof Larsson
parent 9c6bb882b0
commit cc6bf1bfbb
14 changed files with 341 additions and 169 deletions

View File

@ -229,5 +229,5 @@ public class MassiveCore extends MassivePlugin
MassiveCoreTaskDeleteFiles.get().run(); MassiveCoreTaskDeleteFiles.get().run();
IdUtil.saveCachefileDatas(); IdUtil.saveCachefileDatas();
} }
} }

View File

@ -16,7 +16,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import com.massivecraft.massivecore.Couple; 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.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
@ -170,7 +170,7 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7
if ( ! meta.hasOwner()) return; if ( ! meta.hasOwner()) return;
// Resolve to avoid MStore sync bouncing. // Resolve to avoid MStore sync bouncing.
Couple<String, UUID> resolved = HeadUtil.resolve(meta); Couple<String, UUID> resolved = NmsHead.resolve(meta);
String name = resolved.getFirst(); String name = resolved.getFirst();
UUID id = resolved.getSecond(); UUID id = resolved.getSecond();
@ -190,13 +190,13 @@ public class ItemStackAdapterInnerV1_8 extends ItemStackAdapterInnerV1_7
if (element != null) id = UUID.fromString(element.getAsString()); if (element != null) id = UUID.fromString(element.getAsString());
// Resolve to avoid MStore sync bouncing. // Resolve to avoid MStore sync bouncing.
Couple<String, UUID> resolved = HeadUtil.resolve(name, id); Couple<String, UUID> resolved = NmsHead.resolve(name, id);
name = resolved.getFirst(); name = resolved.getFirst();
id = resolved.getSecond(); id = resolved.getSecond();
if (name != null || id != null) if (name != null || id != null)
{ {
HeadUtil.set(meta, name, id); NmsHead.set(meta, name, id);
} }
} }
} }

View File

@ -1,18 +1,7 @@
package com.massivecraft.massivecore.cmd.massivecore; 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.MassiveException;
import com.massivecraft.massivecore.cmd.MassiveCommand; 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 public class CmdMassiveCoreTest extends MassiveCommand
{ {
@ -25,8 +14,10 @@ public class CmdMassiveCoreTest extends MassiveCommand
// Aliases // Aliases
this.addAliases("test"); this.addAliases("test");
// OLD STUFF
// Arg // 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(), "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(), "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"); 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()); this.addRequirements(ReqIsPlayer.get());
// VisibilityMode // VisibilityMode
this.setVisibilityMode(VisibilityMode.SECRET); this.setVisibilityMode(VisibilityMode.SECRET);*/
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -48,20 +39,23 @@ public class CmdMassiveCoreTest extends MassiveCommand
@Override @Override
public void perform() throws MassiveException public void perform() throws MassiveException
{ {
// Args
sendRaw(mson("This is your ", mson("item").tooltip(me.getItemInHand())));
// OLD STUFF
/*// Args
ParticleEffect particleEffect = this.readArg(); ParticleEffect particleEffect = this.readArg();
Location center = me.getEyeLocation().add(0, 0, 0); Location center = me.getEyeLocation().add(0, 0, 0);
float offsetX = (Float) this.readArg(); float offsetX = this.readArg();
float offsetY = (Float) this.readArg(); float offsetY = this.readArg();
float offsetZ = (Float) this.readArg(); float offsetZ = this.readArg();
float speed = (Float) this.readArg(); float speed = this.readArg();
int amount = (Integer) this.readArg(); int amount = this.readArg();
Player player = me; Player player = me;
particleEffect.display(center, offsetX, offsetY, offsetZ, speed, amount, player); particleEffect.display(center, offsetX, offsetY, offsetZ, speed, amount, player);*/
} }
} }

View File

@ -1,13 +1,11 @@
package com.massivecraft.massivecore.mixin; package com.massivecraft.massivecore.mixin;
import java.lang.reflect.Constructor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils; import com.massivecraft.massivecore.nms.NmsInventory;
public class InventoryMixinDefault extends InventoryMixinAbstract public class InventoryMixinDefault extends InventoryMixinAbstract
{ {
@ -18,32 +16,6 @@ public class InventoryMixinDefault extends InventoryMixinAbstract
private static InventoryMixinDefault i = new InventoryMixinDefault(); private static InventoryMixinDefault i = new InventoryMixinDefault();
public static InventoryMixinDefault get() { return i; } 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 // OVERRIDE
// -------------------------------------------- // // -------------------------------------------- //
@ -51,17 +23,7 @@ public class InventoryMixinDefault extends InventoryMixinAbstract
@Override @Override
public PlayerInventory createPlayerInventory() public PlayerInventory createPlayerInventory()
{ {
try return NmsInventory.createPlayerInventory();
{
Object playerInventory = playerInventoryConstructor.newInstance(new Object[]{null});
Object craftInventoryPlayer = craftInventoryPlayerConstructor.newInstance(playerInventory);
return (PlayerInventory)craftInventoryPlayer;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
} }
@Override @Override

View File

@ -7,8 +7,8 @@ import org.bukkit.entity.Player;
import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.nms.NmsPacket;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.PacketUtil;
public class MessageMixinDefault extends MessageMixinAbstract public class MessageMixinDefault extends MessageMixinAbstract
{ {
@ -90,12 +90,12 @@ public class MessageMixinDefault extends MessageMixinAbstract
CommandSender sender = IdUtil.getSender(sendeeObject); CommandSender sender = IdUtil.getSender(sendeeObject);
if (sender == null) return false; if (sender == null) return false;
if (sender instanceof Player && PacketUtil.isRawAvailable()) if (sender instanceof Player && NmsPacket.get().isAvailable())
{ {
Player player = (Player) sender; Player player = (Player) sender;
for (Mson mson : msons) for (Mson mson : msons)
{ {
PacketUtil.sendRaw(player, mson.toRaw()); NmsPacket.sendRaw(player, mson.toRaw());
} }
} }
else else

View File

@ -2,8 +2,8 @@ package com.massivecraft.massivecore.mixin;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.massivecore.nms.NmsPacket;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.PacketUtil;
public class TitleMixinDefault extends TitleMixinAbstract public class TitleMixinDefault extends TitleMixinAbstract
{ {
@ -29,13 +29,13 @@ public class TitleMixinDefault extends TitleMixinAbstract
if (titleSub == null) titleSub = ""; if (titleSub == null) titleSub = "";
if (titleMain == null) titleMain = ""; if (titleMain == null) titleMain = "";
return PacketUtil.sendTitle(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub); return NmsPacket.sendTitle(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub);
} }
@Override @Override
public boolean isTitlesAvailable() public boolean isTitlesAvailable()
{ {
return PacketUtil.isTitleAvailable(); return NmsPacket.get().isAvailable();
} }
} }

View File

@ -11,6 +11,7 @@ import java.util.regex.Pattern;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.adapter.LowercaseEnumAdapter; 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(String... tooltip) { this.hoverEvent(MsonEvent.hoverTextParse(text)); return this; }
public Mson tooltipParse(Collection<String> tooltip) { this.hoverEvent(MsonEvent.hoverTextParse(text)); return this; } public Mson tooltipParse(Collection<String> tooltip) { this.hoverEvent(MsonEvent.hoverTextParse(text)); return this; }
public Mson tooltip(ItemStack item) { this.hoverEvent(MsonEvent.item(item)); return this; }
// -------------------------------------------- // // -------------------------------------------- //
// CONVENIENCE STYLE // CONVENIENCE STYLE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -4,6 +4,9 @@ import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; 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.MUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
@ -60,7 +63,7 @@ public class MsonEvent implements Serializable
return MsonEvent.valueOf(MsonEventAction.RUN_COMMAND, cmd); return MsonEvent.valueOf(MsonEventAction.RUN_COMMAND, cmd);
} }
// hoverEvents // showText
public static MsonEvent hoverText(String hoverText) public static MsonEvent hoverText(String hoverText)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, 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")); return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n"));
} }
// hoverEventsParsed // showTextParsed
public static MsonEvent hoverTextParse(String hoverText) public static MsonEvent hoverTextParse(String hoverText)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(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"))); 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 // CONVENIENCE

View File

@ -9,11 +9,12 @@ public enum MsonEventAction
SUGGEST_COMMAND(), SUGGEST_COMMAND(),
RUN_COMMAND(), RUN_COMMAND(),
OPEN_URL(), OPEN_URL(),
SHOW_TEXT(true) SHOW_TEXT(true),
SHOW_ITEM(true),
// End of list // End of list
; ;
// -------------------------------------------- // // -------------------------------------------- //
// FIELD // FIELD
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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();
}
}

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.util; package com.massivecraft.massivecore.nms;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.UUID; import java.util.UUID;
@ -7,9 +7,20 @@ import org.bukkit.inventory.meta.SkullMeta;
import com.massivecraft.massivecore.Couple; import com.massivecraft.massivecore.Couple;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; 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 // REFLECTION CACHE
// -------------------------------------------- // // -------------------------------------------- //
@ -21,23 +32,27 @@ public class HeadUtil
public static Field fieldGameProfileDotId; public static Field fieldGameProfileDotId;
public static Field fieldGameProfileDotName; public static Field fieldGameProfileDotName;
static // -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public int getRequiredVersion()
{ {
try return 8;
{
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();
}
} }
@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 // GAMEPROFILE: SIMPLE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -1,9 +1,8 @@
package com.massivecraft.massivecore.util; package com.massivecraft.massivecore.nms;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.logging.Level;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.json.simple.JSONObject; 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;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils.PackageType; 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 // FIELDS
// -------------------------------------------- // // -------------------------------------------- //
// Is using
private static boolean useTitles = false;
private static boolean useRaw = false;
// The enums used to tell which packet it is. // The enums used to tell which packet it is.
// They correspond to the commands with the same name. // They correspond to the commands with the same name.
private static Class<?> titleEnumClass; private static Class<?> titleEnumClass;
@ -47,64 +49,51 @@ public final class PacketUtil
private static Constructor<?> chatPacketConstructor; 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 Enum<?> e = (Enum<?>) o;
titleEnumClass = getTitleEnumClass(); if (e.name().equalsIgnoreCase("TITLE")) titleMainEnum = e;
else if (e.name().equalsIgnoreCase("SUBTITLE")) titleSubEnum = e;
// Get the title enum values. else if (e.name().equalsIgnoreCase("TIMES")) titleTimesEnum = e;
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;
} }
// 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 public static Class<?> getTitleEnumClass() throws ClassNotFoundException
@ -150,27 +139,13 @@ public final class PacketUtil
sendPacket.setAccessible(true); sendPacket.setAccessible(true);
} }
// -------------------------------------------- //
// AVAILABLE
// -------------------------------------------- //
public static boolean isTitleAvailable()
{
return useTitles;
}
public static boolean isRawAvailable()
{
return useRaw;
}
// -------------------------------------------- // // -------------------------------------------- //
// SEND TITLES // SEND TITLES
// -------------------------------------------- // // -------------------------------------------- //
public static boolean sendTitle(Player player, int ticksIn, int ticksStay, int ticksOut, String titleMain, String titleSub) 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 try
{ {
@ -217,7 +192,7 @@ public final class PacketUtil
public static boolean sendRaw(Player player, String string) public static boolean sendRaw(Player player, String string)
{ {
if ( ! useRaw) return false; if ( ! get().isAvailable()) return false;
try try
{ {