Add PredicateOnlineTo

Provides a predicate for checking if a given SenderEntity is online to a given command sender

Allow priority lines to use mson
This commit is contained in:
TheComputerGeek2 2017-05-21 22:13:12 -07:00 committed by Olof Larsson
parent 1d3738e3a5
commit e9b62e3fbc
3 changed files with 120 additions and 24 deletions

View File

@ -1,12 +1,13 @@
package com.massivecraft.massivecore;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.comparator.ComparatorIdentity;
import com.massivecraft.massivecore.comparator.ComparatorPrioritized;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.FlattenUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
@ -20,30 +21,32 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
@Override public int getPriority() { return this.priority; }
public void setPriority(int priority) { this.priority = priority; }
private List<String> lines;
public List<String> getLines() { return this.lines; }
public void setLines(List<String> lines) { this.lines = lines; }
public void setLines(String... lines) { this.setLines(Arrays.asList(lines)); }
public void setLines(String line) { this.setLines(Collections.singletonList(line)); }
private List<Mson> lines;
public List<String> getLines() { return Mson.toPlain(this.lines, true); }
public void setLines(Object... lines)
{
Collection<Object> linesList = FlattenUtil.flatten(lines);
List<Mson> msonLines = new MassiveList<>();
// Extract the items from the array
for (Object line: linesList)
{
msonLines.add(ensureMson(line));
}
this.lines = msonLines;
}
public void setLine(Object line) { this.setLines(line); };
public List<Mson> getLinesMson() { return new MassiveList<>(this.lines); }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public PriorityLines(int priority, List<String> lines)
public PriorityLines(int priority, Object... lines)
{
this.priority = priority;
this.lines = lines;
}
public PriorityLines(int priority, String... lines)
{
this(priority, Arrays.asList(lines));
}
public PriorityLines(int priority)
{
this(priority, new LinkedList<String>());
this.setLines(lines);
}
// -------------------------------------------- //
@ -71,7 +74,6 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
if (ret != 0) return ret;
ret = ComparatorIdentity.get().compare(this, that);
if (ret != 0) return ret;
// Return
return ret;
@ -80,7 +82,7 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
@Override
public boolean equals(Object object)
{
if ( ! (object instanceof PriorityLines)) return false;
if (!(object instanceof PriorityLines)) return false;
PriorityLines that = (PriorityLines)object;
return MUtil.equals(
this.getPriority(), that.getPriority(),
@ -97,6 +99,15 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
private static Mson ensureMson(Object messageItem)
{
if (messageItem instanceof Mson) return (Mson) messageItem;
if (messageItem instanceof String) return Mson.fromParsedMessage((String) messageItem);
throw new IllegalArgumentException(String.format("messageItem is neither an Mson or String, %s", messageItem));
}
}

View File

@ -0,0 +1,35 @@
package com.massivecraft.massivecore.predicate;
import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.util.IdUtil;
public class PredicateSenderEntityOnlineTo implements Predicate<SenderEntity>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String watcherId;
public String getWatcherId() { return this.watcherId; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static PredicateSenderEntityOnlineTo get(Object watcherObject) { return new PredicateSenderEntityOnlineTo(watcherObject); }
public PredicateSenderEntityOnlineTo(Object watcherObject)
{
this.watcherId = IdUtil.getId(watcherObject);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(SenderEntity watchee)
{
return watchee.isOnline(this.getWatcherId());
}
}

View File

@ -0,0 +1,50 @@
package com.massivecraft.massivecore.util;
import com.massivecraft.massivecore.collections.MassiveList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class FlattenUtil
{
// Did you offer the client varargs?
// Worried about getting an array of a single list instead of an array of the list's contents?
// Throw it in here, we'll flatten it out for you and give you back a nice, nonnull, list.
// NOTE: This should never return null
// No matter what we put it, it should give us a List back
public static List<Object> flatten(Object packed)
{
// Null Check
if (packed == null) return Collections.singletonList(null);
// Is it iterable?
if (packed instanceof Iterable)
{
// Create
List<Object> ret = new MassiveList<>();
// Fill
for (Object item : (Iterable<Object>)packed)
{
// Recurse
ret.addAll(flatten(item));
}
// Return
return ret;
}
// Is it an array?
if (packed.getClass().isArray())
{
// Send it through again as a list
List<Object> asList = Arrays.asList((Object[])packed);
return flatten(asList);
}
// Doesn't seem to hold elements we care about, just return it in a list
return Collections.singletonList(packed);
}
}