[Untested] [Experimental] [Need @drtshock to fix build server] Fully support all versions of Spigot and CraftBukkit from 1.8+

This commit is contained in:
vemacs 2015-06-17 10:25:46 -04:00
parent e4f6bb363d
commit 8793add589
19 changed files with 376 additions and 94 deletions

View File

@ -28,8 +28,22 @@
<showDeprecation>false</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>BOSEconomy</groupId>
@ -77,7 +91,45 @@
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>1.5.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>BlockMetaProvider</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>1_8_R1Provider</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>LegacyProvider</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>

View File

@ -30,13 +30,14 @@ 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.earth2me.essentials.utils.SpawnerProviderFactory;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import net.ess3.api.*;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import net.ess3.nms.SpawnerProvider;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
@ -69,8 +70,6 @@ import java.lang.reflect.Method;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.earth2me.essentials.I18n.tl;
@ -96,7 +95,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
private transient EssentialsTimer timer;
private final transient List<String> vanishedPlayers = new ArrayList<>();
private transient Method oldGetOnlinePlayers;
private transient SpawnerUtil spawnerUtil;
private transient SpawnerProvider spawnerProvider;
public Essentials() {
}
@ -186,7 +185,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);
spawnerProvider = new SpawnerProviderFactory(this).getProvider();
reload();
} catch (YAMLException exception) {
if (pm.getPlugin("EssentialsUpdate") != null) {
@ -746,8 +745,8 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
}
@Override
public SpawnerUtil getSpawnerUtil() {
return spawnerUtil;
public SpawnerProvider getSpawnerProvider() {
return spawnerProvider;
}
private static class EssentialsWorldListener implements Listener, Runnable {

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 = ess.getSpawnerUtil().getEntityType(event.getItemInHand());
final EntityType type = ess.getSpawnerProvider().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,7 +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 net.ess3.nms.SpawnerProvider;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -98,5 +98,5 @@ public interface IEssentials extends Plugin {
Iterable<User> getOnlineUsers();
SpawnerUtil getSpawnerUtil();
SpawnerProvider getSpawnerProvider();
}

View File

@ -139,7 +139,7 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb {
retval.setAmount(mat.getMaxStackSize());
if (mat == Material.MOB_SPAWNER) {
try {
ess.getSpawnerUtil().setEntityType(retval, EntityType.fromId(metaData));
ess.getSpawnerProvider().setEntityType(retval, EntityType.fromId(metaData));
} catch (IllegalArgumentException e) {
throw new Exception("Can't spawn entity ID " + metaData + " from mob spawners.");
}

View File

@ -0,0 +1,36 @@
package com.earth2me.essentials.utils;
import com.earth2me.essentials.IEssentials;
import net.ess3.nms.SpawnerProvider;
import net.ess3.nms.blockmeta.BlockMetaSpawnerProvider;
import net.ess3.nms.legacy.LegacyProvider;
import net.ess3.nms.v1_8_R1.v1_8_R1SpawnerProvider;
import java.util.Arrays;
import java.util.List;
public class SpawnerProviderFactory {
private IEssentials ess;
public SpawnerProviderFactory(IEssentials ess) {
this.ess = ess;
}
public SpawnerProvider getProvider() {
List<SpawnerProvider> availableProviders = Arrays.asList(
new BlockMetaSpawnerProvider(),
new v1_8_R1SpawnerProvider(),
new LegacyProvider()
);
SpawnerProvider finalProvider = null;
for (SpawnerProvider provider : availableProviders) {
finalProvider = provider;
if (finalProvider.tryProvider()) {
break;
}
}
assert finalProvider != null;
ess.getLogger().info("Using " + finalProvider.getHumanName() + " as spawner provider.");
return finalProvider;
}
}

View File

@ -1,80 +0,0 @@
package com.earth2me.essentials.utils;
import com.google.common.collect.ImmutableMap;
import net.ess3.api.IEssentials;
import org.bukkit.ChatColor;
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;
import java.util.Map;
public class SpawnerUtil {
private boolean useMeta;
private Map<EntityType, String> entityToDisplayName = ImmutableMap.<EntityType, String>builder()
.put(EntityType.CAVE_SPIDER, "Cave Spider")
.put(EntityType.PIG_ZOMBIE, "Zombie Pigman")
.put(EntityType.MAGMA_CUBE, "Magma Cube")
.put(EntityType.ENDER_DRAGON, "Ender Dragon")
.put(EntityType.MUSHROOM_COW, "Mooshroom")
.put(EntityType.SNOWMAN, "Snow Golem")
.put(EntityType.OCELOT, "Ocelot")
.put(EntityType.IRON_GOLEM, "Iron Golem")
.put(EntityType.WITHER, "Wither")
.put(EntityType.HORSE, "Horse")
.build();
public SpawnerUtil(IEssentials ess) {
try {
ItemStack is = new ItemStack(Material.MOB_SPAWNER, 1);
ItemMeta meta = is.getItemMeta();
useMeta = meta instanceof BlockStateMeta;
} catch (Throwable e) {
useMeta = false;
}
if (useMeta) {
ess.getLogger().info("Using BlockStateMeta for spawners");
} else {
ess.getLogger().warning("Using legacy item data for spawners");
ess.getLogger().warning(ChatColor.RED + "UPGRADE TO 1.8.3 OR HIGHER FOR WORKING 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());
}
ItemMeta meta = is.getItemMeta();
String displayName;
if (entityToDisplayName.containsKey(type)) {
displayName = entityToDisplayName.get(type);
} else {
displayName = type.getName();
}
meta.setDisplayName(ChatColor.RESET + displayName + " Spawner");
is.setItemMeta(meta);
return is;
}
public EntityType getEntityType(ItemStack is) {
if (useMeta) {
BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState();
return bs.getSpawnedType();
} else {
return EntityType.fromId((int) is.getData().getData());
}
}
}

View File

@ -19,8 +19,6 @@ EssentialsX is almost a completely drop-in replacement for Essentials. However,
* **EssentialsX requires Java 7.**
* **EssentialsX requires Spigot/CraftBukkit 1.8, 1.8.3+ recommended (for spawner support)**
Building
--------
To build with Maven, use the command

1
nms/1_8_R1Provider/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
craftbukkit-*.jar

View File

@ -0,0 +1 @@
Put a 1.8 Spigot/CraftBukkit JAR with filename `craftbukkit-1.8-R0.1-SNAPSHOT.jar` in `lib/org/bukkit/craftbukkit/1.8-R0.1-SNAPSHOT`.

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EssentialsParent</artifactId>
<groupId>net.ess3</groupId>
<version>2.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>1_8_R1Provider</artifactId>
<repositories>
<repository>
<id>project.local</id>
<name>local repo</name>
<url>file:${project.basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.8-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

@ -0,0 +1,45 @@
package net.ess3.nms.v1_8_R1;
import net.ess3.nms.SpawnerProvider;
import net.minecraft.server.v1_8_R1.NBTTagCompound;
import org.bukkit.craftbukkit.v1_8_R1.inventory.CraftItemStack;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
public class v1_8_R1SpawnerProvider extends SpawnerProvider {
@Override
public ItemStack setEntityType(ItemStack is, EntityType type) {
net.minecraft.server.v1_8_R1.ItemStack itemStack;
CraftItemStack craftStack = CraftItemStack.asCraftCopy(is);
itemStack = CraftItemStack.asNMSCopy(craftStack);
NBTTagCompound tag = itemStack.getTag();
if (tag == null) {
tag = new NBTTagCompound();
itemStack.setTag(tag);
}
if (!tag.hasKey("BlockEntityTag")) {
tag.set("BlockEntityTag", new NBTTagCompound());
}
tag = itemStack.getTag().getCompound("BlockEntityTag");
tag.setString("EntityId", type.getName());
return CraftItemStack.asCraftMirror(itemStack);
}
@Override
public EntityType getEntityType(ItemStack is) {
net.minecraft.server.v1_8_R1.ItemStack itemStack;
CraftItemStack craftStack = CraftItemStack.asCraftCopy(is);
itemStack = CraftItemStack.asNMSCopy(craftStack);
NBTTagCompound tag = itemStack.getTag();
if (tag == null || !tag.hasKey("BlockEntityTag")) {
throw new IllegalArgumentException();
}
String name = tag.getCompound("BlockEntityTag").getString("EntityId");
return EntityType.fromName(name);
}
@Override
public String getHumanName() {
return "CraftBukkit 1.8 NMS-based provider";
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EssentialsParent</artifactId>
<groupId>net.ess3</groupId>
<version>2.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>BlockMetaProvider</artifactId>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.7-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
package net.ess3.nms.blockmeta;
import net.ess3.nms.SpawnerProvider;
import org.bukkit.ChatColor;
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 BlockMetaSpawnerProvider extends SpawnerProvider {
@Override
public ItemStack setEntityType(ItemStack is, EntityType type) {
BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
BlockState bs = bsm.getBlockState();
((CreatureSpawner) bs).setSpawnedType(type);
bsm.setBlockState(bs);
is.setItemMeta(bsm);
// Legacy behavior
is.setDurability(type.getTypeId());
ItemMeta meta = is.getItemMeta();
String displayName;
if (entityToDisplayName.containsKey(type)) {
displayName = entityToDisplayName.get(type);
} else {
displayName = type.getName();
}
meta.setDisplayName(ChatColor.RESET + displayName + " Spawner");
is.setItemMeta(meta);
return is;
}
@Override
public EntityType getEntityType(ItemStack is) {
BlockStateMeta bsm = (BlockStateMeta) is.getItemMeta();
CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState();
return bs.getSpawnedType();
}
@Override
public String getHumanName() {
return "1.8.3+ BlockStateMeta provider";
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EssentialsParent</artifactId>
<groupId>net.ess3</groupId>
<version>2.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>LegacyProvider</artifactId>
<dependencies>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>NMSProvider</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,34 @@
package net.ess3.nms.legacy;
import net.ess3.nms.SpawnerProvider;
import org.bukkit.ChatColor;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class LegacyProvider extends SpawnerProvider {
@Override
public ItemStack setEntityType(ItemStack is, EntityType type) {
is.getData().setData((byte) type.getTypeId());
ItemMeta meta = is.getItemMeta();
String displayName;
if (entityToDisplayName.containsKey(type)) {
displayName = entityToDisplayName.get(type);
} else {
displayName = type.getName();
}
meta.setDisplayName(ChatColor.RESET + displayName + " Spawner");
is.setItemMeta(meta);
return is;
}
@Override
public EntityType getEntityType(ItemStack is) {
return EntityType.fromId((int) is.getData().getData());
}
@Override
public String getHumanName() {
return "legacy item data provider";
}
}

14
nms/NMSProvider/pom.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EssentialsParent</artifactId>
<groupId>net.ess3</groupId>
<version>2.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>NMSProvider</artifactId>
</project>

View File

@ -0,0 +1,38 @@
package net.ess3.nms;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import java.util.Map;
public abstract class SpawnerProvider {
protected Map<EntityType, String> entityToDisplayName = ImmutableMap.<EntityType, String>builder()
.put(EntityType.CAVE_SPIDER, "Cave Spider")
.put(EntityType.PIG_ZOMBIE, "Zombie Pigman")
.put(EntityType.MAGMA_CUBE, "Magma Cube")
.put(EntityType.ENDER_DRAGON, "Ender Dragon")
.put(EntityType.MUSHROOM_COW, "Mooshroom")
.put(EntityType.SNOWMAN, "Snow Golem")
.put(EntityType.OCELOT, "Ocelot")
.put(EntityType.IRON_GOLEM, "Iron Golem")
.put(EntityType.WITHER, "Wither")
.put(EntityType.HORSE, "Horse")
.build();
public abstract ItemStack setEntityType(ItemStack is, EntityType type) throws IllegalArgumentException;
public abstract EntityType getEntityType(ItemStack is) throws IllegalArgumentException;
public abstract String getHumanName();
public boolean tryProvider() {
try {
EntityType type = EntityType.CREEPER;
ItemStack is = setEntityType(new ItemStack(Material.MOB_SPAWNER), type);
EntityType readType = getEntityType(is);
return type == readType;
} catch (Throwable t) {
return false;
}
}
}

View File

@ -28,7 +28,7 @@
<url>http://repo.ess3.net/content/groups/public</url>
</repository>
<repository>
<id>spigot-repo</id>
<id>bukkit-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
</repositories>
@ -41,6 +41,10 @@
<module>EssentialsProtect</module>
<module>EssentialsSpawn</module>
<module>EssentialsXMPP</module>
<module>nms/NMSProvider</module>
<module>nms/BlockMetaProvider</module>
<module>nms/1_8_R1Provider</module>
<module>nms/LegacyProvider</module>
</modules>
<dependencies>
@ -48,6 +52,7 @@
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.7-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
@ -59,6 +64,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.2</version>
<scope>provided</scope>
</dependency>
</dependencies>