Search in sources :

Example 81 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class DenizenNetworkManagerImpl method processDisguiseForPacket.

public boolean processDisguiseForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (DisguiseCommand.disguises.isEmpty() || antiDuplicate) {
        return false;
    }
    try {
        int ider = -1;
        if (packet instanceof ClientboundSetEntityDataPacket) {
            ider = ((ClientboundSetEntityDataPacket) packet).getId();
        }
        if (packet instanceof ClientboundUpdateAttributesPacket) {
            ider = ((ClientboundUpdateAttributesPacket) packet).getEntityId();
        }
        if (packet instanceof ClientboundAddPlayerPacket) {
            ider = ((ClientboundAddPlayerPacket) packet).getEntityId();
        } else if (packet instanceof ClientboundAddEntityPacket) {
            ider = ((ClientboundAddEntityPacket) packet).getId();
        } else if (packet instanceof ClientboundAddMobPacket) {
            ider = ((ClientboundAddMobPacket) packet).getId();
        }
        if (ider != -1) {
            Entity e = player.getLevel().getEntity(ider);
            if (e == null) {
                return false;
            }
            HashMap<UUID, DisguiseCommand.TrackedDisguise> playerMap = DisguiseCommand.disguises.get(e.getUUID());
            if (playerMap == null) {
                return false;
            }
            DisguiseCommand.TrackedDisguise disguise = playerMap.get(player.getUUID());
            if (disguise == null) {
                disguise = playerMap.get(null);
                if (disguise == null) {
                    return false;
                }
            }
            if (!disguise.isActive) {
                return false;
            }
            if (NMSHandler.debugPackets) {
                doPacketOutput("DISGUISED packet " + packet.getClass().getName() + " for entity " + ider + " to player " + player.getScoreboardName());
            }
            if (packet instanceof ClientboundSetEntityDataPacket) {
                ClientboundSetEntityDataPacket metadataPacket = (ClientboundSetEntityDataPacket) packet;
                if (e.getId() == player.getId()) {
                    if (!disguise.shouldFake) {
                        return false;
                    }
                    List<SynchedEntityData.DataItem<?>> data = metadataPacket.getUnpackedData();
                    for (SynchedEntityData.DataItem item : data) {
                        EntityDataAccessor<?> watcherObject = item.getAccessor();
                        int watcherId = watcherObject.getId();
                        if (watcherId == 0) {
                            // Entity flags
                            ClientboundSetEntityDataPacket altPacket = new ClientboundSetEntityDataPacket(copyPacket(metadataPacket));
                            data = new ArrayList<>(data);
                            ENTITY_METADATA_LIST.set(altPacket, data);
                            data.remove(item);
                            byte flags = (byte) item.getValue();
                            // Invisible flag
                            flags |= 0x20;
                            data.add(new SynchedEntityData.DataItem(watcherObject, flags));
                            ClientboundSetEntityDataPacket updatedPacket = getModifiedMetadataFor(altPacket);
                            oldManager.send(updatedPacket == null ? altPacket : updatedPacket, genericfuturelistener);
                            return true;
                        }
                    }
                } else {
                    ClientboundSetEntityDataPacket altPacket = new ClientboundSetEntityDataPacket(e.getId(), ((CraftEntity) disguise.toOthers.entity.entity).getHandle().getEntityData(), true);
                    oldManager.send(altPacket, genericfuturelistener);
                    return true;
                }
                return false;
            }
            if (packet instanceof ClientboundUpdateAttributesPacket) {
                FakeEntity fake = ider == player.getId() ? disguise.fakeToSelf : disguise.toOthers;
                if (fake == null) {
                    return false;
                }
                if (fake.entity.entity instanceof LivingEntity) {
                    return false;
                }
                // Non-living don't have attributes
                return true;
            }
            antiDuplicate = true;
            disguise.sendTo(Collections.singletonList(new PlayerTag(player.getBukkitEntity())));
            antiDuplicate = false;
            return true;
        }
    } catch (Throwable ex) {
        antiDuplicate = false;
        Debug.echoError(ex);
    }
    return false;
}
Also used : FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) LivingEntity(org.bukkit.entity.LivingEntity) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) DisguiseCommand(com.denizenscript.denizen.scripts.commands.player.DisguiseCommand) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) LivingEntity(org.bukkit.entity.LivingEntity)

Aggregations

PlayerTag (com.denizenscript.denizen.objects.PlayerTag)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)41 Player (org.bukkit.entity.Player)26 List (java.util.List)23 NPCTag (com.denizenscript.denizen.objects.NPCTag)22 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 ListTag (com.denizenscript.denizencore.objects.core.ListTag)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)12 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)11 EventHandler (org.bukkit.event.EventHandler)11 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)8 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)7 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)6 Argument (com.denizenscript.denizencore.objects.Argument)6