Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spawner delay feature #3239

Merged
merged 12 commits into from
Aug 5, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.StringUtil;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.nms.refl.ReflUtil;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;

import static com.earth2me.essentials.I18n.tl;
Expand Down Expand Up @@ -57,8 +62,31 @@ protected void run(final Server server, final User user, final String commandLab
final Trade charge = new Trade("spawner-" + mob.name.toLowerCase(Locale.ENGLISH), ess);
charge.isAffordableFor(user);
try {
CreatureSpawner spawner = (CreatureSpawner) target.getBlock().getState();
Block block = target.getBlock();
CreatureSpawner spawner = (CreatureSpawner) block.getState();
spawner.setSpawnedType(mob.getType());
if (delay > 0) {
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_12_2_R01)) {
spawner.setMinSpawnDelay(0);
spawner.setMaxSpawnDelay(Integer.MAX_VALUE);
spawner.setMinSpawnDelay(delay);
spawner.setMaxSpawnDelay(delay);
} else {
Class<?> craftWorld = ReflUtil.getOBCClass("CraftWorld");
Class<?> tileEntityMobSpawner = ReflUtil.getNMSClass("TileEntityMobSpawner");
Class<?> mobSpawnerAbstract = ReflUtil.getNMSClass("MobSpawnerAbstract");
Method getSpawner = tileEntityMobSpawner.getDeclaredMethod("getSpawner");
Method getTileEntityAt = craftWorld.getDeclaredMethod("getTileEntityAt", int.class, int.class, int.class);
Object craftTileEntity = getTileEntityAt.invoke(block.getWorld(), block.getX(), block.getY(), block.getZ());
Object nmsSpawner = getSpawner.invoke(craftTileEntity);
Field minSpawnDelay = ReflUtil.getFieldCached(mobSpawnerAbstract, "minSpawnDelay");
Field maxSpawnDelay = ReflUtil.getFieldCached(mobSpawnerAbstract, "maxSpawnDelay");
if (minSpawnDelay != null && maxSpawnDelay != null) {
minSpawnDelay.setInt(nmsSpawner, delay);
maxSpawnDelay.setInt(nmsSpawner, delay);
}
}
}
spawner.setDelay(delay);
spawner.update();
} catch (Throwable ex) {
Expand Down