Adds SpawnerManager to EpicSpawnerApi class

This commit is contained in:
Christian Koop 2023-08-15 21:26:33 +02:00
parent 6bb9a5e58a
commit d2bca0f1a7
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
6 changed files with 121 additions and 9 deletions

View File

@ -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";
}
}

View File

@ -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<SpawnerData> getAllSpawnerData();
Collection<SpawnerData> 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<PlacedSpawner> getSpawners();
void addSpawners(Map<Location, PlacedSpawner> spawners);
void addSpawners(List<PlacedSpawner> 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);
}

View File

@ -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

View File

@ -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;

View File

@ -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<SpawnerData> getAllSpawnerData() {
return Collections.unmodifiableCollection(this.registeredSpawnerData.values());
}
@Override
public Collection<SpawnerData> 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<PlacedSpawner> getSpawners() {
return Collections.unmodifiableCollection(this.spawnersInWorld.values());
}
@Override
public void addSpawners(Map<Location, PlacedSpawner> spawners) {
this.spawnersInWorld.putAll(spawners);
}
@Override
public void addSpawners(List<PlacedSpawner> 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);
}

View File

@ -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;