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
1 changed files with 39 additions and 43 deletions

View File

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