Search in sources :

Example 1 with MetaIndex

use of me.libraryaddict.disguise.disguisetypes.MetaIndex in project LibsDisguises by libraryaddict.

the class DisguiseUtilities method getNamePackets.

public static ArrayList<PacketContainer> getNamePackets(Disguise disguise, String[] internalOldNames) {
    ArrayList<PacketContainer> packets = new ArrayList<>();
    String[] newNames = (disguise instanceof PlayerDisguise && !((PlayerDisguise) disguise).isNameVisible()) ? new String[0] : reverse(disguise.getMultiName());
    int[] standIds = disguise.getArmorstandIds();
    int[] destroyIds = new int[0];
    if (!LibsPremium.isPremium()) {
        if (internalOldNames.length > 1) {
            internalOldNames = new String[] { StringUtils.join(internalOldNames, "\\n") };
        }
        if (newNames.length > 1) {
            newNames = new String[] { StringUtils.join(newNames, "\\n") };
            if (!disguise.isPlayerDisguise() || ((PlayerDisguise) disguise).isNameVisible()) {
                if (disguise.getMultiName().length > 1) {
                    getLogger().info("Multiline names is a premium feature, sorry!");
                }
            }
        }
    }
    if (internalOldNames.length > newNames.length) {
        // Destroy packet
        destroyIds = Arrays.copyOfRange(standIds, newNames.length, internalOldNames.length);
    }
    // Don't need to offset with DisguiseUtilities.getYModifier, because that's a visual offset and not an actual location offset
    double height = disguise.getHeight() + disguise.getWatcher().getYModifier() + disguise.getWatcher().getNameYModifier();
    for (int i = 0; i < newNames.length; i++) {
        if (i < internalOldNames.length) {
            if (newNames[i].equals(internalOldNames[i])) {
                continue;
            }
            WrappedDataWatcher watcher = new WrappedDataWatcher();
            Object name;
            if (NmsVersion.v1_13.isSupported()) {
                name = Optional.of(WrappedChatComponent.fromJson(ComponentSerializer.toString(DisguiseUtilities.getColoredChat(newNames[i]))));
            } else {
                name = ChatColor.translateAlternateColorCodes('&', newNames[i]);
            }
            WrappedDataWatcher.WrappedDataWatcherObject obj = ReflectionManager.createDataWatcherObject(NmsVersion.v1_13.isSupported() ? MetaIndex.ENTITY_CUSTOM_NAME : MetaIndex.ENTITY_CUSTOM_NAME_OLD, name);
            watcher.setObject(obj, ReflectionManager.convertInvalidMeta(name));
            PacketContainer metaPacket = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, 0, watcher, true).createPacket(standIds[i], watcher, true);
            packets.add(metaPacket);
        } else if (newNames[i].isEmpty()) {
            destroyIds = Arrays.copyOf(destroyIds, destroyIds.length + 1);
            destroyIds[destroyIds.length - 1] = standIds[i];
        } else {
            PacketContainer packet = new PacketContainer(Server.SPAWN_ENTITY_LIVING);
            packet.getIntegers().write(0, standIds[i]);
            packet.getIntegers().write(1, DisguiseType.ARMOR_STAND.getTypeId());
            packet.getUUIDs().write(0, UUID.randomUUID());
            Location loc = disguise.getEntity().getLocation();
            packet.getDoubles().write(0, loc.getX());
            packet.getDoubles().write(1, loc.getY() + height + (0.28 * i));
            packet.getDoubles().write(2, loc.getZ());
            packets.add(packet);
            WrappedDataWatcher watcher = new WrappedDataWatcher();
            for (MetaIndex index : MetaIndex.getMetaIndexes(ArmorStandWatcher.class)) {
                Object val = index.getDefault();
                if (index == MetaIndex.ENTITY_META) {
                    val = (byte) 32;
                } else if (index == MetaIndex.ARMORSTAND_META) {
                    val = (byte) 19;
                } else if (index == MetaIndex.ENTITY_CUSTOM_NAME) {
                    val = Optional.of(WrappedChatComponent.fromJson(ComponentSerializer.toString(DisguiseUtilities.getColoredChat(newNames[i]))));
                } else if (index == MetaIndex.ENTITY_CUSTOM_NAME_OLD) {
                    val = ChatColor.translateAlternateColorCodes('&', newNames[i]);
                } else if (index == MetaIndex.ENTITY_CUSTOM_NAME_VISIBLE) {
                    val = true;
                }
                WrappedDataWatcher.WrappedDataWatcherObject obj = ReflectionManager.createDataWatcherObject(index, val);
                watcher.setObject(obj, ReflectionManager.convertInvalidMeta(val));
            }
            if (NmsVersion.v1_15.isSupported()) {
                PacketContainer metaPacket = ProtocolLibrary.getProtocolManager().createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, standIds[i], watcher, true).createPacket(standIds[i], watcher, true);
                packets.add(metaPacket);
            } else {
                packet.getDataWatcherModifier().write(0, watcher);
            }
        }
    }
    if (destroyIds.length > 0) {
        packets.add(getDestroyPacket(destroyIds));
    }
    return packets;
}
Also used : ArmorStandWatcher(me.libraryaddict.disguise.disguisetypes.watchers.ArmorStandWatcher) PacketContainer(com.comphenix.protocol.events.PacketContainer) ArrayList(java.util.ArrayList) PlayerDisguise(me.libraryaddict.disguise.disguisetypes.PlayerDisguise) SerializerMetaIndex(me.libraryaddict.disguise.utilities.json.SerializerMetaIndex) MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) Location(org.bukkit.Location)

