From c3c9b965e54f985166e3dc36ee4e6f3077b56a67 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Fri, 26 Mar 2021 13:28:03 -0500 Subject: [PATCH] Creating some DI --- .../simplexcore/SimplexCorePlugin.java | 2 +- .../simplexcore/ban/BanFactory.java | 10 ++- .../simplexdev/simplexcore/ban/BanType.java | 3 +- .../simplexcore/enchanting/SimplexEnch.java | 2 +- .../simplexcore/module/SimplexModule.java | 9 ++- .../simplexcore/particle/ParticleFactory.java | 11 ++- .../simplexcore/potion/PotionsFactory.java | 2 +- .../simplexcore/structures/Structure.java | 77 ++++++++++++++++++- .../simplexcore/task/Announcer.java | 8 +- .../simplexcore/task/SimplexTask.java | 17 ++-- 10 files changed, 120 insertions(+), 21 deletions(-) diff --git a/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java b/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java index 9feb92b..ca3b78e 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java +++ b/src/main/java/io/github/simplexdev/simplexcore/SimplexCorePlugin.java @@ -43,7 +43,7 @@ public final class SimplexCorePlugin extends SimplexModule { getInternals().reload(); // SimplexListener.register(new DependencyListener(), this); - new Announcer(); + new Announcer(this); } catch (Exception ex) { suspended = true; // TODO: Write an output to a file with why it suspended. diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java index 38e0d62..45de0c0 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/BanFactory.java @@ -11,8 +11,10 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.io.File; +import java.util.Arrays; import java.util.Date; import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; import static io.github.simplexdev.simplexcore.utils.Utilities.pathway; @@ -124,12 +126,18 @@ public final class BanFactory { return null; } - private VoidSupplier assignBanDuration() { + private VoidSupplier assignBanDuration(Long... time) { return () -> { if (type.equals(BanType.PERMANENT)) { banDuration = TickedTime.YEAR * 99; } else if (type.equals(BanType.TEMPORARY)) { banDuration = TickedTime.DAY; + } else if (type.equals(BanType.CUSTOM)) { + long tmp = 0L; + for (long t : time) { + tmp += t; + } + banDuration = tmp; } else { banDuration = TickedTime.MINUTE * 5; } diff --git a/src/main/java/io/github/simplexdev/simplexcore/ban/BanType.java b/src/main/java/io/github/simplexdev/simplexcore/ban/BanType.java index 4395eed..65e3ea1 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/ban/BanType.java +++ b/src/main/java/io/github/simplexdev/simplexcore/ban/BanType.java @@ -2,7 +2,8 @@ package io.github.simplexdev.simplexcore.ban; public enum BanType { PERMANENT("P-"), - TEMPORARY("T-"); + TEMPORARY("T-"), + CUSTOM("C-"); private final String prefix; diff --git a/src/main/java/io/github/simplexdev/simplexcore/enchanting/SimplexEnch.java b/src/main/java/io/github/simplexdev/simplexcore/enchanting/SimplexEnch.java index e3d01d2..72bdbc1 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/enchanting/SimplexEnch.java +++ b/src/main/java/io/github/simplexdev/simplexcore/enchanting/SimplexEnch.java @@ -8,7 +8,7 @@ import net.minecraft.server.v1_16_R3.Enchantment; import org.bukkit.NamespacedKey; public abstract class SimplexEnch extends Enchantment implements IEnchant { - protected final SimplexModule plugin; // What was your question? + protected final SimplexModule plugin; protected SimplexEnch(SimplexModule plugin, Rarity rarity, EnchantmentSlotType type, EnumItemSlot[] enumSlot) { super(rarity, type, enumSlot); diff --git a/src/main/java/io/github/simplexdev/simplexcore/module/SimplexModule.java b/src/main/java/io/github/simplexdev/simplexcore/module/SimplexModule.java index 1e91c4e..3ee4f67 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/module/SimplexModule.java +++ b/src/main/java/io/github/simplexdev/simplexcore/module/SimplexModule.java @@ -1,5 +1,6 @@ package io.github.simplexdev.simplexcore.module; +import io.github.simplexdev.simplexcore.SimplexCorePlugin; import io.github.simplexdev.simplexcore.command.CommandLoader; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -63,11 +64,15 @@ public abstract class SimplexModule> extends JavaPlug return this.getDataFolder(); } - public synchronized ModuleRegistry getRegistry() { + public ModuleRegistry getRegistry() { return ModuleRegistry.getInstance(); } - public synchronized CommandLoader getCommandLoader() { + public CommandLoader getCommandLoader() { return CommandLoader.getInstance(); } + + public SimplexCorePlugin getProvider() { + return SimplexCorePlugin.getInstance(); + } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/particle/ParticleFactory.java b/src/main/java/io/github/simplexdev/simplexcore/particle/ParticleFactory.java index 58329b6..c4fd0a9 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/particle/ParticleFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/particle/ParticleFactory.java @@ -1,18 +1,21 @@ package io.github.simplexdev.simplexcore.particle; import io.github.simplexdev.api.IParticleEffect; +import io.github.simplexdev.simplexcore.module.SimplexModule; import org.bukkit.*; import java.util.*; -public class ParticleFactory { +public final class ParticleFactory { + private final SimplexModule plugin; private float size = 2F; private long duration = 20L * 15L; private final Set particleSet = new HashSet<>(); private final Map particleColorMap = new HashMap<>(); private final Set particleEffects = new HashSet<>(); - public ParticleFactory() { + public ParticleFactory(SimplexModule plugin) { + this.plugin = plugin; } public ParticleFactory setSize(float size) { @@ -90,4 +93,8 @@ public class ParticleFactory { world.spawnParticle(particle, location, 20); }); } + + public final SimplexModule getPlugin() { + return plugin; + } } diff --git a/src/main/java/io/github/simplexdev/simplexcore/potion/PotionsFactory.java b/src/main/java/io/github/simplexdev/simplexcore/potion/PotionsFactory.java index b8ef8a4..1e5afa5 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/potion/PotionsFactory.java +++ b/src/main/java/io/github/simplexdev/simplexcore/potion/PotionsFactory.java @@ -9,7 +9,7 @@ import java.util.stream.Collectors; public final class PotionsFactory { private PotionsFactory() { - + throw new AssertionError("This class should not be instantiated!"); } public static void applyEffect(LivingEntity entity, PotionEffect... effect) { diff --git a/src/main/java/io/github/simplexdev/simplexcore/structures/Structure.java b/src/main/java/io/github/simplexdev/simplexcore/structures/Structure.java index 07c07df..1e78487 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/structures/Structure.java +++ b/src/main/java/io/github/simplexdev/simplexcore/structures/Structure.java @@ -1,5 +1,80 @@ package io.github.simplexdev.simplexcore.structures; -public class Structure { +import io.github.simplexdev.api.IStructure; +import io.github.simplexdev.simplexcore.math.Size; +import io.github.simplexdev.simplexcore.module.SimplexModule; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.World; +import org.bukkit.block.Block; + +import java.io.File; +import java.util.Map; + +public class Structure implements IStructure { + private final SimplexModule plugin; + + public Structure(SimplexModule plugin) { + this.plugin = plugin; + } + + @Override + public NamespacedKey getNamespacedKey() { + return null; + } + + @Override + public String getName() { + return null; + } + + @Override + public Location getLocation() { + return null; + } + + @Override + public World getWorld() { + return null; + } + + @Override + public boolean shouldGenerateNaturally() { + return false; + } + + @Override + public void generate(Location location, World world) { + + } + + @Override + public void generate(Location location, World world, boolean generateNaturally) { + + } + + @Override + public Size getApproximateSize() { + return null; + } + + @Override + public Block[] getBlocks() { + return new Block[0]; + } + + @Override + public Map getBlockLocations() { + return null; + } + + @Override + public File getStructureFile() { + return null; + } + + public SimplexModule getPlugin() { + return plugin; + } // TODO: Write this } diff --git a/src/main/java/io/github/simplexdev/simplexcore/task/Announcer.java b/src/main/java/io/github/simplexdev/simplexcore/task/Announcer.java index cac4bc8..ea0a39f 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/task/Announcer.java +++ b/src/main/java/io/github/simplexdev/simplexcore/task/Announcer.java @@ -2,6 +2,7 @@ package io.github.simplexdev.simplexcore.task; import io.github.simplexdev.simplexcore.SimplexCorePlugin; import io.github.simplexdev.simplexcore.chat.Messages; +import io.github.simplexdev.simplexcore.module.SimplexModule; import io.github.simplexdev.simplexcore.utils.TickedTime; import org.apache.commons.lang.math.RandomUtils; import org.bukkit.Bukkit; @@ -11,16 +12,15 @@ import java.util.ArrayList; import java.util.List; public class Announcer extends SimplexTask { - private final List stringList = new ArrayList<>() {{ add("Join our discord!" + Messages.DISCORD.getMessage()); add("Thank you for using SimplexCore!"); add("https://github.com/SimplexDevelopment/SimplexCore"); }}; - public Announcer() { - super(TickedTime.HOUR); - register(this, SimplexCorePlugin.getInstance(), true, false); + public Announcer(SimplexCorePlugin plugin) { + super(plugin, TickedTime.HOUR); + register(this, true, false); } @Override diff --git a/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java b/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java index b540096..108c4ba 100644 --- a/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java +++ b/src/main/java/io/github/simplexdev/simplexcore/task/SimplexTask.java @@ -15,6 +15,7 @@ import java.util.function.Consumer; public abstract class SimplexTask implements Consumer { protected final long DELAY; protected final long INTERVAL; + protected final SimplexModule plugin; protected Date lastRan = new Date(); protected Timer timer = new Timer(); protected int taskId; @@ -25,9 +26,10 @@ public abstract class SimplexTask implements Consumer { * @param initialDelay How long before the first time the task executes. * @param interval How long before the task repeats itself. */ - protected SimplexTask(long initialDelay, long interval) { + protected SimplexTask(SimplexModule plugin, long initialDelay, long interval) { DELAY = initialDelay; INTERVAL = interval; + this.plugin = plugin; } /** @@ -37,16 +39,18 @@ public abstract class SimplexTask implements Consumer { * @param interval How often the task should repeat. Every 20L is 1 second. * You should use {@link TickedTime} for determining the related server timings. */ - protected SimplexTask(long interval) { + protected SimplexTask(SimplexModule plugin, long interval) { DELAY = 0L; INTERVAL = interval; + this.plugin = plugin; } /** * Constructor which automatically registers the DELAY as 30 seconds * and the INTERVAL at which it executes as 5 minutes. */ - protected SimplexTask() { + protected SimplexTask(SimplexModule plugin) { + this.plugin = plugin; DELAY = TickedTime.SECOND * 30; // 30 seconds until the task triggers for the first time. INTERVAL = TickedTime.MINUTE * 5; // Task will run at 5 minute intervals once the first trigger has been called. } @@ -56,12 +60,11 @@ public abstract class SimplexTask implements Consumer { * {@link org.bukkit.scheduler.BukkitScheduler}. * * @param task The instance of a subclass of this class. - * @param plugin Your plugin instance. * @param repeating Whether or not the task should repeat. * @param delayed Whether or not to delay the first time the task runs. * @param A subclass of SimplexTask. */ - public void register(T task, SimplexModule plugin, boolean repeating, boolean delayed) { + public void register(T task, boolean repeating, boolean delayed) { if (delayed && repeating) { SimplexCorePlugin.getInstance().getScheduler().runTaskTimer(plugin, task, DELAY, INTERVAL); } else if (delayed) { @@ -85,7 +88,7 @@ public abstract class SimplexTask implements Consumer { * @param A subclass of SimplexTask. */ public void register(T task, SimplexModule plugin, boolean delayed) { - register(task, plugin, false, delayed); + register(task, false, delayed); } /** @@ -98,7 +101,7 @@ public abstract class SimplexTask implements Consumer { * @param A subclass of SimplexTask. */ public void register(T task, SimplexModule plugin) { - register(task, plugin, false, false); + register(task, false, false); } /**