Add SpawnerUtil with primary Spigot 1.8.3+ EntityType setting, legacy data fallback. Fixes #22

This commit is contained in:
vemacs 2015-06-02 07:32:10 -06:00 committed by drtshock
parent be1acfdccd
commit 2273329e29
5 changed files with 77 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.SpawnerUtil;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
@ -95,6 +96,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient EssentialsTimer timer;
private final transient List<String> vanishedPlayers = new ArrayList<String>();
private transient Method oldGetOnlinePlayers;
private transient SpawnerUtil spawnerUtil;
public Essentials() {
}
@ -192,6 +194,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
execTimer.mark("Init(Worth/ItemDB)");
jails = new Jails(this);
confList.add(jails);
spawnerUtil = new SpawnerUtil(this);
reload();
} catch (YAMLException exception) {
if (pm.getPlugin("EssentialsUpdate") != null) {
@ -757,6 +760,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
});
}
@Override
public SpawnerUtil getSpawnerUtil() {
return spawnerUtil;
}
private static class EssentialsWorldListener implements Listener, Runnable {
private transient final IEssentials ess;

View File

@ -36,7 +36,7 @@ public class EssentialsBlockListener implements Listener {
final BlockState blockState = event.getBlockPlaced().getState();
if (blockState instanceof CreatureSpawner) {
final CreatureSpawner spawner = (CreatureSpawner) blockState;
final EntityType type = EntityType.fromId(event.getItemInHand().getData().getData());
final EntityType type = ess.getSpawnerUtil().getEntityType(event.getItemInHand());
if (type != null && Mob.fromBukkitType(type) != null) {
if (ess.getUser(event.getPlayer()).isAuthorized("essentials.spawnerconvert." + Mob.fromBukkitType(type).name().toLowerCase(Locale.ENGLISH))) {
spawner.setSpawnedType(type);

View File

@ -6,6 +6,7 @@ import com.earth2me.essentials.api.IWarps;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import com.earth2me.essentials.utils.SpawnerUtil;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -96,4 +97,6 @@ public interface IEssentials extends Plugin {
Collection<Player> getOnlinePlayers();
Iterable<User> getOnlineUsers();
SpawnerUtil getSpawnerUtil();
}

View File

@ -8,6 +8,7 @@ import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.potion.Potion;
@ -136,7 +137,15 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb {
}
final ItemStack retval = new ItemStack(mat);
retval.setAmount(mat.getMaxStackSize());
retval.setDurability(metaData);
if (mat == Material.MOB_SPAWNER) {
try {
ess.getSpawnerUtil().setEntityType(retval, EntityType.fromId(metaData));
} catch (IllegalArgumentException e) {
throw new Exception("Can't spawn entity ID " + metaData + " from mob spawners.");
}
} else {
retval.setDurability(metaData);
}
return retval;
}

View File

@ -0,0 +1,55 @@
package com.earth2me.essentials.utils;
import net.ess3.api.IEssentials;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
public class SpawnerUtil {
private boolean useMeta;
public SpawnerUtil(IEssentials ess) {
try {
ItemStack is = new ItemStack(Material.MOB_SPAWNER, 1);
ItemMeta meta = is.getItemMeta();
useMeta = meta instanceof BlockStateMeta;
} catch (Exception e) {
useMeta = false;
}
if (useMeta) {
ess.getLogger().info("Using BlockStateMeta for spawners");
} else {
ess.getLogger().info("Using legacy item data for spawners");
}
}
public ItemStack setEntityType(ItemStack is, EntityType type) throws IllegalArgumentException {
if (useMeta) {
// Supported in 1.8.3-R0.1-SNAPSHOT and above
BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
BlockState bs = bsm.getBlockState();
((CreatureSpawner) bs).setSpawnedType(type);
bsm.setBlockState(bs);
is.setItemMeta(bsm);
} else {
// Legacy behavior
is.setDurability(type.getTypeId());
}
return is;
}
public EntityType getEntityType(ItemStack is) {
ItemMeta meta = is.getItemMeta();
if (useMeta) {
BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState();
return bs.getSpawnedType();
} else {
return EntityType.fromId((int) is.getData().getData());
}
}
}