Skip to content

Commit

Permalink
🚀 Added a new queue system
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Nov 15, 2021
1 parent e239e33 commit 6a60256
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 28 deletions.
30 changes: 30 additions & 0 deletions src/main/java/info/itsthesky/skriptstorage/api/queue/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package info.itsthesky.skriptstorage.api.queue;

import de.leonhard.storage.internal.FlatFile;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Queue {

private final String path;
private final List<Consumer<FlatFile>> consumers;

public String getPath() {
return path;
}

public List<Consumer<FlatFile>> getConsumers() {
return consumers;
}

public Queue(String path) {
this.path = path;
this.consumers = new ArrayList<>();
}

public void add(Consumer<FlatFile> queue) {
consumers.add(queue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package info.itsthesky.skriptstorage.api.queue;

import java.util.HashMap;

public class QueueManager {

private static final HashMap<String, Queue> QUEUES = new HashMap<>();

public static void addQueue(Queue queue) {
QUEUES.put(queue.getPath(), queue);
}

public static boolean queueExist(String path) {
return QUEUES.containsKey(path);
}

public static Queue parse(String path) {
return QUEUES.getOrDefault(path, null);
}

public static void remove(String path) {
QUEUES.remove(path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import ch.njol.util.Kleenean;
import de.leonhard.storage.internal.FlatFile;
import info.itsthesky.skriptstorage.api.Utils;
import info.itsthesky.skriptstorage.api.queue.Queue;
import info.itsthesky.skriptstorage.api.queue.QueueManager;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("Clear Nodes")
@Description({"Reset the specified file by deleting every comments (if YAML case) and possible nodes values.",
Expand All @@ -37,10 +40,15 @@ protected void execute(@NotNull Event e) {
final String path = CreateShortcut.parse(exprPath.getSingle(e), node);
if (path == null)
return;
final FlatFile data = Utils.parse(path);
if (data == null)
return;
data.clear();
final @Nullable Queue queue = QueueManager.parse(path);
if (queue == null) {
final FlatFile data = Utils.parse(path);
if (data == null)
return;
data.clear();
} else {
queue.add(FlatFile::clear);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import de.leonhard.storage.util.FileUtils;
import info.itsthesky.skriptstorage.api.MultiplyPropertyExpression;
import info.itsthesky.skriptstorage.api.Utils;
import info.itsthesky.skriptstorage.api.queue.Queue;
import info.itsthesky.skriptstorage.api.queue.QueueManager;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -82,6 +84,7 @@ public void change(@NotNull Event e, Object[] delta, Changer.@NotNull ChangeMode
return;
final List<String> comments = Arrays.stream(delta).map(obj -> "#" + obj.toString()).collect(Collectors.toList());
final FlatFile data = Utils.parse(path);
final @Nullable Queue queue = QueueManager.parse(path);
if (data == null)
return;
if (!(data instanceof Yaml))
Expand All @@ -94,20 +97,42 @@ public void change(@NotNull Event e, Object[] delta, Changer.@NotNull ChangeMode
final List<String> current = getComments(key, yaml);
switch (mode) {
case SET:
setComment(key, yaml, comments);
if (queue == null) {
setComment(key, yaml, comments);
} else {
queue.add(file -> setComment(key, (Yaml) file, comments));
}
break;
case ADD:
current.addAll(comments);
setComment(key, yaml, current);
if (queue == null) {
current.addAll(comments);
setComment(key, yaml, current);
} else {
queue.add(file -> {
current.addAll(comments);
setComment(key, (Yaml) file, current);
});
}
break;
case REMOVE:
current.removeAll(comments);
setComment(key, yaml, current);
if (queue == null) {
current.removeAll(comments);
setComment(key, yaml, current);
} else {
queue.add(file -> {
current.removeAll(comments);
setComment(key, (Yaml) file, current);
});
}
break;
case DELETE:
case REMOVE_ALL:
case RESET:
setComment(key, yaml, null);
if (queue == null) {
setComment(key, yaml, null);
} else {
queue.add(file -> setComment(key, (Yaml) file, null));
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
import de.leonhard.storage.internal.FlatFile;
import info.itsthesky.skriptstorage.api.MultiplyPropertyExpression;
import info.itsthesky.skriptstorage.api.Utils;
import info.itsthesky.skriptstorage.api.queue.Queue;
import info.itsthesky.skriptstorage.api.queue.QueueManager;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Name("YAML Headers")
Expand Down Expand Up @@ -53,27 +57,29 @@ public void change(@NotNull Event e, Object[] delta, Changer.@NotNull ChangeMode
final String path = CreateShortcut.parse(getExpr().getSingle(e), node);
if (path == null)
return;
final FlatFile data = Utils.parse(path);
if (!(data instanceof Yaml))
final FlatFile flatFile = Utils.parse(path);
final @Nullable Queue queue = QueueManager.parse(path);
Consumer<FlatFile> consumer = null;
if (!(flatFile instanceof Yaml))
return;
final Yaml yaml = ((Yaml) data);
final Yaml data = ((Yaml) flatFile);

switch (mode) {
case SET:
if (framed) {
yaml.framedHeader(Arrays
consumer = yaml -> ((Yaml) yaml).framedHeader(Arrays
.stream(delta)
.map(Object::toString)
.toArray(String[]::new));
} else {
yaml.setHeader(Arrays
consumer = yaml -> ((Yaml) yaml).setHeader(Arrays
.stream(delta)
.map(Object::toString)
.collect(Collectors.toList()));
}
break;
case ADD:
yaml.addHeader(Arrays
consumer = yaml -> ((Yaml) yaml).addHeader(Arrays
.stream(delta)
.map(Object::toString)
.toArray(String[]::new));
Expand All @@ -82,9 +88,17 @@ public void change(@NotNull Event e, Object[] delta, Changer.@NotNull ChangeMode
case DELETE:
case REMOVE:
case REMOVE_ALL:
yaml.setHeader(new ArrayList<>());
consumer = yaml -> ((Yaml) yaml).setHeader(new ArrayList<>());
break;
}

if (consumer == null)
throw new UnsupportedOperationException();
if (queue == null) {
consumer.accept(data);
} else {
queue.add(consumer);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import de.leonhard.storage.Yaml;
import de.leonhard.storage.internal.FlatFile;
import info.itsthesky.skriptstorage.api.Utils;
import info.itsthesky.skriptstorage.api.queue.Queue;
import info.itsthesky.skriptstorage.api.queue.QueueManager;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;

@Name("Node Value")
@Description({"Represent a node value.",
Expand Down Expand Up @@ -90,32 +93,41 @@ public void change(@NotNull Event e, @Nullable Object[] values, Changer.@NotNull
final String path = CreateShortcut.parse(exprFile.getSingle(e), node);
if (path == null || key == null || values == null || values.length == 0)
return;
final FlatFile data = Utils.parse(path);
if (data == null)
final FlatFile flatFile = Utils.parse(path);
final @Nullable Queue queue = QueueManager.parse(path);
Consumer<FlatFile> consumer = null;
if (flatFile == null)
return;
final boolean single = values.length == 1;
switch (mode) {
case SET:
if (single) {
if (changeDefault) {
data.setDefault(key, (forceList ? Collections.singletonList(values[0]) : values[0]));
consumer = data -> data.setDefault(key, (forceList ? Collections.singletonList(values[0]) : values[0]));
} else {
data.set(key, (forceList ? Collections.singletonList(values[0]) : values[0]));
consumer = data -> data.set(key, (forceList ? Collections.singletonList(values[0]) : values[0]));
}
} else {
if (changeDefault) {
data.setDefault(key, Arrays.asList(values));
consumer = data -> data.setDefault(key, Arrays.asList(values));
} else {
data.set(key, Arrays.asList(values));
consumer = data -> data.set(key, Arrays.asList(values));
}
}
return;
break;
case REMOVE:
data.remove(key);
return;
consumer = data -> data.remove(key);
break;
case REMOVE_ALL:
data.clear();
return;
consumer = FlatFile::clear;
break;
}
if (consumer == null)
throw new UnsupportedOperationException();
if (queue == null) {
consumer.accept(flatFile);
} else {
queue.add(consumer);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package info.itsthesky.skriptstorage.core.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.util.Kleenean;
import info.itsthesky.skriptstorage.api.queue.Queue;
import info.itsthesky.skriptstorage.api.queue.QueueManager;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

@Name("Start Queue")
@Description({"Start a new queue for a specific file path.",
"Every change made with that path will be queued, and not saved directly.",
"You will either need to save the queue (make every save) or clear it (cancel every changes) to return to a normal state.",
"This system is recommended when you are managing a huge quantity of data in a single time."})
@Examples({"start queue for \"config\"",
"enable the queue for \"plugins/test.json\""})
@Since("1.3.0")
public class StartQueue extends Effect {

static {
Skript.registerEffect(
StartQueue.class,
"(enable|start) [the] queue for [the] [path] %string%"
);
}

private Expression<String> exprPath;
private Node node;

@Override
protected void execute(@NotNull Event e) {
final String path = CreateShortcut.parse(exprPath.getSingle(e), node);
if (path == null)
return;
if (QueueManager.queueExist(path)) {
Skript.error("Unable to create a queue for '"+path+"' since there's already one started.");
return;
}
final Queue queue = new Queue(path);
QueueManager.addQueue(queue);
}

@Override
public @NotNull String toString(Event e, boolean debug) {
return "start the queue for " + exprPath.toString(e, debug);
}

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
exprPath = (Expression<String>) exprs[0];
node = SkriptLogger.getNode();
return true;
}
}
Loading

0 comments on commit 6a60256

Please sign in to comment.