From f457aafbe08f950d71046658e6033574d6ef2732 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 15 Mar 2016 22:33:37 +0100 Subject: [PATCH] Button and Insertion Mson --- src/com/massivecraft/massivecore/Button.java | 159 ++++++++++++++++++ .../massivecraft/massivecore/mson/Mson.java | 44 ++--- .../massivecore/mson/MsonEventType.java | 4 +- 3 files changed, 183 insertions(+), 24 deletions(-) create mode 100644 src/com/massivecraft/massivecore/Button.java diff --git a/src/com/massivecraft/massivecore/Button.java b/src/com/massivecraft/massivecore/Button.java new file mode 100644 index 00000000..46ec3d34 --- /dev/null +++ b/src/com/massivecraft/massivecore/Button.java @@ -0,0 +1,159 @@ +package com.massivecraft.massivecore; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.command.MassiveCommand; +import com.massivecraft.massivecore.command.requirement.Requirement; +import com.massivecraft.massivecore.command.requirement.RequirementAbstract; +import com.massivecraft.massivecore.mson.Mson; +import static com.massivecraft.massivecore.mson.Mson.mson; + +public class Button +{ + // -------------------------------------------- // + // INSTANCE + // -------------------------------------------- // + + public static Button get() { return new Button(); } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public Button() + { + + } + + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + // A button is either enabled or disabled. These are the two corresponding colors. + public static final ChatColor COLOR_ENABLED = ChatColor.AQUA; + public static final ChatColor COLOR_DISABLED = ChatColor.GRAY; + + // -------------------------------------------- // + // FIELDS > COMMON + // -------------------------------------------- // + + // This is the text inside the button. + private String name = null; + public String getName() { return this.name; } + public Button setName(String name) { this.name = name; return this; } + + // Padding right, left or none. + public Boolean paddingRight = null; + public Boolean isPaddingRight() { return this.paddingRight; } + public Button setPaddingRight(Boolean paddingRight) { this.paddingRight = paddingRight; return this; } + + // Verbose visible as grey. Otherwise hidden. + public boolean verbose = true; + public boolean isVerbose() { return this.verbose; } + public Button setVerbose(boolean verbose) { this.verbose = verbose; return this; } + + // When you just want to error really hard! + private String error = null; + public String getError() { return this.error; } + public Button setError(String error) { this.error = error; return this; } + + // Requirements to always be validated. + private List requirements = new MassiveList<>(); + public List getRequirements() { return this.requirements; } + public Button setRequirements(Collection requirements) { this.requirements = new MassiveList<>(requirements); return this; } + public Button setRequirements(Requirement... requirements) { this.setRequirements(Arrays.asList(requirements)); return this; } + + // -------------------------------------------- // + // FIELDS > COMMAND + // -------------------------------------------- // + + private CommandSender sender = null; + public CommandSender getSender() { return this.sender; } + public Button setSender(CommandSender sender) { this.sender = sender; return this; } + + private MassiveCommand command = null; + public MassiveCommand getCommand() { return this.command; } + public Button setCommand(MassiveCommand command) { this.command = command; return this; } + + private List args = new MassiveList<>(); + public List getArgs() { return this.args; } + public Button setArgs(Collection args) { this.args = new MassiveList<>(args); return this; } + public Button setArgs(String... args) { this.setArgs(Arrays.asList(args)); return this; } + + // -------------------------------------------- // + // FIELDS > LINK + // -------------------------------------------- // + + private String link = null; + public String getLink() { return this.link; } + public Button setLink(String link) { this.link = link; return this; } + + // -------------------------------------------- // + // RENDER + // -------------------------------------------- // + + public Mson render() + { + // Create + Mson ret = mson( + "[", + Mson.parse(this.getName()), + "]" + ); + + // Error and Enabled + String error = this.getError(); + if (error == null) + { + // Get Requirements + List requirements = new MassiveList<>(); + if (this.getCommand() != null) requirements.addAll(this.getCommand().getRequirements()); + + // Check Requirements + error = RequirementAbstract.getRequirementsError(requirements, this.getSender(), this.getCommand(), true); + } + + boolean enabled = (error == null); + + // Check Verbose + if ( ! enabled && ! this.isVerbose()) return null; + + // Colorize + ChatColor color = (enabled ? COLOR_ENABLED : COLOR_DISABLED); + ret = ret.color(color); + + // Empower + if (enabled) + { + if (this.getCommand() != null) + { + ret = ret.command(this.getCommand(), this.getArgs()); + } + else if (this.getLink() != null) + { + ret = ret.link(this.getLink()); + } + else + { + throw new RuntimeException(); + } + } + else + { + ret = ret.tooltip(error); + } + + // Pad + if (Boolean.TRUE.equals(this.isPaddingRight())) return mson(ret, " "); + if (Boolean.FALSE.equals(this.isPaddingRight())) return mson(" ", ret); + + // Return + return ret; + } +} diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index 2fc1af4b..a67d727b 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -134,10 +134,10 @@ public class Mson implements Serializable 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; } - public String getEffectiveInsertionString() { return insertionString != null ? insertionString : getInheritedInsertionString(); } - protected String getInheritedInsertionString() { return this.hasParent() ? this.getParent().getEffectiveInsertionString() : null; } + private final String insertion; + public String getInsertion() { return this.insertion; } + public String getEffectiveInsertion() { return insertion != null ? insertion : getInheritedInsertion(); } + protected String getInheritedInsertion() { return this.hasParent() ? this.getParent().getEffectiveInsertion() : null; } // The other parts of the message private final List extra; @@ -197,7 +197,7 @@ public class Mson implements Serializable if (this.isObfuscated() != 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.getInsertion() != null) return false; if (this.hasExtra()) return false; return true; } @@ -206,7 +206,7 @@ public class Mson implements Serializable { if (this.getEvent(MsonEventType.CLICK) != null) return true; if (this.getEvent(MsonEventType.HOVER) != null) return true; - if (this.getInsertionString() != null) return true; + if (this.getInsertion() != null) return true; if (this.hasExtra()) { @@ -223,13 +223,13 @@ public class Mson implements Serializable // WITH FIELDS // -------------------------------------------- // - public Mson text(String text) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } - public Mson color(ChatColor color) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } - public Mson bold(Boolean bold) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } - public Mson italic(Boolean italic) { 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 obfuscated(Boolean obfuscated) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertionString, extra, parent); } + public Mson text(String text) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson color(ChatColor color) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson bold(Boolean bold) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson italic(Boolean italic) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson underlined(Boolean underlined) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson strikethrough(Boolean strikethrough) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } + public Mson obfuscated(Boolean obfuscated) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } public Mson event(Boolean tooltip, MsonEventType type, MsonEvent event) { @@ -255,9 +255,9 @@ public class Mson implements Serializable } 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, extra == null ? null : ImmutableList.copyOf(extra), parent); } - public Mson parent(Mson parent) { 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, insertion, extra, parent); } + public Mson extra(Mson[] extra) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra == null ? null : ImmutableList.copyOf(extra), parent); } + public Mson parent(Mson parent) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); } // -------------------------------------------- // // ADD @@ -358,12 +358,12 @@ public class Mson implements Serializable { // NOTE: We can't use null. // Since we want to override color and format in parents. - return Mson.valueOf(text, ChatColor.WHITE, false, false, false, false, false, clickEvent, hoverEvent, insertionString, extra, parent); + return Mson.valueOf(text, ChatColor.WHITE, false, false, false, false, false, clickEvent, hoverEvent, insertion, extra, parent); } public Mson stripStyle() { - Mson ret = Mson.valueOf(text, null, null, null, null, null, null, clickEvent, hoverEvent, insertionString, null, parent); + Mson ret = Mson.valueOf(text, null, null, null, null, null, null, clickEvent, hoverEvent, insertion, null, parent); if (this.hasExtra()) { @@ -394,7 +394,7 @@ public class Mson implements Serializable this.isEffectiveObfuscated(), this.getEffectiveEvent(MsonEventType.CLICK), this.getEffectiveEvent(MsonEventType.HOVER), - this.getEffectiveInsertionString(), + this.getEffectiveInsertion(), this.getExtra(), null ); @@ -456,7 +456,7 @@ public class Mson implements Serializable // Insertionstring - this.insertionString = insertionString; + this.insertion = insertionString; // Mojang doesn't allow zero sized arrays, but null is fine. So null. if (extra != null && extra.size() == 0) extra = null; @@ -1293,7 +1293,7 @@ public class Mson implements Serializable result = prime * result + Objects.hashCode(this.underlined); result = prime * result + Objects.hashCode(this.clickEvent); result = prime * result + Objects.hashCode(this.hoverEvent); - result = prime * result + Objects.hashCode(this.insertionString); + result = prime * result + Objects.hashCode(this.insertion); result = prime * result + Objects.hashCode(this.extra); return result; } @@ -1314,7 +1314,7 @@ public class Mson implements Serializable if ( ! MUtil.equals(this.underlined, that.underlined)) return false; if ( ! MUtil.equals(this.clickEvent, that.clickEvent)) return false; if ( ! MUtil.equals(this.hoverEvent, that.hoverEvent)) return false; - if ( ! MUtil.equals(this.insertionString, that.insertionString)) return false; + if ( ! MUtil.equals(this.insertion, that.insertion)) return false; if ( ! MUtil.equals(this.extra, that.extra)) return false; return true; diff --git a/src/com/massivecraft/massivecore/mson/MsonEventType.java b/src/com/massivecraft/massivecore/mson/MsonEventType.java index d228936c..673d6515 100644 --- a/src/com/massivecraft/massivecore/mson/MsonEventType.java +++ b/src/com/massivecraft/massivecore/mson/MsonEventType.java @@ -27,7 +27,7 @@ public abstract class MsonEventType mson.isObfuscated(), event, mson.getEvent(HOVER), - mson.getInsertionString(), + mson.getInsertion(), mson.getExtra(), mson.getParent() ); @@ -55,7 +55,7 @@ public abstract class MsonEventType mson.isObfuscated(), mson.getEvent(CLICK), event, - mson.getInsertionString(), + mson.getInsertion(), mson.getExtra(), mson.getParent() );