Search in sources :

Example 1 with GrimPlayer

use of ac.grim.grimac.player.GrimPlayer in project Grim by MWHunter.

the class KnockbackHandler method onPacketSend.

@Override
public void onPacketSend(final PacketPlaySendEvent event) {
    byte packetID = event.getPacketId();
    if (packetID == PacketType.Play.Server.ENTITY_VELOCITY) {
        WrappedPacketOutEntityVelocity velocity = new WrappedPacketOutEntityVelocity(event.getNMSPacket());
        int entityId = velocity.getEntityId();
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        // Detect whether this knockback packet affects the player or if it is useless
        Entity playerVehicle = player.bukkitPlayer.getVehicle();
        if ((playerVehicle == null && entityId != player.entityID) || (playerVehicle != null && entityId != playerVehicle.getEntityId())) {
            return;
        }
        // If the player isn't in a vehicle and the ID is for the player, the player will take kb
        // If the player is in a vehicle and the ID is for the player's vehicle, the player will take kb
        Vector3d playerVelocity = velocity.getVelocity();
        // Wrap velocity between two transactions
        player.sendTransaction();
        addPlayerKnockback(entityId, player.lastTransactionSent.get(), new Vector(playerVelocity.getX(), playerVelocity.getY(), playerVelocity.getZ()));
        event.setPostTask(player::sendTransaction);
    }
}
Also used : Entity(org.bukkit.entity.Entity) Vector3d(io.github.retrooper.packetevents.utils.vector.Vector3d) WrappedPacketOutEntityVelocity(io.github.retrooper.packetevents.packetwrappers.play.out.entityvelocity.WrappedPacketOutEntityVelocity) GrimPlayer(ac.grim.grimac.player.GrimPlayer) Vector(org.bukkit.util.Vector)

Example 2 with GrimPlayer

use of ac.grim.grimac.player.GrimPlayer in project Grim by MWHunter.

the class PacketPlayerAttack method onPacketPlayReceive.

