Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Test #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public void onLoad() {
Class<? extends CorePlugin> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Scheduler> 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<LocalTime> getStartScheduleTime() {
return Optional.ofNullable(this.startTime);
}

@Override
public Optional<LocalTime> getStartRunnerTime() {
return Optional.ofNullable(this.startTime);
}

@Override
public Optional<LocalTime> 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<Scheduler> getRunner() {
return this.consumer;
}

@Override
public Optional<Thread> getRunning() {
return Optional.ofNullable(this.currentThread);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,11 +120,28 @@ public Optional<ChunkExtent> getChunk(Vector3<Integer> vector) {

@Override
public ChunkExtent loadChunk(Vector3<Integer> vector) {
this.world.loadChunk(vector.getX(), vector.getZ());
Chunk chunk = this.world.getChunkAt(vector.getX(), vector.getZ());
return new BChunkExtent(chunk);
}

@Override
public CompletableFuture<ChunkExtent> loadChunkAsynced(Vector3<Integer> vector) {
try {
//paper specific method
CompletableFuture<Chunk> future = (CompletableFuture<Chunk>) 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<ChunkExtent> future = new CompletableFuture<>();
ChunkExtent extent = this.loadChunk(vector);
future.complete(extent);
return future;
}
}

@Override
public int getMinimumBlockHeight() {
return this.world.getMinHeight();
Expand Down