Search in sources :

Example 1 with WrappedParticle

use of com.comphenix.protocol.wrappers.WrappedParticle in project LibsDisguises by libraryaddict.

the class ReflectionManager method convertInvalidMeta.

public static Object convertInvalidMeta(Object value) {
    if (value instanceof Optional) {
        Optional opt = (Optional) value;
        if (!opt.isPresent()) {
            return NmsVersion.v1_13.isSupported() ? value : com.google.common.base.Optional.absent();
        }
        if (nmsReflection != null) {
            return nmsReflection.convertOptional(opt.get());
        }
        Object val = opt.get();
        if (val instanceof BlockPosition) {
            BlockPosition pos = (BlockPosition) val;
            try {
                Object obj = blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof WrappedBlockData) {
            try {
                Object obj = ((WrappedBlockData) val).getHandle();
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof ItemStack) {
            val = getNmsItem((ItemStack) val);
            if (val == null) {
                return NmsVersion.v1_13.isSupported() ? Optional.empty() : com.google.common.base.Optional.absent();
            } else {
                return Optional.of(val);
            }
        } else if (val instanceof WrappedChatComponent) {
            Object obj = ((WrappedChatComponent) val).getHandle();
            return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
        } else if (!NmsVersion.v1_13.isSupported()) {
            return com.google.common.base.Optional.of(val);
        } else {
            return Optional.of(val);
        }
    } else if (value instanceof Vector3F) {
        Vector3F angle = (Vector3F) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance(angle.getX(), angle.getY(), angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof EulerAngle) {
        EulerAngle angle = (EulerAngle) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance((float) angle.getX(), (float) angle.getY(), (float) angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof Direction) {
        if (nmsReflection != null) {
            return nmsReflection.convertDirection((Direction) value);
        }
        try {
            return enumDirectionFrom.invoke(null, ((Direction) value).ordinal());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof BlockPosition) {
        BlockPosition pos = (BlockPosition) value;
        if (nmsReflection != null) {
            return nmsReflection.getBlockPosition(pos.getX(), pos.getY(), pos.getZ());
        }
        try {
            return blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof ItemStack) {
        return getNmsItem((ItemStack) value);
    } else if (value instanceof Double) {
        return ((Double) value).floatValue();
    } else if (value instanceof NbtWrapper) {
        return ((NbtWrapper) value).getHandle();
    } else if (value instanceof WrappedParticle) {
        return ((WrappedParticle) value).getHandle();
    } else if (value instanceof EntityPose) {
        return getNmsEntityPose((EntityPose) value);
    } else if (value instanceof VillagerData) {
        return getNmsVillagerData((VillagerData) value);
    } else if (value instanceof WrappedChatComponent) {
        return ((WrappedChatComponent) value).getHandle();
    }
    return value;
}
Also used : WrappedBlockData(com.comphenix.protocol.wrappers.WrappedBlockData) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) Optional(java.util.Optional) BlockPosition(com.comphenix.protocol.wrappers.BlockPosition) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) Direction(com.comphenix.protocol.wrappers.EnumWrappers.Direction) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) Vector3F(com.comphenix.protocol.wrappers.Vector3F) VillagerData(me.libraryaddict.disguise.disguisetypes.VillagerData) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) AccessibleObject(java.lang.reflect.AccessibleObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) ItemStack(org.bukkit.inventory.ItemStack) EulerAngle(org.bukkit.util.EulerAngle) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) EntityPose(me.libraryaddict.disguise.disguisetypes.EntityPose)

Example 2 with WrappedParticle

use of com.comphenix.protocol.wrappers.WrappedParticle in project LibsDisguises by libraryaddict.

the class ParamInfoParticle method toString.

@Override
public String toString(Object object) {
    WrappedParticle particle = (WrappedParticle) object;
    Object data = particle.getData();
    String returns = particle.getParticle().name();
    if (data != null) {
        if (data instanceof ItemStack) {
            returns += "," + ((ItemStack) data).getType().name();
        } else if (data instanceof WrappedBlockData) {
            returns += "," + ((WrappedBlockData) data).getType().name();
        } else if (data instanceof Particle.DustOptions) {
            returns += "," + ParamInfoManager.getParamInfo(Color.class).toString(((Particle.DustOptions) data).getColor());
            if (((Particle.DustOptions) data).getSize() != 1f) {
                returns += "," + ((Particle.DustOptions) data).getSize();
            }
        }
    }
    return returns;
}
Also used : WrappedBlockData(com.comphenix.protocol.wrappers.WrappedBlockData) Particle(org.bukkit.Particle) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) Color(org.bukkit.Color) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with WrappedParticle

use of com.comphenix.protocol.wrappers.WrappedParticle 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)

Example 4 with WrappedParticle

use of com.comphenix.protocol.wrappers.WrappedParticle in project LibsDisguises by libraryaddict.

the class AreaEffectCloudWatcher method getParticle.

@NmsAddedIn(NmsVersion.v1_13)
public WrappedParticle getParticle() {
    if (NmsVersion.v1_13.isSupported()) {
        return getData(MetaIndex.AREA_EFFECT_PARTICLE);
    } else {
        // Item crack, block crack, block dust, falling dust
        int particleId = getData(MetaIndex.AREA_EFFECT_PARTICLE_OLD);
        Particle particle = Particle.values()[particleId];
        return WrappedParticle.create(particle, null);
    }
}
Also used : Particle(org.bukkit.Particle) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) NmsAddedIn(me.libraryaddict.disguise.utilities.reflection.annotations.NmsAddedIn)

Example 5 with WrappedParticle

use of com.comphenix.protocol.wrappers.WrappedParticle in project GuardiansOfAdelia by Lix3nn53.

the class MyPacketListeners method addPacketListeners.

public static void addPacketListeners() {
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    protocolManager.addPacketListener(new PacketAdapter(GuardiansOfAdelia.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Server.WORLD_PARTICLES) {

        @Override
        public void onPacketSending(PacketEvent event) {
            WrappedParticle wrappedParticle = event.getPacket().getNewParticles().read(0);
            Particle particle = wrappedParticle.getParticle();
            if (particle.equals(Particle.DAMAGE_INDICATOR)) {
                event.setCancelled(true);
            }
        }
    });
}
Also used : Particle(org.bukkit.Particle) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) ProtocolManager(com.comphenix.protocol.ProtocolManager) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) PacketEvent(com.comphenix.protocol.events.PacketEvent)

Aggregations

WrappedParticle (com.comphenix.protocol.wrappers.WrappedParticle)5 Particle (org.bukkit.Particle)3 ItemStack (org.bukkit.inventory.ItemStack)3 WrappedBlockData (com.comphenix.protocol.wrappers.WrappedBlockData)2 Optional (java.util.Optional)2 EntityPose (me.libraryaddict.disguise.disguisetypes.EntityPose)2 VillagerData (me.libraryaddict.disguise.disguisetypes.VillagerData)2 ProtocolManager (com.comphenix.protocol.ProtocolManager)1 PacketAdapter (com.comphenix.protocol.events.PacketAdapter)1 PacketEvent (com.comphenix.protocol.events.PacketEvent)1 FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)1 BlockPosition (com.comphenix.protocol.wrappers.BlockPosition)1 Direction (com.comphenix.protocol.wrappers.EnumWrappers.Direction)1 Vector3F (com.comphenix.protocol.wrappers.Vector3F)1 WrappedChatComponent (com.comphenix.protocol.wrappers.WrappedChatComponent)1 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 NbtWrapper (com.comphenix.protocol.wrappers.nbt.NbtWrapper)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1