@Override
public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
    if (event.getPacketId() == PacketType.Play.Client.USE_ENTITY) {
        WrappedPacketInUseEntity action = new WrappedPacketInUseEntity(event.getNMSPacket());
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        if (action.getAction() == WrappedPacketInUseEntity.EntityUseAction.ATTACK) {
            ItemStack heldItem = player.bukkitPlayer.getInventory().getItem(player.packetStateData.lastSlotSelected);
            Entity attackedEntity = action.getEntity();
            // You don't get a release use item with block hitting with a sword?
            if (heldItem != null && player.getClientVersion().isOlderThan(ClientVersion.v_1_9)) {
                if (heldItem.getType().toString().endsWith("_SWORD"))
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.FALSE;
            }
            if (attackedEntity != null && (!(attackedEntity instanceof LivingEntity) || attackedEntity instanceof Player)) {
                boolean hasKnockbackSword = heldItem != null && heldItem.getEnchantmentLevel(Enchantment.KNOCKBACK) > 0;
                boolean isLegacyPlayer = player.getClientVersion().isOlderThanOrEquals(ClientVersion.v_1_8);
                boolean hasNegativeKB = heldItem != null && heldItem.getEnchantmentLevel(Enchantment.KNOCKBACK) < 0;
                // Players with knockback enchantments always get slowed
                if ((player.isSprinting && !hasNegativeKB && isLegacyPlayer) || hasKnockbackSword) {
                    player.minPlayerAttackSlow += 1;
                    player.maxPlayerAttackSlow += 1;
                    // Players cannot slow themselves twice in one tick without a knockback sword
                    if (!hasKnockbackSword) {
                        player.minPlayerAttackSlow = 0;
                        player.maxPlayerAttackSlow = 1;
                    }
                } else if (!isLegacyPlayer && player.isSprinting) {
                    // 1.9+ player who might have been slowed, but we can't be sure
                    player.maxPlayerAttackSlow += 1;
                }
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) WrappedPacketInUseEntity(io.github.retrooper.packetevents.packetwrappers.play.in.useentity.WrappedPacketInUseEntity) GrimPlayer(ac.grim.grimac.player.GrimPlayer) Player(org.bukkit.entity.Player) WrappedPacketInUseEntity(io.github.retrooper.packetevents.packetwrappers.play.in.useentity.WrappedPacketInUseEntity) ItemStack(org.bukkit.inventory.ItemStack) GrimPlayer(ac.grim.grimac.player.GrimPlayer)

Example 3 with GrimPlayer

use of ac.grim.grimac.player.GrimPlayer in project Grim by MWHunter.

the class PacketPlayerCooldown method onPacketPlaySend.

@Override
public void onPacketPlaySend(PacketPlaySendEvent event) {
    if (event.getPacketId() == PacketType.Play.Server.SET_COOLDOWN) {
        WrappedPacketOutSetCooldown cooldown = new WrappedPacketOutSetCooldown(event.getNMSPacket());
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        int lastTransactionSent = player.lastTransactionSent.get();
        if (cooldown.getCooldownTicks() == 0) {
            // for removing the cooldown
            player.latencyUtils.addRealTimeTask(lastTransactionSent + 1, () -> {
                player.checkManager.getCompensatedCooldown().removeCooldown(cooldown.getItemStack().getType());
            });
        } else {
            // Not for removing the cooldown
            player.latencyUtils.addRealTimeTask(lastTransactionSent, () -> {
                player.checkManager.getCompensatedCooldown().addCooldown(cooldown.getItemStack().getType(), cooldown.getCooldownTicks(), lastTransactionSent);
            });
        }
    }
}
Also used : WrappedPacketOutSetCooldown(io.github.retrooper.packetevents.packetwrappers.play.out.setcooldown.WrappedPacketOutSetCooldown) GrimPlayer(ac.grim.grimac.player.GrimPlayer)

Example 4 with GrimPlayer

use of ac.grim.grimac.player.GrimPlayer in project Grim by MWHunter.

the class PacketPlayerDigging method onPacketPlayReceive.

@Override
public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
    byte packetID = event.getPacketId();
    if (packetID == PacketType.Play.Client.BLOCK_DIG) {
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        WrappedPacketInBlockDig dig = new WrappedPacketInBlockDig(event.getNMSPacket());
        WrappedPacketInBlockDig.PlayerDigType type = dig.getDigType();
        if (type == WrappedPacketInBlockDig.PlayerDigType.RELEASE_USE_ITEM) {
            player.packetStateData.slowedByUsingItem = AlmostBoolean.FALSE;
            player.packetStateData.slowedByUsingItemTransaction = player.lastTransactionReceived.get();
            if (XMaterial.supports(13)) {
                ItemStack main = player.bukkitPlayer.getInventory().getItemInMainHand();
                ItemStack off = player.bukkitPlayer.getInventory().getItemInOffHand();
                int j = 0;
                if (main.getType() == TRIDENT) {
                    j = main.getEnchantmentLevel(Enchantment.RIPTIDE);
                } else if (off.getType() == TRIDENT) {
                    j = off.getEnchantmentLevel(Enchantment.RIPTIDE);
                }
                if (j > 0) {
                // TODO: Re-add riptide support
                }
            }
        }
    }
    if (packetID == PacketType.Play.Client.HELD_ITEM_SLOT) {
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        WrappedPacketInHeldItemSlot slot = new WrappedPacketInHeldItemSlot(event.getNMSPacket());
        // Stop people from spamming the server with out of bounds exceptions
        if (slot.getCurrentSelectedSlot() > 8)
            return;
        player.packetStateData.lastSlotSelected = slot.getCurrentSelectedSlot();
    }
    if (packetID == PacketType.Play.Client.BLOCK_PLACE) {
        WrappedPacketInBlockPlace place = new WrappedPacketInBlockPlace(event.getNMSPacket());
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        if (XMaterial.supports(8) && player.gamemode == GameMode.SPECTATOR)
            return;
        // This was an interaction with a block, not a use item
        if (ServerVersion.getVersion().isOlderThan(ServerVersion.v_1_9) && place.getDirection() != Direction.OTHER)
            return;
        player.packetStateData.slowedByUsingItemTransaction = player.lastTransactionReceived.get();
        // Design inspired by NoCheatPlus, but rewritten to be faster
        // https://github.com/Updated-NoCheatPlus/NoCheatPlus/blob/master/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/NoSlow.java
        ItemStack item = place.getHand() == Hand.MAIN_HAND ? player.bukkitPlayer.getInventory().getItem(player.packetStateData.lastSlotSelected) : player.bukkitPlayer.getInventory().getItemInOffHand();
        if (item != null) {
            Material material = item.getType();
            if (player.checkManager.getCompensatedCooldown().hasMaterial(material)) {
                // resync, not required
                player.packetStateData.slowedByUsingItem = AlmostBoolean.FALSE;
                // The player has a cooldown, and therefore cannot use this item!
                return;
            }
            // 1.14 and below players cannot eat in creative, exceptions are potions or milk
            if ((player.getClientVersion().isNewerThanOrEquals(ClientVersion.v_1_15) || player.gamemode != GameMode.CREATIVE && material.isEdible()) || material == POTION || material == MILK_BUCKET) {
                // pre1.9 splash potion
                if (ServerVersion.getVersion().isOlderThanOrEquals(ServerVersion.v_1_8_8) && item.getDurability() > 16384)
                    return;
                // Eatable items that don't require any hunger to eat
                if (material == Material.POTION || material == Material.MILK_BUCKET || material == GOLDEN_APPLE || material == ENCHANTED_GOLDEN_APPLE || material == HONEY_BOTTLE) {
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.TRUE;
                    player.packetStateData.eatingHand = place.getHand();
                    return;
                }
                // The other items that do require it
                if (item.getType().isEdible() && (event.getPlayer().getFoodLevel() < 20 || player.gamemode == GameMode.CREATIVE)) {
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.TRUE;
                    player.packetStateData.eatingHand = place.getHand();
                    return;
                }
                // The player cannot eat this item, resync use status
                player.packetStateData.slowedByUsingItem = AlmostBoolean.FALSE;
            }
            if (material == SHIELD) {
                player.packetStateData.slowedByUsingItem = AlmostBoolean.TRUE;
                player.packetStateData.eatingHand = place.getHand();
                return;
            }
            // Avoid releasing crossbow as being seen as slowing player
            if (material == CROSSBOW) {
                CrossbowMeta crossbowMeta = (CrossbowMeta) item.getItemMeta();
                if (crossbowMeta != null && crossbowMeta.hasChargedProjectiles())
                    return;
            }
            // The client and server don't agree on trident status because mojang is incompetent at netcode.
            if (material == TRIDENT) {
                if (item.getEnchantmentLevel(Enchantment.RIPTIDE) > 0)
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.MAYBE;
                else
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.TRUE;
                player.packetStateData.eatingHand = place.getHand();
            }
            // Crossbow charge checked previously
            if (material == BOW || material == CROSSBOW) {
                player.packetStateData.slowedByUsingItem = (player.gamemode == GameMode.CREATIVE || hasItem(player, ARROW) || hasItem(player, TIPPED_ARROW) || hasItem(player, SPECTRAL_ARROW)) ? AlmostBoolean.TRUE : AlmostBoolean.FALSE;
                player.packetStateData.eatingHand = place.getHand();
            }
            // Only 1.8 and below players can block with swords
            if (material.toString().endsWith("_SWORD")) {
                if (player.getClientVersion().isOlderThanOrEquals(ClientVersion.v_1_8))
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.TRUE;
                else if (// ViaVersion stuff
                ServerVersion.getVersion().isOlderThan(ServerVersion.v_1_9))
                    player.packetStateData.slowedByUsingItem = AlmostBoolean.MAYBE;
            }
        } else {
            player.packetStateData.slowedByUsingItem = AlmostBoolean.FALSE;
        }
    }
}
Also used : WrappedPacketInBlockDig(io.github.retrooper.packetevents.packetwrappers.play.in.blockdig.WrappedPacketInBlockDig) XMaterial(ac.grim.grimac.utils.nmsutil.XMaterial) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) GrimPlayer(ac.grim.grimac.player.GrimPlayer) WrappedPacketInHeldItemSlot(io.github.retrooper.packetevents.packetwrappers.play.in.helditemslot.WrappedPacketInHeldItemSlot) CrossbowMeta(org.bukkit.inventory.meta.CrossbowMeta) WrappedPacketInBlockPlace(io.github.retrooper.packetevents.packetwrappers.play.in.blockplace.WrappedPacketInBlockPlace)

