Add Actionbar functionality.

This commit is contained in:
ulumulu1510 2015-11-10 19:49:55 +01:00 committed by Olof Larsson
parent 3ab7abc9ca
commit a0685d0b5b
6 changed files with 151 additions and 6 deletions

View File

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

View File

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

View File

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

View File

@ -58,6 +58,10 @@ public class Mixin
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; }
public static void setTitleMixin(TitleMixin val) { titleMixin = val; }
@ -340,6 +344,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
// -------------------------------------------- //

View File

@ -29,6 +29,9 @@ public class TitleMixinDefault extends TitleMixinAbstract
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);
}

View File

@ -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("<b>Sending title failed!");
MassiveCore.get().log(Txt.parse("<b>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("<b>Sending raw chat failed!");
MassiveCore.get().log(Txt.parse("<b>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("<b>Sending actionbar failed!"));
ex.printStackTrace();
// So we failed and it didn't work.
return false;