From 6f73837b4fdb7d2d1feabbdda203c27a5031fb22 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 25 Jan 2016 09:48:58 +0100 Subject: [PATCH] Add PredicateVisibleTo and improve PredicateAnd --- .../massivecore/PredicateAnd.java | 23 ++++++----- .../massivecore/PredicateVisibleTo.java | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 src/com/massivecraft/massivecore/PredicateVisibleTo.java diff --git a/src/com/massivecraft/massivecore/PredicateAnd.java b/src/com/massivecraft/massivecore/PredicateAnd.java index 8e2f70a8..1cfa9940 100644 --- a/src/com/massivecraft/massivecore/PredicateAnd.java +++ b/src/com/massivecraft/massivecore/PredicateAnd.java @@ -1,36 +1,35 @@ package com.massivecraft.massivecore; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; +import com.google.common.collect.ImmutableList; + public class PredicateAnd implements Predicate { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @SafeVarargs - public static PredicateAnd get(Predicate... predicates) { return new PredicateAnd(predicates); } + public static PredicateAnd get(Predicate... predicates) { return new PredicateAnd(predicates); } @SafeVarargs - public PredicateAnd(Predicate... predicates) + public PredicateAnd(Predicate... predicates) { - this(Arrays.asList(predicates)); + this(ImmutableList.copyOf(predicates)); } - public static PredicateAnd get(Collection> predicates) { return new PredicateAnd(predicates); } - public PredicateAnd(Collection> predicates) + public static PredicateAnd get(Collection> predicates) { return new PredicateAnd(predicates); } + public PredicateAnd(Collection> predicates) { - this.predicates = Collections.unmodifiableList(new ArrayList>(predicates)); + this.predicates = ImmutableList.copyOf(predicates); } // -------------------------------------------- // // FIELDS // -------------------------------------------- // - private final List> predicates; - public List> getPredicates() { return this.predicates; } + private final List> predicates; + public List> getPredicates() { return this.predicates; } // -------------------------------------------- // // OVERRIDE @@ -39,7 +38,7 @@ public class PredicateAnd implements Predicate @Override public boolean apply(T type) { - for (Predicate predicate : this.getPredicates()) + for (Predicate predicate : this.getPredicates()) { if (!predicate.apply(type)) return false; } diff --git a/src/com/massivecraft/massivecore/PredicateVisibleTo.java b/src/com/massivecraft/massivecore/PredicateVisibleTo.java new file mode 100644 index 00000000..fa72ac4f --- /dev/null +++ b/src/com/massivecraft/massivecore/PredicateVisibleTo.java @@ -0,0 +1,38 @@ +package com.massivecraft.massivecore; +import java.lang.ref.WeakReference; + +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.mixin.Mixin; +import com.massivecraft.massivecore.util.IdUtil; + +public class PredicateVisibleTo implements Predicate +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final WeakReference watcher; + public CommandSender getWatcher() { return this.watcher.get(); } + + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + public static PredicateVisibleTo get(Object watcherObject) { return new PredicateVisibleTo(watcherObject); } + public PredicateVisibleTo(Object watcherObject) + { + this.watcher = new WeakReference<>(IdUtil.getSender(watcherObject)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public boolean apply(Object watcheeObject) + { + return Mixin.isVisible(watcheeObject, this.getWatcher()); + } + +}