Button and Insertion Mson

This commit is contained in:
Olof Larsson 2016-03-15 22:33:37 +01:00
parent 9c55b4836e
commit f457aafbe0
3 changed files with 183 additions and 24 deletions

View File

@ -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<Requirement> requirements = new MassiveList<>();
public List<Requirement> getRequirements() { return this.requirements; }
public Button setRequirements(Collection<Requirement> 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<String> args = new MassiveList<>();
public List<String> getArgs() { return this.args; }
public Button setArgs(Collection<String> 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<Requirement> 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;
}
}

View File

@ -134,10 +134,10 @@ public class Mson implements Serializable
public MsonEvent getEffectiveEvent(MsonEventType type) { return type.get(this) != null ? type.get(this) : getInheritedEvent(type); } 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; } protected MsonEvent getInheritedEvent(MsonEventType type) { return this.hasParent() ? this.getParent().getEffectiveEvent(type) : null; }
private final String insertionString; private final String insertion;
public String getInsertionString() { return this.insertionString; } public String getInsertion() { return this.insertion; }
public String getEffectiveInsertionString() { return insertionString != null ? insertionString : getInheritedInsertionString(); } public String getEffectiveInsertion() { return insertion != null ? insertion : getInheritedInsertion(); }
protected String getInheritedInsertionString() { return this.hasParent() ? this.getParent().getEffectiveInsertionString() : null; } protected String getInheritedInsertion() { return this.hasParent() ? this.getParent().getEffectiveInsertion() : null; }
// The other parts of the message // The other parts of the message
private final List<Mson> extra; private final List<Mson> extra;
@ -197,7 +197,7 @@ public class Mson implements Serializable
if (this.isObfuscated() != null) return false; if (this.isObfuscated() != null) return false;
if (this.getEvent(MsonEventType.CLICK) != null) return false; if (this.getEvent(MsonEventType.CLICK) != null) return false;
if (this.getEvent(MsonEventType.HOVER) != 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; if (this.hasExtra()) return false;
return true; return true;
} }
@ -206,7 +206,7 @@ public class Mson implements Serializable
{ {
if (this.getEvent(MsonEventType.CLICK) != null) return true; if (this.getEvent(MsonEventType.CLICK) != null) return true;
if (this.getEvent(MsonEventType.HOVER) != 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()) if (this.hasExtra())
{ {
@ -223,13 +223,13 @@ public class Mson implements Serializable
// WITH FIELDS // WITH FIELDS
// -------------------------------------------- // // -------------------------------------------- //
public Mson text(String text) { 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, insertionString, 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, insertionString, 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, insertionString, 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, insertionString, 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, insertionString, 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, insertionString, 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) 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 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, insertion, 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 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, insertionString, extra, parent); } public Mson parent(Mson parent) { return Mson.valueOf(text, color, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, insertion, extra, parent); }
// -------------------------------------------- // // -------------------------------------------- //
// ADD // ADD
@ -358,12 +358,12 @@ public class Mson implements Serializable
{ {
// NOTE: We can't use null. // NOTE: We can't use null.
// Since we want to override color and format in parents. // 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() 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()) if (this.hasExtra())
{ {
@ -394,7 +394,7 @@ public class Mson implements Serializable
this.isEffectiveObfuscated(), this.isEffectiveObfuscated(),
this.getEffectiveEvent(MsonEventType.CLICK), this.getEffectiveEvent(MsonEventType.CLICK),
this.getEffectiveEvent(MsonEventType.HOVER), this.getEffectiveEvent(MsonEventType.HOVER),
this.getEffectiveInsertionString(), this.getEffectiveInsertion(),
this.getExtra(), this.getExtra(),
null null
); );
@ -456,7 +456,7 @@ public class Mson implements Serializable
// Insertionstring // Insertionstring
this.insertionString = insertionString; this.insertion = insertionString;
// Mojang doesn't allow zero sized arrays, but null is fine. So null. // Mojang doesn't allow zero sized arrays, but null is fine. So null.
if (extra != null && extra.size() == 0) extra = 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.underlined);
result = prime * result + Objects.hashCode(this.clickEvent); result = prime * result + Objects.hashCode(this.clickEvent);
result = prime * result + Objects.hashCode(this.hoverEvent); 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); result = prime * result + Objects.hashCode(this.extra);
return result; return result;
} }
@ -1314,7 +1314,7 @@ public class Mson implements Serializable
if ( ! MUtil.equals(this.underlined, that.underlined)) return false; if ( ! MUtil.equals(this.underlined, that.underlined)) return false;
if ( ! MUtil.equals(this.clickEvent, that.clickEvent)) return false; if ( ! MUtil.equals(this.clickEvent, that.clickEvent)) return false;
if ( ! MUtil.equals(this.hoverEvent, that.hoverEvent)) 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; if ( ! MUtil.equals(this.extra, that.extra)) return false;
return true; return true;

View File

@ -27,7 +27,7 @@ public abstract class MsonEventType
mson.isObfuscated(), mson.isObfuscated(),
event, event,
mson.getEvent(HOVER), mson.getEvent(HOVER),
mson.getInsertionString(), mson.getInsertion(),
mson.getExtra(), mson.getExtra(),
mson.getParent() mson.getParent()
); );
@ -55,7 +55,7 @@ public abstract class MsonEventType
mson.isObfuscated(), mson.isObfuscated(),
mson.getEvent(CLICK), mson.getEvent(CLICK),
event, event,
mson.getInsertionString(), mson.getInsertion(),
mson.getExtra(), mson.getExtra(),
mson.getParent() mson.getParent()
); );