Experimental Automatic Tooltip Mson

This commit is contained in:
Olof Larsson 2015-09-23 15:48:37 +02:00
parent 19f38125e4
commit 5323829018
5 changed files with 195 additions and 83 deletions

View File

@ -40,7 +40,7 @@ public class CmdMassiveCoreTest extends MassiveCommand
public void perform() throws MassiveException 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 // OLD STUFF
/*// Args /*// Args
ParticleEffect particleEffect = this.readArg(); ParticleEffect particleEffect = this.readArg();

View File

@ -125,15 +125,11 @@ public class Mson implements Serializable
protected boolean isInheritedObfuscated() { return hasParent() && getParent().isEffectiveObfuscated(); } protected boolean isInheritedObfuscated() { return hasParent() && getParent().isEffectiveObfuscated(); }
// FIELD: The Events which happen when you click, hover over or shift-click the message // FIELD: The Events which happen when you click, hover over or shift-click the message
private final MsonEvent clickEvent; protected final MsonEvent clickEvent;
public MsonEvent getClickEvent() { return this.clickEvent; } protected final MsonEvent hoverEvent;
public MsonEvent getEffectiveClickEvent() { return clickEvent != null ? clickEvent : getInheritedClickEvent(); } public MsonEvent getEvent(MsonEventType type) { return type.get(this); }
protected MsonEvent getInheritedClickEvent() { return this.hasParent() ? this.getParent().getEffectiveClickEvent() : null; } 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 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; }
private final String insertionString; private final String insertionString;
public String getInsertionString() { return this.insertionString; } public String getInsertionString() { return this.insertionString; }
@ -196,8 +192,8 @@ public class Mson implements Serializable
if (this.isUnderlined() != null) return false; if (this.isUnderlined() != null) return false;
if (this.isStrikethrough() != null) return false; if (this.isStrikethrough() != null) return false;
if (this.isObfuscated() != null) return false; if (this.isObfuscated() != null) return false;
if (this.getClickEvent() != null) return false; if (this.getEvent(MsonEventType.CLICK) != null) return false;
if (this.getHoverEvent() != null) return false; if (this.getEvent(MsonEventType.HOVER) != null) return false;
if (this.getInsertionString() != null) return false; if (this.getInsertionString() != null) return false;
if (this.hasExtra()) return false; if (this.hasExtra()) return false;
return true; return true;
@ -205,8 +201,8 @@ public class Mson implements Serializable
public boolean hasSpecialBehaviour() public boolean hasSpecialBehaviour()
{ {
if (this.getClickEvent() != null) return true; if (this.getEvent(MsonEventType.CLICK) != null) return true;
if (this.getHoverEvent() != null) return true; if (this.getEvent(MsonEventType.HOVER) != null) return true;
if (this.getInsertionString() != null) return true; if (this.getInsertionString() != null) return true;
if (this.hasExtra()) 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 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 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 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 insertionString(String insertionString) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); }
public Mson extra(List<Mson> extra) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } public Mson extra(List<Mson> 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); } 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 // 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(String suggest) { return this.event(MsonEvent.suggest(suggest)); }
public Mson suggest(MassiveCommand command, String... args) { return this.clickEvent(MsonEvent.replace(command, args)); } public Mson suggest(MassiveCommand command, String... args) { return this.event(MsonEvent.suggest(command, args)); }
public Mson suggest(MassiveCommand command, Iterable<String> args) { return this.clickEvent(MsonEvent.replace(command, args)); } public Mson suggest(MassiveCommand command, Iterable<String> args) { return this.event(MsonEvent.suggest(command, args)); }
public Mson command(String command) { return this.clickEvent(MsonEvent.performCmd(command)); } public Mson command(String command) { return this.event(MsonEvent.command(command)); }
public Mson command(MassiveCommand command, String... args) { return this.clickEvent(MsonEvent.performCmd(command, args)); } public Mson command(MassiveCommand command, String... args) { return this.event(MsonEvent.command(command, args)); }
public Mson command(MassiveCommand command, Iterable<String> args) { return this.clickEvent(MsonEvent.performCmd(command, args)); } public Mson command(MassiveCommand command, Iterable<String> 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.event(MsonEvent.tooltip(tooltip)); }
public Mson tooltip(String... tooltip) { return this.hoverEvent(MsonEvent.hoverText(tooltip)); } public Mson tooltip(String... tooltip) { return this.event(MsonEvent.tooltip(tooltip)); }
public Mson tooltip(Collection<String> tooltip) { return this.hoverEvent(MsonEvent.hoverText(tooltip)); } public Mson tooltip(Collection<String> 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<String> tooltip) { return this.event(MsonEvent.tooltipParse(tooltip)); }
public Mson tooltipParse(String tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); } public Mson item(ItemStack item) { return this.event(MsonEvent.item(item)); }
public Mson tooltipParse(String... tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); }
public Mson tooltipParse(Collection<String> tooltip) { return this.hoverEvent(MsonEvent.hoverTextParse(tooltip)); }
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 // CONVENIENCE STYLE
@ -377,14 +404,23 @@ public class Mson implements Serializable
this.strikethrough = strikethrough; this.strikethrough = strikethrough;
this.obfuscated = obfuscated; this.obfuscated = obfuscated;
// Click event // Set Events
if ( clickEvent != null && ! clickEvent.isClickEvent()) throw new IllegalArgumentException(clickEvent.getEventActionType().name() + " is not a clickEventType");
this.clickEvent = clickEvent; this.clickEvent = clickEvent;
// Hover event
if ( hoverEvent != null && ! hoverEvent.isHoverEvent()) throw new IllegalArgumentException(hoverEvent.getEventActionType().name() + " is not a hoverEventType");
this.hoverEvent = hoverEvent; 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 // Insertionstring
this.insertionString = insertionString; this.insertionString = insertionString;

