Search in sources :

Example 6 with PlayerConnection

use of net.minecraft.server.v1_16_R3.PlayerConnection in project Citizens2 by CitizensDev.

the class EntityHumanNPC method initialise.

private void initialise(MinecraftServer minecraftServer) {
    Socket socket = new EmptySocket();
    NetworkManager conn = null;
    try {
        conn = new EmptyNetworkManager(EnumProtocolDirection.CLIENTBOUND);
        playerConnection = new EmptyNetHandler(minecraftServer, conn, this);
        conn.setPacketListener(playerConnection);
        socket.close();
    } catch (IOException e) {
    // swallow
    }
    AttributeModifiable range = getAttributeInstance(GenericAttributes.FOLLOW_RANGE);
    if (range == null) {
        try {
            AttributeProvider provider = (AttributeProvider) ATTRIBUTE_MAP.invoke(getAttributeMap());
            Map<AttributeBase, AttributeModifiable> all = Maps.newHashMap((Map<AttributeBase, AttributeModifiable>) ATTRIBUTE_PROVIDER_MAP.invoke(provider));
            all.put(GenericAttributes.FOLLOW_RANGE, new AttributeModifiable(GenericAttributes.FOLLOW_RANGE, new Consumer<AttributeModifiable>() {

                @Override
                public void accept(AttributeModifiable att) {
                    throw new UnsupportedOperationException("Tried to change value for default attribute instance FOLLOW_RANGE");
                }
            }));
            ATTRIBUTE_PROVIDER_MAP_SETTER.invoke(provider, ImmutableMap.copyOf(all));
        } catch (Throwable e) {
            e.printStackTrace();
        }
        range = getAttributeMap().a(GenericAttributes.FOLLOW_RANGE);
    }
    range.setValue(Setting.DEFAULT_PATHFINDING_RANGE.asDouble());
    controllerJump = new PlayerControllerJump(this);
    controllerLook = new PlayerControllerLook(this);
    controllerMove = new PlayerControllerMove(this);
    navigation = new PlayerNavigation(this, world);
    invulnerableTicks = 0;
    // the default (0) breaks step climbing
    NMS.setStepHeight(getBukkitEntity(), 1);
    setSkinFlags((byte) 0xFF);
    EmptyAdvancementDataPlayer.clear(this.getAdvancementData());
    NMSImpl.setAdvancement(this.getBukkitEntity(), new EmptyAdvancementDataPlayer(minecraftServer.getDataFixer(), minecraftServer.getPlayerList(), minecraftServer.getAdvancementData(), CitizensAPI.getDataFolder().getParentFile(), this));
}
Also used : EmptyNetworkManager(net.citizensnpcs.nms.v1_16_R3.network.EmptyNetworkManager) PlayerControllerLook(net.citizensnpcs.nms.v1_16_R3.util.PlayerControllerLook) EmptySocket(net.citizensnpcs.nms.v1_16_R3.network.EmptySocket) IOException(java.io.IOException) EmptyNetHandler(net.citizensnpcs.nms.v1_16_R3.network.EmptyNetHandler) PlayerControllerMove(net.citizensnpcs.nms.v1_16_R3.util.PlayerControllerMove) Consumer(java.util.function.Consumer) AttributeModifiable(net.minecraft.server.v1_16_R3.AttributeModifiable) PlayerNavigation(net.citizensnpcs.nms.v1_16_R3.util.PlayerNavigation) PlayerControllerJump(net.citizensnpcs.nms.v1_16_R3.util.PlayerControllerJump) AttributeBase(net.minecraft.server.v1_16_R3.AttributeBase) EmptyAdvancementDataPlayer(net.citizensnpcs.nms.v1_16_R3.util.EmptyAdvancementDataPlayer) EmptyNetworkManager(net.citizensnpcs.nms.v1_16_R3.network.EmptyNetworkManager) NetworkManager(net.minecraft.server.v1_16_R3.NetworkManager) Socket(java.net.Socket) EmptySocket(net.citizensnpcs.nms.v1_16_R3.network.EmptySocket) AttributeProvider(net.minecraft.server.v1_16_R3.AttributeProvider)

Example 7 with PlayerConnection

use of net.minecraft.server.v1_16_R3.PlayerConnection in project Denizen by DenizenScript.

the class PlayerHelperImpl method sendEntitySpawn.

