mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-11-27 04:25:21 +01:00
spawner fix
This commit is contained in:
parent
93a64e0362
commit
c827354d9e
@ -3,13 +3,9 @@ package com.songoda.ultimatestacker.spawner;
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
import com.songoda.ultimatestacker.utils.Reflection;
|
||||
import com.songoda.ultimatestacker.utils.ServerVersion;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.CreatureSpawner;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class SpawnerStack {
|
||||
|
||||
private final Location location;
|
||||
@ -31,8 +27,15 @@ public class SpawnerStack {
|
||||
public void setAmount(int amount) {
|
||||
this.amount = amount;
|
||||
|
||||
int count = 4 * amount;
|
||||
int maxNearby = amount > 6 ? amount + 3 : 6;
|
||||
CreatureSpawner creatureSpawner = (CreatureSpawner)location.getBlock().getState();
|
||||
Reflection.setRange(creatureSpawner, 4 * amount, amount > 6 ? amount + 3 : 6);
|
||||
if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_12)) {
|
||||
creatureSpawner.setMaxNearbyEntities(maxNearby);
|
||||
creatureSpawner.setSpawnCount(count);
|
||||
} else {
|
||||
Reflection.updateSpawner(creatureSpawner, count, maxNearby);
|
||||
}
|
||||
creatureSpawner.update();
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,9 @@ import java.lang.reflect.Method;
|
||||
public class Reflection {
|
||||
private static Class<?> clazzCraftCreatureSpawner, clazzTileEntityMobSpawner = null;
|
||||
private static Method methodGetTileEntity, methodGetSpawner;
|
||||
private static Field fieldSpawnRange, fieldMaxNearbyEntities;
|
||||
private static Field fieldSpawnount, fieldMaxNearbyEntities;
|
||||
|
||||
public static CreatureSpawner setRange(CreatureSpawner creatureSpawner, int amount, int max) {
|
||||
public static CreatureSpawner updateSpawner(CreatureSpawner creatureSpawner, int count, int max) {
|
||||
try {
|
||||
if (creatureSpawner == null) return creatureSpawner;
|
||||
if (clazzCraftCreatureSpawner == null) {
|
||||
@ -21,16 +21,16 @@ public class Reflection {
|
||||
Class<?> clazzMobSpawnerAbstract = Class.forName("net.minecraft.server." + ver + ".MobSpawnerAbstract");
|
||||
methodGetTileEntity = clazzCraftCreatureSpawner.getDeclaredMethod("getTileEntity");
|
||||
methodGetSpawner = clazzTileEntityMobSpawner.getDeclaredMethod("getSpawner");
|
||||
fieldSpawnRange = clazzMobSpawnerAbstract.getDeclaredField("spawnRange");
|
||||
fieldSpawnRange.setAccessible(true);
|
||||
fieldMaxNearbyEntities = clazzMobSpawnerAbstract.getDeclaredField("maxNearbyEntities");
|
||||
fieldSpawnRange.setAccessible(true);
|
||||
fieldSpawnount = clazzMobSpawnerAbstract.getDeclaredField("spawnRange");
|
||||
fieldSpawnount.setAccessible(true);
|
||||
fieldMaxNearbyEntities = clazzMobSpawnerAbstract.getDeclaredField("spawnCount");
|
||||
fieldSpawnount.setAccessible(true);
|
||||
}
|
||||
|
||||
Object objCraftCreatureSpawner = clazzCraftCreatureSpawner.cast(creatureSpawner);
|
||||
Object objTileEntityMobSpawner = clazzTileEntityMobSpawner.cast(methodGetTileEntity.invoke(objCraftCreatureSpawner));
|
||||
Object objMobSpawnerAbstract = methodGetSpawner.invoke(objTileEntityMobSpawner);
|
||||
fieldSpawnRange.set(objMobSpawnerAbstract, amount);
|
||||
fieldSpawnount.set(objMobSpawnerAbstract, count);
|
||||
fieldMaxNearbyEntities.set(objMobSpawnerAbstract, max);
|
||||
|
||||
} catch (ReflectiveOperationException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user