Example 2 with MetaIndex

use of me.libraryaddict.disguise.disguisetypes.MetaIndex in project LibsDisguises by libraryaddict.

the class DisguiseUtilities method createSanitizedDataWatcher.

/**
 * Create a new datawatcher but with the 'correct' values
 */
public static WrappedDataWatcher createSanitizedDataWatcher(Player player, WrappedDataWatcher entityWatcher, FlagWatcher disguiseWatcher) {
    WrappedDataWatcher newWatcher = new WrappedDataWatcher();
    try {
        List<WrappedWatchableObject> list = DisguiseConfig.isMetaPacketsEnabled() ? disguiseWatcher.convert(player, entityWatcher.getWatchableObjects()) : disguiseWatcher.getWatchableObjects();
        for (WrappedWatchableObject watchableObject : list) {
            if (watchableObject == null) {
                continue;
            }
            Object object = watchableObject.getRawValue();
            if (object == null) {
                continue;
            }
            MetaIndex metaIndex = MetaIndex.getMetaIndex(disguiseWatcher, watchableObject.getIndex());
            WrappedDataWatcher.WrappedDataWatcherObject obj = ReflectionManager.createDataWatcherObject(metaIndex, object);
            newWatcher.setObject(obj, object);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return newWatcher;
}
Also used : SerializerMetaIndex(me.libraryaddict.disguise.utilities.json.SerializerMetaIndex) MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 3 with MetaIndex

use of me.libraryaddict.disguise.disguisetypes.MetaIndex in project LibsDisguises by libraryaddict.

the class PacketHandlerEquipment method handleOld.

public void handleOld(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) {
    // Else if the disguise is updating equipment
    EquipmentSlot slot = ReflectionManager.createEquipmentSlot(packets.getPackets().get(0).getModifier().read(1));
    org.bukkit.inventory.ItemStack itemStack = disguise.getWatcher().getItemStack(slot);
    if (itemStack != null) {
        packets.clear();
        PacketContainer equipPacket = sentPacket.shallowClone();
        packets.addPacket(equipPacket);
        equipPacket.getModifier().write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack));
    }
    if ((disguise.getWatcher().isMainHandRaised() && slot == EquipmentSlot.HAND) || (disguise.getWatcher() instanceof LivingWatcher && ((LivingWatcher) disguise.getWatcher()).isOffhandRaised() && slot == EquipmentSlot.OFF_HAND)) {
        if (itemStack == null) {
            itemStack = packets.getPackets().get(0).getItemModifier().read(0);
        }
        if (itemStack != null && itemStack.getType() != Material.AIR) {
            // Convert the datawatcher
            List<WrappedWatchableObject> list = new ArrayList<>();
            MetaIndex toUse = NmsVersion.v1_13.isSupported() ? MetaIndex.LIVING_META : MetaIndex.ENTITY_META;
            if (DisguiseConfig.isMetaPacketsEnabled()) {
                WrappedWatchableObject watch = ReflectionManager.createWatchable(toUse, WrappedDataWatcher.getEntityWatcher(entity).getByte(toUse.getIndex()));
                if (watch != null) {
                    list.add(watch);
                }
                list = disguise.getWatcher().convert(observer, list);
            } else {
                for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects()) {
                    if (obj.getIndex() == toUse.getIndex()) {
                        list.add(obj);
                        break;
                    }
                }
            }
            // Construct the packets to return
            PacketContainer packetBlock = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
            packetBlock.getModifier().write(0, entity.getEntityId());
            packetBlock.getWatchableCollectionModifier().write(0, list);
            PacketContainer packetUnblock = packetBlock.deepClone();
            // Make a packet to send the 'unblock'
            for (WrappedWatchableObject watcher : packetUnblock.getWatchableCollectionModifier().read(0)) {
                if (NmsVersion.v1_13.isSupported()) {
                    watcher.setValue((byte) 0);
                } else {
                    watcher.setValue((byte) ((byte) watcher.getRawValue() & ~(1 << 4)));
                }
            }
            // Send the unblock before the itemstack change so that the 2nd metadata packet works. Why?
            // Scheduler
            // delay.
            PacketContainer packet1 = packets.getPackets().get(0);
            packets.clear();
            packets.addPacket(packetUnblock);
            packets.addPacket(packet1);
            packets.addPacket(packetBlock);
        // Silly mojang made the right clicking datawatcher value only valid for one use. So I have
        // to reset
        // it.
        }
    }
}
Also used : MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) LivingWatcher(me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher) PacketContainer(com.comphenix.protocol.events.PacketContainer) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) ArrayList(java.util.ArrayList)

