From d2bca0f1a7a94053ef016329ad8b38aaa7462a08 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Tue, 15 Aug 2023 21:26:33 +0200 Subject: [PATCH] Adds SpawnerManager to EpicSpawnerApi class --- .../epicspawners/api/EpicSpawnersApi.java | 11 ++- .../api/spawners/spawner/SpawnerManager.java | 74 +++++++++++++++++++ .../craftaro/epicspawners/EpicSpawners.java | 7 +- .../listeners/InteractListeners.java | 2 +- ...erManager.java => SpawnerManagerImpl.java} | 34 ++++++++- .../epicspawners/tasks/SpawnerSpawnTask.java | 2 +- 6 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/spawners/spawner/SpawnerManager.java rename EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/{SpawnerManager.java => SpawnerManagerImpl.java} (97%) diff --git a/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/EpicSpawnersApi.java b/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/EpicSpawnersApi.java index b2df7d5..84602b6 100644 --- a/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/EpicSpawnersApi.java +++ b/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/EpicSpawnersApi.java @@ -1,5 +1,6 @@ package com.craftaro.epicspawners.api; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerManager; import com.craftaro.epicspawners.api.utils.SpawnerDataBuilder; import com.craftaro.epicspawners.api.utils.SpawnerTierBuilder; import org.bukkit.plugin.Plugin; @@ -11,14 +12,16 @@ import org.bukkit.plugin.Plugin; */ public class EpicSpawnersApi { private static Plugin plugin; + private static SpawnerManager spawnerManager; private static SpawnerDataBuilder spawnerDataBuilder; private static SpawnerTierBuilder spawnerTierBuilder; - public EpicSpawnersApi(Plugin plugin, SpawnerDataBuilder spawnerDataBuilder, SpawnerTierBuilder spawnerTierBuilder) { + public EpicSpawnersApi(Plugin plugin, SpawnerManager spawnerManager, SpawnerDataBuilder spawnerDataBuilder, SpawnerTierBuilder spawnerTierBuilder) { if (EpicSpawnersApi.plugin != null) { throw new IllegalStateException("EpicSpawnersAPI has already been initialized!"); } EpicSpawnersApi.plugin = plugin; + EpicSpawnersApi.spawnerManager = spawnerManager; EpicSpawnersApi.spawnerDataBuilder = spawnerDataBuilder; EpicSpawnersApi.spawnerTierBuilder = spawnerTierBuilder; } @@ -30,6 +33,10 @@ public class EpicSpawnersApi { return plugin; } + public static SpawnerManager getSpawnerManager() { + return spawnerManager; + } + /** * @return A new instance of {@link SpawnerDataBuilder}. */ @@ -50,6 +57,6 @@ public class EpicSpawnersApi { * @return The version of the plugin */ public static String getVersion() { - return "UKNOWN_VERSION"; + return "UNKNOWN_VERSION"; } } diff --git a/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/spawners/spawner/SpawnerManager.java b/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/spawners/spawner/SpawnerManager.java new file mode 100644 index 0000000..f11576e --- /dev/null +++ b/EpicSpawners-API/src/main/java/com/craftaro/epicspawners/api/spawners/spawner/SpawnerManager.java @@ -0,0 +1,74 @@ +package com.craftaro.epicspawners.api.spawners.spawner; + +import com.craftaro.core.configuration.Config; +import com.craftaro.epicspawners.api.spawners.spawner.PlacedSpawner; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerData; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerTier; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +public interface SpawnerManager { + SpawnerData getSpawnerData(String name); + + SpawnerData getSpawnerData(EntityType type); + + SpawnerTier getSpawnerTier(ItemStack item); + + SpawnerData addSpawnerData(String name, SpawnerData spawnerData); + + void addSpawnerData(SpawnerData spawnerData); + + void removeSpawnerData(String name); + + Collection getAllSpawnerData(); + + Collection getAllEnabledSpawnerData(); + + boolean isSpawner(Location location); + + boolean isSpawnerData(String type); + + PlacedSpawner getSpawnerFromWorld(Location location); + + void addSpawnerToWorld(Location location, PlacedSpawner spawner); + + PlacedSpawner removeSpawnerFromWorld(Location location); + + PlacedSpawner removeSpawnerFromWorld(PlacedSpawner spawner); + + Collection getSpawners(); + + void addSpawners(Map spawners); + + void addSpawners(List spawners); + + void addCooldown(PlacedSpawner spawner); + + void removeCooldown(PlacedSpawner spawner); + + boolean hasCooldown(PlacedSpawner spawner); + + int getAmountPlaced(Player player); + + void loadSpawnerDataFromFile(); + + void reloadSpawnerData(); + + void saveSpawnerDataToFile(); + + boolean wasConfigModified(); + + Config getSpawnerConfig(); + + void reloadFromFile(); + + PlacedSpawner getSpawner(int id); + + PlacedSpawner getSpawner(Location location); +} diff --git a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/EpicSpawners.java b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/EpicSpawners.java index dc4f049..3b2f8e5 100644 --- a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/EpicSpawners.java +++ b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/EpicSpawners.java @@ -47,7 +47,8 @@ import com.craftaro.epicspawners.player.PlayerDataManagerImpl; import com.craftaro.epicspawners.settings.Settings; import com.craftaro.epicspawners.spawners.SpawnManager; import com.craftaro.epicspawners.spawners.spawner.PlacedSpawnerImpl; -import com.craftaro.epicspawners.spawners.spawner.SpawnerManager; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerManager; +import com.craftaro.epicspawners.spawners.spawner.SpawnerManagerImpl; import com.craftaro.epicspawners.spawners.spawner.SpawnerStackImpl; import com.craftaro.epicspawners.tasks.AppearanceTask; import com.craftaro.epicspawners.tasks.SpawnerParticleTask; @@ -147,7 +148,7 @@ public class EpicSpawners extends SongodaPlugin { this.boostManager = new BoostManagerImpl(); this.spawnManager = new SpawnManager(); - this.spawnerManager = new SpawnerManager(this); + this.spawnerManager = new SpawnerManagerImpl(this); this.blacklistHandler = new BlacklistHandler(); this.playerActionManager = new PlayerDataManagerImpl(); @@ -174,7 +175,7 @@ public class EpicSpawners extends SongodaPlugin { this.appearanceTask = AppearanceTask.startTask(this); initDatabase(Arrays.asList(new _1_InitialMigration(), new _2_AddTiers())); - new EpicSpawnersApi(this, new SpawnerDataBuilderImpl(""), new SpawnerTierBuilderImpl()); + new EpicSpawnersApi(this, this.spawnerManager, new SpawnerDataBuilderImpl(""), new SpawnerTierBuilderImpl()); } @Override diff --git a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/listeners/InteractListeners.java b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/listeners/InteractListeners.java index e6a4e71..a6b7683 100644 --- a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/listeners/InteractListeners.java +++ b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/listeners/InteractListeners.java @@ -13,7 +13,7 @@ import com.craftaro.epicspawners.api.spawners.spawner.PlacedSpawner; import com.craftaro.epicspawners.api.spawners.spawner.SpawnerStack; import com.craftaro.epicspawners.api.spawners.spawner.SpawnerTier; import com.craftaro.epicspawners.settings.Settings; -import com.craftaro.epicspawners.spawners.spawner.SpawnerManager; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerManager; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; diff --git a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManager.java b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManagerImpl.java similarity index 97% rename from EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManager.java rename to EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManagerImpl.java index 0b90e6e..939ac7d 100644 --- a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManager.java +++ b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/spawners/spawner/SpawnerManagerImpl.java @@ -13,6 +13,7 @@ import com.craftaro.epicspawners.api.particles.ParticleType; import com.craftaro.epicspawners.api.spawners.condition.SpawnCondition; import com.craftaro.epicspawners.api.spawners.spawner.PlacedSpawner; import com.craftaro.epicspawners.api.spawners.spawner.SpawnerData; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerManager; import com.craftaro.epicspawners.api.spawners.spawner.SpawnerStack; import com.craftaro.epicspawners.api.spawners.spawner.SpawnerTier; import com.craftaro.epicspawners.api.utils.SpawnerTierBuilder; @@ -49,7 +50,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -public class SpawnerManager { +public class SpawnerManagerImpl implements SpawnerManager { private final EpicSpawners plugin; // These are the spawner types loaded into memory. @@ -65,7 +66,7 @@ public class SpawnerManager { private String lastLoad = null; - public SpawnerManager(EpicSpawners plugin) { + public SpawnerManagerImpl(EpicSpawners plugin) { this.plugin = plugin; this.spawnerConfig.load(); Arrays.stream(EntityType.values()).filter(entityType -> entityType.isSpawnable() @@ -77,6 +78,7 @@ public class SpawnerManager { loadSpawnerDataFromFile(); } + @Override public SpawnerData getSpawnerData(String name) { return this.registeredSpawnerData.values() .stream() @@ -85,10 +87,12 @@ public class SpawnerManager { .orElse(null); } + @Override public SpawnerData getSpawnerData(EntityType type) { return getSpawnerData(type.name()); } + @Override public SpawnerTier getSpawnerTier(ItemStack item) { if (item == null) { return null; @@ -111,76 +115,94 @@ public class SpawnerManager { return getSpawnerData(cs.getSpawnedType()).getFirstTier(); } + @Override public SpawnerData addSpawnerData(String name, SpawnerData spawnerData) { this.registeredSpawnerData.put(name.toLowerCase(), spawnerData); spawnerData.reloadSpawnMethods(); return spawnerData; } + @Override public void addSpawnerData(SpawnerData spawnerData) { this.registeredSpawnerData.put(spawnerData.getIdentifyingName().toLowerCase(), spawnerData); } + @Override public void removeSpawnerData(String name) { this.registeredSpawnerData.remove(name.toLowerCase()); } + @Override public Collection getAllSpawnerData() { return Collections.unmodifiableCollection(this.registeredSpawnerData.values()); } + @Override public Collection getAllEnabledSpawnerData() { return this.registeredSpawnerData.values().stream().filter(SpawnerData::isActive).collect(Collectors.toList()); } + @Override public boolean isSpawner(Location location) { return this.spawnersInWorld.containsKey(location); } + @Override public boolean isSpawnerData(String type) { return this.registeredSpawnerData.containsKey(type.toLowerCase()); } + @Override public PlacedSpawner getSpawnerFromWorld(Location location) { return this.spawnersInWorld.get(location); } + @Override public void addSpawnerToWorld(Location location, PlacedSpawner spawner) { this.spawnersInWorld.put(location, spawner); } + @Override public PlacedSpawner removeSpawnerFromWorld(Location location) { return this.spawnersInWorld.remove(location); } + @Override public PlacedSpawner removeSpawnerFromWorld(PlacedSpawner spawner) { return this.spawnersInWorld.remove(spawner.getLocation()); } + @Override public Collection getSpawners() { return Collections.unmodifiableCollection(this.spawnersInWorld.values()); } + @Override public void addSpawners(Map spawners) { this.spawnersInWorld.putAll(spawners); } + @Override public void addSpawners(List spawners) { spawners.forEach(spawner -> this.spawnersInWorld.put(spawner.getLocation(), (PlacedSpawnerImpl) spawner)); } + @Override public void addCooldown(PlacedSpawner spawner) { this.pickingUp.add(spawner); } + @Override public void removeCooldown(PlacedSpawner spawner) { this.pickingUp.remove(spawner); } + @Override public boolean hasCooldown(PlacedSpawner spawner) { return this.pickingUp.contains(spawner); } + @Override public int getAmountPlaced(Player player) { return Math.toIntExact(this.spawnersInWorld.values().stream().filter(spawner -> spawner.getPlacedBy() != null && player.getUniqueId().equals(spawner.getPlacedBy().getUniqueId())).count()) + 1; @@ -255,6 +277,7 @@ public class SpawnerManager { this.registeredSpawnerData.put(typeString, spawnerData); } + @Override @SuppressWarnings("unchecked") public void loadSpawnerDataFromFile() { this.registeredSpawnerData.clear(); @@ -360,6 +383,7 @@ public class SpawnerManager { reloadSpawnerData(); } + @Override public void reloadSpawnerData() { for (PlacedSpawner spawner : this.spawnersInWorld.values()) { for (SpawnerStack stack : spawner.getSpawnerStacks()) { @@ -370,6 +394,7 @@ public class SpawnerManager { } } + @Override public void saveSpawnerDataToFile() { // Save spawner settings FileConfiguration spawnerConfig = this.spawnerConfig.getFileConfig(); @@ -461,24 +486,29 @@ public class SpawnerManager { this.spawnerConfig.save(); } + @Override public boolean wasConfigModified() { getSpawnerConfig().load(); return !this.spawnerConfig.getFileConfig().saveToString().equals(this.lastLoad); } + @Override public Config getSpawnerConfig() { return this.spawnerConfig; } + @Override public void reloadFromFile() { getSpawnerConfig().load(); loadSpawnerDataFromFile(); } + @Override public PlacedSpawner getSpawner(int id) { return this.spawnersInWorld.values().stream().filter(spawner -> spawner.getId() == id).findFirst().orElse(null); } + @Override public PlacedSpawner getSpawner(Location location) { return this.spawnersInWorld.get(location); } diff --git a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/tasks/SpawnerSpawnTask.java b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/tasks/SpawnerSpawnTask.java index 6d05add..9cee5ec 100644 --- a/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/tasks/SpawnerSpawnTask.java +++ b/EpicSpawners-Plugin/src/main/java/com/craftaro/epicspawners/tasks/SpawnerSpawnTask.java @@ -4,7 +4,7 @@ import com.craftaro.core.third_party.com.cryptomorin.xseries.XMaterial; import com.craftaro.epicspawners.EpicSpawners; import com.craftaro.epicspawners.api.spawners.spawner.PlacedSpawner; import com.craftaro.epicspawners.settings.Settings; -import com.craftaro.epicspawners.spawners.spawner.SpawnerManager; +import com.craftaro.epicspawners.api.spawners.spawner.SpawnerManager; import org.bukkit.block.CreatureSpawner; import org.bukkit.scheduler.BukkitRunnable;