Skip to content

Commit

Permalink
slightly smarter queue id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 25, 2021
1 parent cfee015 commit f651054
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ public static ScriptQueue getExistingQueue(String id) {
}
}

public static String getNextId(String prefix) {
// DUUIDs v2.1
int size = QueueWordList.FinalWordList.size();
String id = prefix + "_"
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size))
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size))
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size));
// DUUIDs v3.1
/*
String id = prefix.replace(' ', '_')
+ "_"
+ randomEntry(QueueWordList.Pronouns)
+ randomEntry(QueueWordList.Verbs)
+ randomEntry(QueueWordList.Modifiers)
+ randomEntry(QueueWordList.Adjectives)
+ randomEntry(QueueWordList.Nouns);*/
return allQueues.containsKey(id) ? getNextId(prefix) : id;
}

protected static LinkedHashMap<String, ScriptQueue> allQueues = new LinkedHashMap<>();

public static Collection<ScriptQueue> getQueues() {
Expand Down Expand Up @@ -122,7 +103,7 @@ public static boolean queueExists(String id) {

protected ScriptQueue(String id) {
this.id = id;
generateId(id);
generateId(id, 0);
total_queues++;
}

Expand Down Expand Up @@ -213,7 +194,7 @@ public void delayUntil(long delayTime) {
// Public 'functional' methods
//////////////////

public void generateId(String prefix) {
public void generateId(String prefix, int depth) {
if (prefix.startsWith("FORCE:")) {
id = prefix.substring("FORCE:".length());
debugId = id;
Expand All @@ -224,16 +205,19 @@ public void generateId(String prefix) {
Random random = CoreUtilities.getRandom();
String wordOne = QueueWordList.FinalWordList.get(random.nextInt(size));
String wordTwo = QueueWordList.FinalWordList.get(random.nextInt(size));
String wordThree = QueueWordList.FinalWordList.get(random.nextInt(size));
id = prefix + "_" + wordOne + wordTwo + wordThree;
if (queueExists(id)) {
generateId(prefix);
return;
}
String colorOne = DenizenCore.getImplementation().getRandomColor();
String colorTwo = DenizenCore.getImplementation().getRandomColor();
String colorThree = DenizenCore.getImplementation().getRandomColor();
debugId = prefix + "_" + colorOne + wordOne + colorTwo + wordTwo + colorThree + wordThree;
id = prefix + "_" + wordOne + wordTwo;
debugId = "<LG>" + prefix + "_" + colorOne + wordOne + colorTwo + wordTwo;
for (int i = 0; i < depth; i++) {
String wordThree = QueueWordList.FinalWordList.get(random.nextInt(size));
String colorThree = DenizenCore.getImplementation().getRandomColor();
id += wordThree;
debugId += colorThree + wordThree;
}
if (queueExists(id)) {
generateId(prefix, depth + 1);
}
}

public ScriptQueue replacementQueue = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,6 @@

public class InstantQueue extends ScriptQueue {

/**
* Gets an InstantQueue instance.
* <p/>
* If a queue already exists with the given id, it will return that instance,
* which may be currently running, unless the type of Queue is not an InstantQueue.
* If a queue does not exist, a new stopped queue is created instead.
* <p/>
* IDs are case insensitive. If having an easy-to-recall ID is not necessary, just
* pass along null as the id, and it will use ScriptQueue's static method _getNextId()
* which will return a random UUID.
* <p/>
* The default speed node will be automatically read from the configuration,
* and new ScriptQueues may need further information before they
* can start(), including entries, delays, loops, and possibly context.
*
* @param id unique id of the queue
* @return a ScriptQueue
*/
public static InstantQueue getQueue(String id) {
// Get id if not specified.
if (id == null) {
throw new IllegalArgumentException("ID cannot be null!");
}
InstantQueue scriptQueue;
// Does the queue already exist?
if (queueExists(id)) {
scriptQueue = (InstantQueue) allQueues.get(id);
}
// If not, create a new one.
else {
scriptQueue = new InstantQueue(id);
}
return scriptQueue;
}

/////////////////////
// Private instance fields and constructors
/////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.QueueWordList;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
Expand Down Expand Up @@ -156,9 +156,12 @@ else if (attribute.startsWith("uuid")) {
// Optionally specify the source context to base the value on.
// -->
else if (attribute.startsWith("duuid")) {
event.setReplacedObject(CoreUtilities.autoAttrib(new ElementTag(
ScriptQueue.getNextId(attribute.hasContext(1) ? attribute.getContext(1) : "DUUID")),
attribute.fulfill(1)));
int size = QueueWordList.FinalWordList.size();
String id = (attribute.hasContext(1) ? attribute.getContext(1) : "DUUID") + "_"
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size))
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size))
+ QueueWordList.FinalWordList.get(CoreUtilities.getRandom().nextInt(size));
event.setReplacedObject(CoreUtilities.autoAttrib(new ElementTag(id), attribute.fulfill(1)));
}
}

Expand Down

0 comments on commit f651054

Please sign in to comment.