@Override
public FakeEntity sendEntitySpawn(List<PlayerTag> players, DenizenEntityType entityType, LocationTag location, ArrayList<Mechanism> mechanisms, int customId, UUID customUUID, boolean autoTrack) {
    CraftWorld world = ((CraftWorld) location.getWorld());
    net.minecraft.server.v1_16_R3.Entity nmsEntity;
    if (entityType.isCustom()) {
        if (entityType.customEntityType == CustomEntityType.ITEM_PROJECTILE) {
            org.bukkit.inventory.ItemStack itemStack = new ItemStack(Material.STONE);
            for (Mechanism mechanism : mechanisms) {
                if (mechanism.matches("item") && mechanism.requireObject(ItemTag.class)) {
                    itemStack = mechanism.valueAsType(ItemTag.class).getItemStack();
                }
            }
            nmsEntity = new EntityItemProjectileImpl(world.getHandle(), location, CraftItemStack.asNMSCopy(itemStack));
        } else if (entityType.customEntityType == CustomEntityType.FAKE_PLAYER) {
            String name = null;
            String skin = null;
            for (Mechanism mechanism : new ArrayList<>(mechanisms)) {
                if (mechanism.matches("name")) {
                    name = mechanism.getValue().asString();
                    mechanisms.remove(mechanism);
                } else if (mechanism.matches("skin")) {
                    skin = mechanism.getValue().asString();
                    mechanisms.remove(mechanism);
                }
                if (name != null && skin != null) {
                    break;
                }
            }
            nmsEntity = ((CraftFakePlayerImpl) CustomEntityHelperImpl.spawnFakePlayer(location, name, skin, false)).getHandle();
        } else {
            throw new IllegalArgumentException("entityType");
        }
    } else {
        nmsEntity = world.createEntity(location, entityType.getBukkitEntityType().getEntityClass());
    }
    if (customUUID != null) {
        nmsEntity.e(customId);
        nmsEntity.a_(customUUID);
    }
    EntityTag entity = new EntityTag(nmsEntity.getBukkitEntity());
    for (Mechanism mechanism : mechanisms) {
        entity.safeAdjustDuplicate(mechanism);
    }
    nmsEntity.dead = false;
    FakeEntity fake = new FakeEntity(players, location, entity.getBukkitEntity().getEntityId());
    fake.entity = new EntityTag(entity.getBukkitEntity());
    fake.entity.isFake = true;
    fake.entity.isFakeValid = true;
    List<TrackerData> trackers = new ArrayList<>();
    fake.triggerSpawnPacket = (player) -> {
        EntityPlayer nmsPlayer = ((CraftPlayer) player.getPlayerEntity()).getHandle();
        PlayerConnection conn = nmsPlayer.playerConnection;
        final EntityTrackerEntry tracker = new EntityTrackerEntry(world.getHandle(), nmsEntity, 1, true, conn::sendPacket, Collections.singleton(nmsPlayer));
        tracker.b(nmsPlayer);
        final TrackerData data = new TrackerData();
        data.player = player;
        data.tracker = tracker;
        trackers.add(data);
        if (autoTrack) {
            new BukkitRunnable() {

                boolean wasOnline = true;

                @Override
                public void run() {
                    if (!fake.entity.isFakeValid) {
                        cancel();
                        return;
                    }
                    if (player.isOnline()) {
                        if (!wasOnline) {
                            tracker.b(((CraftPlayer) player.getPlayerEntity()).getHandle());
                            wasOnline = true;
                        }
                        tracker.a();
                    } else if (wasOnline) {
                        wasOnline = false;
                    }
                }
            }.runTaskTimer(Denizen.getInstance(), 1, 1);
        }
    };
    for (PlayerTag player : players) {
        fake.triggerSpawnPacket.accept(player);
    }
    fake.triggerUpdatePacket = () -> {
        for (TrackerData tracker : trackers) {
            if (tracker.player.isOnline()) {
                tracker.tracker.a();
            }
        }
    };
    fake.triggerDestroyPacket = () -> {
        for (TrackerData tracker : trackers) {
            if (tracker.player.isOnline()) {
                tracker.tracker.a(((CraftPlayer) tracker.player.getPlayerEntity()).getHandle());
            }
        }
        trackers.clear();
    };
    return fake;
}
Also used : FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) EntityItemProjectileImpl(com.denizenscript.denizen.nms.v1_16.impl.entities.EntityItemProjectileImpl) CraftFakePlayerImpl(com.denizenscript.denizen.nms.v1_16.impl.entities.CraftFakePlayerImpl) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) Mechanism(com.denizenscript.denizencore.objects.Mechanism) EntityTag(com.denizenscript.denizen.objects.EntityTag) CraftWorld(org.bukkit.craftbukkit.v1_16_R3.CraftWorld) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) ItemStack(org.bukkit.inventory.ItemStack) net.minecraft.server.v1_16_R3(net.minecraft.server.v1_16_R3) CraftItemStack(org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 8 with PlayerConnection

use of net.minecraft.server.v1_16_R3.PlayerConnection in project THP-Engine by TheHollowPlanetMC.

the class FlyPacketHandler method rewrite.

@Override
public Object rewrite(Object packet, EnginePlayer EnginePlayer, boolean cacheSetting) {
    ParallelUniverse universe = EnginePlayer.getUniverse();
    if (universe == null)
        return packet;
    World world = EnginePlayer.getBukkitPlayer().getWorld();
    String worldName = world.getName();
    ParallelWorld parallelWorld = universe.getWorld(worldName);
    EntityPlayer entityPlayer = ((CraftPlayer) EnginePlayer.getBukkitPlayer()).getHandle();
    int x = NumberConversions.floor(entityPlayer.locX());
    int y = NumberConversions.floor(entityPlayer.locY());
    int z = NumberConversions.floor(entityPlayer.locZ());
    int downY = y - 1;
    downY = Math.max(0, downY);
    if (parallelWorld.hasBlockData(x, y, z) || parallelWorld.hasBlockData(x, downY, z)) {
        try {
            PlayerConnection playerConnection = entityPlayer.playerConnection;
            C.set(playerConnection, 0);
            E.set(playerConnection, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return packet;
}
Also used : ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) ParallelUniverse(thpmc.engine.api.world.parallel.ParallelUniverse) EntityPlayer(net.minecraft.server.v1_16_R3.EntityPlayer) CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) World(org.bukkit.World) ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) PlayerConnection(net.minecraft.server.v1_16_R3.PlayerConnection)

Example 9 with PlayerConnection

use of net.minecraft.server.v1_16_R3.PlayerConnection in project MechanicsMain by WeaponMechanics.

the class FakeEntity_1_16_R3 method show.

@Override
public void show(@NotNull Player player) {
    PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
    if (connections.contains(connection))
        throw new IllegalArgumentException();
    connection.sendPacket(type.isAlive() ? new PacketPlayOutSpawnEntityLiving((EntityLiving) entity) : new PacketPlayOutSpawnEntity(entity, type == EntityType.FALLING_BLOCK ? Block.getCombinedId(block) : 0));
    connection.sendPacket(new PacketPlayOutEntityMetadata(cache, entity.getDataWatcher(), true));
    connection.sendPacket(new PacketPlayOutEntityLook(cache, convertYaw(getYaw()), convertPitch(getPitch()), false));
    connection.sendPacket(new PacketPlayOutEntityVelocity(cache, new Vec3D(motion.getX(), motion.getY(), motion.getZ())));
    connection.sendPacket(new PacketPlayOutEntityHeadRotation(entity, convertYaw(getYaw())));
    PacketPlayOutEntityEquipment equipment = getEquipmentPacket();
    if (equipment != null)
        connection.sendPacket(equipment);
    // Inject the player's packet connection into this listener, so we can
    // show the player position/velocity/rotation changes
    connections.add(connection);
}
Also used : PacketPlayOutEntityLook(net.minecraft.server.v1_16_R3.PacketPlayOutEntity.PacketPlayOutEntityLook)

Aggregations

CraftPlayer (org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer)5 CraftWorld (org.bukkit.craftbukkit.v1_16_R3.CraftWorld)3 CraftFakePlayerImpl (com.denizenscript.denizen.nms.v1_16.impl.entities.CraftFakePlayerImpl)2 EntityItemProjectileImpl (com.denizenscript.denizen.nms.v1_16.impl.entities.EntityItemProjectileImpl)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)2 ItemTag (com.denizenscript.denizen.objects.ItemTag)2 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)2 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)2 Mechanism (com.denizenscript.denizencore.objects.Mechanism)2 net.minecraft.server.v1_16_R3 (net.minecraft.server.v1_16_R3)2 EntityPlayer (net.minecraft.server.v1_16_R3.EntityPlayer)2 NetworkManager (net.minecraft.server.v1_16_R3.NetworkManager)2 PacketPlayOutEntityLook (net.minecraft.server.v1_16_R3.PacketPlayOutEntity.PacketPlayOutEntityLook)2 PlayerConnection (net.minecraft.server.v1_16_R3.PlayerConnection)2 World (org.bukkit.World)2 CraftItemStack (org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack)2 Player (org.bukkit.entity.Player)2 ItemStack (org.bukkit.inventory.ItemStack)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 ParallelUniverse (thpmc.engine.api.world.parallel.ParallelUniverse)2