Skip to content

Commit

Permalink
refactor: fixed time bugs on TaskContext and externalized all sink …
Browse files Browse the repository at this point in the history
…plugin options
  • Loading branch information
arthurfiorette committed Aug 11, 2021
1 parent 3b0458a commit f3b9697
Show file tree
Hide file tree
Showing 22 changed files with 310 additions and 143 deletions.
1 change: 1 addition & 0 deletions docs/examples/ReadmeExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.arthurfiorette.sinklibrary.core.SinkPlugin;
import com.github.arthurfiorette.sinklibrary.interfaces.ComponentLoader;
import com.github.arthurfiorette.sinklibrary.listener.SinkListener;

import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.command.PluginIdentifiableCommand;

public interface BaseCommand extends BaseComponent, PluginIdentifiableCommand {

void handle(final CommandSender sender, final Collection<String> args);

List<String> onTabComplete(final CommandSender sender, final Collection<String> args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.arthurfiorette.sinklibrary.command.reflection;

import com.github.arthurfiorette.sinklibrary.exceptions.EnablingException;
import com.github.arthurfiorette.sinklibrary.exception.service.EnablingException;
import com.github.arthurfiorette.sinklibrary.interfaces.BaseComponent;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.PluginManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.arthurfiorette.sinklibrary.command.reflection;

import com.github.arthurfiorette.sinklibrary.core.BasePlugin;
import com.github.arthurfiorette.sinklibrary.exceptions.EnablingException;
import com.github.arthurfiorette.sinklibrary.exception.service.EnablingException;

import java.lang.reflect.Field;
import lombok.Getter;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.github.arthurfiorette.sinklibrary.command.wrapper;

import java.util.ArrayList;
import java.util.List;
import lombok.*;

import org.bukkit.ChatColor;

import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.Singular;

@Builder
public class CommandInfo {

private static String defaultPermissionMessage =
ChatColor.RED +
"I'm sorry, but you do not have permission to perform this command. " +
"Please contact the server administrators if you believe that this is in error.";
private static String defaultPermissionMessage = ChatColor.RED
+ "I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";

@Getter
@NonNull
Expand All @@ -34,6 +37,7 @@ public class CommandInfo {

@Getter
@NonNull
@Singular
private List<String> aliases;

@Getter
Expand All @@ -45,21 +49,5 @@ public class CommandInfo {
@NonNull
@Builder.Default
private String description = "";

public static class CommandInfoBuilder {

public CommandInfoBuilder() {
this.aliases = new ArrayList<>();
}

// Disable these builder methods
void subCommands() {}

void aliases() {}

public CommandInfoBuilder alias(final String alias) {
this.aliases.add(alias);
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public boolean execute(
wrapper.command.handle(sender, argsList);
} catch (final Throwable e) {
sender.sendMessage("§cThis command had some problems, we are sorry for the inconvenience...");
wrapper.command.getBasePlugin().treatThrowable(wrapper.command.getClass(), e, "");
wrapper.command.getBasePlugin().getExceptionHandler().handle(wrapper.command.getClass(), e, "");
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.arthurfiorette.sinklibrary.components;

import com.github.arthurfiorette.sinklibrary.exceptions.ComponentNotRegisteredException;
import com.github.arthurfiorette.sinklibrary.exception.sink.ComponentNotRegisteredException;
import com.github.arthurfiorette.sinklibrary.interfaces.BaseComponent;

public interface ComponentManager {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.arthurfiorette.sinklibrary.components;

import com.github.arthurfiorette.sinklibrary.core.SinkPlugin;
import com.github.arthurfiorette.sinklibrary.exceptions.ComponentNotRegisteredException;
import com.github.arthurfiorette.sinklibrary.exceptions.IllegalComponentException;
import com.github.arthurfiorette.sinklibrary.exception.sink.ComponentNotRegisteredException;
import com.github.arthurfiorette.sinklibrary.exception.sink.IllegalComponentException;
import com.github.arthurfiorette.sinklibrary.interfaces.*;
import com.github.arthurfiorette.sinklibrary.logging.Level;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void enableServices() throws IllegalStateException {
service.getClass().getSimpleName()
);
} catch (final Exception e) {
this.plugin.treatThrowable(service.getClass(), e, "Could not enable this service.");
this.plugin.getExceptionHandler().handle(service.getClass(), e, "Could not enable this service.");
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ public void disableServices() throws IllegalStateException {
service.getClass().getSimpleName()
);
} catch (final Exception e) {
this.plugin.treatThrowable(
this.plugin.getExceptionHandler().handle(
service.getClass(),
// Prevent infinite loop while disabling.
new RuntimeException(e),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.arthurfiorette.sinklibrary.core;

import com.github.arthurfiorette.sinklibrary.components.ComponentManager;
import com.github.arthurfiorette.sinklibrary.exception.BaseExceptionHandler;
import com.github.arthurfiorette.sinklibrary.executor.v2.TaskContext;
import com.github.arthurfiorette.sinklibrary.interfaces.BaseComponent;
import com.github.arthurfiorette.sinklibrary.logging.BaseLogger;
Expand All @@ -9,7 +10,6 @@
import org.bukkit.plugin.Plugin;

public interface BasePlugin extends Plugin {
void treatThrowable(Object author, Throwable throwable, String message, Object... args);

ComponentManager getManager();

Expand All @@ -22,15 +22,12 @@ public interface BasePlugin extends Plugin {
* {@link TaskContext#runLater(BasePlugin, Runnable, long)} or
* {@link TaskContext#runTimer(BasePlugin, Runnable, long, long)}, that this
* executor returns an instance of {@link ScheduledExecutorService}
* <p>
* Defaults to {@link Executors#newScheduledThreadPool(int)} with the number
* of processors available at runtime.
*
* @return the executor of this plugin
*/
default ExecutorService getExecutor() {
return Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
}
ExecutorService getExecutor();

BaseExceptionHandler getExceptionHandler();

default <T extends BaseComponent> T getComponent(final Class<T> clazz) {
return this.getManager().getComponent(clazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.arthurfiorette.sinklibrary.core;

import com.github.arthurfiorette.sinklibrary.components.ComponentManager;
import com.github.arthurfiorette.sinklibrary.components.SimpleComponentManager;
import com.github.arthurfiorette.sinklibrary.exception.BaseExceptionHandler;
import com.github.arthurfiorette.sinklibrary.exception.SimpleExceptionHandler;
import com.github.arthurfiorette.sinklibrary.logging.BaseLogger;
import com.github.arthurfiorette.sinklibrary.logging.BukkitLogger;
import com.github.arthurfiorette.sinklibrary.logging.Level;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

@AllArgsConstructor
@Builder(builderMethodName = "builder")
public class SinkOptions {

@Getter
@NonNull
private ComponentManager manager;

@Getter
@NonNull
private BaseLogger baseLogger;

@Getter
@NonNull
private ExecutorService executor;

@Getter
@NonNull
private BaseExceptionHandler exceptionHandler;

/**
* Create a new builder for the provided {@link SinkPlugin}
*
* @param plugin the plugin owner
*
* @return the new builder
*/
public static SinkOptionsBuilder builder(SinkPlugin plugin) {
return new SinkOptionsBuilder(plugin);
}

/**
* Class overrided to apply the default settings
*
* @author /~https://github.com/Hazork/sink-library/
*/
public static class SinkOptionsBuilder {
private SinkOptionsBuilder(SinkPlugin plugin) {
manager(new SimpleComponentManager(plugin));
baseLogger(new BukkitLogger(plugin, Level.ALL));
executor(new ScheduledThreadPoolExecutor(1));
exceptionHandler(new SimpleExceptionHandler(plugin));
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.github.arthurfiorette.sinklibrary.core;

import com.github.arthurfiorette.sinklibrary.components.ComponentManager;
import com.github.arthurfiorette.sinklibrary.components.SimpleComponentManager;
import com.github.arthurfiorette.sinklibrary.executor.v2.TaskContext;
import com.github.arthurfiorette.sinklibrary.core.SinkOptions.SinkOptionsBuilder;
import com.github.arthurfiorette.sinklibrary.exception.BaseExceptionHandler;
import com.github.arthurfiorette.sinklibrary.interfaces.BaseService;
import com.github.arthurfiorette.sinklibrary.interfaces.ComponentLoader;
import com.github.arthurfiorette.sinklibrary.logging.*;
import lombok.*;
import com.github.arthurfiorette.sinklibrary.logging.BaseLogger;

import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;

import org.bukkit.plugin.java.JavaPlugin;

@RequiredArgsConstructor
import lombok.Getter;
import lombok.NonNull;

public abstract class SinkPlugin extends JavaPlugin implements BasePlugin {

@Getter
Expand All @@ -20,9 +25,29 @@ public abstract class SinkPlugin extends JavaPlugin implements BasePlugin {
@NonNull
private final BaseLogger baseLogger;

@Getter
@NonNull
private final ExecutorService executor;

@Getter
@NonNull
private final BaseExceptionHandler exceptionHandler;

public SinkPlugin() {
this.manager = new SimpleComponentManager(this);
this.baseLogger = new BukkitLogger(this, Level.ALL);
this((options) -> { /* Default Options */ });
}

public SinkPlugin(Consumer<SinkOptionsBuilder> options) {
// Create and build this plugin options.
final SinkOptionsBuilder builder = SinkOptions.builder(this);
options.accept(builder);
SinkOptions so = builder.build();

// Apply his properties
this.manager = so.getManager();
this.baseLogger = so.getBaseLogger();
this.executor = so.getExecutor();
this.exceptionHandler = so.getExceptionHandler();
}

/**
Expand All @@ -32,8 +57,8 @@ public SinkPlugin() {
public abstract ComponentLoader[] components();

/**
* If you need to execute code on startup, implements the {@link BaseService}
* class on your {@link SinkPlugin} and register it on
* @implNote If you need to execute code on startup, implements the
* {@link BaseService} class on your {@link SinkPlugin} and register it on
* {@link SinkPlugin#components()}
*/
@Override
Expand All @@ -42,30 +67,12 @@ public final void onEnable() {
}

/**
* If you need to execute code on startup, implements the {@link BaseService}
* class on your {@link SinkPlugin} and register it on
* @implNote If you need to execute code on startup, implements the
* {@link BaseService} class on your {@link SinkPlugin} and register it on
* {@link SinkPlugin#components()}
*/
@Override
public final void onDisable() {
this.manager.disableServices();
}

@Override
public void treatThrowable(
final Object author,
final Throwable exc,
final String message,
final Object... args
) {
if (exc instanceof RuntimeException) {
this.log(Level.ERROR, author, "Exception caugth", exc);
return;
}

// Disable this plugin if it isn't a runtime exception.

this.log(Level.FATAL, author, "Exception caugth, disabling this plugin.", exc);
TaskContext.BUKKIT.runLater(this, () -> this.getPluginLoader().disablePlugin(this), 1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.arthurfiorette.sinklibrary.exception;

import com.github.arthurfiorette.sinklibrary.interfaces.BaseComponent;

public interface BaseExceptionHandler extends BaseComponent {

public void handle(final Class<?> author, final Throwable exc, final String message,
final Object... args);

public void handle(final Class<?> author, final Throwable exc);

//

default void handle(final Object author, final Throwable exc, final String message,
final Object... args) {
this.handle(author.getClass(), exc, message, args);
}

default void handle(final Object author, final Throwable exc) {
this.handle(author.getClass(), exc);
}

default void handle(final Throwable exc, final String message, final Object... args) {
this.handle(getBasePlugin().getClass(), exc, message, args);
}

default void handle(final Throwable exc) {
this.handle(getBasePlugin().getClass(), exc);
}

}
Loading

0 comments on commit f3b9697

Please sign in to comment.