From 08cbdf46e253f6bc5e5e7cda977857720b6e4062 Mon Sep 17 00:00:00 2001 From: mosemister Date: Mon, 2 Jan 2023 16:30:06 +0000 Subject: [PATCH 1/2] Fixed entity getter --- .../scheduler/BInstanceThreadScheduler.java | 78 +++++++++++++++++++ .../bukkit/scheduler/BSchedulerBuilder.java | 7 +- .../bukkit/world/BWorldExtent.java | 21 ++++- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/core/implementation/bukkit/scheduler/BInstanceThreadScheduler.java diff --git a/src/main/java/org/core/implementation/bukkit/scheduler/BInstanceThreadScheduler.java b/src/main/java/org/core/implementation/bukkit/scheduler/BInstanceThreadScheduler.java new file mode 100644 index 0000000..bda4f51 --- /dev/null +++ b/src/main/java/org/core/implementation/bukkit/scheduler/BInstanceThreadScheduler.java @@ -0,0 +1,78 @@ +package org.core.implementation.bukkit.scheduler; + +import org.core.platform.plugin.Plugin; +import org.core.schedule.Scheduler; +import org.core.schedule.SchedulerBuilder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.LocalTime; +import java.util.Optional; +import java.util.function.Consumer; + +public class BInstanceThreadScheduler implements Scheduler.Threaded { + + private @Nullable LocalTime startTime; + private @Nullable LocalTime endTime; + private final @NotNull String displayName; + private final @NotNull Plugin plugin; + private final @NotNull Consumer consumer; + private @Nullable Thread currentThread; + + public BInstanceThreadScheduler(@NotNull SchedulerBuilder builder, @NotNull Plugin plugin) { + this.consumer = builder.getRunner(); + this.plugin = plugin; + this.displayName = builder.getDisplayName().orElseThrow(() -> new RuntimeException("No Displayname")); + } + + + @Override + public Optional getStartScheduleTime() { + return Optional.ofNullable(this.startTime); + } + + @Override + public Optional getStartRunnerTime() { + return Optional.ofNullable(this.startTime); + } + + @Override + public Optional getEndTime() { + return Optional.ofNullable(this.endTime); + } + + @Override + public boolean isAsync() { + return true; + } + + @Override + public String getDisplayName() { + return this.displayName; + } + + @Override + public Plugin getPlugin() { + return this.plugin; + } + + @Override + public void run() { + this.currentThread = new Thread(() -> { + this.consumer.accept(this); + this.endTime = LocalTime.now(); + }); + this.startTime = LocalTime.now(); + this.currentThread.start(); + } + + @Override + public Consumer getRunner() { + return this.consumer; + } + + @Override + public Optional getRunning() { + return Optional.ofNullable(this.currentThread); + } +} diff --git a/src/main/java/org/core/implementation/bukkit/scheduler/BSchedulerBuilder.java b/src/main/java/org/core/implementation/bukkit/scheduler/BSchedulerBuilder.java index cf20e06..4148ec0 100644 --- a/src/main/java/org/core/implementation/bukkit/scheduler/BSchedulerBuilder.java +++ b/src/main/java/org/core/implementation/bukkit/scheduler/BSchedulerBuilder.java @@ -116,7 +116,12 @@ public Scheduler build(Plugin plugin) { if (this.delay != null && this.delayUnit == null) { throw new IllegalArgumentException("Invalid delayUnit in build"); } - Scheduler scheduler = new BNativeScheduler(this, plugin); + Scheduler scheduler; + if ((this.delay == null || this.delay == 0) && this.iteration == null && this.async) { + scheduler = new BInstanceThreadScheduler(this, plugin); + } else { + scheduler = new BNativeScheduler(this, plugin); + } ((BScheduleManager) TranslateCore.getScheduleManager()).register(scheduler); return scheduler; } diff --git a/src/main/java/org/core/implementation/bukkit/world/BWorldExtent.java b/src/main/java/org/core/implementation/bukkit/world/BWorldExtent.java index 8074c40..0e3e18e 100644 --- a/src/main/java/org/core/implementation/bukkit/world/BWorldExtent.java +++ b/src/main/java/org/core/implementation/bukkit/world/BWorldExtent.java @@ -18,10 +18,12 @@ import org.core.world.position.impl.sync.SyncBlockPosition; import org.core.world.position.impl.sync.SyncExactPosition; +import java.lang.reflect.InvocationTargetException; import java.util.HashSet; import java.util.Optional; import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -118,11 +120,28 @@ public Optional getChunk(Vector3 vector) { @Override public ChunkExtent loadChunk(Vector3 vector) { - this.world.loadChunk(vector.getX(), vector.getZ()); Chunk chunk = this.world.getChunkAt(vector.getX(), vector.getZ()); return new BChunkExtent(chunk); } + @Override + public CompletableFuture loadChunkAsynced(Vector3 vector) { + try { + //paper specific method + CompletableFuture future = (CompletableFuture) this.world + .getClass() + .getMethod("getChunkAtAsync", int.class, int.class) + .invoke(this.world, vector.getX(), vector.getZ()); + return future.thenApply(BChunkExtent::new); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + //fallback to bukkit + CompletableFuture future = new CompletableFuture<>(); + ChunkExtent extent = this.loadChunk(vector); + future.complete(extent); + return future; + } + } + @Override public int getMinimumBlockHeight() { return this.world.getMinHeight(); From 316d87f595a072bdfaaac75618817196efbbceb9 Mon Sep 17 00:00:00 2001 From: mosemister Date: Tue, 3 Jan 2023 16:29:10 +0000 Subject: [PATCH 2/2] Fixed running on Spigot- servers --- .../bukkit/platform/plugin/boot/TranslateCoreBoot.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/core/implementation/bukkit/platform/plugin/boot/TranslateCoreBoot.java b/src/main/java/org/core/implementation/bukkit/platform/plugin/boot/TranslateCoreBoot.java index ea853ef..3a76989 100644 --- a/src/main/java/org/core/implementation/bukkit/platform/plugin/boot/TranslateCoreBoot.java +++ b/src/main/java/org/core/implementation/bukkit/platform/plugin/boot/TranslateCoreBoot.java @@ -68,12 +68,12 @@ public void onLoad() { Class pluginClass = opLauncher.get(); CorePlugin plugin = CommonLoad.loadStandAlonePlugin(pluginClass); - Logger logger = new BJavaLogger(this.getLogger()); + Logger logger; try { Method method = JavaPlugin.class.getMethod("getSLF4JLogger"); logger = new BSLF4JLogger(method.invoke(this)); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - throw new RuntimeException(e); + logger = new BJavaLogger(this.getLogger()); } plugin.onConstruct(this, logger);