Example 5 with GrimPlayer

use of ac.grim.grimac.player.GrimPlayer in project Grim by MWHunter.

the class PacketPlayerSteer method onPacketPlayReceive.

@Override
public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
    byte packetID = event.getPacketId();
    if (packetID == PacketType.Play.Client.STEER_VEHICLE) {
        WrappedPacketInSteerVehicle steer = new WrappedPacketInSteerVehicle(event.getNMSPacket());
        GrimPlayer player = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(event.getPlayer());
        if (player == null)
            return;
        // Otherwise other checks may false because the player's position is unknown.
        if (player.packetStateData.receivedSteerVehicle && player.playerVehicle != null) {
            // Tick updates AFTER updating bounding box and actual movement
            player.compensatedWorld.tickPlayerInPistonPushingArea();
            // Note for the movement check
            player.vehicleData.lastDummy = true;
            // Keep a reference of this just in case the next like sets this to null
            PacketEntity vehicle = player.playerVehicle;
            // Tick player vehicle after we update the packet entity state
            player.lastVehicle = player.playerVehicle;
            player.playerVehicle = player.vehicle == null ? null : player.compensatedEntities.getEntity(player.vehicle);
            player.inVehicle = player.playerVehicle != null;
            // Update knockback and explosions after getting the vehicle
            player.firstBreadKB = player.checkManager.getKnockbackHandler().getFirstBreadOnlyKnockback(player.inVehicle ? player.vehicle : player.entityID, player.lastTransactionReceived.get());
            player.likelyKB = player.checkManager.getKnockbackHandler().getRequiredKB(player.inVehicle ? player.vehicle : player.entityID, player.lastTransactionReceived.get());
            player.firstBreadExplosion = player.checkManager.getExplosionHandler().getFirstBreadAddedExplosion(player.lastTransactionReceived.get());
            player.likelyExplosions = player.checkManager.getExplosionHandler().getPossibleExplosions(player.lastTransactionReceived.get());
            // Not responsible for applying knockback/explosions
            player.checkManager.getExplosionHandler().forceExempt();
            player.checkManager.getKnockbackHandler().forceExempt();
            // Set position now to support "dummy" riding without control
            // Warning - on pigs and striders players, can turn into dummies independent of whether they have
            // control of the vehicle or not (which could be abused to set velocity to 0 repeatedly and kind
            // of float in the air, although what's the point inside a vehicle?)
            player.lastX = player.x;
            player.lastY = player.y;
            player.lastZ = player.z;
            SimpleCollisionBox vehiclePos = vehicle.getPossibleCollisionBoxes();
            player.x = (vehiclePos.minX + vehiclePos.maxX) / 2;
            player.y = (vehiclePos.minY + vehiclePos.maxY) / 2;
            player.z = (vehiclePos.minZ + vehiclePos.maxZ) / 2;
            // Use bukkit location, not packet location, to stop ping spoof attacks on entity position
            Entity playerVehicle = player.bukkitPlayer.getVehicle();
            if (playerVehicle != null) {
                double x = playerVehicle.getLocation().getX();
                double y = playerVehicle.getLocation().getY();
                double z = playerVehicle.getLocation().getZ();
                player.getSetbackTeleportUtil().setSafeSetbackLocation(player.bukkitPlayer.getWorld(), new Vector3d(x, y, z));
            }
            return;
        } else {
            // Try and get the player's vehicle to the queue for next time
            player.movementCheckRunner.runTransactionQueue(player);
        }
        player.packetStateData.receivedSteerVehicle = true;
        player.vehicleData.nextVehicleForward = steer.getForwardValue();
        player.vehicleData.nextVehicleForward = steer.getSideValue();
    }
}
Also used : SimpleCollisionBox(ac.grim.grimac.utils.collisions.datatypes.SimpleCollisionBox) Entity(org.bukkit.entity.Entity) PacketEntity(ac.grim.grimac.utils.data.packetentity.PacketEntity) PacketEntity(ac.grim.grimac.utils.data.packetentity.PacketEntity) Vector3d(io.github.retrooper.packetevents.utils.vector.Vector3d) WrappedPacketInSteerVehicle(io.github.retrooper.packetevents.packetwrappers.play.in.steervehicle.WrappedPacketInSteerVehicle) GrimPlayer(ac.grim.grimac.player.GrimPlayer)

