Skip to content

Commit

Permalink
notable system improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 7, 2019
1 parent 0f0c9c8 commit 950711b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 69 deletions.
Expand Up @@ -498,10 +498,9 @@ public boolean tryInventory(InventoryTag inv, String comparedto) {
if (inv.scriptName != null && equalityCheck(inv.scriptName, comparedto, regexd)) {
return true;
}
if (NotableManager.isSaved(inv)) {
if (equalityCheck(NotableManager.getSavedId(inv), comparedto, regexd)) {
return true;
}
String notedId = NotableManager.getSavedId(inv);
if (notedId != null && equalityCheck(notedId, comparedto, regexd)) {
return true;
}
return false;
}
Expand Down
Expand Up @@ -170,8 +170,9 @@ else if (string.contains(",")) {
}
}

if (NotableManager.isType(string, CuboidTag.class)) {
return (CuboidTag) NotableManager.getSavedObject(string);
Notable noted = NotableManager.getSavedObject(string);
if (noted instanceof CuboidTag) {
return (CuboidTag) noted;
}

if (context == null || context.debug) {
Expand Down
Expand Up @@ -75,8 +75,9 @@ public static EllipsoidTag valueOf(String string, TagContext context) {
string = string.substring(10);
}

if (NotableManager.isType(string, EllipsoidTag.class)) {
return (EllipsoidTag) NotableManager.getSavedObject(string);
Notable noted = NotableManager.getSavedObject(string);
if (noted instanceof EllipsoidTag) {
return (EllipsoidTag) noted;
}

List<String> split = CoreUtilities.split(string, ',');
Expand Down
Expand Up @@ -113,8 +113,9 @@ public static ItemTag valueOf(String string, TagContext context) {
return ObjectFetcher.getObjectFrom(ItemTag.class, string, context);
}

if (NotableManager.isSaved(string) && NotableManager.isType(string, ItemTag.class)) {
return (ItemTag) NotableManager.getSavedObject(string);
Notable noted = NotableManager.getSavedObject(string);
if (noted instanceof ItemTag) {
return (ItemTag) noted;
}
if (string.startsWith("i@")) {
string = string.substring("i@".length());
Expand Down
Expand Up @@ -174,8 +174,9 @@ public static LocationTag valueOf(String string, TagContext context) {
string = string.substring(2);
}

if (NotableManager.isSaved(string) && NotableManager.isType(string, LocationTag.class)) {
return (LocationTag) NotableManager.getSavedObject(string);
Notable noted = NotableManager.getSavedObject(string);
if (noted instanceof LocationTag) {
return (LocationTag) noted;
}

////////
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -36,58 +35,35 @@ public NotableManager() {
registerWithNotableManager(LocationTag.class);
}


public static Map<String, Notable>
notableObjects = new ConcurrentHashMap<>();
public static Map<String, Class>
typeTracker = new ConcurrentHashMap<>();
public static Map<Notable, String>
reverseObjects = new ConcurrentHashMap<>();


public static boolean isSaved(String id) {
return notableObjects.containsKey(CoreUtilities.toLowerCase(id));
}

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 boolean isSaved(Notable object) {
return reverseObjects.containsKey(object);
}


public static boolean isExactSavedObject(Notable object) {
String id = reverseObjects.get(object);
if (id == null) {
return false;
}
// Reference equality
return notableObjects.get(id) == object;
}

public static Notable getSavedObject(String id) {
if (notableObjects.containsKey(CoreUtilities.toLowerCase(id))) {
return notableObjects.get(CoreUtilities.toLowerCase(id));
}
else {
return null;
}
return notableObjects.get(CoreUtilities.toLowerCase(id));
}


public static String getSavedId(Notable object) {
if (reverseObjects.containsKey(object)) {
return reverseObjects.get(object);
}
return null;
return reverseObjects.get(object);
}


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


public static void saveAs(Notable object, String id) {
if (object == null) {
return;
Expand All @@ -97,12 +73,15 @@ public static void saveAs(Notable object, String id) {
typeTracker.put(CoreUtilities.toLowerCase(id), object.getClass());
}


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

public static void remove(Notable obj) {
Expand All @@ -128,23 +107,16 @@ public static <T extends ObjectTag> List<T> getAllType(Class<T> type) {
* Called on '/denizen reload notables'.
*/
private static void _recallNotables() {

notableObjects.clear();
typeTracker.clear();
reverseObjects.clear();

// Find each type of notable
for (String key : DenizenAPI.getCurrentInstance().notableManager().getNotables().getKeys(false)) {

Class<? extends ObjectTag> clazz = reverse_objects.get(key);

ConfigurationSection section = DenizenAPI.getCurrentInstance().notableManager().getNotables()
.getConfigurationSection(key);

ConfigurationSection section = DenizenAPI.getCurrentInstance().notableManager().getNotables().getConfigurationSection(key);
if (section == null) {
continue;
}

for (String notableRaw : section.getKeys(false)) {
String notable = EscapeTagBase.unEscape(notableRaw.replace("DOT", "."));
Notable obj = (Notable) ObjectFetcher.getObjectFrom(clazz, section.getString(notableRaw), CoreUtilities.noDebugContext);
Expand All @@ -155,21 +127,17 @@ private static void _recallNotables() {
Debug.echoError("Notable '" + notable + "' failed to load!");
}
}

}

}

/**
* Called on by '/denizen save'.
*/
private static void _saveNotables() {

FileConfiguration notables = DenizenAPI.getCurrentInstance().notableManager().getNotables();
for (String key : notables.getKeys(false)) {
notables.set(key, null);
}

for (Map.Entry<String, Notable> notable : notableObjects.entrySet()) {

try {
Expand Down Expand Up @@ -246,17 +214,8 @@ public static void registerWithNotableManager(Class notable) {
}
}

public static boolean canFetch(Class notable) {
return objects.containsKey(notable);
}

public static String getClassId(Class notable) {
if (canFetch(notable)) {
return objects.get(notable);
}
else {
return null;
}
return objects.get(notable);
}

public static Map<String, Class> getReverseClassIdMap() {
Expand Down
Expand Up @@ -91,8 +91,7 @@ public void execute(ScriptEntry scriptEntry) {
}

if (remove.asBoolean()) {
if (NotableManager.isSaved(id.asString())) {
NotableManager.remove(id.asString());
if (NotableManager.remove(id.asString()) != null) {
Debug.echoDebug(scriptEntry, "notable '" + id.asString() + "' removed");
}
else {
Expand Down

0 comments on commit 950711b

Please sign in to comment.