UltimateStacker/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/stackable/spawner/SpawnerStackImpl.java

204 lines
5.7 KiB
Java
Raw Normal View History

2023-05-25 19:20:03 +02:00
package com.craftaro.ultimatestacker.stackable.spawner;
2018-11-06 04:33:10 +01:00
import com.craftaro.core.database.Data;
import com.craftaro.core.database.SerializedLocation;
import com.craftaro.core.nms.world.SpawnedEntity;
2024-01-07 12:16:27 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
2023-07-08 11:20:05 +02:00
import com.craftaro.core.utils.EntityUtils;
import com.craftaro.core.world.SSpawner;
2024-01-07 12:16:27 +01:00
import com.craftaro.third_party.org.jooq.impl.DSL;
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;
2018-11-06 04:33:10 +01:00
import org.bukkit.Location;
2019-08-02 15:59:10 +02:00
import org.bukkit.World;
2018-11-06 04:33:10 +01:00
import org.bukkit.block.CreatureSpawner;
2023-05-05 10:57:07 +02:00
import org.bukkit.entity.EntityType;
2018-11-06 04:33:10 +01:00
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
2023-05-25 19:20:03 +02:00
import java.util.Set;
2021-12-22 23:11:16 +01:00
import java.util.UUID;
public class SpawnerStackImpl implements SpawnerStack {
2018-11-06 04:33:10 +01:00
2023-07-08 11:20:05 +02:00
private final UUID uniqueHologramId = UUID.randomUUID();
2019-08-02 20:51:15 +02:00
private int id;
private Location location;
2020-08-05 23:05:57 +02:00
private int amount;
2018-11-06 04:33:10 +01:00
private SSpawner sSpawner;
2020-08-25 01:01:11 +02:00
public SpawnerStackImpl() {
}
2023-05-25 19:20:03 +02:00
public SpawnerStackImpl(Location location, int amount) {
2023-09-18 15:34:19 +02:00
this.id = UltimateStacker.getInstance().getDataManager().getNextId("spawners");
this.location = location;
2020-08-05 23:05:57 +02:00
this.amount = amount;
this.sSpawner = new SSpawner(this.location);
2018-11-06 04:33:10 +01:00
}
2020-08-25 01:01:11 +02:00
@Override
2018-11-06 04:33:10 +01:00
public int getAmount() {
return this.amount;
2018-11-06 04:33:10 +01:00
}
2020-08-26 15:40:51 +02:00
@Override
public boolean isValid() {
2023-06-29 11:18:18 +02:00
return XMaterial.matchXMaterial(this.location.getBlock().getType().name()).filter(material -> material == XMaterial.SPAWNER).isPresent();
2020-08-26 15:40:51 +02:00
}
2023-05-25 19:20:03 +02:00
@Override
2018-11-06 04:33:10 +01:00
public void setAmount(int amount) {
this.amount = amount;
2020-08-05 23:05:57 +02:00
}
2018-11-06 04:33:10 +01:00
2023-05-25 19:20:03 +02:00
@Override
public void add(int amount) {
this.amount += amount;
}
@Override
public void take(int amount) {
this.amount -= amount;
}
2023-07-08 11:20:05 +02:00
@Override
public int calculateSpawnCount(EntityType type, boolean ignoreRestrictions) {
if (!UltimateStacker.getInstance().getMobFile().getBoolean("Mobs." + type.name() + ".Enabled") && !ignoreRestrictions) {
2023-05-05 10:57:07 +02:00
return 0;
}
2019-08-02 20:51:15 +02:00
Random random = new Random();
int count = 0;
for (int i = 0; i < getAmount(); ++i) {
count += random.nextInt(3) + 1;
2019-08-02 20:51:15 +02:00
}
return count;
}
2023-07-08 11:20:05 +02:00
@Override
public int spawn() {
return spawn(-1, Settings.NO_AI.getBoolean());
2023-07-08 11:20:05 +02:00
}
@Override
public int spawn(boolean noAI) {
return spawn(-1, noAI);
}
@Override
public int spawn(int toSpawn, boolean noAI) {
CreatureSpawner creatureSpawner = (CreatureSpawner) this.location.getBlock().getState();
if (toSpawn <= 0) toSpawn = calculateSpawnCount(creatureSpawner.getSpawnedType(), false);
int finalToSpawn = toSpawn;
return spawn(toSpawn, "EXPLOSION_NORMAL", null, (e) -> {
if (noAI) {
EntityUtils.setUnaware(e);
}
UltimateStacker.getInstance().getEntityStackManager().createStackedEntity(e, finalToSpawn);
return true;
}, creatureSpawner.getSpawnedType());
}
2023-06-29 11:18:18 +02:00
@Override
2023-05-25 19:20:03 +02:00
public int spawn(int amountToSpawn, EntityType... types) {
return this.sSpawner.spawn(amountToSpawn, types);
2023-05-25 19:20:03 +02:00
}
2023-06-29 11:18:18 +02:00
@Override
public int spawn(int amountToSpawn, String particle, Set<XMaterial> canSpawnOn, SpawnedEntity spawned, EntityType... types) {
return this.sSpawner.spawn(amountToSpawn, particle, canSpawnOn, spawned, types);
2023-05-25 19:20:03 +02:00
}
2019-08-02 20:51:15 +02:00
public int getId() {
return this.id;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
map.put("id", this.id);
map.put("amount", this.amount);
map.putAll(new SerializedLocation(this.location).asMap());
return map;
}
@Override
public Data deserialize(Map<String, Object> map) {
this.id = (int) map.get("id");
this.amount = (int) map.get("amount");
this.location = SerializedLocation.of(map);
this.sSpawner = new SSpawner(this.location);
return this;
}
@Override
public String getTableName() {
return "spawners";
}
2019-08-02 20:51:15 +02:00
public void setId(int id) {
this.id = id;
}
2019-08-02 15:59:10 +02:00
public Location getLocation() {
return this.location.clone();
2019-08-02 15:59:10 +02:00
}
2020-08-25 01:01:11 +02:00
@Override
public String getHologramName() {
if (!(this.location.getBlock().getState() instanceof CreatureSpawner)) {
UltimateStackerApi.getSpawnerStackManager().removeSpawner(this.location);
2020-08-31 16:01:13 +02:00
return null;
}
CreatureSpawner creatureSpawner = (CreatureSpawner) this.location.getBlock().getState();
if (creatureSpawner.getSpawnedType() == null) {
return null;
}
return Methods.compileSpawnerName(creatureSpawner.getSpawnedType(), this.amount);
2020-08-25 01:01:11 +02:00
}
@Override
public boolean areHologramsEnabled() {
return Settings.SPAWNER_HOLOGRAMS.getBoolean();
}
2019-08-02 15:59:10 +02:00
public int getX() {
return this.location.getBlockX();
2019-08-02 15:59:10 +02:00
}
public int getY() {
return this.location.getBlockY();
2019-08-02 15:59:10 +02:00
}
public int getZ() {
return this.location.getBlockZ();
2019-08-02 15:59:10 +02:00
}
public World getWorld() {
return this.location.getWorld();
2019-08-02 15:59:10 +02:00
}
2021-12-22 23:11:16 +01:00
@Override
public String getHologramId() {
return "UltimateStacker-" + this.uniqueHologramId;
2021-12-22 23:11:16 +01:00
}
2018-11-06 04:33:10 +01:00
@Override
public String toString() {
return "SpawnerStackImpl{" +
"uniqueHologramId=" + this.uniqueHologramId +
", id=" + this.id +
", location=" + this.location +
", amount=" + this.amount +
'}';
2018-11-06 04:33:10 +01:00
}
}