UltimateStacker/src/main/java/com/songoda/ultimatestacker/stackable/spawner/SpawnerStack.java

127 lines
3.6 KiB
Java
Raw Normal View History

2020-08-25 01:01:11 +02:00
package com.songoda.ultimatestacker.stackable.spawner;
2018-11-06 04:33:10 +01:00
2019-09-03 22:38:00 +02:00
import com.songoda.core.compatibility.ServerVersion;
2018-11-06 05:41:58 +01:00
import com.songoda.ultimatestacker.UltimateStacker;
2019-09-07 23:55:16 +02:00
import com.songoda.ultimatestacker.settings.Settings;
2020-08-25 01:01:11 +02:00
import com.songoda.ultimatestacker.stackable.Hologramable;
import com.songoda.ultimatestacker.stackable.Stackable;
import com.songoda.ultimatestacker.utils.Methods;
2018-11-06 05:41:58 +01:00
import com.songoda.ultimatestacker.utils.Reflection;
import org.bukkit.Bukkit;
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;
import java.util.Random;
2020-08-25 01:01:11 +02:00
public class SpawnerStack implements Stackable, Hologramable {
2018-11-06 04:33:10 +01:00
2019-08-02 20:51:15 +02:00
private int id;
2020-08-05 23:05:57 +02:00
private boolean initialized = false;
2019-08-02 20:51:15 +02:00
2018-11-06 04:33:10 +01:00
private final Location location;
2020-08-05 23:05:57 +02:00
private int amount;
2018-11-06 04:33:10 +01:00
2020-08-25 01:01:11 +02:00
private static final UltimateStacker plugin = UltimateStacker.getInstance();
2018-11-06 04:33:10 +01:00
public SpawnerStack(Location location, int amount) {
this.location = location;
2020-08-05 23:05:57 +02:00
this.amount = amount;
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 amount;
}
public void setAmount(int amount) {
this.amount = amount;
2019-08-02 15:59:10 +02:00
plugin.getDataManager().updateSpawner(this);
2020-08-05 23:05:57 +02:00
}
2018-11-06 04:33:10 +01:00
2020-08-05 23:05:57 +02:00
public void updateAmount() {
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (!(location.getBlock().getState() instanceof CreatureSpawner)) return;
2019-09-07 23:55:16 +02:00
int count = Settings.STACK_ENTITIES.getBoolean()
&& !plugin.getStackingTask().isWorldDisabled(location.getWorld()) ? 1 : calculateSpawnCount();
int maxNearby = amount > 6 ? amount + 3 : 6;
CreatureSpawner creatureSpawner = (CreatureSpawner) location.getBlock().getState();
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) {
creatureSpawner.setMaxNearbyEntities(maxNearby);
creatureSpawner.setSpawnCount(count);
} else {
Reflection.updateSpawner(creatureSpawner, count, maxNearby);
}
creatureSpawner.update();
}, 1L);
2018-11-06 04:33:10 +01:00
}
2019-08-02 20:51:15 +02:00
public int calculateSpawnCount() {
Random random = new Random();
int count = 0;
2019-08-10 19:10:58 +02:00
for (int i = 0; i < getAmount(); i++) {
count += random.nextInt(3) + 1;
2019-08-02 20:51:15 +02:00
}
return count;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
2019-08-02 15:59:10 +02:00
public Location getLocation() {
return location.clone();
}
2020-08-25 01:01:11 +02:00
@Override
public String getHologramName() {
CreatureSpawner creatureSpawner = (CreatureSpawner) location.getBlock().getState();
return Methods.compileSpawnerName(creatureSpawner.getSpawnedType(), amount);
}
@Override
public boolean areHologramsEnabled() {
return Settings.SPAWNER_HOLOGRAMS.getBoolean();
}
2019-08-02 15:59:10 +02:00
public int getX() {
return location.getBlockX();
}
public int getY() {
return location.getBlockY();
}
public int getZ() {
return location.getBlockZ();
}
public World getWorld() {
return location.getWorld();
}
2020-08-05 23:05:57 +02:00
public void initialize() {
if (!initialized) {
updateAmount();
this.initialized = true;
}
}
2018-11-06 04:33:10 +01:00
@Override
public String toString() {
return "SpawnerStack:{"
+ "Amount:\"" + amount + "\","
+ "Location:{"
+ "World:\"" + location.getWorld().getName() + "\","
+ "X:" + location.getBlockX() + ","
+ "Y:" + location.getBlockY() + ","
+ "Z:" + location.getBlockZ()
+ "}"
+ "}";
}
}