Smarter default visual implementation

This commit is contained in:
BuildTools 2016-02-28 21:03:44 +01:00 committed by Olof Larsson
parent d1aacefdec
commit 5d868d5081
6 changed files with 105 additions and 49 deletions

View File

@ -68,7 +68,9 @@ public class HelpCommand extends MassiveCommand
if ( ! (this.hasParent() && visible)) return visible;
int pageHeight = (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE;
int size = this.getParent().getChildren().size();
// Minues one, don't count ourself.
int size = this.getParent().getChildren().size()-1;
if (size <= pageHeight)
{

View File

@ -54,7 +54,6 @@ public interface Type<T>
// A visual mson.
public Mson getVisualMsonInner(T value, CommandSender sender);
public Mson getVisualMsonInner(T value);
public Mson getVisualMson(T value, CommandSender sender);
public Mson getVisualMson(T value);
@ -64,7 +63,6 @@ public interface Type<T>
// A visual and colorful representation. Possibly with added detail such as simple ASCII art.
public String getVisualInner(T value, CommandSender sender);
public String getVisualInner(T value);
public String getVisual(T value, CommandSender sender);
public String getVisual(T value);

View File

@ -25,6 +25,7 @@ import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.util.ContainerUtil;
import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt;
public abstract class TypeAbstract<T> implements Type<T>
@ -71,15 +72,15 @@ public abstract class TypeAbstract<T> implements Type<T>
protected List<Type<?>> innerTypes = new MassiveList<>();
@SuppressWarnings("unchecked")
public <I extends Type<?>> List<I> getInnerTypes() { return (List<I>) this.innerTypes; }
@Override public <I extends Type<?>> List<I> getInnerTypes() { return (List<I>) this.innerTypes; }
@SuppressWarnings("unchecked")
public <I extends Type<?>> I getInnerType(int index) { return (I) this.getInnerTypes().get(index); }
public <I extends Type<?>> I getInnerType() { return this.getInnerType(0); }
@Override public <I extends Type<?>> I getInnerType(int index) { return (I) this.getInnerTypes().get(index); }
@Override public <I extends Type<?>> I getInnerType() { return this.getInnerType(0); }
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setInnerTypes(Collection<Type<?>> innerTypes) { this.innerTypes = new MassiveList(innerTypes); }
public void setInnerTypes(Type<?>... innerTypes) { this.setInnerTypes(Arrays.asList(innerTypes)); };
public void setInnerType(Type<?> innerType) { this.setInnerTypes(innerType); }
@Override public void setInnerTypes(Collection<Type<?>> innerTypes) { this.innerTypes = new MassiveList(innerTypes); }
@Override public void setInnerTypes(Type<?>... innerTypes) { this.setInnerTypes(Arrays.asList(innerTypes)); };
@Override public void setInnerType(Type<?> innerType) { this.setInnerTypes(innerType); }
private List<Integer> userOrder = null;
@Override public void setUserOrder(List<Integer> userOrder) { this.userOrder = userOrder; }
@ -102,14 +103,17 @@ public abstract class TypeAbstract<T> implements Type<T>
// -------------------------------------------- //
protected ChatColor visualColor = COLOR_DEFAULT;
@Override
public void setVisualColor(ChatColor color)
{
this.visualColor = color;
}
@Override
public ChatColor getVisualColor(T value, CommandSender sender)
{
return this.visualColor;
}
@Override
public ChatColor getVisualColor(T value)
{
return this.getVisualColor(value, null);
@ -120,31 +124,37 @@ public abstract class TypeAbstract<T> implements Type<T>
// -------------------------------------------- //
// A visual mson.
protected boolean visualMsonOverridden = calcVisualMsonOverridden();
public boolean isVisualMsonOverridden() { return this.visualMsonOverridden; }
public void setVisualMsonOverridden(boolean visualMsonOverridden) { this.visualMsonOverridden = visualMsonOverridden; }
public boolean calcVisualMsonOverridden()
{
return ! TypeAbstract.class.equals(ReflectionUtil.getSuperclassDeclaringMethod(this.getClass(), true, "getVisualMsonInner"));
}
@Override
public Mson getVisualMsonInner(T value, CommandSender sender)
{
return Mson.fromParsedMessage(this.getVisualInner(value, sender));
}
public Mson getVisualMsonInner(T value)
{
return this.getVisualMson(value, null);
}
@Override
public Mson getVisualMson(T value, CommandSender sender)
{
if (value == null) return MSON_NULL;
return this.getVisualMsonInner(value, sender);
}
@Override
public Mson getVisualMson(T value)
{
if (value == null) return MSON_NULL;
return this.getVisualMsonInner(value);
return this.getVisualMson(value, null);
}
// -------------------------------------------- //
// WRITE VISUAL
// -------------------------------------------- //
@Override
public String getVisualInner(T value, CommandSender sender)
{
if (value instanceof SenderEntity<?>)
@ -153,19 +163,23 @@ public abstract class TypeAbstract<T> implements Type<T>
String ret = senderEntity.getDisplayName(sender);
return ret == null ? NULL : ret;
}
return this.getVisualColor(value, sender) + this.getNameInner(value);
if (this.isVisualMsonOverridden())
{
return this.getVisualMsonInner(value, sender).toPlain(true);
}
else
{
return this.getVisualColor(value, sender) + this.getNameInner(value);
}
}
public String getVisualInner(T value)
{
return this.getVisualInner(value, null);
}
@Override
public String getVisual(T value, CommandSender sender)
{
if (value == null) return NULL;
return this.getVisualInner(value, sender);
}
@Override
public String getVisual(T value)
{
return this.getVisual(value, null);
@ -175,6 +189,7 @@ public abstract class TypeAbstract<T> implements Type<T>
// WRITE NAME
// -------------------------------------------- //
@Override
public String getNameInner(T value)
{
if (value instanceof Named)
@ -186,17 +201,20 @@ public abstract class TypeAbstract<T> implements Type<T>
return this.getIdInner(value);
}
@Override
public String getName(T value)
{
if (value == null) return null;
return this.getNameInner(value);
}
@Override
public Set<String> getNamesInner(T value)
{
return Collections.singleton(this.getNameInner(value));
}
@Override
public Set<String> getNames(T value)
{
if (value == null) return Collections.emptySet();
@ -207,6 +225,7 @@ public abstract class TypeAbstract<T> implements Type<T>
// WRITE ID
// -------------------------------------------- //
@Override
public String getIdInner(T value)
{
if (value instanceof Entity)
@ -222,17 +241,20 @@ public abstract class TypeAbstract<T> implements Type<T>
return null;
}
@Override
public String getId(T value)
{
if (value == null) return null;
return this.getIdInner(value);
}
@Override
public Set<String> getIdsInner(T value)
{
return Collections.singleton(this.getIdInner(value));
}
@Override
public Set<String> getIds(T value)
{
if (value == null) return Collections.emptySet();
@ -243,16 +265,19 @@ public abstract class TypeAbstract<T> implements Type<T>
// READ
// -------------------------------------------- //
@Override
public T read(CommandSender sender) throws MassiveException
{
return this.read(null, sender);
}
@Override
public T read(String arg) throws MassiveException
{
return this.read(arg, null);
}
@Override
public T read() throws MassiveException
{
return this.read(null, null);
@ -262,6 +287,7 @@ public abstract class TypeAbstract<T> implements Type<T>
// VALID
// -------------------------------------------- //
@Override
public boolean isValid(String arg, CommandSender sender)
{
try
@ -279,11 +305,13 @@ public abstract class TypeAbstract<T> implements Type<T>
// TAB LIST
// -------------------------------------------- //
@Override
public boolean allowSpaceAfterTab()
{
return true;
}
@Override
public List<String> getTabListFiltered(CommandSender sender, String arg)
{
// Get the raw tab list.
@ -431,22 +459,22 @@ public abstract class TypeAbstract<T> implements Type<T>
// -------------------------------------------- //
private Boolean container = null;
public boolean isContainer() { this.calcContainer(); return this.container; }
@Override public boolean isContainer() { this.calcContainer(); return this.container; }
private Boolean containerMap = null;
public boolean isContainerMap() { this.calcContainer(); return this.containerMap; }
@Override public boolean isContainerMap() { this.calcContainer(); return this.containerMap; }
private Boolean containerCollection = null;
public boolean isContainerCollection() { this.calcContainer(); return this.containerCollection; }
@Override public boolean isContainerCollection() { this.calcContainer(); return this.containerCollection; }
private Boolean containerIndexed = null;
public boolean isContainerIndexed() { this.calcContainer(); return this.containerIndexed; }
@Override public boolean isContainerIndexed() { this.calcContainer(); return this.containerIndexed; }
private Boolean containerOrdered = null;
public boolean isContainerOrdered() { this.calcContainer(); return this.containerOrdered; }
@Override public boolean isContainerOrdered() { this.calcContainer(); return this.containerOrdered; }
private Boolean containerSorted = null;
public boolean isContainerSorted() { this.calcContainer(); return this.containerSorted; }
@Override public boolean isContainerSorted() { this.calcContainer(); return this.containerSorted; }
private void calcContainer()
{
@ -466,6 +494,7 @@ public abstract class TypeAbstract<T> implements Type<T>
private Comparator<Object> elementComparator = null;
@SuppressWarnings("unchecked")
@Override
public <E> Comparator<E> getContainerComparator()
{
if (this.elementComparator != null) return (Comparator<E>) this.elementComparator;
@ -473,8 +502,10 @@ public abstract class TypeAbstract<T> implements Type<T>
return (Comparator<E>) ComparatorHashCode.get().getLenient();
}
@SuppressWarnings("unchecked")
@Override
public void setContainerComparator(Comparator<?> comparator) { this.elementComparator = (Comparator<Object>) comparator; }
@Override
public <E> List<E> getContainerElementsOrdered(Iterable<E> elements)
{
if (elements == null) return null;
@ -510,6 +541,7 @@ public abstract class TypeAbstract<T> implements Type<T>
// EQUALS
// -------------------------------------------- //
@Override
public boolean equals(T type1, T type2)
{
if (type1 == null) return type2 == null;
@ -517,6 +549,7 @@ public abstract class TypeAbstract<T> implements Type<T>
return this.equalsInner(type1, type2);
}
@Override
public boolean equalsInner(T type1, T type2)
{
return type1.equals(type2);
@ -526,11 +559,13 @@ public abstract class TypeAbstract<T> implements Type<T>
// EDITOR
// -------------------------------------------- //
@Override
public <O> CommandEditAbstract<O, T> createEditCommand(EditSettings<O> settings, Property<O, T> property)
{
return new CommandEditSimple<O, T>(settings, property);
}
@Override
public T createNewInstance()
{
return null;

View File

@ -123,14 +123,6 @@ public abstract class TypeTransformer<I, O> extends TypeAbstract<O>
return INNER.getVisualMsonInner(inner, sender);
}
@Override
public Mson getVisualMsonInner(O outer)
{
I inner = outerToInner(outer);
if (outer != null && inner == null) return prefix(OUTER.getVisualMsonInner(outer));
return INNER.getVisualMsonInner(inner);
}
@Override
public Mson getVisualMson(O outer, CommandSender sender)
{
@ -159,14 +151,6 @@ public abstract class TypeTransformer<I, O> extends TypeAbstract<O>
return INNER.getVisualInner(inner, sender);
}
@Override
public String getVisualInner(O outer)
{
I inner = outerToInner(outer);
if (outer != null && inner == null) return prefix(OUTER.getVisualInner(outer));
return INNER.getVisualInner(inner);
}
@Override
public String getVisual(O outer, CommandSender sender)
{

View File

@ -1,12 +1,12 @@
package com.massivecraft.massivecore.command.type.combined;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.AbstractMap.SimpleEntry;
import org.bukkit.command.CommandSender;
@ -14,6 +14,7 @@ import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.TypeAbstract;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.Txt;
public abstract class TypeCombined<T> extends TypeAbstract<T>
@ -46,6 +47,15 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
public String getTypeNameSeparator() { return this.typeNameSeparator; }
public void setTypeNameSeparator(String typeNameSeparator) { this.typeNameSeparator = typeNameSeparator; }
// Visual Mson
private boolean visualMsonNullIncluded = true;
public boolean isVisualMsonNullIncluded() { return this.visualMsonNullIncluded; }
public void setVisualMsonNullIncluded(boolean visualMsonNullIncluded) { this.visualMsonNullIncluded = visualMsonNullIncluded; }
private Mson visualMsonSeparator = Mson.SPACE;
public Mson getVisualMsonSeparator() { return this.visualMsonSeparator; }
public void setVisualMsonSeparator(Mson visualMsonSeparator) { this.visualMsonSeparator = visualMsonSeparator; }
// Visual
private boolean visualNullIncluded = true;
public boolean isVisualNullIncluded() { return this.visualNullIncluded; }
@ -135,6 +145,32 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
return Txt.implode(parts, this.getTypeNameSeparator());
}
// -------------------------------------------- //
// WRITE VISUAL MSON
// -------------------------------------------- //
@SuppressWarnings("unchecked")
@Override
public Mson getVisualMsonInner(T value, CommandSender sender)
{
// Create
List<Mson> parts = new MassiveList<>();
// Fill
List<Entry<Type<?>, Object>> entries = this.splitEntries(value);
for (int i = 0; i < entries.size(); i++)
{
Entry<Type<?>, Object> entry = entries.get(this.getIndexTech(i));
Type<Object> type = (Type<Object>) entry.getKey();
Mson part = type.getVisualMson(entry.getValue(), sender);
if ( ! this.isVisualMsonNullIncluded() && part == null) continue;
parts.add(part);
}
// Return
return Mson.implode(parts, this.getVisualMsonSeparator());
}
// -------------------------------------------- //
// WRITE VISUAL
// -------------------------------------------- //
@ -144,7 +180,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
public String getVisualInner(T value, CommandSender sender)
{
// Create
List<String> parts = new MassiveList<String>();
List<String> parts = new MassiveList<>();
// Fill
List<Entry<Type<?>, Object>> entries = this.splitEntries(value);
@ -169,7 +205,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
public String getNameInner(T value)
{
// Create
List<String> parts = new MassiveList<String>();
List<String> parts = new MassiveList<>();
// Fill
for (Entry<Type<?>, Object> entry : this.splitEntries(value))
@ -193,7 +229,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
public String getIdInner(T value)
{
// Create
List<String> parts = new MassiveList<String>();
List<String> parts = new MassiveList<>();
// Fill
for (Entry<Type<?>, Object> entry : this.splitEntries(value))

View File

@ -48,6 +48,7 @@ public class Mson implements Serializable
public static final Mson SPACE = mson(" ");
public static final Mson EMPTY = mson("");
public static final Mson NEWLINE = mson("\n");
public static final Mson DOT = mson(".");
// -------------------------------------------- //
// GSON