Add backwards compatibility for 1.14

This commit is contained in:
libraryaddict
2020-02-06 12:15:20 +13:00
parent 7661635eb9
commit 9656ed4249
15 changed files with 283 additions and 65 deletions

View File

@@ -19,8 +19,8 @@ public enum DisguiseSound {
BAT(Sound.ENTITY_BAT_HURT, null, Sound.ENTITY_BAT_DEATH, Sound.ENTITY_BAT_AMBIENT, Sound.ENTITY_PLAYER_SMALL_FALL,
Sound.ENTITY_BAT_LOOP, Sound.ENTITY_PLAYER_BIG_FALL, Sound.ENTITY_BAT_TAKEOFF),
BEE(Sound.ENTITY_BEE_HURT, null, Sound.ENTITY_BEE_DEATH, null, Sound.ENTITY_BEE_LOOP,
Sound.ENTITY_BEE_LOOP_AGGRESSIVE, Sound.ENTITY_BEE_POLLINATE, Sound.ENTITY_BEE_STING),
BEE("ENTITY_BEE_HURT", null, "ENTITY_BEE_DEATH", null, "ENTITY_BEE_LOOP", "ENTITY_BEE_LOOP_AGGRESSIVE",
"ENTITY_BEE_POLLINATE", "ENTITY_BEE_STING"),
BLAZE(Sound.ENTITY_BLAZE_HURT, null, Sound.ENTITY_BLAZE_DEATH, Sound.ENTITY_BLAZE_AMBIENT,
Sound.ENTITY_PLAYER_SMALL_FALL, Sound.ENTITY_PLAYER_BIG_FALL, Sound.ENTITY_BLAZE_BURN,
@@ -249,28 +249,60 @@ public enum DisguiseSound {
private float damageSoundVolume = 1F;
private LinkedHashMap<Object, SoundType> disguiseSounds = new LinkedHashMap<>();
DisguiseSound(Object hurt, Object step, Object death, Object idle, Sound... sounds) {
DisguiseSound(Object hurt, Object step, Object death, Object idle, Object... sounds) {
addSound(hurt, SoundType.HURT);
addSound(step, SoundType.STEP);
addSound(death, SoundType.DEATH);
addSound(idle, SoundType.IDLE);
for (Sound obj : sounds) {
for (Object obj : sounds) {
addSound(obj, SoundType.CANCEL);
}
}
private Sound parseSound(String name) {
try {
return Sound.valueOf(name);
}
catch (Exception ex) {
}
return null;
}
private void addSound(Object sound, SoundType type) {
if (sound == null) {
return;
}
if (sound instanceof Sound) {
addSound((Sound) sound, type);
if (sound instanceof String[]) {
for (String s : (String[]) sound) {
Sound so = parseSound(s);
if (so == null) {
continue;
}
addSound(so, type);
}
} else if (sound instanceof String) {
Sound so = parseSound((String) sound);
if (so == null) {
return;
}
addSound(so, type);
} else if (sound instanceof Sound[]) {
for (Sound s : (Sound[]) sound) {
if (s == null) {
continue;
}
addSound(s, type);
}
} else if (sound instanceof Sound) {
addSound((Sound) sound, type);
} else {
throw new IllegalArgumentException("Was given an unknown object " + sound);
}
@@ -290,6 +322,10 @@ public enum DisguiseSound {
return damageSoundVolume;
}
public void setDamageAndIdleSoundVolume(float strength) {
this.damageSoundVolume = strength;
}
public Object getSound(SoundType type) {
if (type == null) {
return null;
@@ -334,8 +370,4 @@ public enum DisguiseSound {
public boolean isCancelSound(String sound) {
return getSound(sound) == SoundType.CANCEL;
}
public void setDamageAndIdleSoundVolume(float strength) {
this.damageSoundVolume = strength;
}
}

View File

@@ -17,6 +17,7 @@ import me.libraryaddict.disguise.utilities.packets.IPacketHandler;
import me.libraryaddict.disguise.utilities.packets.LibsPackets;
import me.libraryaddict.disguise.utilities.packets.PacketsHandler;
import me.libraryaddict.disguise.utilities.reflection.DisguiseValues;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Art;
import org.bukkit.Location;
@@ -224,11 +225,15 @@ public class PacketHandlerSpawn implements IPacketHandler {
packets.addPacket(spawnPlayer);
PacketContainer metaPacket = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, entityId, newWatcher, true)
.createPacket(entityId, newWatcher, true);
if (ReflectionManager.isSupported(NmsVersion.v1_15)) {
PacketContainer metaPacket = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, entityId, newWatcher, true)
.createPacket(entityId, newWatcher, true);
packets.addPacket(metaPacket);
packets.addPacket(metaPacket);
} else {
spawnPlayer.getDataWatcherModifier().write(0, newWatcher);
}
if (!selfDisguise) {
// Teleport the player back to where he's supposed to be
@@ -248,7 +253,7 @@ public class PacketHandlerSpawn implements IPacketHandler {
packets.addDelayedPacket(teleportPacket, 3);
// Send a metadata packet
metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
PacketContainer metaPacket = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
newWatcher = DisguiseUtilities
.createSanitizedDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity),
@@ -309,11 +314,15 @@ public class PacketHandlerSpawn implements IPacketHandler {
.createSanitizedDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity),
disguise.getWatcher());
PacketContainer metaPacket = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, disguisedEntity.getEntityId(),
newWatcher, true).createPacket(disguisedEntity.getEntityId(), newWatcher, true);
if (ReflectionManager.isSupported(NmsVersion.v1_15)) {
PacketContainer metaPacket = ProtocolLibrary.getProtocolManager()
.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, disguisedEntity.getEntityId(),
newWatcher, true).createPacket(disguisedEntity.getEntityId(), newWatcher, true);
packets.addPacket(metaPacket);
packets.addPacket(metaPacket);
} else {
spawnEntity.getDataWatcherModifier().write(0, newWatcher);
}
} else if (disguise.getType().isMisc()) {
int data = ((MiscDisguise) disguise).getData();
double x = loc.getX();

View File

@@ -37,6 +37,10 @@ public class DisguiseParser {
public static void createDefaultMethods() {
try {
for (DisguiseType type : DisguiseType.values()) {
if (type.getEntityType() == null) {
continue;
}
Disguise disguise;
if (type.isMisc()) {
@@ -284,19 +288,21 @@ public class DisguiseParser {
}
public static DisguisePerm[] getDisguisePerms() {
DisguisePerm[] perms = new DisguisePerm[DisguiseType.values().length +
DisguiseConfig.getCustomDisguises().size()];
int i = 0;
ArrayList<DisguisePerm> perms = new ArrayList<>();
for (DisguiseType disguiseType : DisguiseType.values()) {
perms[i++] = new DisguisePerm(disguiseType);
if (disguiseType.getEntityType() == null) {
continue;
}
perms.add(new DisguisePerm(disguiseType));
}
for (Entry<DisguisePerm, String> entry : DisguiseConfig.getCustomDisguises().entrySet()) {
perms[i++] = entry.getKey();
perms.add(entry.getKey());
}
return perms;
return perms.toArray(new DisguisePerm[0]);
}
/**

View File

@@ -9,6 +9,7 @@ import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfoTypes;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.ChatColor;
import javax.annotation.Nullable;
@@ -90,7 +91,9 @@ public class ParamInfoManager {
while (itel.hasNext()) {
Method method = itel.next();
if (method.getParameterTypes().length != 1) {
if (!ReflectionManager.isSupported(method)) {
itel.remove();
} else if (method.getParameterTypes().length != 1) {
itel.remove();
} else if (method.getName().startsWith("get")) {
itel.remove();

View File

@@ -0,0 +1,12 @@
package me.libraryaddict.disguise.utilities.reflection;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Created by libraryaddict on 6/02/2020.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface NmsAdded {
NmsVersion added();
}

View File

@@ -0,0 +1,12 @@
package me.libraryaddict.disguise.utilities.reflection;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Created by libraryaddict on 6/02/2020.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface NmsRemoved {
NmsVersion removed();
}

View File

@@ -0,0 +1,9 @@
package me.libraryaddict.disguise.utilities.reflection;
/**
* Created by libraryaddict on 6/02/2020.
*/
public enum NmsVersion {
v1_14,
v1_15;
}

View File

@@ -5,6 +5,7 @@ import com.comphenix.protocol.wrappers.*;
import com.comphenix.protocol.wrappers.EnumWrappers.Direction;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
import lombok.Getter;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.*;
@@ -47,6 +48,12 @@ public class ReflectionManager {
private static Field chunkProviderField;
private static Field entityTrackerField;
private static Field trackedEntitiesField;
@Getter
private static NmsVersion version;
public static boolean isSupported(NmsVersion version) {
return getVersion().ordinal() >= version.ordinal();
}
public static void init() {
try {
@@ -96,6 +103,52 @@ public class ReflectionManager {
entityCountField.setAccessible(true);
}
public static boolean isSupported(AccessibleObject obj) {
if (obj.isAnnotationPresent(NmsAdded.class)) {
NmsAdded added = obj.getAnnotation(NmsAdded.class);
// If it was added after/on this version
if (!isSupported(added.added())) {
return false;
}
}
if (obj.isAnnotationPresent(NmsRemoved.class)) {
NmsRemoved removed = obj.getAnnotation(NmsRemoved.class);
if (isSupported(removed.removed())) {
return false;
}
}
return true;
}
public static boolean isSupported(Class cl, String name) {
try {
for (Field field : cl.getFields()) {
if (!field.getName().equals(name)) {
continue;
}
return isSupported(field);
}
for (Method method : cl.getMethods()) {
if (!method.getName().equals(name)) {
continue;
}
return isSupported(method);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
public static YamlConfiguration getPluginYaml(ClassLoader loader) {
try (InputStream stream = loader.getResourceAsStream("plugin.yml")) {
YamlConfiguration config = new YamlConfiguration();
@@ -286,6 +339,15 @@ public class ReflectionManager {
public static String getBukkitVersion() {
if (bukkitVersion == null) {
bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
for (NmsVersion v : NmsVersion.values()) {
if (!getBukkitVersion().startsWith(v.name())) {
continue;
}
version = v;
break;
}
}
return bukkitVersion;

View File

@@ -17,7 +17,8 @@ public class TranslateFiller {
// Fill the configs
for (ParamInfo info : ParamInfoManager.getParamInfos()) {
TranslateType.DISGUISE_OPTIONS_PARAMETERS.save(info.getRawName(), "A disguise option name, has description " + info.getDescription());
TranslateType.DISGUISE_OPTIONS_PARAMETERS
.save(info.getRawName(), "A disguise option name, has description " + info.getDescription());
if (!info.getRawName().equals(info.getRawDescriptiveName())) {
TranslateType.DISGUISE_OPTIONS_PARAMETERS
@@ -51,6 +52,10 @@ public class TranslateFiller {
TranslateType.DISGUISES.save(StringUtils.join(split, " "), "Name for the " + type.name() + " disguise");
if (type.getEntityType() == null) {
continue;
}
for (Method method : ParamInfoManager.getDisguiseWatcherMethods(type.getWatcherClass())) {
Class para = method.getParameterTypes()[0];
String className = method.getDeclaringClass().getSimpleName().replace("Watcher", "");