Skip to content

Commit

Permalink
More more more work for paper-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Jun 28, 2023
1 parent eb95472 commit 472dd5f
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 101 deletions.
21 changes: 11 additions & 10 deletions bundle/src/main/java/net/okocraft/box/bootstrap/BoxBootstrap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.okocraft.box.bootstrap;

import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import net.okocraft.box.bundle.BuiltinFeatures;
Expand All @@ -24,27 +25,27 @@ public final class BoxBootstrap implements PluginBootstrap {
}

@MonotonicNonNull
private BootstrapContext bootstrapContext;
private BoxBootstrapContext boxBootstrapContext;

@Override
public void bootstrap(@NotNull PluginProviderContext context) {
public void bootstrap(@NotNull BootstrapContext context) {
BoxBootstrap.instance = this;
bootstrapContext = BootstrapContext.create(context);
boxBootstrapContext = BoxBootstrapContext.create(context);

BuiltinFeatures.addToContext(bootstrapContext);
BuiltinStorages.addToRegistry(bootstrapContext.getStorageRegistry());
BuiltinFeatures.addToContext(boxBootstrapContext);
BuiltinStorages.addToRegistry(boxBootstrapContext.getStorageRegistry());

bootstrapContext.onLanguageDirectoryCreated().add(directory -> BuiltinTranslations.saveDefaultTranslationFiles(bootstrapContext.getJarFile(), directory));
bootstrapContext.getTranslationLoaderCreators().addCreator(locale -> BuiltinTranslations.loadDefaultTranslation(bootstrapContext.getJarFile(), locale));
boxBootstrapContext.onLanguageDirectoryCreated().add(directory -> BuiltinTranslations.saveDefaultTranslationFiles(boxBootstrapContext.getJarFile(), directory));
boxBootstrapContext.getTranslationLoaderCreators().addCreator(locale -> BuiltinTranslations.loadDefaultTranslation(boxBootstrapContext.getJarFile(), locale));
}

@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) { // PluginProviderContext is immutable, so this argument can be ignored.
BoxBootstrap.instance = null;
return new BoxPlugin(bootstrapContext);
return new BoxPlugin(boxBootstrapContext);
}

