Refactors SpawnerStackImpl to not extend SSpawner

I don't think thats a good idea as SSpawner is part of the NMS system, even if the import does not
hint at that.
It will probably change gratly again in the near future anyway (hopefully before CraftaroCore v3.0.0 releases).
This commit is contained in:
Christian Koop 2023-06-11 13:32:13 +02:00
parent d0dd63999b
commit 439b0aa7ca
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -1,15 +1,15 @@
package com.craftaro.ultimatestacker.stackable.spawner; package com.craftaro.ultimatestacker.stackable.spawner;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.UltimateStackerAPI;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
import com.craftaro.ultimatestacker.settings.Settings;
import com.craftaro.ultimatestacker.utils.Methods;
import com.craftaro.core.compatibility.CompatibleMaterial; import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.core.database.Data; import com.craftaro.core.database.Data;
import com.craftaro.core.database.SerializedLocation; import com.craftaro.core.database.SerializedLocation;
import com.craftaro.core.nms.world.SpawnedEntity; import com.craftaro.core.nms.world.SpawnedEntity;
import com.craftaro.core.world.SSpawner; import com.craftaro.core.world.SSpawner;
import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.UltimateStackerAPI;
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
import com.craftaro.ultimatestacker.settings.Settings;
import com.craftaro.ultimatestacker.utils.Methods;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.CreatureSpawner; import org.bukkit.block.CreatureSpawner;
@ -21,38 +21,36 @@ import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
public class SpawnerStackImpl extends SSpawner implements SpawnerStack { public class SpawnerStackImpl implements SpawnerStack {
private final UUID uniqueHologramId = UUID.randomUUID();
// This is the unique identifier for this spawner.
// It is reset on every plugin load.
// Used for holograms.
private final UUID uniqueId = UUID.randomUUID();
private int id; private int id;
private Location location;
private int amount; private int amount;
private static final UltimateStacker plugin = UltimateStacker.getInstance(); private SSpawner sSpawner;
public SpawnerStackImpl(Location location, int amount) { public SpawnerStackImpl(Location location, int amount) {
super(location); this.location = location;
this.amount = amount; this.amount = amount;
this.sSpawner = new SSpawner(this.location);
} }
@Override @Override
public int getAmount() { public int getAmount() {
return amount; return this.amount;
} }
@Override @Override
public boolean isValid() { public boolean isValid() {
return CompatibleMaterial.getMaterial(location.getBlock()) == CompatibleMaterial.SPAWNER; return CompatibleMaterial.getMaterial(this.location.getBlock()) == CompatibleMaterial.SPAWNER;
} }
@Override @Override
public void setAmount(int amount) { public void setAmount(int amount) {
this.amount = amount; this.amount = amount;
plugin.getPluginDataManager().save(this); UltimateStacker.getInstance().getPluginDataManager().save(this);
} }
@Override @Override
@ -72,18 +70,18 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
Random random = new Random(); Random random = new Random();
int count = 0; int count = 0;
for (int i = 0; i < getAmount(); i++) { for (int i = 0; i < getAmount(); ++i) {
count += random.nextInt(3) + 1; count += random.nextInt(3) + 1;
} }
return count; return count;
} }
public int spawn(int amountToSpawn, EntityType... types) { public int spawn(int amountToSpawn, EntityType... types) {
return super.spawn(amountToSpawn, types); return this.sSpawner.spawn(amountToSpawn, types);
} }
public int spawn(int amountToSpawn, String particle, Set<CompatibleMaterial> canSpawnOn, SpawnedEntity spawned, EntityType... types) { public int spawn(int amountToSpawn, String particle, Set<CompatibleMaterial> canSpawnOn, SpawnedEntity spawned, EntityType... types) {
return super.spawn(amountToSpawn, particle, canSpawnOn, spawned, types); return this.sSpawner.spawn(amountToSpawn, particle, canSpawnOn, spawned, types);
} }
public int getId() { public int getId() {
@ -93,9 +91,9 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
@Override @Override
public Map<String, Object> serialize() { public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("id", id); map.put("id", this.id);
map.put("amount", amount); map.put("amount", this.amount);
map.putAll(new SerializedLocation(location).asMap()); map.putAll(new SerializedLocation(this.location).asMap());
return map; return map;
} }
@ -103,7 +101,8 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
public Data deserialize(Map<String, Object> map) { public Data deserialize(Map<String, Object> map) {
this.id = (int) map.get("id"); this.id = (int) map.get("id");
this.amount = (int) map.get("amount"); this.amount = (int) map.get("amount");
this.initFromData(map); this.location = SerializedLocation.of(map);
this.sSpawner = new SSpawner(this.location);
return this; return this;
} }
@ -117,17 +116,17 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
} }
public Location getLocation() { public Location getLocation() {
return location.clone(); return this.location.clone();
} }
@Override @Override
public String getHologramName() { public String getHologramName() {
if (!(location.getBlock().getState() instanceof CreatureSpawner)) { if (!(this.location.getBlock().getState() instanceof CreatureSpawner)) {
UltimateStackerAPI.getSpawnerStackManager().removeSpawner(location); UltimateStackerAPI.getSpawnerStackManager().removeSpawner(this.location);
return null; return null;
} }
CreatureSpawner creatureSpawner = (CreatureSpawner) location.getBlock().getState(); CreatureSpawner creatureSpawner = (CreatureSpawner) this.location.getBlock().getState();
return Methods.compileSpawnerName(creatureSpawner.getSpawnedType(), amount); return Methods.compileSpawnerName(creatureSpawner.getSpawnedType(), this.amount);
} }
@Override @Override
@ -136,36 +135,33 @@ public class SpawnerStackImpl extends SSpawner implements SpawnerStack {
} }
public int getX() { public int getX() {
return location.getBlockX(); return this.location.getBlockX();
} }
public int getY() { public int getY() {
return location.getBlockY(); return this.location.getBlockY();
} }
public int getZ() { public int getZ() {
return location.getBlockZ(); return this.location.getBlockZ();
} }
public World getWorld() { public World getWorld() {
return location.getWorld(); return this.location.getWorld();
} }
@Override @Override
public String getHologramId() { public String getHologramId() {
return "UltimateStacker-" + uniqueId; return "UltimateStacker-" + this.uniqueHologramId;
} }
@Override @Override
public String toString() { public String toString() {
return "SpawnerStackImpl:{" return "SpawnerStackImpl{" +
+ "Amount:\"" + amount + "\"," "uniqueHologramId=" + this.uniqueHologramId +
+ "Location:{" ", id=" + this.id +
+ "World:\"" + location.getWorld().getName() + "\"," ", location=" + this.location +
+ "X:" + location.getBlockX() + "," ", amount=" + this.amount +
+ "Y:" + location.getBlockY() + "," '}';
+ "Z:" + location.getBlockZ()
+ "}"
+ "}";
} }
} }