From 22d6d854b1f7114fb08e5b2dfc8323011c9ea55f Mon Sep 17 00:00:00 2001 From: Theobald Date: Mon, 11 Mar 2024 02:39:21 -0500 Subject: [PATCH 1/2] Fix fake players using wrong reach values The bots still have a 4.5 block range while interacting with blocks, but entity interaction range has been reduced to 3 blocks, fixing #1882 Player attacking is more realistic now. Using /player attack once will swing even while not targeting an entity, and will properly reset attack cooldown #1881 is also fixed, as players will properly lower their movement speed while using slow items --- .../helpers/EntityPlayerActionPack.java | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/carpet/helpers/EntityPlayerActionPack.java b/src/main/java/carpet/helpers/EntityPlayerActionPack.java index e0601ea28c..499b60926b 100644 --- a/src/main/java/carpet/helpers/EntityPlayerActionPack.java +++ b/src/main/java/carpet/helpers/EntityPlayerActionPack.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; +import carpet.logging.Logger; import carpet.patches.EntityPlayerMPFake; import carpet.script.utils.Tracer; import net.minecraft.commands.arguments.EntityAnchorArgument; @@ -24,7 +25,9 @@ import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.vehicle.Boat; import net.minecraft.world.entity.vehicle.Minecart; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.EntityHitResult; @@ -45,6 +48,7 @@ public class EntityPlayerActionPack private boolean sneaking; private boolean sprinting; + private boolean slowed; private float forward; private float strafing; @@ -67,6 +71,7 @@ public void copyFrom(EntityPlayerActionPack other) sprinting = other.sprinting; forward = other.forward; strafing = other.strafing; + slowed = other.slowed; itemUseCooldown = other.itemUseCooldown; } @@ -110,6 +115,13 @@ public EntityPlayerActionPack setStrafing(float value) strafing = value; return this; } + + public EntityPlayerActionPack setSlowed(boolean value) + { + slowed = value; + return this; + } + public EntityPlayerActionPack look(Direction direction) { return switch (direction) @@ -238,7 +250,16 @@ public void onUpdate() } } } - float vel = sneaking?0.3F:1.0F; + + if (player.isUsingItem()) + { + ItemStack item = player.getUseItem(); + ((ServerPlayerInterface) player).getActionPack().setSlowed(item.isEdible() || item.is(Items.SHIELD) || item.is(Items.BRUSH) || item.is(Items.BOW) || item.is(Items.CROSSBOW) || item.is(Items.TRIDENT) || item.is(Items.GOAT_HORN) || item.is(Items.SPYGLASS)); + } + else {setSlowed(false);} + + float vel = sneaking?0.30F:1.0F; + vel *= slowed?0.20F:1.0F; // The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately if (forward != 0.0F || player instanceof EntityPlayerMPFake) { player.zza = forward * vel; @@ -250,8 +271,13 @@ public void onUpdate() static HitResult getTarget(ServerPlayer player) { - double reach = player.gameMode.isCreative() ? 5 : 4.5f; - return Tracer.rayTrace(player, 1, reach, false); + double blockReach = player.gameMode.isCreative() ? 5 : 4.5f; + double entityReach = player.gameMode.isCreative() ? 5 : 3f; + + HitResult hit = Tracer.rayTrace(player, 1, blockReach, false); + + if (hit.getType() == HitResult.Type.BLOCK) return hit; + return Tracer.rayTrace(player, 1, entityReach, false); } private void dropItemFromSlot(int slot, boolean dropAll) @@ -378,8 +404,6 @@ boolean execute(ServerPlayer player, Action action) { player.attack(entityHit.getEntity()); player.swing(InteractionHand.MAIN_HAND); } - player.resetAttackStrengthTicker(); - player.resetLastActionTime(); return true; } case BLOCK: { @@ -443,11 +467,13 @@ else if (ap.currentBlock == null || !ap.currentBlock.equals(pos)) player.level().destroyBlockProgress(-1, pos, (int) (ap.curBlockDamageMP * 10)); } - player.resetLastActionTime(); player.swing(InteractionHand.MAIN_HAND); return blockBroken; } } + if (!action.isContinuous) player.swing(InteractionHand.MAIN_HAND); + player.resetAttackStrengthTicker(); + player.resetLastActionTime(); return false; } From 6be7527f006e6183a7d6a5e9350367ea8a6448bc Mon Sep 17 00:00:00 2001 From: Theobald Date: Mon, 20 May 2024 18:40:00 -0500 Subject: [PATCH 2/2] Cleaned up item movement speed logic --- .../carpet/helpers/EntityPlayerActionPack.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/main/java/carpet/helpers/EntityPlayerActionPack.java b/src/main/java/carpet/helpers/EntityPlayerActionPack.java index 499b60926b..bc9299da18 100644 --- a/src/main/java/carpet/helpers/EntityPlayerActionPack.java +++ b/src/main/java/carpet/helpers/EntityPlayerActionPack.java @@ -48,7 +48,6 @@ public class EntityPlayerActionPack private boolean sneaking; private boolean sprinting; - private boolean slowed; private float forward; private float strafing; @@ -71,7 +70,6 @@ public void copyFrom(EntityPlayerActionPack other) sprinting = other.sprinting; forward = other.forward; strafing = other.strafing; - slowed = other.slowed; itemUseCooldown = other.itemUseCooldown; } @@ -116,12 +114,6 @@ public EntityPlayerActionPack setStrafing(float value) return this; } - public EntityPlayerActionPack setSlowed(boolean value) - { - slowed = value; - return this; - } - public EntityPlayerActionPack look(Direction direction) { return switch (direction) @@ -251,15 +243,11 @@ public void onUpdate() } } - if (player.isUsingItem()) - { - ItemStack item = player.getUseItem(); - ((ServerPlayerInterface) player).getActionPack().setSlowed(item.isEdible() || item.is(Items.SHIELD) || item.is(Items.BRUSH) || item.is(Items.BOW) || item.is(Items.CROSSBOW) || item.is(Items.TRIDENT) || item.is(Items.GOAT_HORN) || item.is(Items.SPYGLASS)); - } - else {setSlowed(false);} + boolean slowedByItem = player.isUsingItem(); float vel = sneaking?0.30F:1.0F; - vel *= slowed?0.20F:1.0F; + vel *= slowedByItem?0.20F:1.0F; + // The != 0.0F checks are needed given else real players can't control minecarts, however it works with fakes and else they don't stop immediately if (forward != 0.0F || player instanceof EntityPlayerMPFake) { player.zza = forward * vel;