From 87e810c6081f59b4fecf46ae52563560f40afa33 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 9 Jun 2017 09:14:36 +1200 Subject: [PATCH] Commit remaining work --- .../libraryaddict/disguise/LibsDisguises.java | 11 ++++++ .../disguise/disguisetypes/MetaIndex.java | 39 +++++++++++++------ .../utilities/backwards/BackwardsSupport.java | 14 +++++-- .../backwards/metadata/Version_1_10.java | 7 ++++ .../{Version_1_1.java => Version_1_11.java} | 2 +- 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_10.java rename src/me/libraryaddict/disguise/utilities/backwards/metadata/{Version_1_1.java => Version_1_11.java} (92%) diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index c073fc87..63d76bc4 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -3,6 +3,9 @@ package me.libraryaddict.disguise; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; import me.libraryaddict.disguise.disguisetypes.watchers.*; import org.bukkit.Bukkit; @@ -322,6 +325,7 @@ public class LibsDisguises extends JavaPlugin { bukkitEntity instanceof Damageable ? ((Damageable) bukkitEntity).getMaxHealth() : 0); WrappedDataWatcher watcher = WrappedDataWatcher.getEntityWatcher(bukkitEntity); + ArrayList indexes = MetaIndex.getFlags(disguiseType.getWatcherClass()); for (WrappedWatchableObject watch : watcher.getWatchableObjects()) { MetaIndex flagType = MetaIndex.getFlag(watcherClass, watch.getIndex()); @@ -335,6 +339,8 @@ public class LibsDisguises extends JavaPlugin { continue; } + indexes.remove(flagType); + if (ReflectionManager.convertInvalidItem( flagType.getDefault()).getClass() != ReflectionManager.convertInvalidItem( watch.getValue()).getClass()) { @@ -346,6 +352,11 @@ public class LibsDisguises extends JavaPlugin { } } + for (MetaIndex index : indexes) { + System.out.println( + disguiseType + " has MetaIndex remaining! " + index.getFlagWatcher().getSimpleName() + " at index " + index.getIndex()); + } + DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); if (sound != null) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/MetaIndex.java b/src/me/libraryaddict/disguise/disguisetypes/MetaIndex.java index cb63abe8..31261469 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/MetaIndex.java +++ b/src/me/libraryaddict/disguise/disguisetypes/MetaIndex.java @@ -220,14 +220,8 @@ public class MetaIndex { public static MetaIndex SPIDER_CLIMB = new MetaIndex<>(SpiderWatcher.class, 0, (byte) 0); - public static MetaIndex SPLASH_POTION_ITEM = new MetaIndex<>(SplashPotionWatcher.class, 1, - new ItemStack(Material.SPLASH_POTION)); // Yeah, the '1' isn't a bug. No idea why but MC thinks - // there's a '0' already. - - public static MetaIndex SPLASH_POTION_ITEM_BAD = new MetaIndex<>(SplashPotionWatcher.class, 0, - new ItemStack(Material.SPLASH_POTION)); // Yeah, the '1' isn't a bug. No - // idea why but MC thinks there's a - // '0' already. + public static MetaIndex SPLASH_POTION_ITEM = new MetaIndex<>(SplashPotionWatcher.class, 0, + new ItemStack(Material.SPLASH_POTION)); public static MetaIndex TAMEABLE_META = new MetaIndex<>(TameableWatcher.class, 0, (byte) 0); @@ -438,6 +432,25 @@ public class MetaIndex { return _values; } + public static void addMetaIndexes(MetaIndex... metaIndexes) { + _values = Arrays.copyOf(values(), values().length + metaIndexes.length); + + for (int i = values().length - metaIndexes.length, a = 0; i < values().length; i++, a++) { + MetaIndex index = metaIndexes[a]; + + for (MetaIndex metaIndex : values()) { + if (metaIndex.getFlagWatcher() != index.getFlagWatcher() || metaIndex.getIndex() != index.getIndex()) { + continue; + } + + System.err.println( + "[LibsDisguises] MetaIndex " + metaIndex.getFlagWatcher().getSimpleName() + " at index " + metaIndex.getIndex() + " has already registered this! (" + metaIndex.getDefault() + "," + index.getDefault() + ")"); + } + + values()[i] = metaIndexes[a]; + } + } + public static void setValues() { try { _values = new MetaIndex[0]; @@ -460,7 +473,10 @@ public class MetaIndex { } } - public static void setMetaIndex(String name, MetaIndex metaIndex) { + /** + * Returns true if success, false if the field doesn't exist + */ + public static boolean setMetaIndex(String name, MetaIndex metaIndex) { try { Field field = MetaIndex.class.getField(name); MetaIndex index = (MetaIndex) field.get(null); @@ -468,12 +484,13 @@ public class MetaIndex { field.set(null, metaIndex); } catch (NoSuchFieldException ex) { - System.out.println("The field '" + name + "' doesn't exist in MetaIndex!"); - Thread.dumpStack(); + return false; } catch (Exception ex) { ex.printStackTrace(); } + + return true; } private Y _defaultValue; diff --git a/src/me/libraryaddict/disguise/utilities/backwards/BackwardsSupport.java b/src/me/libraryaddict/disguise/utilities/backwards/BackwardsSupport.java index 49443b47..584f98dd 100644 --- a/src/me/libraryaddict/disguise/utilities/backwards/BackwardsSupport.java +++ b/src/me/libraryaddict/disguise/utilities/backwards/BackwardsSupport.java @@ -3,9 +3,10 @@ package me.libraryaddict.disguise.utilities.backwards; import me.libraryaddict.disguise.disguisetypes.MetaIndex; import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.ReflectionManager; -import me.libraryaddict.disguise.utilities.backwards.metadata.Version_1_1; +import me.libraryaddict.disguise.utilities.backwards.metadata.Version_1_11; import java.lang.reflect.Field; +import java.util.ArrayList; /** * Created by libraryaddict on 8/06/2017. @@ -17,7 +18,7 @@ public class BackwardsSupport { if (LibsPremium.isPremium()) { if (version.equals("v1_11_R1")) { - return setupMetadata(Version_1_1.class); + return setupMetadata(Version_1_11.class); } } @@ -33,16 +34,23 @@ public class BackwardsSupport { private static BackwardMethods setupMetadata(Class backwardsClass) { try { BackwardMethods backwards = backwardsClass.newInstance(); + ArrayList newIndexes = new ArrayList<>(); for (Field field : backwards.getClass().getFields()) { if (field.getType() != MetaIndex.class) continue; - MetaIndex.setMetaIndex(field.getName(), (MetaIndex) field.get(backwards)); + if (MetaIndex.setMetaIndex(field.getName(), (MetaIndex) field.get(backwards))) { + continue; + } + + newIndexes.add((MetaIndex) field.get(backwards)); } MetaIndex.setValues(); + MetaIndex.addMetaIndexes(newIndexes.toArray(new MetaIndex[0])); + if (backwards.isOrderedIndexes()) { MetaIndex.fillInBlankIndexes(); MetaIndex.orderMetaIndexes(); diff --git a/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_10.java b/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_10.java new file mode 100644 index 00000000..a221be3a --- /dev/null +++ b/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_10.java @@ -0,0 +1,7 @@ +package me.libraryaddict.disguise.utilities.backwards.metadata; + +/** + * Created by libraryaddict on 9/06/2017. + */ +public class Version_1_10 { +} diff --git a/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_1.java b/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_11.java similarity index 92% rename from src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_1.java rename to src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_11.java index ac4edfc3..7eb88ce2 100644 --- a/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_1.java +++ b/src/me/libraryaddict/disguise/utilities/backwards/metadata/Version_1_11.java @@ -10,7 +10,7 @@ import me.libraryaddict.disguise.utilities.backwards.BackwardMethods; * Created by libraryaddict on 8/06/2017. * Supports 1.11.0 1.11.1 and 1.11.2 */ -public class Version_1_1 extends BackwardMethods { +public class Version_1_11 extends BackwardMethods { private MetaIndex ILLAGER_META; private MetaIndex PARROT_VARIANT; private MetaIndex PLAYER_LEFT_SHOULDER_ENTITY;