From a0685d0b5b01b1a3b6f4fc256b51122c4b4ea0f8 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Tue, 10 Nov 2015 19:49:55 +0100 Subject: [PATCH] Add Actionbar functionality. --- .../massivecore/mixin/ActionbarMixin.java | 19 +++++++ .../mixin/ActionbarMixinAbstract.java | 13 +++++ .../mixin/ActionbarMixinDefault.java | 52 +++++++++++++++++++ .../massivecraft/massivecore/mixin/Mixin.java | 32 ++++++++++++ .../massivecore/mixin/TitleMixinDefault.java | 3 ++ .../massivecore/nms/NmsPacket.java | 38 +++++++++++--- 6 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 src/com/massivecraft/massivecore/mixin/ActionbarMixin.java create mode 100644 src/com/massivecraft/massivecore/mixin/ActionbarMixinAbstract.java create mode 100644 src/com/massivecraft/massivecore/mixin/ActionbarMixinDefault.java diff --git a/src/com/massivecraft/massivecore/mixin/ActionbarMixin.java b/src/com/massivecraft/massivecore/mixin/ActionbarMixin.java new file mode 100644 index 00000000..6a827f9c --- /dev/null +++ b/src/com/massivecraft/massivecore/mixin/ActionbarMixin.java @@ -0,0 +1,19 @@ +package com.massivecraft.massivecore.mixin; + +import com.massivecraft.massivecore.mson.Mson; + +public interface ActionbarMixin +{ + // Abstract + public boolean sendActionbarMessage(Object watcherObject, String message); + + // Parsed + public boolean sendActionbarMsg(Object watcherObject, String message); + + // Mson + public boolean sendActionbarMson(Object watcherObject, Mson mson); + + // Available + public boolean isActionbarAvailable(); + +} diff --git a/src/com/massivecraft/massivecore/mixin/ActionbarMixinAbstract.java b/src/com/massivecraft/massivecore/mixin/ActionbarMixinAbstract.java new file mode 100644 index 00000000..fda51d05 --- /dev/null +++ b/src/com/massivecraft/massivecore/mixin/ActionbarMixinAbstract.java @@ -0,0 +1,13 @@ +package com.massivecraft.massivecore.mixin; + +import com.massivecraft.massivecore.util.Txt; + +public abstract class ActionbarMixinAbstract implements ActionbarMixin +{ + // Parsed + @Override + public boolean sendActionbarMsg(Object watcherObject, String message) + { + return this.sendActionbarMessage(watcherObject, Txt.parse(message)); + } +} diff --git a/src/com/massivecraft/massivecore/mixin/ActionbarMixinDefault.java b/src/com/massivecraft/massivecore/mixin/ActionbarMixinDefault.java new file mode 100644 index 00000000..f933158f --- /dev/null +++ b/src/com/massivecraft/massivecore/mixin/ActionbarMixinDefault.java @@ -0,0 +1,52 @@ +package com.massivecraft.massivecore.mixin; + +import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.nms.NmsPacket; +import com.massivecraft.massivecore.util.IdUtil; +import org.bukkit.entity.Player; + +public class ActionbarMixinDefault extends ActionbarMixinAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static ActionbarMixinDefault i = new ActionbarMixinDefault(); + public static ActionbarMixinDefault get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean sendActionbarMessage(Object watcherObject, String message) + { + // Get the player + Player player = IdUtil.getPlayer(watcherObject); + if(player == null) return false; + + message = NmsPacket.toJson(message); + + return NmsPacket.sendActionbar(player, message); + } + + @Override + public boolean sendActionbarMson(Object watcherObject, Mson mson) + { + // Get the player + Player player = IdUtil.getPlayer(watcherObject); + if(player == null) return false; + + // Convert to raw + String message = mson.toRaw(); + + return NmsPacket.sendActionbar(player, message); + } + + @Override + public boolean isActionbarAvailable() + { + return NmsPacket.get().isAvailable(); + } + +} diff --git a/src/com/massivecraft/massivecore/mixin/Mixin.java b/src/com/massivecraft/massivecore/mixin/Mixin.java index c08ee8a5..4d46f97f 100644 --- a/src/com/massivecraft/massivecore/mixin/Mixin.java +++ b/src/com/massivecraft/massivecore/mixin/Mixin.java @@ -57,6 +57,10 @@ public class Mixin private static MessageMixin messageMixin = MessageMixinDefault.get(); public static MessageMixin getMessageMixin() { return messageMixin; } public static void setMessageMixin(MessageMixin val) { messageMixin = val; } + + private static ActionbarMixin actionbarMixin = ActionbarMixinDefault.get(); + public static ActionbarMixin getActionbarMixin() { return actionbarMixin; } + public static void setActionbarMixin(ActionbarMixin val) { actionbarMixin = val; } private static TitleMixin titleMixin = TitleMixinDefault.get(); public static TitleMixin getTitleMixin() { return titleMixin; } @@ -339,6 +343,34 @@ public class Mixin { return getMessageMixin().messageOne(sendeeObject, messages); } + + // -------------------------------------------- // + // STATIC EXPOSE: ACTIONBAR + // -------------------------------------------- // + + // Default + public static boolean sendActionbarMessage(Object sendeeObject, String message) + { + return getActionbarMixin().sendActionbarMessage(sendeeObject, message); + } + + // Parsed + public static boolean sendActionbarMsg(Object sendeeObject, String message) + { + return getActionbarMixin().sendActionbarMsg(sendeeObject, message); + } + + // Mson + public static boolean sendActionbarMson(Object sendeeObject, Mson mson) + { + return getActionbarMixin().sendActionbarMson(sendeeObject, mson); + } + + // Available + public static boolean isActionbarAvailable() + { + return getActionbarMixin().isActionbarAvailable(); + } // -------------------------------------------- // // STATIC EXPOSE: TITLE diff --git a/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java b/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java index 02c6478c..1bd526de 100644 --- a/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/TitleMixinDefault.java @@ -28,6 +28,9 @@ public class TitleMixinDefault extends TitleMixinAbstract // If we don't send any message (empty is ok) we might end up displaying old messages. if (titleSub == null) titleSub = ""; if (titleMain == null) titleMain = ""; + + titleSub = NmsPacket.toJson(titleSub); + titleMain = NmsPacket.toJson(titleMain); return NmsPacket.sendTitle(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub); } diff --git a/src/com/massivecraft/massivecore/nms/NmsPacket.java b/src/com/massivecraft/massivecore/nms/NmsPacket.java index 391ebcd2..1d38acb3 100644 --- a/src/com/massivecraft/massivecore/nms/NmsPacket.java +++ b/src/com/massivecraft/massivecore/nms/NmsPacket.java @@ -4,6 +4,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; +import com.massivecraft.massivecore.util.Txt; import org.bukkit.entity.Player; import org.json.simple.JSONObject; @@ -45,8 +46,9 @@ public final class NmsPacket extends NmsAbstract private static Constructor titlePacketConstructor; private static Constructor titlePacketConstructorTimes; - // The chat packet and its constructor + // The chat packet and its constructors private static Constructor chatPacketConstructor; + private static Constructor chatPacketActionbarConstructor; // -------------------------------------------- // // OVERRIDE @@ -86,6 +88,7 @@ public final class NmsPacket extends NmsAbstract // Get Chat packet and it's constructor Class chatPacketClass = PackageType.MINECRAFT_SERVER.getClass("PacketPlayOutChat"); chatPacketConstructor = ReflectionUtils.getConstructor(chatPacketClass, iChatBaseComponent); + chatPacketActionbarConstructor = ReflectionUtils.getConstructor(chatPacketClass, iChatBaseComponent, Byte.TYPE); // Player connection getHandle = ReflectionUtils.getMethod("CraftPlayer", PackageType.CRAFTBUKKIT_ENTITY, "getHandle"); @@ -155,8 +158,6 @@ public final class NmsPacket extends NmsAbstract if (titleMain != null) { - titleMain = toJson(titleMain); - // Title Object titleMainChat = toChatBaseComponent(titleMain); Object titleMainPacket = titlePacketConstructor.newInstance(titleMainEnum, titleMainChat); @@ -166,7 +167,6 @@ public final class NmsPacket extends NmsAbstract if (titleSub != null) { - titleSub = toJson(titleSub); // SubTitle Object titleSubChat = toChatBaseComponent(titleSub); Object titleSubPacket = titlePacketConstructor.newInstance(titleSubEnum, titleSubChat); @@ -176,7 +176,7 @@ public final class NmsPacket extends NmsAbstract } catch (Exception ex) { - MassiveCore.get().log("Sending title failed!"); + MassiveCore.get().log(Txt.parse("Sending title failed!")); ex.printStackTrace(); // So we failed, didn't work. return false; @@ -203,7 +203,33 @@ public final class NmsPacket extends NmsAbstract } catch (Exception ex) { - MassiveCore.get().log("Sending raw chat failed!"); + MassiveCore.get().log(Txt.parse("Sending raw chat failed!")); + ex.printStackTrace(); + // So we failed and it didn't work. + return false; + } + + return true; + } + + // -------------------------------------------- // + // SEND ACTIONBAR + // -------------------------------------------- // + + public static boolean sendActionbar(Player player, String string) + { + if ( ! get().isAvailable()) return false; + + try + { + Object actionbar = toChatBaseComponent(string); + Object chatPacket = chatPacketActionbarConstructor.newInstance(actionbar, (byte) 2); + + sendPacket(player, chatPacket); + } + catch(Exception ex) + { + MassiveCore.get().log(Txt.parse("Sending actionbar failed!")); ex.printStackTrace(); // So we failed and it didn't work. return false;