Skip to content

Commit

Permalink
eliminate redundants
Browse files Browse the repository at this point in the history
  • Loading branch information
rudde0 committed Jan 15, 2025
1 parent 23cd32b commit d417b96
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 72 deletions.
43 changes: 11 additions & 32 deletions src/main/java/gg/projects/mundoskasync/EffSynchronicity.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package gg.projects.mundoskasync;

import ch.njol.skript.Skript;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;

import java.util.logging.Level;

public class EffSynchronicity extends Effect {

static {
Expand All @@ -21,10 +17,8 @@ public class EffSynchronicity extends Effect {
private boolean isSync;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
isSync = matchedPattern == 1;

getParser().setHasDelayBefore(Kleenean.TRUE);
return true;
}

Expand All @@ -33,38 +27,23 @@ protected void execute(Event e) { }

@Override
public TriggerItem walk(Event e) {
debug(e, true);

Object localVars = Variables.removeLocals(e);
Delay.addDelayedEvent(e);

Runnable runnable = () -> {
try {
if (localVars != null)
Variables.setLocalVariables(e, localVars);

TriggerItem next = getNext();
if (next != null)
walk(next, e);
} catch (Exception ex) {
MundoSKAsync.getInstance().getLogger().log(Level.SEVERE, "Error in synchronicity task", ex);
} finally {
Variables.removeLocals(e);
Runnable task = () -> {
TriggerItem next = getNext();
if (next != null) {
walk(next, e);
}
};

if (isSync)
Scheduling.sync(runnable);
else
Scheduling.async(runnable);

if (isSync) {
Scheduling.sync(task);
} else {
Scheduling.async(task);
}
return null;
}


@Override
public String toString(Event e, boolean debug) {
return isSync ? "sync" : "async";
}

}
19 changes: 11 additions & 8 deletions src/main/java/gg/projects/mundoskasync/EffWaitAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.njol.skript.util.Timespan;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;

import java.util.logging.Level;
Expand Down Expand Up @@ -40,23 +41,26 @@ public TriggerItem walk(Event e) {
Object localVars = Variables.removeLocals(e);

Timespan delay = this.delay.getSingle(e);
if (delay == null)
if (delay == null) {
return null;
}

Scheduling.asyncDelay((int) delay.getTicks_i(), () -> {
Bukkit.getScheduler().runTaskLater(MundoSKAsync.getInstance(), () -> {
try {
if (localVars != null)
if (localVars != null) {
Variables.setLocalVariables(e, localVars);

}
TriggerItem next = getNext();
if (next != null)
if (next != null) {
walk(next, e);
}
} catch (Exception ex) {
MundoSKAsync.getInstance().getLogger().log(Level.SEVERE, "Error in async wait task", ex);
MundoSKAsync.getInstance().getLogger().severe("Error in async wait continuation: " + ex.getMessage());
} finally {
Variables.removeLocals(e);
}
});
}, delay.getTicks_i());

return null;
}

Expand All @@ -65,5 +69,4 @@ public TriggerItem walk(Event e) {
public String toString(Event e, boolean debug) {
return "async wait " + delay.toString(e, debug);
}

}
21 changes: 4 additions & 17 deletions src/main/java/gg/projects/mundoskasync/Scheduling.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,18 @@ public class Scheduling {
private static final BukkitScheduler scheduler = Bukkit.getScheduler();

public static void sync(Runnable runnable) {
scheduler.runTask(MundoSKAsync.getInstance(), wrapWithLogging(runnable, "sync"));
scheduler.runTask(MundoSKAsync.getInstance(), runnable);
}

/*public static void async(Runnable runnable) {
scheduler.runTaskAsynchronously(MundoSKAsync.getInstance(), wrapWithLogging(runnable, "async"));
}*/
public static void async(Runnable runnable) {
TaskExecutor.executeAsync(runnable);
}

public static void syncDelay(long ticks, Runnable runnable) {
scheduler.runTaskLater(MundoSKAsync.getInstance(), wrapWithLogging(runnable, "syncDelay"), ticks);
scheduler.runTaskLater(MundoSKAsync.getInstance(), runnable, ticks);
}

public static void asyncDelay(long ticks, Runnable runnable) {
scheduler.runTaskLaterAsynchronously(MundoSKAsync.getInstance(), wrapWithLogging(runnable, "asyncDelay"), ticks);
scheduler.runTaskLaterAsynchronously(MundoSKAsync.getInstance(), runnable, ticks);
}

private static Runnable wrapWithLogging(Runnable runnable, String context) {
return () -> {
try {
runnable.run();
} catch (Exception e) {
MundoSKAsync.getInstance().getLogger().log(Level.SEVERE, "Error during " + context + " task", e);
}
};
}
}
}
33 changes: 18 additions & 15 deletions src/main/java/gg/projects/mundoskasync/TaskExecutor.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package gg.projects.mundoskasync;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.*;

public class TaskExecutor {
private static final ExecutorService asyncExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(), new CustomThreadFactory("MundoSK-Async")
private static final ExecutorService asyncExecutor = new ThreadPoolExecutor(
Runtime.getRuntime().availableProcessors(),
Runtime.getRuntime().availableProcessors() * 2,
60L, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100),
new CustomThreadFactory("MundoSK-Async")
);

public static void executeAsync(Runnable runnable) {
asyncExecutor.execute(() -> {
try {
runnable.run();
} catch (Exception e) {
MundoSKAsync.getInstance().getLogger().severe("Error in async task: " + e.getMessage());
e.printStackTrace();
}
});
asyncExecutor.execute(runnable);
}

public static void shutdown() {
asyncExecutor.shutdownNow();
asyncExecutor.shutdown();
try {
if (!asyncExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
asyncExecutor.shutdownNow();
}
} catch (InterruptedException e) {
asyncExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
}

private static class CustomThreadFactory implements ThreadFactory {
Expand All @@ -37,4 +40,4 @@ public Thread newThread(Runnable r) {
return new Thread(r, namePrefix + "-Thread-" + count++);
}
}
}
}

0 comments on commit d417b96

Please sign in to comment.