Moved BukkitRunnables into internal runnable factory

This commit is contained in:
Auxilor
2020-12-22 19:43:40 +00:00
parent f9a186f58f
commit 07e1848054
66 changed files with 360 additions and 334 deletions

View File

@@ -13,7 +13,7 @@ public class EcoEventManager extends PluginDependent implements EventManager {
@Override
public void registerEvents(Listener listener) {
Bukkit.getPluginManager().registerEvents(listener, this.plugin);
Bukkit.getPluginManager().registerEvents(listener, this.getPlugin());
}
@Override
@@ -23,6 +23,6 @@ public class EcoEventManager extends PluginDependent implements EventManager {
@Override
public void unregisterAllEvents() {
HandlerList.unregisterAll(this.plugin);
HandlerList.unregisterAll(this.getPlugin());
}
}

View File

@@ -10,6 +10,6 @@ public class NamespacedKeyFactory extends PluginDependentFactory {
}
public NamespacedKey create(String key) {
return new NamespacedKey(this.plugin, key);
return new NamespacedKey(this.getPlugin(), key);
}
}

View File

@@ -11,16 +11,16 @@ public class EcoLogger extends PluginDependent implements Logger {
@Override
public void info(String message) {
this.plugin.getLogger().info(StringUtils.translate(message));
this.getPlugin().getLogger().info(StringUtils.translate(message));
}
@Override
public void warn(String message) {
this.plugin.getLogger().warning(StringUtils.translate(message));
this.getPlugin().getLogger().warning(StringUtils.translate(message));
}
@Override
public void error(String message) {
this.plugin.getLogger().severe(StringUtils.translate(message));
this.getPlugin().getLogger().severe(StringUtils.translate(message));
}
}

View File

@@ -10,6 +10,6 @@ public class MetadataValueFactory extends PluginDependentFactory {
}
public FixedMetadataValue create(Object value) {
return new FixedMetadataValue(this.plugin, value);
return new FixedMetadataValue(this.getPlugin(), value);
}
}

View File

@@ -2,11 +2,47 @@ package com.willfp.eco.util.bukkit.scheduling;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
public abstract class EcoBukkitRunnable extends BukkitRunnable {
protected final AbstractEcoPlugin plugin;
private final AbstractEcoPlugin plugin;
protected EcoBukkitRunnable(AbstractEcoPlugin plugin) {
EcoBukkitRunnable(AbstractEcoPlugin plugin) {
this.plugin = plugin;
}
protected AbstractEcoPlugin getPlugin() {
return this.plugin;
}
@NotNull
public synchronized BukkitTask runTask() {
return super.runTask(plugin);
}
@NotNull
public synchronized BukkitTask runTaskAsynchronously() {
return super.runTaskAsynchronously(plugin);
}
@NotNull
public synchronized BukkitTask runTaskLater(long delay) {
return super.runTaskLater(plugin, delay);
}
@NotNull
public synchronized BukkitTask runTaskLaterAsynchronously(long delay) {
return super.runTaskLaterAsynchronously(plugin, delay);
}
@NotNull
public synchronized BukkitTask runTaskTimer(long delay, long period) {
return super.runTaskTimer(plugin, delay, period);
}
@NotNull
public synchronized BukkitTask runTaskTimerAsynchronously(long delay, long period) {
return super.runTaskTimerAsynchronously(plugin, delay, period);
}
}

View File

@@ -13,36 +13,36 @@ public class EcoScheduler extends PluginDependent implements Scheduler {
@Override
public BukkitTask runLater(Callable callable, long ticksLater) {
return Bukkit.getServer().getScheduler().runTaskLater(this.plugin, callable::call, ticksLater);
return Bukkit.getServer().getScheduler().runTaskLater(this.getPlugin(), callable::call, ticksLater);
}
@Override
public BukkitTask runTimer(Callable callable, long delay, long repeat) {
return Bukkit.getServer().getScheduler().runTaskTimer(this.plugin, callable::call, delay, repeat);
return Bukkit.getServer().getScheduler().runTaskTimer(this.getPlugin(), callable::call, delay, repeat);
}
@Override
public BukkitTask runAsyncTimer(Callable callable, long delay, long repeat) {
return Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, callable::call, delay, repeat);
return Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(this.getPlugin(), callable::call, delay, repeat);
}
@Override
public BukkitTask run(Runnable runnable) {
return Bukkit.getServer().getScheduler().runTask(this.plugin, runnable::run);
return Bukkit.getServer().getScheduler().runTask(this.getPlugin(), runnable::run);
}
@Override
public BukkitTask runAsync(Callable callable) {
return Bukkit.getServer().getScheduler().runTaskAsynchronously(this.plugin, callable::call);
return Bukkit.getServer().getScheduler().runTaskAsynchronously(this.getPlugin(), callable::call);
}
@Override
public int syncRepeating(Runnable runnable, long delay, long repeat) {
return Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, runnable, delay, repeat);
return Bukkit.getScheduler().scheduleSyncRepeatingTask(this.getPlugin(), runnable, delay, repeat);
}
@Override
public void cancelAll() {
Bukkit.getScheduler().cancelTasks(this.plugin);
Bukkit.getScheduler().cancelTasks(this.getPlugin());
}
}

View File

@@ -0,0 +1,20 @@
package com.willfp.eco.util.bukkit.scheduling;
import com.willfp.eco.util.factory.PluginDependentFactory;
import com.willfp.eco.util.lambda.InputCallable;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
public class RunnableFactory extends PluginDependentFactory {
public RunnableFactory(AbstractEcoPlugin plugin) {
super(plugin);
}
public EcoBukkitRunnable create(InputCallable<EcoBukkitRunnable> callable) {
return new EcoBukkitRunnable(this.getPlugin()) {
@Override
public void run() {
callable.call(this);
}
};
}
}

