2018-11-06 04:33:10 +01:00
|
|
|
package com.songoda.ultimatestacker.spawner;
|
|
|
|
|
2018-11-06 05:41:58 +01:00
|
|
|
import com.songoda.ultimatestacker.UltimateStacker;
|
|
|
|
import com.songoda.ultimatestacker.utils.Reflection;
|
|
|
|
import com.songoda.ultimatestacker.utils.ServerVersion;
|
2019-07-17 14:56:50 +02:00
|
|
|
import com.songoda.ultimatestacker.utils.settings.Setting;
|
2019-06-27 21:06:35 +02:00
|
|
|
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;
|
|
|
|
|
2019-07-17 14:56:50 +02:00
|
|
|
import java.util.Random;
|
|
|
|
|
2018-11-06 04:33:10 +01:00
|
|
|
public class SpawnerStack {
|
|
|
|
|
|
|
|
private final Location location;
|
2019-01-23 19:01:31 +01:00
|
|
|
private int amount = 1;
|
2018-11-06 04:33:10 +01:00
|
|
|
|
|
|
|
public SpawnerStack(Location location, int amount) {
|
|
|
|
this.location = location;
|
|
|
|
setAmount(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getAmount() {
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAmount(int amount) {
|
2019-07-31 21:47:01 +02:00
|
|
|
UltimateStacker plugin = UltimateStacker.getInstance();
|
2018-11-06 04:33:10 +01:00
|
|
|
this.amount = amount;
|
2019-08-02 15:59:10 +02:00
|
|
|
plugin.getDataManager().updateSpawner(this);
|
2018-11-06 04:33:10 +01:00
|
|
|
|
2019-07-31 21:47:01 +02:00
|
|
|
Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
2019-06-28 22:03:13 +02:00
|
|
|
if (!(location.getBlock().getState() instanceof CreatureSpawner)) return;
|
2019-07-31 21:47:01 +02:00
|
|
|
int count = Setting.STACK_ENTITIES.getBoolean()
|
|
|
|
&& !plugin.getStackingTask().isWorldDisabled(location.getWorld()) ? 1 : calculateSpawnCount();
|
2019-06-27 21:06:35 +02:00
|
|
|
int maxNearby = amount > 6 ? amount + 3 : 6;
|
|
|
|
CreatureSpawner creatureSpawner = (CreatureSpawner) location.getBlock().getState();
|
|
|
|
if (UltimateStacker.getInstance().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 15:59:10 +02:00
|
|
|
public Location getLocation() {
|
|
|
|
return location.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX() {
|
|
|
|
return location.getBlockX();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getY() {
|
|
|
|
return location.getBlockY();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getZ() {
|
|
|
|
return location.getBlockZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public World getWorld() {
|
|
|
|
return location.getWorld();
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
+ "}"
|
|
|
|
+ "}";
|
|
|
|
}
|
2019-07-17 14:56:50 +02:00
|
|
|
|
|
|
|
public int calculateSpawnCount() {
|
|
|
|
Random random = new Random();
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < getAmount(); i ++) {
|
|
|
|
count += random.nextInt(3 - 1 + 1) + 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2018-11-06 04:33:10 +01:00
|
|
|
}
|