public @NotNull BootstrapContext getBootstrapContext() { // This method exists for external access.
return bootstrapContext;
public @NotNull BoxBootstrapContext getBootstrapContext() { // This method exists for external access.
return boxBootstrapContext;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.okocraft.box.bootstrap;

import com.github.siroshun09.event4j.bus.EventBus;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.okocraft.box.api.event.BoxEvent;
import net.okocraft.box.api.feature.BoxFeature;
import net.okocraft.box.compatible.PlatformDependent;
import net.okocraft.box.core.util.executor.ExecutorProvider;
import net.okocraft.box.storage.api.registry.StorageRegistry;
import net.okocraft.box.util.TranslationDirectoryUtil;
Expand All @@ -15,12 +17,12 @@
import java.util.ArrayList;
import java.util.List;

public final class BootstrapContext {
public final class BoxBootstrapContext {

@Contract("_ -> new")
@SuppressWarnings("UnstableApiUsage")
public static @NotNull BootstrapContext create(@NotNull PluginProviderContext context) {
return new BootstrapContext(
public static @NotNull BoxBootstrapContext create(@NotNull BootstrapContext context) {
return new BoxBootstrapContext(
context.getDataDirectory(),
context.getLogger(),
context.getPluginSource(),
Expand All @@ -39,13 +41,13 @@ public final class BootstrapContext {
private final TranslationDirectoryUtil.PathConsumerWrapper onLanguageDirectoryCreated;
private final TranslationDirectoryUtil.TranslationLoaderCreatorHolder translationLoaderCreators;

private BootstrapContext(@NotNull Path pluginDirectory, @NotNull ComponentLogger logger, @NotNull Path jarFile, @NotNull String version) {
private BoxBootstrapContext(@NotNull Path pluginDirectory, @NotNull ComponentLogger logger, @NotNull Path jarFile, @NotNull String version) {
this.dataDirectory = pluginDirectory;
this.logger = logger;
this.jarFile = jarFile;
this.version = version;
this.storageRegistry = new StorageRegistry();
this.executorProvider = new ExecutorProvider(logger);
this.executorProvider = PlatformDependent.createExecutorProvider(logger);
this.eventBus = EventBus.create(BoxEvent.class, executorProvider.newSingleThreadExecutor("Event"));
this.onLanguageDirectoryCreated = TranslationDirectoryUtil.createPathConsumer();
this.translationLoaderCreators = TranslationDirectoryUtil.createCreatorHolder();
Expand Down Expand Up @@ -84,7 +86,7 @@ private BootstrapContext(@NotNull Path pluginDirectory, @NotNull ComponentLogger
}

@Contract("_ -> this")
public @NotNull BootstrapContext addFeature(@NotNull BoxFeature feature) {
public @NotNull BoxBootstrapContext addFeature(@NotNull BoxFeature feature) {
boxFeatureList.add(feature);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.okocraft.box.bundle;

import net.okocraft.box.bootstrap.BootstrapContext;
import net.okocraft.box.bootstrap.BoxBootstrapContext;
import net.okocraft.box.feature.autostore.AutoStoreFeature;
import net.okocraft.box.feature.bemode.BEModeFeature;
import net.okocraft.box.feature.category.CategoryFeature;
Expand All @@ -13,7 +13,7 @@

public final class BuiltinFeatures {

public static void addToContext(@NotNull BootstrapContext context) {
public static void addToContext(@NotNull BoxBootstrapContext context) {
context.addFeature(new CommandFeature())
.addFeature(new CategoryFeature())
.addFeature(new GuiFeature())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.okocraft.box.compatible;

import net.okocraft.box.api.util.Folia;
import net.okocraft.box.core.util.executor.ExecutorProvider;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

public final class PlatformDependent {

public static @NotNull ExecutorProvider createExecutorProvider(@NotNull Logger logger) {
if (Folia.check()) {

} else {

}
throw new UnsupportedOperationException("Not implemented yet");
}

private PlatformDependent() {
throw new UnsupportedOperationException();
}
}
22 changes: 11 additions & 11 deletions bundle/src/main/java/net/okocraft/box/plugin/BoxPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.siroshun09.configapi.yaml.YamlConfiguration;
import net.okocraft.box.api.feature.BoxFeature;
import net.okocraft.box.bootstrap.BootstrapContext;
import net.okocraft.box.bootstrap.BoxBootstrapContext;
import net.okocraft.box.core.BoxCore;
import net.okocraft.box.core.PluginContext;
import net.okocraft.box.storage.migrator.StorageMigrator;
Expand All @@ -24,20 +24,20 @@ public final class BoxPlugin extends JavaPlugin {

private Status status = Status.NOT_LOADED;

public BoxPlugin(@NotNull BootstrapContext bootstrapContext) {
public BoxPlugin(@NotNull BoxBootstrapContext boxBootstrapContext) {
this.pluginContext = new PluginContext(
this,
bootstrapContext.getVersion(),
bootstrapContext.getPluginDirectory(),
bootstrapContext.getJarFile(),
bootstrapContext.getExecutorProvider(),
bootstrapContext.getEventBus(),
YamlConfiguration.create(bootstrapContext.getPluginDirectory().resolve("config.yml")),
TranslationDirectoryUtil.fromContext(bootstrapContext),
bootstrapContext.getStorageRegistry()
boxBootstrapContext.getVersion(),
boxBootstrapContext.getPluginDirectory(),
boxBootstrapContext.getJarFile(),
boxBootstrapContext.getExecutorProvider(),
boxBootstrapContext.getEventBus(),
YamlConfiguration.create(boxBootstrapContext.getPluginDirectory().resolve("config.yml")),
TranslationDirectoryUtil.fromContext(boxBootstrapContext),
boxBootstrapContext.getStorageRegistry()
);
this.boxCore = new BoxCore(pluginContext);
this.preregisteredFeatures = bootstrapContext.getBoxFeatureList();
this.preregisteredFeatures = boxBootstrapContext.getBoxFeatureList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.siroshun09.translationloader.directory.TranslationLoaderCreator;
import com.github.siroshun09.translationloader.util.PathConsumer;
import net.kyori.adventure.key.Key;
import net.okocraft.box.bootstrap.BootstrapContext;
import net.okocraft.box.bootstrap.BoxBootstrapContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -25,7 +25,7 @@ public final class TranslationDirectoryUtil {
return new TranslationLoaderCreatorHolder();
}

public static @NotNull TranslationDirectory fromContext(@NotNull BootstrapContext context) {
public static @NotNull TranslationDirectory fromContext(@NotNull BoxBootstrapContext context) {
return TranslationDirectory.newBuilder()
.setDirectory(context.getPluginDirectory().resolve("languages"))
.setKey(Key.key("box", "language"))
Expand Down
10 changes: 3 additions & 7 deletions core/src/main/java/net/okocraft/box/core/BoxCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class BoxCore implements BoxAPI {
private final YamlConfiguration configuration;
private final TranslationDirectory translationDirectory;

private final BoxTaskFactory taskFactory = new BoxTaskFactory();
private final BoxTaskFactory taskFactory;

private final BoxCommandImpl boxCommand = new BoxCommandImpl();
private final BoxAdminCommandImpl boxAdminCommand = new BoxAdminCommandImpl();
Expand All @@ -90,6 +90,7 @@ public BoxCore(@NotNull PluginContext context) {

this.configuration = context.configuration();
this.translationDirectory = context.translationDirectory();
this.taskFactory = new BoxTaskFactory(context.executorProvider());

BoxProvider.set(this);
}
Expand Down Expand Up @@ -231,12 +232,7 @@ public void disable() {

getLogger().info("Shutting down executors...");

try {
taskFactory.shutdown();
context.executorProvider().close();
} catch (InterruptedException e) {
getLogger().log(Level.SEVERE, "Could not shutdown executors", e);
}
context.executorProvider().close(context.plugin());

getLogger().info("Closing the storage...");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
package net.okocraft.box.core.taskfactory;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.okocraft.box.api.BoxProvider;
import net.okocraft.box.api.taskfactory.TaskFactory;
import net.okocraft.box.api.util.Folia;
import net.okocraft.box.api.util.MCDataVersion;
import org.bukkit.Bukkit;
import net.okocraft.box.core.util.executor.ExecutorProvider;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;

public class BoxTaskFactory implements TaskFactory {

private final ExecutorService executor = Executors.newFixedThreadPool(
Math.min(Runtime.getRuntime().availableProcessors(), 4),
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Box Worker - #%d")
.setUncaughtExceptionHandler(this::reportUncaughtException)
.build()
);
private final ExecutorProvider executorProvider;

public BoxTaskFactory(@NotNull ExecutorProvider executorProvider) {
this.executorProvider = executorProvider;
}

@Override
public @NotNull CompletableFuture<Void> run(@NotNull Runnable task) {
Objects.requireNonNull(task);
return CompletableFuture.runAsync(task, getGlobalExecutor());
return CompletableFuture.runAsync(task, executorProvider.getGlobalExecutor());
}

@Override
Expand All @@ -46,69 +36,29 @@ public class BoxTaskFactory implements TaskFactory {
@Override
public @NotNull <T> CompletableFuture<T> supply(@NotNull Supplier<T> supplier) {
Objects.requireNonNull(supplier);
return CompletableFuture.supplyAsync(supplier, getGlobalExecutor());
return CompletableFuture.supplyAsync(supplier, executorProvider.getGlobalExecutor());
}

@Override
public <E extends Entity, T> @NotNull CompletableFuture<T> supplyFromEntity(@NotNull E entity, @NotNull Function<E, T> function) {
Objects.requireNonNull(entity);
Objects.requireNonNull(function);

return CompletableFuture.supplyAsync(() -> function.apply(entity), getExecutorForEntity(entity));
}

@Override
public @NotNull CompletableFuture<Void> runAsync(@NotNull Runnable task) {
Objects.requireNonNull(task);
return CompletableFuture.runAsync(task, executor);
return CompletableFuture.runAsync(task, executorProvider.getWorker());
}

@Override
public @NotNull <T> CompletableFuture<T> supplyAsync(@NotNull Supplier<T> supplier) {
Objects.requireNonNull(supplier);
return CompletableFuture.supplyAsync(supplier, executor);
}

public void shutdown() throws InterruptedException {
executor.shutdown();

//noinspection ResultOfMethodCallIgnored
executor.awaitTermination(1, TimeUnit.MINUTES);
}

private void reportUncaughtException(@NotNull Thread thread, @NotNull Throwable throwable) {
BoxProvider.get().getLogger().log(
Level.SEVERE,
"An exception occurred in the thread " + thread.getName(),
throwable
);
}

private @NotNull Executor getGlobalExecutor() {
return useModernExecutor() ?
command -> Bukkit.getGlobalRegionScheduler().execute(BoxProvider.get().getPluginInstance(), command) :
getMainThread();
return CompletableFuture.supplyAsync(supplier, executorProvider.getWorker());
}

private @NotNull Executor getExecutorForEntity(@NotNull Entity entity) {
if (useModernExecutor()) {
return command -> {
if (entity.isValid()) {
entity.getScheduler().run(BoxProvider.get().getPluginInstance(), $ -> command.run(), null);
}
};
} else {
return getMainThread();
}
}

private @NotNull Executor getMainThread() {
return Bukkit.getScheduler().getMainThreadExecutor(BoxProvider.get().getPluginInstance());
}

private boolean useModernExecutor() {
// In Paper 1.20.1 Build 40, Folia's new scheduler APIs have been moved to Paper
// Or, Box is running on Folia
return MCDataVersion.CURRENT.isAfterOrSame(MCDataVersion.MC_1_20_1) || Folia.check();
return executorProvider.getEntityScheduler(entity, BoxProvider.get().getPluginInstance());
}
}
Loading

0 comments on commit 472dd5f

Please sign in to comment.