Skip to content

Commit

Permalink
enters area_flagged
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 25, 2021
1 parent 6b76fbc commit 20388b5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
Expand Up @@ -5,6 +5,8 @@
import com.denizenscript.denizen.objects.notable.NotableManager;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.Notable;
Expand Down Expand Up @@ -103,6 +105,12 @@ else if (areaName.equals("ellipsoid")) {
return false;
}
}
else if (areaName.startsWith("area_flagged:")) {
AbstractFlagTracker tracker = ((FlaggableObject) area).getFlagTracker();
if (tracker == null || !tracker.hasFlag(areaName.substring("area_flagged:".length()))) {
return false;
}
}
else {
if (!runGenericCheck(areaName, area.getNoteName())) {
return false;
Expand Down Expand Up @@ -184,6 +192,7 @@ public void init() {
boolean needsMatchers = false;
HashSet<String> exacts = new HashSet<>();
List<MatchHelper> matchList = new ArrayList<>();
HashSet<String> flags = new HashSet<>();
onlyTrackPlayers = true;
for (ScriptPath path : eventPaths) {
if (!path.eventArgLowerAt(0).equals("player")) {
Expand All @@ -193,10 +202,7 @@ public void init() {
if (area.equals("notable")) {
area = path.eventArgLowerAt(3);
}
if (area.equals("cuboid")) {
doTrackAll = true;
}
else if (area.equals("ellipsoid")) {
if (area.equals("cuboid") || area.equals("ellipsoid") || area.equals("polygon")) {
doTrackAll = true;
}
MatchHelper matcher = createMatcher(area);
Expand All @@ -210,14 +216,19 @@ else if (!needsMatchers && (matcher instanceof ExactMatchHelper)) {
needsMatchers = true;
}
matchList.add(matcher);
if (area.startsWith("area_flagged:")) {
flags.add(CoreUtilities.toLowerCase(area.substring("area_flagged:".length())));
}
}
exactTracked = needsMatchers ? null : exacts.toArray(new String[0]);
matchers = needsMatchers ? matchList.toArray(new MatchHelper[0]) : null;
flagTracked = flags.size() > 0 ? flags.toArray(new String[0]) : null;
registerCorrectClass();
}

public boolean doTrackAll = false;
public String[] exactTracked = null;
public String[] flagTracked = null;
public MatchHelper[] matchers = null;
public boolean onlyTrackPlayers = true;
public static HashMap<UUID, HashSet<String>> entitiesInArea = new HashMap<>();
Expand All @@ -236,13 +247,23 @@ public void cancellationChanged() {
super.cancellationChanged();
}

public boolean anyMatch(String name) {
public boolean anyMatch(String name, FlaggableObject flaggable) {
if (doTrackAll) {
return true;
}
for (MatchHelper matcher : matchers) {
if (matcher.doesMatch(name)) {
return true;
if (matchers != null) {
for (MatchHelper matcher : matchers) {
if (matcher.doesMatch(name)) {
return true;
}
}
}
if (flagTracked != null) {
for (String flag : flagTracked) {
AbstractFlagTracker tracker = flaggable.getFlagTracker();
if (tracker != null && tracker.hasFlag(flag)) {
return true;
}
}
}
return false;
Expand Down Expand Up @@ -276,19 +297,19 @@ public void processNewPosition(EntityTag entity, Location pos, Event eventCause)
return;
}
HashSet<String> inAreas = entitiesInArea.get(entity.getUUID());
if (doTrackAll || matchers != null) {
if (doTrackAll || matchers != null || flagTracked != null) {
for (CuboidTag cuboid : NotableManager.getAllType(CuboidTag.class)) {
if (anyMatch(cuboid.noteName)) {
if (anyMatch(cuboid.noteName, cuboid)) {
processSingle(cuboid, entity, inAreas, pos, eventCause);
}
}
for (EllipsoidTag ellipsoid : NotableManager.getAllType(EllipsoidTag.class)) {
if (anyMatch(ellipsoid.noteName)) {
if (anyMatch(ellipsoid.noteName, ellipsoid)) {
processSingle(ellipsoid, entity, inAreas, pos, eventCause);
}
}
for (PolygonTag polygon : NotableManager.getAllType(PolygonTag.class)) {
if (anyMatch(polygon.noteName)) {
if (anyMatch(polygon.noteName, polygon)) {
processSingle(polygon, entity, inAreas, pos, eventCause);
}
}
Expand Down
Expand Up @@ -423,7 +423,7 @@ public static boolean matches(String arg) {
if (ScriptRegistry.containsScript(tid, InventoryScriptContainer.class)) {
return true;
}
if (NotableManager.isType(tid, InventoryTag.class)) {
if (NotableManager.getSavedObject(tid) instanceof InventoryTag) {
return true;
}
for (String idType : idTypes) {
Expand Down
Expand Up @@ -19,10 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -38,8 +35,8 @@ public NotableManager() {
}

public static HashMap<String, Notable> notableObjects = new HashMap<>();
public static HashMap<String, Class> typeTracker = new HashMap<>();
public static HashMap<Notable, String> reverseObjects = new HashMap<>();
public static HashMap<Class, HashSet<Notable>> notesByType = new HashMap<>();

public static boolean isSaved(Notable object) {
return reverseObjects.containsKey(object);
Expand All @@ -61,18 +58,13 @@ public static String getSavedId(Notable object) {
return reverseObjects.get(object);
}

public static boolean isType(String id, Class type) {
Class trackedType = typeTracker.get(CoreUtilities.toLowerCase(id));
return trackedType == type;
}

public static void saveAs(Notable object, String id) {
if (object == null) {
return;
}
notableObjects.put(CoreUtilities.toLowerCase(id), object);
reverseObjects.put(object, CoreUtilities.toLowerCase(id));
typeTracker.put(CoreUtilities.toLowerCase(id), object.getClass());
notesByType.get(object.getClass()).add(object);
}

public static Notable remove(String id) {
Expand All @@ -82,35 +74,29 @@ public static Notable remove(String id) {
}
notableObjects.remove(CoreUtilities.toLowerCase(id));
reverseObjects.remove(obj);
typeTracker.remove(CoreUtilities.toLowerCase(id));
notesByType.get(obj.getClass()).remove(obj);
return obj;
}

public static void remove(Notable obj) {
String id = reverseObjects.get(obj);
notableObjects.remove(CoreUtilities.toLowerCase(id));
reverseObjects.remove(obj);
typeTracker.remove(CoreUtilities.toLowerCase(id));
notesByType.get(obj.getClass()).remove(obj);
}

public static <T extends ObjectTag> List<T> getAllType(Class<T> type) {
List<T> objects = new ArrayList<>();
for (Map.Entry<String, Notable> notable : notableObjects.entrySet()) {
// dB.log(notable.toString());
if (isType(notable.getKey(), type)) {
objects.add((T) notable.getValue());
}
}

return objects;
public static <T extends Notable> Set<T> getAllType(Class<T> type) {
return (Set<T>) notesByType.get(type);
}

/**
* Called on '/denizen reload notables'.
*/
private static void _recallNotables() {
notableObjects.clear();
typeTracker.clear();
for (Set set : notesByType.values()) {
set.clear();
}
reverseObjects.clear();
// Find each type of notable
for (String key : Denizen.getInstance().notableManager().getNotables().getKeys(false)) {
Expand Down Expand Up @@ -222,6 +208,7 @@ public static void registerWithNotableManager(Class notable) {
String note = method.getAnnotation(Note.class).value();
objects.put(notable, note);
reverse_objects.put(note, notable);
notesByType.put(notable, new HashSet<>());
}
}
}
Expand Down

0 comments on commit 20388b5

Please sign in to comment.