Updated Spawning system to use the version that is in core.

This commit is contained in:
Brianna 2021-05-27 14:21:57 -05:00
parent 5caf01368e
commit 5a25fa384f
6 changed files with 39 additions and 156 deletions

View File

@ -11,6 +11,7 @@ import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.MySQLConnector;
import com.songoda.core.database.SQLiteConnector;
import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EntityStackerManager;
import com.songoda.core.hooks.HologramManager;
import com.songoda.core.hooks.WorldGuardHook;
import com.songoda.core.utils.TextUtils;
@ -196,6 +197,7 @@ public class UltimateStacker extends SongodaPlugin {
stackerHooks.add(new JobsHook());
HologramManager.load(this);
EntityStackerManager.load();
// Database stuff, go!
try {

View File

@ -4,7 +4,6 @@ import com.songoda.core.compatibility.CompatibleHand;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.nms.NmsManager;
import com.songoda.core.nms.nbt.NBTItem;
import com.songoda.core.utils.PlayerUtils;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.events.SpawnerBreakEvent;
import com.songoda.ultimatestacker.events.SpawnerPlaceEvent;

View File

@ -1,20 +1,20 @@
package com.songoda.ultimatestacker.listeners;
import com.songoda.core.compatibility.CompatibleHand;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.nms.NmsManager;
import com.songoda.core.utils.EntityUtils;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.settings.Settings;
import com.songoda.ultimatestacker.stackable.entity.EntityStack;
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStack;
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStackManager;
import com.songoda.ultimatestacker.utils.Methods;
import com.songoda.ultimatestacker.utils.ReflectionUtil;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -37,18 +37,36 @@ public class SpawnerListeners implements Listener {
if (!Settings.STACK_ENTITIES.getBoolean()
|| !plugin.spawnersEnabled()
|| plugin.getStackingTask().isWorldDisabled(event.getLocation().getWorld())) return;
SpawnerStackManager spawnerStackManager = plugin.getSpawnerStackManager();
if (!spawnerStackManager.isSpawner(event.getSpawner().getLocation())) return;
SpawnerStack spawnerStack = spawnerStackManager.getSpawner(event.getSpawner().getLocation());
Entity entity = event.getEntity();
if (entity.getType() == EntityType.FIREWORK) return;
if (entity.getVehicle() != null) {
entity.getVehicle().remove();
entity.remove();
}
spawnerStack.initialize();
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
if (entity.getPassengers().size() != 0) {
for (Entity e : entity.getPassengers()) {
e.remove();
}
entity.remove();
}
}
entity.remove();
EntityStack stack = plugin.getEntityStackManager().addStack((LivingEntity)event.getEntity());
stack.createDuplicates(spawnerStack.calculateSpawnCount());
stack.updateStack();
Location location = event.getSpawner().getLocation();
plugin.getStackingTask().attemptSplit(stack, (LivingEntity) event.getEntity());
SpawnerStack spawnerStack = spawnerStackManager.getSpawner(location);
spawnerStack.spawn(spawnerStack.calculateSpawnCount(), "EXPLOSION_NORMAL", null, (e) -> {
if (Settings.NO_AI.getBoolean())
EntityUtils.setUnaware(e);
return true;
}, event.getEntityType());
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -92,7 +110,7 @@ public class SpawnerListeners implements Listener {
.replace("MOOSHROOM", "MUSHROOM_COW")
.replace("ZOMBIE_PIGMAN", "PIG_ZOMBIE"));
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) {
String str = ReflectionUtil.getNBTTagCompound(ReflectionUtil.getNMSItemStack(event.getItem())).toString();
String str = NmsManager.getNbt().of(event.getItem()).toString();
if (str.contains("minecraft:"))
entityType = EntityType.fromName(str.substring(str.indexOf("minecraft:") + 10, str.indexOf("\"}")));
else

View File

@ -234,6 +234,9 @@ public class Settings {
public static final ConfigSetting EXPLOSION_DROP_CHANCE_CREEPER = new ConfigSetting(config, "Spawners.Chance On Creeper Explosion", "100%",
"Chance of a creeper explosion dropping a spawner.");
public static final ConfigSetting NO_AI = new ConfigSetting(config, "Spawners.Nerf Spawner Mobs", false,
"If enabled mobs spawned by spawners will not move or attack.");
public static final ConfigSetting NAME_FORMAT_SPAWNER = new ConfigSetting(config, "Spawners.Name Format", "&f{TYPE} Spawner &6{AMT}x",
"The text displayed above a stacked spawner where {TYPE} refers to",
"The entities type and {AMT} is the amount currently stacked.");

View File

@ -1,32 +1,28 @@
package com.songoda.ultimatestacker.stackable.spawner;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.world.SSpawner;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.settings.Settings;
import com.songoda.ultimatestacker.stackable.Hologramable;
import com.songoda.ultimatestacker.utils.Methods;
import com.songoda.ultimatestacker.utils.ReflectionUtil;
import com.songoda.ultimatestacker.utils.Stackable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.CreatureSpawner;
import java.util.Random;
public class SpawnerStack implements Stackable, Hologramable {
public class SpawnerStack extends SSpawner implements Stackable, Hologramable {
private int id;
private boolean initialized = false;
private final Location location;
private int amount;
private static final UltimateStacker plugin = UltimateStacker.getInstance();
public SpawnerStack(Location location, int amount) {
this.location = location;
super(location);
this.amount = amount;
}
@ -45,23 +41,6 @@ public class SpawnerStack implements Stackable, Hologramable {
plugin.getDataManager().updateSpawner(this);
}
public void updateAmount() {
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (!(location.getBlock().getState() instanceof CreatureSpawner)) return;
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 {
ReflectionUtil.updateSpawner(creatureSpawner, count, maxNearby);
}
creatureSpawner.update();
}, 1L);
}
public int calculateSpawnCount() {
Random random = new Random();
int count = 0;
@ -114,13 +93,6 @@ public class SpawnerStack implements Stackable, Hologramable {
return location.getWorld();
}
public void initialize() {
if (!initialized) {
updateAmount();
this.initialized = true;
}
}
@Override
public String toString() {
return "SpawnerStack:{"

View File

@ -1,111 +0,0 @@
package com.songoda.ultimatestacker.utils;
import org.bukkit.Bukkit;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionUtil {
private static Class<?> clazzCraftCreatureSpawner, clazzTileEntityMobSpawner = null;
private static Method methodGetTileEntity, methodGetSpawner;
private static Field fieldSpawnount, fieldMaxNearbyEntities, fieldSpawner;
public static CreatureSpawner updateSpawner(CreatureSpawner creatureSpawner, int count, int max) {
if (!Bukkit.getServer().getClass().getPackage().getName().contains("1.8")) {
try {
if (creatureSpawner == null) return null;
if (clazzCraftCreatureSpawner == null) {
String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23);
clazzCraftCreatureSpawner = Class.forName("org.bukkit.craftbukkit." + ver + ".block.CraftCreatureSpawner");
clazzTileEntityMobSpawner = Class.forName("net.minecraft.server." + ver + ".TileEntityMobSpawner");
Class<?> clazzMobSpawnerAbstract = Class.forName("net.minecraft.server." + ver + ".MobSpawnerAbstract");
methodGetTileEntity = clazzCraftCreatureSpawner.getDeclaredMethod("getTileEntity");
methodGetSpawner = clazzTileEntityMobSpawner.getDeclaredMethod("getSpawner");
fieldSpawnount = clazzMobSpawnerAbstract.getDeclaredField("spawnCount");
fieldSpawnount.setAccessible(true);
fieldMaxNearbyEntities = clazzMobSpawnerAbstract.getDeclaredField("maxNearbyEntities");
fieldMaxNearbyEntities.setAccessible(true);
}
Object objCraftCreatureSpawner = clazzCraftCreatureSpawner.cast(creatureSpawner);
Object objTileEntityMobSpawner = clazzTileEntityMobSpawner.cast(methodGetTileEntity.invoke(objCraftCreatureSpawner));
Object objMobSpawnerAbstract = methodGetSpawner.invoke(objTileEntityMobSpawner);
fieldSpawnount.set(objMobSpawnerAbstract, count);
fieldMaxNearbyEntities.set(objMobSpawnerAbstract, max);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return creatureSpawner;
} else {
try {
if (clazzCraftCreatureSpawner == null) {
String ver = Bukkit.getServer().getClass().getPackage().getName().substring(23);
clazzCraftCreatureSpawner = Class.forName("org.bukkit.craftbukkit." + ver + ".block.CraftCreatureSpawner");
clazzTileEntityMobSpawner = Class.forName("net.minecraft.server." + ver + ".TileEntityMobSpawner");
Class<?> clazzMobSpawnerAbstract = Class.forName("net.minecraft.server." + ver + ".MobSpawnerAbstract");
methodGetSpawner = clazzTileEntityMobSpawner.getDeclaredMethod("getSpawner");
fieldSpawner = clazzCraftCreatureSpawner.getDeclaredField("spawner");
fieldSpawner.setAccessible(true);
fieldSpawnount = clazzMobSpawnerAbstract.getDeclaredField("spawnCount");
fieldSpawnount.setAccessible(true);
fieldMaxNearbyEntities = clazzMobSpawnerAbstract.getDeclaredField("maxNearbyEntities");
fieldMaxNearbyEntities.setAccessible(true);
}
Object objcraftCreatureSpawner = clazzCraftCreatureSpawner.cast(creatureSpawner);
Object objTileEntityMobSpawner = fieldSpawner.get(objcraftCreatureSpawner);
Object objMobSpawnerAbstract = methodGetSpawner.invoke(objTileEntityMobSpawner);
fieldSpawnount.set(objMobSpawnerAbstract, count);
fieldMaxNearbyEntities.set(objMobSpawnerAbstract, max);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return creatureSpawner;
}
}
public static Object getNMSItemStack(ItemStack item) {
Class<?> cis = getCraftItemStack();
java.lang.reflect.Method methodAsNMSCopy;
try {
methodAsNMSCopy = cis.getMethod("asNMSCopy", ItemStack.class);
Object answer = methodAsNMSCopy.invoke(cis, item);
return answer;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Object getNBTTagCompound(Object nmsitem) {
Class<?> c = nmsitem.getClass();
java.lang.reflect.Method methodGetTag;
try {
methodGetTag = c.getMethod("getTag");
return methodGetTag.invoke(nmsitem);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Class<?> getCraftItemStack() {
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
try {
return Class.forName("org.bukkit.craftbukkit." + version + ".inventory.CraftItemStack");
} catch (Exception ex) {
System.out.println("Error in ItemNBTAPI! (Outdated plugin?)");
ex.printStackTrace();
return null;
}
}
}