View File

@ -24,10 +24,10 @@ public final class MsonEvent implements Serializable
// -------------------------------------------- // // -------------------------------------------- //
private final MsonEventAction action; private final MsonEventAction action;
public MsonEventAction getEventActionType() { return this.action; } public MsonEventAction getAction() { return this.action; }
private final String value; private final String value;
public String getActionText() { return this.value; } public String getValue() { return this.value; }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
@ -39,75 +39,86 @@ public final class MsonEvent implements Serializable
this.value = value; this.value = value;
} }
// -------------------------------------------- //
// TOOLTIP
// -------------------------------------------- //
public String createTooltip()
{
String prefix = this.getAction().getTooltipPrefix();
if (prefix == null) return null;
return prefix + this.getValue();
}
// -------------------------------------------- // // -------------------------------------------- //
// FACTORY // 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 // clickEvents
public static MsonEvent openUrl(String url) public static MsonEvent link(String url)
{ {
return MsonEvent.valueOf(MsonEventAction.OPEN_URL, 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<String> args) public static MsonEvent suggest(MassiveCommand cmd, Iterable<String> 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); 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<String> args) public static MsonEvent command(MassiveCommand cmd, Iterable<String> args)
{ {
return MsonEvent.performCmd(cmd.getCommandLine(args)); return MsonEvent.command(cmd.getCommandLine(args));
} }
// showText // showText
public static MsonEvent hoverText(String hoverText) public static MsonEvent tooltip(String hoverText)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, 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")); return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n"));
} }
public static MsonEvent hoverText(Collection<String> hoverTexts) public static MsonEvent tooltip(Collection<String> hoverTexts)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n")); return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.implode(hoverTexts, "\n"));
} }
// showTextParsed // showTextParsed
public static MsonEvent hoverTextParse(String hoverText) public static MsonEvent tooltipParse(String hoverText)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(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"))); return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n")));
} }
public static MsonEvent hoverTextParse(Collection<String> hoverTexts) public static MsonEvent tooltipParse(Collection<String> hoverTexts)
{ {
return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n"))); return MsonEvent.valueOf(MsonEventAction.SHOW_TEXT, Txt.parse(Txt.implode(hoverTexts, "\n")));
} }
@ -123,8 +134,7 @@ public final class MsonEvent implements Serializable
// CONVENIENCE // CONVENIENCE
// -------------------------------------------- // // -------------------------------------------- //
public boolean isHoverEvent() { return this.getEventActionType().isHoverAction(); } public MsonEventType getType() { return this.getAction().getType(); }
public boolean isClickEvent() { return this.getEventActionType().isClickAction(); }
// -------------------------------------------- // // -------------------------------------------- //
// EQUALS AND HASHCODE // EQUALS AND HASHCODE

View File

@ -1,45 +1,39 @@
package com.massivecraft.massivecore.mson; package com.massivecraft.massivecore.mson;
import com.massivecraft.massivecore.util.Txt;
public enum MsonEventAction public enum MsonEventAction
{ {
// -------------------------------------------- // // -------------------------------------------- //
// ENUM // ENUM
// -------------------------------------------- // // -------------------------------------------- //
SUGGEST_COMMAND(), SUGGEST_COMMAND,
RUN_COMMAND(), RUN_COMMAND,
OPEN_URL(), OPEN_URL,
SHOW_TEXT(true), SHOW_TEXT,
SHOW_ITEM(true), SHOW_ITEM
// End of list // End of list
; ;
// -------------------------------------------- // // -------------------------------------------- //
// FIELD // PROPERTIES
// -------------------------------------------- // // -------------------------------------------- //
private boolean isHoverAction; public MsonEventType getType()
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)
{ {
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("<h>Suggest: <c>");
if (this == RUN_COMMAND) return Txt.parse("<h>Command: <c>");
if (this == OPEN_URL) return Txt.parse("<h>Link: <c>");
return null;
} }
} }

View File

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