View File

@@ -9,7 +9,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashSet;
import java.util.List;
@@ -36,12 +35,7 @@ public class EntityDeathByEntityListeners extends PluginDependent implements Lis
builtEvent.setDamager(event.getDamager());
events.add(builtEvent);
new BukkitRunnable() {
@Override
public void run() {
events.remove(builtEvent);
}
}.runTaskLater(this.plugin, 1);
this.getPlugin().getScheduler().runLater(() -> events.remove(builtEvent), 1);
}
@EventHandler
@@ -54,7 +48,7 @@ public class EntityDeathByEntityListeners extends PluginDependent implements Lis
AtomicReference<EntityDeathByEntityBuilder> atomicBuiltEvent = new AtomicReference<>(null);
EntityDeathByEntityBuilder builtEvent;
events.forEach((deathByEntityEvent) -> {
events.forEach(deathByEntityEvent -> {
if (deathByEntityEvent.getVictim().equals(victim)) {
atomicBuiltEvent.set(deathByEntityEvent);
}

View File

@@ -6,11 +6,11 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
* Extensions are a way of interfacing with EcoEnchants
* Extensions are a way of interfacing with the base plugin
* Syntactically similar to Bukkit Plugins.
*/
public abstract class Extension {
protected final AbstractEcoPlugin plugin = AbstractEcoPlugin.getInstance();
private final AbstractEcoPlugin plugin = AbstractEcoPlugin.getInstance();
/**
* Metadata containing version and name
@@ -42,6 +42,14 @@ public abstract class Extension {
*/
protected abstract void onDisable();
/**
* Get instance of the owning plugin
* @return The instance to interface with
*/
protected final AbstractEcoPlugin getPlugin() {
return plugin;
}
/**
* Set the metadata of the extension
* <p>

View File

@@ -30,7 +30,7 @@ public class EcoExtensionLoader extends PluginDependent implements ExtensionLoad
@Override
public void loadExtensions() {
File dir = new File(this.plugin.getDataFolder(), "/extensions");
File dir = new File(this.getPlugin().getDataFolder(), "/extensions");
if (!dir.exists()) {
dir.mkdirs();
}
@@ -46,7 +46,7 @@ public class EcoExtensionLoader extends PluginDependent implements ExtensionLoad
try {
loadExtension(extensionJar);
} catch (MalformedExtensionException e) {
plugin.getLog().error(extensionJar.getName() + " caused MalformedExtensionException: " + e.getMessage());
this.getPlugin().getLog().error(extensionJar.getName() + " caused MalformedExtensionException: " + e.getMessage());
}
}
}
@@ -59,7 +59,7 @@ public class EcoExtensionLoader extends PluginDependent implements ExtensionLoad
e.printStackTrace();
}
ClassLoader cl = new URLClassLoader(new URL[]{url}, this.plugin.getClass().getClassLoader());
ClassLoader cl = new URLClassLoader(new URL[]{url}, this.getPlugin().getClass().getClassLoader());
InputStream ymlIn = cl.getResourceAsStream("extension.yml");

View File

@@ -3,9 +3,13 @@ package com.willfp.eco.util.injection;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
public abstract class PluginDependent {
protected final AbstractEcoPlugin plugin;
private final AbstractEcoPlugin plugin;
protected PluginDependent(AbstractEcoPlugin plugin) {
this.plugin = plugin;
}
protected final AbstractEcoPlugin getPlugin() {
return this.plugin;
}
}

View File

@@ -12,7 +12,7 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
public class AntigriefLands extends PluginDependent implements AntigriefWrapper {
private final LandsIntegration landsIntegration = new LandsIntegration(this.plugin);
private final LandsIntegration landsIntegration = new LandsIntegration(this.getPlugin());
public AntigriefLands(AbstractEcoPlugin plugin) {
super(plugin);

View File

@@ -0,0 +1,11 @@
package com.willfp.eco.util.lambda;
/**
* Functional Interface that requires an object
*
* @param <A> The type of the object to require
*/
@FunctionalInterface
public interface InputCallable<A> {
void call(A object);
}

View File

@@ -8,6 +8,7 @@ import com.willfp.eco.util.bukkit.logging.EcoLogger;
import com.willfp.eco.util.bukkit.logging.Logger;
import com.willfp.eco.util.bukkit.meta.MetadataValueFactory;
import com.willfp.eco.util.bukkit.scheduling.EcoScheduler;
import com.willfp.eco.util.bukkit.scheduling.RunnableFactory;
import com.willfp.eco.util.bukkit.scheduling.Scheduler;
import com.willfp.eco.util.command.AbstractCommand;
import com.willfp.eco.util.config.Configs;
@@ -69,6 +70,7 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
private final EventManager eventManager;
private final NamespacedKeyFactory namespacedKeyFactory;
private final MetadataValueFactory metadataValueFactory;
private final RunnableFactory runnableFactory;
private final ExtensionLoader extensionLoader;
protected boolean outdated = false;
@@ -83,6 +85,7 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
this.eventManager = new EcoEventManager(this);
this.namespacedKeyFactory = new NamespacedKeyFactory(this);
this.metadataValueFactory = new MetadataValueFactory(this);
this.runnableFactory = new RunnableFactory(this);
this.extensionLoader = new EcoExtensionLoader(this);
if (!Bukkit.getServicesManager().isProvidedFor(TelekinesisTests.class)) {
@@ -266,6 +269,10 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
return metadataValueFactory;
}
public final RunnableFactory getRunnableFactory() {
return runnableFactory;
}
public final ExtensionLoader getExtensionLoader() {
return extensionLoader;
}