spawner fix

This commit is contained in:
Brianna O'Keefe 2018-11-29 22:08:34 -05:00
parent 93a64e0362
commit c827354d9e
2 changed files with 15 additions and 12 deletions

View File

@ -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();
}

View File

@ -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) {