This commit is contained in:
Brianna O'Keefe 2018-11-05 23:41:58 -05:00
parent a6b5b2b8dd
commit 116442a51e
8 changed files with 104 additions and 10 deletions

View File

@ -57,7 +57,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.13.2</version>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>com.songoda</groupId>

View File

@ -16,7 +16,9 @@ import com.songoda.ultimatestacker.storage.types.StorageMysql;
import com.songoda.ultimatestacker.storage.types.StorageYaml;
import com.songoda.ultimatestacker.entity.EntityStackManager;
import com.songoda.ultimatestacker.tasks.StackingTask;
import com.songoda.ultimatestacker.utils.ServerVersion;
import com.songoda.ultimatestacker.utils.SettingsManager;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -51,7 +53,8 @@ public class UltimateStacker extends JavaPlugin {
private CommandManager commandManager;
private StackingTask stackingTask;
private HologramHandler hologramHandler;
private ServerVersion serverVersion = ServerVersion.fromPackageName(Bukkit.getServer().getClass().getPackage().getName());
private Storage storage;
public void onDisable() {
@ -265,6 +268,22 @@ public class UltimateStacker extends JavaPlugin {
return null;
}
public ServerVersion getServerVersion() {
return serverVersion;
}
public boolean isServerVersion(ServerVersion version) {
return serverVersion == version;
}
public boolean isServerVersion(ServerVersion... versions) {
return ArrayUtils.contains(versions, serverVersion);
}
public boolean isServerVersionAtLeast(ServerVersion version) {
return serverVersion.ordinal() >= version.ordinal();
}
public HologramHandler getHologramHandler() {
return hologramHandler;
}

View File

@ -33,9 +33,9 @@ public class BlockListeners implements Listener {
public void onSpawnerInteract(PlayerInteractEvent event) {
Block block = event.getClickedBlock();
Player player = event.getPlayer();
ItemStack item = event.getPlayer().getInventory().getItemInMainHand();
ItemStack item = event.getPlayer().getInventory().getItemInHand();
if (block == null || item == null || block.getType() != Material.SPAWNER || item.getType() != Material.SPAWNER || event.getAction() == Action.LEFT_CLICK_BLOCK) return;
if (block == null || item == null || block.getType() != Material.MOB_SPAWNER || item.getType() != Material.MOB_SPAWNER || event.getAction() == Action.LEFT_CLICK_BLOCK) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
@ -85,7 +85,7 @@ public class BlockListeners implements Listener {
public void onSpawnerPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
if (block == null || block.getType() != Material.SPAWNER) return;
if (block == null || block.getType() != Material.MOB_SPAWNER) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
@ -98,7 +98,7 @@ public class BlockListeners implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event) {
Block block = event.getBlock();
if (block.getType() != Material.SPAWNER) return;
if (block.getType() != Material.MOB_SPAWNER) return;
if (!instance.getConfig().getBoolean("Main.Stack Spawners")) return;
@ -107,7 +107,7 @@ public class BlockListeners implements Listener {
EntityType blockType = cs.getSpawnedType();
Player player = event.getPlayer();
ItemStack item = player.getInventory().getItemInMainHand();
ItemStack item = player.getInventory().getItemInHand();
SpawnerStack stack = instance.getSpawnerStackManager().getSpawner(block);

View File

@ -1,8 +1,15 @@
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;
@ -25,7 +32,11 @@ public class SpawnerStack {
this.amount = amount;
CreatureSpawner creatureSpawner = (CreatureSpawner)location.getBlock().getState();
creatureSpawner.setSpawnCount(4 * amount);
if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_12)) {
creatureSpawner.setSpawnCount(4 * amount);
} else {
Reflection.setRange(creatureSpawner, 4 * amount);
}
creatureSpawner.update();
}

View File

@ -98,7 +98,7 @@ public class Methods {
}
public static ItemStack getSpawnerItem(EntityType entityType, int amount) {
ItemStack item = new ItemStack(Material.SPAWNER, 1);
ItemStack item = new ItemStack(Material.MOB_SPAWNER, 1);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(Methods.compileSpawnerName(entityType, amount));
CreatureSpawner cs = (CreatureSpawner) ((BlockStateMeta)meta).getBlockState();

View File

@ -0,0 +1,39 @@
package com.songoda.ultimatestacker.utils;
import org.bukkit.Bukkit;
import org.bukkit.block.CreatureSpawner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Reflection {
private static Class<?> clazzCraftCreatureSpawner, clazzTileEntityMobSpawner = null;
private static Method methodGetTileEntity, methodGetSpawner;
private static Field fieldSpawnRange;
public static CreatureSpawner setRange(CreatureSpawner creatureSpawner, int amount) {
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");
methodGetTileEntity = clazzCraftCreatureSpawner.getDeclaredMethod("getTileEntity");
methodGetSpawner = clazzTileEntityMobSpawner.getDeclaredMethod("getSpawner");
fieldSpawnRange = clazzMobSpawnerAbstract.getDeclaredField("spawnRange");
fieldSpawnRange.setAccessible(true);
}
Object objCraftCreatureSpawner = clazzCraftCreatureSpawner.cast(creatureSpawner);
Object objTileEntityMobSpawner = clazzTileEntityMobSpawner.cast(methodGetTileEntity.invoke(objCraftCreatureSpawner));
Object objMobSpawnerAbstract = methodGetSpawner.invoke(objTileEntityMobSpawner);
fieldSpawnRange.set(objMobSpawnerAbstract, amount);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
return creatureSpawner;
}
}

View File

@ -0,0 +1,26 @@
package com.songoda.ultimatestacker.utils;
public enum ServerVersion {
UNKNOWN("unknown_server_version"),
V1_7("org.bukkit.craftbukkit.v1_7"),
V1_8("org.bukkit.craftbukkit.v1_8"),
V1_9("org.bukkit.craftbukkit.v1_9"),
V1_10("org.bukkit.craftbukkit.v1_10"),
V1_11("org.bukkit.craftbukkit.v1_11"),
V1_12("org.bukkit.craftbukkit.v1_12"),
V1_13("org.bukkit.craftbukkit.v1_13");
private final String packagePrefix;
private ServerVersion(String packagePrefix) {
this.packagePrefix = packagePrefix;
}
public static ServerVersion fromPackageName(String packageName) {
for (ServerVersion version : values())
if (packageName.startsWith(version.packagePrefix)) return version;
return ServerVersion.UNKNOWN;
}
}

View File

@ -4,7 +4,6 @@ version: 1
depend: [Arconix]
main: com.songoda.ultimatestacker.UltimateStacker
author: songoda
api-version: 1.13
commands:
UltimateStacker:
description: I have no idea.