Aggregations

GrimPlayer (ac.grim.grimac.player.GrimPlayer)35 EventHandler (org.bukkit.event.EventHandler)11 Player (org.bukkit.entity.Player)7 Vector3d (io.github.retrooper.packetevents.utils.vector.Vector3d)6 Entity (org.bukkit.entity.Entity)5 Location (org.bukkit.Location)4 SimpleCollisionBox (ac.grim.grimac.utils.collisions.datatypes.SimpleCollisionBox)3 PacketEntity (ac.grim.grimac.utils.data.packetentity.PacketEntity)3 OnlinePlayer (co.aikar.commands.bukkit.contexts.OnlinePlayer)3 WrappedPacket (io.github.retrooper.packetevents.packetwrappers.WrappedPacket)3 WrappedPacketOutEntityMetadata (io.github.retrooper.packetevents.packetwrappers.play.out.entitymetadata.WrappedPacketOutEntityMetadata)3 ArrayList (java.util.ArrayList)3 ItemStack (org.bukkit.inventory.ItemStack)3 PistonData (ac.grim.grimac.utils.data.PistonData)2 WrappedPacketInBlockDig (io.github.retrooper.packetevents.packetwrappers.play.in.blockdig.WrappedPacketInBlockDig)2 WrappedPacketInBlockPlace (io.github.retrooper.packetevents.packetwrappers.play.in.blockplace.WrappedPacketInBlockPlace)2 WrappedPacketInPong (io.github.retrooper.packetevents.packetwrappers.play.in.pong.WrappedPacketInPong)2 WrappedPacketInTransaction (io.github.retrooper.packetevents.packetwrappers.play.in.transaction.WrappedPacketInTransaction)2 WrappedPacketOutEntity (io.github.retrooper.packetevents.packetwrappers.play.out.entity.WrappedPacketOutEntity)2 WrappedPacketOutEntityDestroy (io.github.retrooper.packetevents.packetwrappers.play.out.entitydestroy.WrappedPacketOutEntityDestroy)2