Refactor spawn egg provider to separate classes

This commit is contained in:
vemacs 2016-03-10 09:28:08 -07:00
parent f1c57371a3
commit bdb81123d6
10 changed files with 107 additions and 16 deletions

View File

@ -123,7 +123,7 @@
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>1_9_R1SpawnEggProvider</artifactId>
<artifactId>1_9_R1Provider</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
</dependency>

View File

@ -34,11 +34,14 @@ import com.google.common.collect.Iterables;
import net.ess3.api.*;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.nms.SpawnEggProvider;
import net.ess3.nms.SpawnerProvider;
import net.ess3.nms.blockmeta.BlockMetaSpawnerProvider;
import net.ess3.nms.legacy.LegacySpawnEggProvider;
import net.ess3.nms.legacy.LegacySpawnerProvider;
import net.ess3.nms.v1_8_R1.v1_8_R1SpawnerProvider;
import net.ess3.nms.v1_8_R2.v1_8_R2SpawnerProvider;
import net.ess3.nms.v1_9_R1.v1_9_R1SpawnEggProvider;
import net.ess3.providers.ProviderFactory;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -98,6 +101,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private final transient List<String> vanishedPlayers = new ArrayList<>();
private transient Method oldGetOnlinePlayers;
private transient SpawnerProvider spawnerProvider;
private transient SpawnEggProvider spawnEggProvider;
public Essentials() {
}
@ -204,6 +208,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
v1_8_R1SpawnerProvider.class,
LegacySpawnerProvider.class
), "mob spawner").getProvider();
spawnEggProvider = new ProviderFactory<>(getLogger(),
Arrays.asList(
v1_9_R1SpawnEggProvider.class,
LegacySpawnEggProvider.class
), "spawn egg").getProvider();
reload();
} catch (YAMLException exception) {
if (pm.getPlugin("EssentialsUpdate") != null) {
@ -776,6 +785,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
return spawnerProvider;
}
@Override
public SpawnEggProvider getSpawnEggProvider() {
return spawnEggProvider;
}
private static class EssentialsWorldListener implements Listener, Runnable {
private transient final IEssentials ess;

View File

@ -3,7 +3,6 @@ package com.earth2me.essentials;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.StringUtil;
import net.ess3.api.IEssentials;
import net.ess3.nms.v1_9_R1.SpawnEgg1_9;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
@ -12,7 +11,6 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.material.SpawnEgg;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
@ -152,11 +150,7 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb {
} catch (IllegalArgumentException e) {
throw new Exception("Can't spawn entity ID " + metaData + " from spawn eggs.");
}
try {
retval = new SpawnEgg1_9(type).toItemStack();
} catch (Throwable t) {
retval = new SpawnEgg(type).toItemStack();
}
retval = ess.getSpawnEggProvider().createEggItem(type);
} else {
retval.setDurability(metaData);
}

View File

@ -1,5 +1,8 @@
package net.ess3.api;
import net.ess3.nms.SpawnEggProvider;
public interface IEssentials extends com.earth2me.essentials.IEssentials {
SpawnEggProvider getSpawnEggProvider();
}

View File

@ -10,7 +10,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>1_9_R1SpawnEggProvider</artifactId>
<artifactId>1_9_R1Provider</artifactId>
<repositories>
<repository>
@ -26,5 +26,10 @@
<version>1.9-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
</project>

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package net.ess3.nms.v1_9_R1;
package net.ess3.nms.v1_9_R1.nms;
import net.minecraft.server.v1_9_R1.NBTTagCompound;
import org.bukkit.Material;
@ -92,11 +92,11 @@ public class SpawnEgg1_9 {
* @param item - ItemStack, quantity is disregarded
* @return SpawnEgg 1.9
*/
public static SpawnEgg1_9 fromItemStack(ItemStack item) {
public static SpawnEgg1_9 fromItemStack(ItemStack item) throws IllegalArgumentException {
if (item == null)
throw new IllegalArgumentException("item cannot be null");
throw new IllegalArgumentException("Item cannot be null");
if (item.getType() != Material.MONSTER_EGG)
throw new IllegalArgumentException("item is not a monster egg");
throw new IllegalArgumentException("Item is not a monster egg");
net.minecraft.server.v1_9_R1.ItemStack stack = CraftItemStack.asNMSCopy(item);
NBTTagCompound tagCompound = stack.getTag();
if (tagCompound != null) {
@ -105,10 +105,10 @@ public class SpawnEgg1_9 {
if (type != null) {
return new SpawnEgg1_9(type);
} else {
return null;
throw new IllegalArgumentException("Unable to parse type from item");
}
} else {
return null;
throw new IllegalArgumentException("Item is lacking tag compound");
}
}
}

View File

@ -0,0 +1,23 @@
package net.ess3.nms.v1_9_R1;
import net.ess3.nms.SpawnEggProvider;
import net.ess3.nms.v1_9_R1.nms.SpawnEgg1_9;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
public class v1_9_R1SpawnEggProvider extends SpawnEggProvider {
@Override
public ItemStack createEggItem(EntityType type) throws IllegalArgumentException {
return new SpawnEgg1_9(type).toItemStack();
}
@Override
public EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException {
return SpawnEgg1_9.fromItemStack(eggItem).getSpawnedType();
}
@Override
public String getHumanName() {
return "CraftBukkit 1.9 NMS-based provider";
}
}

View File

@ -0,0 +1,30 @@
package net.ess3.nms.legacy;
import net.ess3.nms.SpawnEggProvider;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import org.bukkit.material.SpawnEgg;
public class LegacySpawnEggProvider extends SpawnEggProvider {
@Override
public ItemStack createEggItem(EntityType type) throws IllegalArgumentException {
return new SpawnEgg(type).toItemStack();
}
@Override
@SuppressWarnings("deprecation")
public EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException {
MaterialData data = eggItem.getData();
if (data instanceof SpawnEgg) {
return ((SpawnEgg) data).getSpawnedType();
} else {
throw new IllegalArgumentException("Item is missing data");
}
}
@Override
public String getHumanName() {
return "legacy item data provider";
}
}

View File

@ -0,0 +1,22 @@
package net.ess3.nms;
import net.ess3.providers.Provider;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
public abstract class SpawnEggProvider implements Provider {
public abstract ItemStack createEggItem(EntityType type) throws IllegalArgumentException;
public abstract EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException;
@Override
public boolean tryProvider() {
try {
EntityType type = EntityType.CREEPER;
ItemStack is = createEggItem(type);
EntityType readType = getSpawnedType(is);
return type == readType;
} catch (Throwable t) {
return false;
}
}
}

View File

@ -47,7 +47,7 @@
<module>nms/1_8_R1Provider</module>
<module>nms/1_8_R2Provider</module>
<module>nms/LegacyProvider</module>
<module>nms/1_9_R1SpawnEggProvider</module>
<module>nms/1_9_R1Provider</module>
</modules>
<dependencies>