diff --git a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java index 9e71b196..391c5f59 100644 --- a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java +++ b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreTest.java @@ -40,7 +40,7 @@ public class CmdMassiveCoreTest extends MassiveCommand public void perform() throws MassiveException { - message(mson("This is your ", mson("item").tooltip(me.getItemInHand()))); + message(mson("This is your ", mson("item").item(me.getItemInHand()))); // OLD STUFF /*// Args ParticleEffect particleEffect = this.readArg(); diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index 52c3c3fc..1fbda9da 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -125,15 +125,11 @@ public class Mson implements Serializable protected boolean isInheritedObfuscated() { return hasParent() && getParent().isEffectiveObfuscated(); } // FIELD: The Events which happen when you click, hover over or shift-click the message - private final MsonEvent clickEvent; - public MsonEvent getClickEvent() { return this.clickEvent; } - public MsonEvent getEffectiveClickEvent() { return clickEvent != null ? clickEvent : getInheritedClickEvent(); } - protected MsonEvent getInheritedClickEvent() { return this.hasParent() ? this.getParent().getEffectiveClickEvent() : null; } - - private final MsonEvent hoverEvent; - public MsonEvent getHoverEvent() { return this.hoverEvent; } - public MsonEvent getEffectiveHoverEvent() { return hoverEvent != null ? hoverEvent : getInheritedHoverEvent(); } - protected MsonEvent getInheritedHoverEvent() { return this.hasParent() ? this.getParent().getEffectiveHoverEvent() : null; } + protected final MsonEvent clickEvent; + protected final MsonEvent hoverEvent; + public MsonEvent getEvent(MsonEventType type) { return type.get(this); } + public MsonEvent getEffectiveEvent(MsonEventType type) { return type.get(this) != null ? type.get(this) : getInheritedEvent(type); } + protected MsonEvent getInheritedEvent(MsonEventType type) { return this.hasParent() ? this.getParent().getEffectiveEvent(type) : null; } private final String insertionString; public String getInsertionString() { return this.insertionString; } @@ -196,8 +192,8 @@ public class Mson implements Serializable if (this.isUnderlined() != null) return false; if (this.isStrikethrough() != null) return false; if (this.isObfuscated() != null) return false; - if (this.getClickEvent() != null) return false; - if (this.getHoverEvent() != null) return false; + if (this.getEvent(MsonEventType.CLICK) != null) return false; + if (this.getEvent(MsonEventType.HOVER) != null) return false; if (this.getInsertionString() != null) return false; if (this.hasExtra()) return false; return true; @@ -205,8 +201,8 @@ public class Mson implements Serializable public boolean hasSpecialBehaviour() { - if (this.getClickEvent() != null) return true; - if (this.getHoverEvent() != null) return true; + if (this.getEvent(MsonEventType.CLICK) != null) return true; + if (this.getEvent(MsonEventType.HOVER) != null) return true; if (this.getInsertionString() != null) return true; if (this.hasExtra()) @@ -231,8 +227,19 @@ public class Mson implements Serializable public Mson underlined(Boolean underlined) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } public Mson strikethrough(Boolean strikethrough) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } public Mson obfuscated(Boolean obfuscated) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } - public Mson clickEvent(MsonEvent clickEvent) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } - public Mson hoverEvent(MsonEvent hoverEvent) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } + + public Mson event(Boolean tooltip, MsonEvent event) + { + MsonEventType type = event.getType(); + Mson ret = type.set(this, event); + String created = event.createTooltip(); + if (created == null) tooltip = false; + if (tooltip == null) tooltip = (this.getTooltip() == null && this.getItem() == null); + if (tooltip) ret = ret.tooltip(created); + return ret; + } + public Mson event(MsonEvent event) { return this.event(null, event); } + public Mson insertionString(String insertionString) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } public Mson extra(List extra) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } public Mson extra(Mson[] extra) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, ImmutableList.copyOf(extra), parent); } @@ -264,25 +271,45 @@ public class Mson implements Serializable // CONVENIENCE MSON EVENT // -------------------------------------------- // - public Mson link(String link) { return this.clickEvent(MsonEvent.openUrl(link)); } + public Mson link(String link) { return this.event(MsonEvent.link(link)); } - public Mson suggest(String replace) { return this.clickEvent(MsonEvent.replace(replace)); } - public Mson suggest(MassiveCommand command, String... args) { return this.clickEvent(MsonEvent.replace(command, args)); } - public Mson suggest(MassiveCommand command, Iterable args) { return this.clickEvent(MsonEvent.replace(command, args)); } + public Mson suggest(String suggest) { return this.event(MsonEvent.suggest(suggest)); } + public Mson suggest(MassiveCommand command, String... args) { return this.event(MsonEvent.suggest(command, args)); } + public Mson suggest(MassiveCommand command, Iterable args) { return this.event(MsonEvent.suggest(command, args)); } - public Mson command(String command) { return this.clickEvent(MsonEvent.performCmd(command)); } - public Mson command(MassiveCommand command, String... args) { return this.clickEvent(MsonEvent.performCmd(command, args)); } - public Mson command(MassiveCommand command, Iterable args) { return this.clickEvent(MsonEvent.performCmd(command, args)); } + public Mson command(String command) { return this.event(MsonEvent.command(command)); } + public Mson command(MassiveCommand command, String... args) { return this.event(MsonEvent.command(command, args)); } + public Mson command(MassiveCommand command, Iterable args) { return this.event(MsonEvent.command(command, args)); } - public Mson tooltip(String tooltip) { return this.hoverEvent(MsonEvent.hoverText(tooltip)); } - public Mson tooltip(String... tooltip) { return this.hoverEvent(MsonEvent.hoverText(tooltip)); } - public Mson tooltip(Collection tooltip) { return this.hoverEvent(MsonEvent.hoverText(tooltip)); } + public Mson tooltip(String tooltip) { return this.event(MsonEvent.tooltip(tooltip)); } + public Mson tooltip(String... tooltip) { return this.event(MsonEvent.tooltip(tooltip)); } + public Mson tooltip(Collection tooltip) { return this.event(MsonEvent.tooltip(tooltip)); } + public Mson tooltipParse(String tooltip) { return this.event(MsonEvent.tooltipParse(tooltip)); } + public Mson tooltipParse(String... tooltip) { return this.event(MsonEvent.tooltipParse(tooltip)); } + public Mson tooltipParse(Collection tooltip) { return this.event(MsonEvent.tooltipParse(tooltip)); } - public Mson tooltipParse(String tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); } - public Mson tooltipParse(String... tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); } - public Mson tooltipParse(Collection tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); } + public Mson item(ItemStack item) { return this.event(MsonEvent.item(item)); } - public Mson tooltip(ItemStack item) { return this.hoverEvent(MsonEvent.item(item)); } + public String getLink() { return this.getEventValue(MsonEventAction.OPEN_URL); } + public String getSuggest() { return this.getEventValue(MsonEventAction.SUGGEST_COMMAND); } + public String getCommand() { return this.getEventValue(MsonEventAction.RUN_COMMAND); } + public String getTooltip() { return this.getEventValue(MsonEventAction.SHOW_TEXT); } + public String getItem() { return this.getEventValue(MsonEventAction.SHOW_ITEM); } + + protected String getEventValue(MsonEventAction targetAction) + { + MsonEventType type = targetAction.getType(); + + MsonEvent event = this.getEvent(type); + if (event == null) return null; + + MsonEventAction action = event.getAction(); + if (action == null) return null; + + if (action != targetAction) return null; + + return event.getValue(); + } // -------------------------------------------- // // CONVENIENCE STYLE @@ -377,14 +404,23 @@ public class Mson implements Serializable this.strikethrough = strikethrough; this.obfuscated = obfuscated; - // Click event - if ( clickEvent != null && ! clickEvent.isClickEvent()) throw new IllegalArgumentException(clickEvent.getEventActionType().name() + " is not a clickEventType"); + // Set Events this.clickEvent = clickEvent; - - // Hover event - if ( hoverEvent != null && ! hoverEvent.isHoverEvent()) throw new IllegalArgumentException(hoverEvent.getEventActionType().name() + " is not a hoverEventType"); this.hoverEvent = hoverEvent; + // Validate Events + MsonEventType type; + MsonEvent event; + + type = MsonEventType.CLICK; + event = this.getEvent(type); + if (event != null && event.getType() != type) throw new IllegalArgumentException(event.getAction().name() + " is not of type " + type); + + type = MsonEventType.HOVER; + event = this.getEvent(type); + if (event != null && event.getType() != type) throw new IllegalArgumentException(event.getAction().name() + " is not of type " + type); + + // Insertionstring this.insertionString = insertionString; diff --git a/src/com/massivecraft/massivecore/mson/MsonEvent.java b/src/com/massivecraft/massivecore/mson/MsonEvent.java index a8281773..f0b4ec6e 100644 --- a/src/com/massivecraft/massivecore/mson/MsonEvent.java +++ b/src/com/massivecraft/massivecore/mson/MsonEvent.java @@ -24,10 +24,10 @@ public final class MsonEvent implements Serializable // -------------------------------------------- // private final MsonEventAction action; - public MsonEventAction getEventActionType() { return this.action; } + public MsonEventAction getAction() { return this.action; } private final String value; - public String getActionText() { return this.value; } + public String getValue() { return this.value; } // -------------------------------------------- // // CONSTRUCT @@ -39,75 +39,86 @@ public final class MsonEvent implements Serializable this.value = value; } + // -------------------------------------------- // + // TOOLTIP + // -------------------------------------------- // + + public String createTooltip() + { + String prefix = this.getAction().getTooltipPrefix(); + if (prefix == null) return null; + return prefix + this.getValue(); + } + // -------------------------------------------- // // FACTORY // -------------------------------------------- // - public static MsonEvent valueOf(MsonEventAction type, String action) + public static MsonEvent valueOf(MsonEventAction action, String value) { - return new MsonEvent(type, action); + return new MsonEvent(action, value); } // clickEvents - public static MsonEvent openUrl(String url) + public static MsonEvent link(String url) { return MsonEvent.valueOf(MsonEventAction.OPEN_URL, url); } - public static MsonEvent replace(String replace) + public static MsonEvent suggest(String replace) { - return MsonEvent.valueOf(MsonEventAction.SUGGEST_COMMAND , replace); + return MsonEvent.valueOf(MsonEventAction.SUGGEST_COMMAND, replace); } - public static MsonEvent replace(MassiveCommand cmd, String... args) + public static MsonEvent suggest(MassiveCommand cmd, String... args) { - return MsonEvent.replace(cmd.getCommandLine(args)); + return MsonEvent.suggest(cmd.getCommandLine(args)); } - public static MsonEvent replace(MassiveCommand cmd, Iterable args) + public static MsonEvent suggest(MassiveCommand cmd, Iterable args) { - return MsonEvent.replace(cmd.getCommandLine(args)); + return MsonEvent.suggest(cmd.getCommandLine(args)); } - public static MsonEvent performCmd(String cmd) + public static MsonEvent command(String cmd) { return MsonEvent.valueOf(MsonEventAction.RUN_COMMAND, cmd); } - public static MsonEvent performCmd(MassiveCommand cmd, String... args) + public static MsonEvent command(MassiveCommand cmd, String... args) { - return MsonEvent.performCmd(cmd.getCommandLine(args)); + return MsonEvent.command(cmd.getCommandLine(args)); } - public static MsonEvent performCmd(MassiveCommand cmd, Iterable args) + public static MsonEvent command(MassiveCommand cmd, Iterable args) { - return MsonEvent.performCmd(cmd.getCommandLine(args)); + return MsonEvent.command(cmd.getCommandLine(args)); } // showText - public static MsonEvent hoverText(String hoverText) + public static MsonEvent tooltip(String hoverText) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, hoverText); } - public static MsonEvent hoverText(String... hoverTexts) + public static MsonEvent tooltip(String... hoverTexts) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n")); } - public static MsonEvent hoverText(Collection hoverTexts) + public static MsonEvent tooltip(Collection hoverTexts) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n")); } // showTextParsed - public static MsonEvent hoverTextParse(String hoverText) + public static MsonEvent tooltipParse(String hoverText) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(hoverText)); } - public static MsonEvent hoverTextParse(String... hoverTexts) + public static MsonEvent tooltipParse(String... hoverTexts) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n"))); } - public static MsonEvent hoverTextParse(Collection hoverTexts) + public static MsonEvent tooltipParse(Collection hoverTexts) { return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n"))); } @@ -123,8 +134,7 @@ public final class MsonEvent implements Serializable // CONVENIENCE // -------------------------------------------- // - public boolean isHoverEvent() { return this.getEventActionType().isHoverAction(); } - public boolean isClickEvent() { return this.getEventActionType().isClickAction(); } + public MsonEventType getType() { return this.getAction().getType(); } // -------------------------------------------- // // EQUALS AND HASHCODE diff --git a/src/com/massivecraft/massivecore/mson/MsonEventAction.java b/src/com/massivecraft/massivecore/mson/MsonEventAction.java index cd2e242c..f8b6a939 100644 --- a/src/com/massivecraft/massivecore/mson/MsonEventAction.java +++ b/src/com/massivecraft/massivecore/mson/MsonEventAction.java @@ -1,45 +1,39 @@ package com.massivecraft.massivecore.mson; +import com.massivecraft.massivecore.util.Txt; + public enum MsonEventAction { // -------------------------------------------- // // ENUM // -------------------------------------------- // - SUGGEST_COMMAND(), - RUN_COMMAND(), - OPEN_URL(), - SHOW_TEXT(true), - SHOW_ITEM(true), + SUGGEST_COMMAND, + RUN_COMMAND, + OPEN_URL, + SHOW_TEXT, + SHOW_ITEM // End of list ; // -------------------------------------------- // - // FIELD + // PROPERTIES // -------------------------------------------- // - private boolean isHoverAction; - public boolean isHoverAction() { return this.isHoverAction; } - - // NOTE: This behaviour might change. - // So to check if something is a click action this method should be called. - // Doing !action.isHoverAction(); - // Shouldn't be done outside of this class. - public boolean isClickAction() { return ! this.isHoverAction(); } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - private MsonEventAction(boolean isHoveraction) + public MsonEventType getType() { - this.isHoverAction = isHoveraction; + if (this == SHOW_TEXT) return MsonEventType.HOVER; + if (this == SHOW_ITEM) return MsonEventType.HOVER; + return MsonEventType.CLICK; } - - private MsonEventAction() + + public String getTooltipPrefix() { - this(false); + if (this == SUGGEST_COMMAND) return Txt.parse("Suggest: "); + if (this == RUN_COMMAND) return Txt.parse("Command: "); + if (this == OPEN_URL) return Txt.parse("Link: "); + return null; } } diff --git a/src/com/massivecraft/massivecore/mson/MsonEventType.java b/src/com/massivecraft/massivecore/mson/MsonEventType.java new file mode 100644 index 00000000..d228936c --- /dev/null +++ b/src/com/massivecraft/massivecore/mson/MsonEventType.java @@ -0,0 +1,72 @@ +package com.massivecraft.massivecore.mson; + +public abstract class MsonEventType +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + public static MsonEventType CLICK = new MsonEventType() + { + @Override + public MsonEvent get(Mson mson) + { + return mson.clickEvent; + } + + @Override + public Mson set(Mson mson, MsonEvent event) + { + return Mson.valueOf( + mson.getText(), + mson.getColor(), + mson.isBold(), + mson.isItalic(), + mson.isUnderlined(), + mson.isStrikethrough(), + mson.isObfuscated(), + event, + mson.getEvent(HOVER), + mson.getInsertionString(), + mson.getExtra(), + mson.getParent() + ); + } + }; + + public static MsonEventType HOVER = new MsonEventType() + { + @Override + public MsonEvent get(Mson mson) + { + return mson.hoverEvent; + } + + @Override + public Mson set(Mson mson, MsonEvent event) + { + return Mson.valueOf( + mson.getText(), + mson.getColor(), + mson.isBold(), + mson.isItalic(), + mson.isUnderlined(), + mson.isStrikethrough(), + mson.isObfuscated(), + mson.getEvent(CLICK), + event, + mson.getInsertionString(), + mson.getExtra(), + mson.getParent() + ); + } + }; + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract MsonEvent get(Mson mson); + public abstract Mson set(Mson mson, MsonEvent event); + +}