Example 4 with MetaIndex

use of me.libraryaddict.disguise.disguisetypes.MetaIndex in project LibsDisguises by libraryaddict.

the class SerializerMetaIndex method deserialize.

@Override
public MetaIndex deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    String name = obj.get("flagwatcher").getAsString();
    int index = obj.get("index").getAsInt();
    for (MetaIndex i : MetaIndex.values()) {
        if (i.getIndex() != index) {
            continue;
        }
        if (!i.getFlagWatcher().getSimpleName().equals(name)) {
            continue;
        }
        return i;
    }
    return null;
}
Also used : MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) JsonObject(com.google.gson.JsonObject)

Example 5 with MetaIndex

use of me.libraryaddict.disguise.disguisetypes.MetaIndex in project LibsDisguises by libraryaddict.

the class SerializerFlagWatcher method correct.

private void correct(FlagWatcher watcher, Class<? extends FlagWatcher> flagWatcher, String name) throws NoSuchFieldException, IllegalAccessException, DisguiseParseException {
    Field field = FlagWatcher.class.getDeclaredField(name);
    field.setAccessible(true);
    HashMap<Integer, Object> map = (HashMap<Integer, Object>) field.get(watcher);
    int count = 0;
    for (Map.Entry<Integer, Object> entry : map.entrySet()) {
        MetaIndex index = MetaIndex.getMetaIndex(flagWatcher, entry.getKey());
        if (entry.getValue() instanceof Double) {
            Object def = index.getDefault();
            if (def instanceof Long) {
                entry.setValue(((Double) entry.getValue()).longValue());
            } else if (def instanceof Float) {
                entry.setValue(((Double) entry.getValue()).floatValue());
            } else if (def instanceof Integer) {
                entry.setValue(((Double) entry.getValue()).intValue());
            } else if (def instanceof Short) {
                entry.setValue(((Double) entry.getValue()).shortValue());
            } else if (def instanceof Byte) {
                entry.setValue(((Double) entry.getValue()).byteValue());
            }
        } else if (entry.getValue() instanceof String) {
            if (index.getDefault() instanceof WrappedParticle) {
                entry.setValue(((ParamInfoParticle) ParamInfoManager.getParamInfo(WrappedParticle.class)).fromString((String) entry.getValue()));
            } else if (index.getDefault() instanceof EntityPose) {
                entry.setValue(((ParamInfoEnum) ParamInfoManager.getParamInfo(EntityPose.class)).fromString((String) entry.getValue()));
            }
        } else if (entry.getValue() instanceof LinkedTreeMap) {
            // If the default value is not VillagerData
            if (index.getDefault() instanceof VillagerData) {
                entry.setValue(new Gson().fromJson(new Gson().toJson(entry.getValue()), VillagerData.class));
            } else if (index.getDefault() instanceof Optional) {
                for (Field f : MetaIndex.class.getFields()) {
                    try {
                        if (f.get(null) != index) {
                            continue;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    Type type = f.getGenericType();
                    Type opt = ((ParameterizedType) type).getActualTypeArguments()[0];
                    if (opt instanceof ParameterizedType) {
                        Type val = ((ParameterizedType) opt).getActualTypeArguments()[0];
                        Optional value;
                        if (((LinkedTreeMap) entry.getValue()).isEmpty()) {
                            value = Optional.empty();
                        } else {
                            value = Optional.of(gson.fromJson(gson.toJson(((LinkedTreeMap) entry.getValue()).get("value")), val));
                        }
                        entry.setValue(value);
                    }
                }
            } else if (index.getDefault() instanceof ItemStack) {
                entry.setValue(gson.fromJson(gson.toJson(entry.getValue()), ItemStack.class));
            }
        }
        // If the deserialized class is not the same class type as the default
        if (!index.getDefault().getClass().isInstance(entry.getValue())) {
            entry.setValue(index.getDefault());
            count++;
        }
    }
    if (count > 0) {
        DisguiseUtilities.getLogger().info("Fixed " + count + " incorrect disguise flags on saved disguise");
    }
}
Also used : HashMap(java.util.HashMap) Gson(com.google.gson.Gson) MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParamInfoParticle(me.libraryaddict.disguise.utilities.params.types.custom.ParamInfoParticle) EntityPose(me.libraryaddict.disguise.disguisetypes.EntityPose) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Optional(java.util.Optional) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) DisguiseType(me.libraryaddict.disguise.disguisetypes.DisguiseType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) VillagerData(me.libraryaddict.disguise.disguisetypes.VillagerData) JsonObject(com.google.gson.JsonObject) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap)

Aggregations

MetaIndex (me.libraryaddict.disguise.disguisetypes.MetaIndex)8 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)5 WrappedDataWatcher (com.comphenix.protocol.wrappers.WrappedDataWatcher)4 ArrayList (java.util.ArrayList)3 PacketContainer (com.comphenix.protocol.events.PacketContainer)2 JsonObject (com.google.gson.JsonObject)2 DisguiseType (me.libraryaddict.disguise.disguisetypes.DisguiseType)2 PlayerDisguise (me.libraryaddict.disguise.disguisetypes.PlayerDisguise)2 LivingWatcher (me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher)2 SerializerMetaIndex (me.libraryaddict.disguise.utilities.json.SerializerMetaIndex)2 LivingEntity (org.bukkit.entity.LivingEntity)2 ItemStack (org.bukkit.inventory.ItemStack)2 FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)1 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)1 WrappedParticle (com.comphenix.protocol.wrappers.WrappedParticle)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 IOException (java.io.IOException)1 AccessibleObject (java.lang.reflect.AccessibleObject)1