From 1cb9ddf9fe66a8b852f52f567334876395a4129f Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sun, 23 Jun 2013 16:11:24 +0100 Subject: [PATCH] [Feature] Allow the placing of spawners with predefined network id's (/i spawner:50) [Permission] essentials.spawnerconvert. - Allow the placing of specific mobspawners with premade network id's. --- .../essentials/EssentialsBlockListener.java | 40 +++++++++++++++---- .../src/com/earth2me/essentials/Mob.java | 7 ++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java b/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java index 1dd37ef2d..19258c55b 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsBlockListener.java @@ -1,7 +1,12 @@ package com.earth2me.essentials; import com.earth2me.essentials.utils.LocationUtil; +import java.util.Locale; import org.bukkit.GameMode; +import org.bukkit.Material; +import org.bukkit.block.BlockState; +import org.bukkit.block.CreatureSpawner; +import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -28,19 +33,38 @@ public class EssentialsBlockListener implements Listener { return; } + + if (is.getType() == Material.MOB_SPAWNER && event.getItemInHand() != null && event.getPlayer() != null + && event.getItemInHand().getType() == Material.MOB_SPAWNER) + { + final BlockState blockState = event.getBlockPlaced().getState(); + if (blockState instanceof CreatureSpawner) + { + final CreatureSpawner spawner = (CreatureSpawner)blockState; + final EntityType type = EntityType.fromId(event.getItemInHand().getData().getData()); + 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); + } + } + } + } + final User user = ess.getUser(event.getPlayer()); if (user.hasUnlimited(is) && user.getGameMode() == GameMode.SURVIVAL) { ess.scheduleSyncDelayedTask( new Runnable() - { - @Override - public void run() - { - user.getInventory().addItem(is); - user.updateInventory(); - } - }); + { + @Override + public void run() + { + user.getInventory().addItem(is); + user.updateInventory(); + } + }); } } } diff --git a/Essentials/src/com/earth2me/essentials/Mob.java b/Essentials/src/com/earth2me/essentials/Mob.java index b08281df4..3afcf1b85 100644 --- a/Essentials/src/com/earth2me/essentials/Mob.java +++ b/Essentials/src/com/earth2me/essentials/Mob.java @@ -50,6 +50,7 @@ public enum Mob MINECART_FURNACE("FurnaceMinecart", Enemies.NEUTRAL, EntityType.MINECART_FURNACE), MINECART_TNT("TNTMinecart", Enemies.NEUTRAL, EntityType.MINECART_TNT), MINECART_HOPPER("HopperMinecart", Enemies.NEUTRAL, EntityType.MINECART_HOPPER), + MINECART_MOB_SPAWNER("SpawnerMinecart", Enemies.NEUTRAL, EntityType.MINECART_MOB_SPAWNER), ENDERCRYSTAL("EnderCrystal", Enemies.NEUTRAL, EntityType.ENDER_CRYSTAL), EXPERIENCEORB("ExperienceOrb", Enemies.NEUTRAL, EntityType.EXPERIENCE_ORB); public static final Logger logger = Logger.getLogger("Minecraft"); @@ -73,12 +74,14 @@ public enum Mob final public Enemies type; final private EntityType bukkitType; private static final Map hashMap = new HashMap(); + private static final Map bukkitMap = new HashMap(); static { for (Mob mob : Mob.values()) { hashMap.put(mob.name.toLowerCase(Locale.ENGLISH), mob); + bukkitMap.put(mob.bukkitType, mob); } } @@ -127,6 +130,10 @@ public enum Mob return hashMap.get(name.toLowerCase(Locale.ENGLISH)); } + public static Mob fromBukkitType(final EntityType type) + { + return bukkitMap.get(type); + } public static class MobException extends Exception {