From 3c40a27c8f1ac14cbec729b510cee8d679b8dad6 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 13 Apr 2020 21:14:04 +1200 Subject: [PATCH] Initial work for custom disguises --- .../commands/utils/GrabSkinCommand.java | 9 +++-- .../disguise/disguisetypes/DisguiseType.java | 8 +++- .../disguise/disguisetypes/FlagWatcher.java | 7 ++++ .../disguise/disguisetypes/MiscDisguise.java | 7 ++-- .../disguisetypes/watchers/CustomWatcher.java | 39 +++++++++++++++++++ .../packethandlers/PacketHandlerSpawn.java | 10 ++++- .../utilities/translations/LibsMsg.java | 6 +-- .../disguise/utilities/DisguiseTypesTest.java | 2 + 8 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/CustomWatcher.java diff --git a/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java b/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java index 9cff85f0..1d79cce7 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java @@ -45,7 +45,8 @@ public class GrabSkinCommand implements CommandExecutor { } String[] args = DisguiseUtilities.split(StringUtils.join(strings, " ")); - String tName = args.length > 1 ? args[1] : null; + String tName = args.length > 1 ? args[0] : null; + String skin = args.length > 1 ? args[1] : args[0]; String usable = SkinUtils.getUsableStatus(); @@ -54,8 +55,8 @@ public class GrabSkinCommand implements CommandExecutor { return true; } - if (tName == null && args[0].matches("(.*\\/)?[a-zA-Z0-9_-]{3,20}\\.png")) { - tName = args[0].substring(args[0].lastIndexOf("/") + 1, args[0].lastIndexOf(".")); + if (tName == null && skin.matches("(.*\\/)?[a-zA-Z0-9_-]{3,20}\\.png")) { + tName = skin.substring(skin.lastIndexOf("/") + 1, skin.lastIndexOf(".")); if (DisguiseUtilities.hasGameProfile(tName)) { tName = null; @@ -146,7 +147,7 @@ public class GrabSkinCommand implements CommandExecutor { } }; - SkinUtils.grabSkin(args[0], callback); + SkinUtils.grabSkin(skin, callback); return true; } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java index 2df8bb40..db8373b5 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java @@ -35,6 +35,8 @@ public enum DisguiseType { CREEPER, + CUSTOM, + DOLPHIN, DONKEY, @@ -261,7 +263,11 @@ public enum DisguiseType { } try { - setEntityType(EntityType.valueOf(name())); + if (name().equalsIgnoreCase("CUSTOM")) { + setEntityType(EntityType.UNKNOWN); + } else { + setEntityType(EntityType.valueOf(name())); + } } catch (Exception ex) { } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index f2717cef..94942e57 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -9,6 +9,8 @@ import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.google.common.base.Strings; +import lombok.AccessLevel; +import lombok.Getter; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; @@ -33,8 +35,13 @@ public class FlagWatcher { /** * These are the entity values I need to add else it could crash them.. */ + @Getter(value = AccessLevel.PROTECTED) private HashMap backupEntityValues = new HashMap<>(); private transient TargetedDisguise disguise; + /** + * Disguise set data + */ + @Getter(value = AccessLevel.PROTECTED) private HashMap entityValues = new HashMap<>(); private LibsEquipment equipment; private boolean hasDied; diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java index d11abf03..6dcdbd6a 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java @@ -1,9 +1,6 @@ package me.libraryaddict.disguise.disguisetypes; -import me.libraryaddict.disguise.disguisetypes.watchers.DroppedItemWatcher; -import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; -import me.libraryaddict.disguise.disguisetypes.watchers.PaintingWatcher; -import me.libraryaddict.disguise.disguisetypes.watchers.SplashPotionWatcher; +import me.libraryaddict.disguise.disguisetypes.watchers.*; import org.bukkit.Art; import org.bukkit.Material; import org.bukkit.entity.Entity; @@ -147,6 +144,8 @@ public class MiscDisguise extends TargetedDisguise { public int getId() { if (getType() == DisguiseType.FALLING_BLOCK) { return ((FallingBlockWatcher) getWatcher()).getBlock().getType().ordinal(); + } else if (getType() == DisguiseType.CUSTOM) { + return ((CustomWatcher) getWatcher()).getTypeId(); } return id; diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/CustomWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/CustomWatcher.java new file mode 100644 index 00000000..0e34dc60 --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/CustomWatcher.java @@ -0,0 +1,39 @@ +package me.libraryaddict.disguise.disguisetypes.watchers; + +import lombok.Getter; +import lombok.Setter; +import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.disguisetypes.DisguiseType; +import me.libraryaddict.disguise.disguisetypes.FlagWatcher; +import me.libraryaddict.disguise.disguisetypes.MetaIndex; + +/** + * Created by libraryaddict on 13/04/2020. + */ +public class CustomWatcher extends FlagWatcher { + @Getter + private DisguiseType inherits; + @Getter + @Setter + private int typeId; + + public CustomWatcher(Disguise disguise) { + super(disguise); + } + + public void setInherits(DisguiseType toClone) { + this.inherits = toClone; + } + + /** + * @param index + * @param object + */ + public void setMetadata(int index, Object object) { + getEntityValues().put(index, object); + } + + public Object getMetadata(int index) { + return getEntityValues().get(index); + } +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java index 332653cd..1e09b9cb 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java @@ -9,6 +9,7 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedGameProfile; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.*; +import me.libraryaddict.disguise.disguisetypes.watchers.CustomWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; @@ -221,7 +222,7 @@ public class PacketHandlerSpawn implements IPacketHandler { } else { spawnPlayer.getDataWatcherModifier().write(0, newWatcher); } - } else if (disguise.getType().isMob() || disguise.getType() == DisguiseType.ARMOR_STAND) { + } else if (disguise.isMobDisguise() || disguise.getType() == DisguiseType.ARMOR_STAND) { Vector vec = disguisedEntity.getVelocity(); PacketContainer spawnEntity = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY_LIVING); @@ -231,7 +232,12 @@ public class PacketHandlerSpawn implements IPacketHandler { mods.write(0, disguisedEntity.getEntityId()); mods.write(1, disguisedEntity.getUniqueId()); - mods.write(2, disguise.getType().getTypeId()); + + if (disguise.getType() != DisguiseType.CUSTOM) { + mods.write(2, disguise.getType().getTypeId()); + } else { + mods.write(2, ((CustomWatcher) disguise.getWatcher()).getTypeId()); + } // region Vector calculations double d1 = 3.9D; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index 7039cce9..c665a61b 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -231,9 +231,9 @@ public enum LibsMsg { GRAB_DISG_HELP_1(ChatColor.GREEN + "You can choose a name to save the skins under, the names will be usable as if it was an actual player " + "skin"), - GRAB_DISG_HELP_2(ChatColor.DARK_GREEN + "/grabskin https://somesite.com/myskin.png "), - GRAB_DISG_HELP_3(ChatColor.DARK_GREEN + "/grabskin myskin.png - Skins must be in the folder!"), - GRAB_DISG_HELP_4(ChatColor.DARK_GREEN + "/grabskin "), + GRAB_DISG_HELP_2(ChatColor.DARK_GREEN + "/grabskin https://somesite.com/myskin.png"), + GRAB_DISG_HELP_3(ChatColor.DARK_GREEN + "/grabskin myskin.png - Skins must be in the folder!"), + GRAB_DISG_HELP_4(ChatColor.DARK_GREEN + "/grabskin "), GRAB_DISG_HELP_5(ChatColor.GREEN + "If you want the slim Alex version of the skin, append :slim. So 'myskin.png:slim'"), GRAB_DISG_HELP_6( ChatColor.GREEN + "You will be sent the skin data, but you can also use the saved names in disguises"), diff --git a/src/test/java/me/libraryaddict/disguise/utilities/DisguiseTypesTest.java b/src/test/java/me/libraryaddict/disguise/utilities/DisguiseTypesTest.java index ca896e0c..7467baa9 100644 --- a/src/test/java/me/libraryaddict/disguise/utilities/DisguiseTypesTest.java +++ b/src/test/java/me/libraryaddict/disguise/utilities/DisguiseTypesTest.java @@ -14,6 +14,8 @@ public class DisguiseTypesTest { for (EntityType entityType : EntityType.values()) { if (entityType == EntityType.LIGHTNING) { continue; + } else if (entityType == EntityType.UNKNOWN) { + continue; } DisguiseType disguiseType = DisguiseType.getType(entityType);