mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-10 01:11:25 +01:00
fix: update sprinting movement speed modifier serverside to keep synced with client state
This commit is contained in:
parent
435534368b
commit
edb73f0a5a
@ -14,6 +14,9 @@ import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.entity.ItemEntity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.attribute.Attribute;
|
||||
import net.minestom.server.entity.attribute.AttributeModifier;
|
||||
import net.minestom.server.entity.attribute.AttributeOperation;
|
||||
import net.minestom.server.entity.damage.Damage;
|
||||
import net.minestom.server.event.Event;
|
||||
import net.minestom.server.event.EventNode;
|
||||
@ -46,6 +49,7 @@ import net.minestom.server.potion.CustomPotionEffect;
|
||||
import net.minestom.server.potion.PotionEffect;
|
||||
import net.minestom.server.sound.SoundEvent;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
|
||||
import java.time.Duration;
|
||||
@ -106,6 +110,18 @@ public class PlayerInit {
|
||||
int z = Math.abs(ThreadLocalRandom.current().nextInt()) % 500 - 250;
|
||||
player.setRespawnPoint(new Pos(0, 40f, 0));
|
||||
})
|
||||
.addListener(PlayerHandAnimationEvent.class, event -> {
|
||||
class A {
|
||||
static boolean b = false;
|
||||
}
|
||||
if (A.b) {
|
||||
event.getPlayer().getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).removeModifier(NamespaceID.from("test"));
|
||||
} else {
|
||||
event.getPlayer().getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).addModifier(new AttributeModifier(NamespaceID.from("test"), 0.5, AttributeOperation.ADD_VALUE));
|
||||
}
|
||||
A.b = !A.b;
|
||||
})
|
||||
|
||||
.addListener(PlayerSpawnEvent.class, event -> {
|
||||
final Player player = event.getPlayer();
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
|
@ -6,6 +6,8 @@ import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.coordinate.Vec;
|
||||
import net.minestom.server.entity.attribute.Attribute;
|
||||
import net.minestom.server.entity.attribute.AttributeInstance;
|
||||
import net.minestom.server.entity.attribute.AttributeModifier;
|
||||
import net.minestom.server.entity.attribute.AttributeOperation;
|
||||
import net.minestom.server.entity.damage.Damage;
|
||||
import net.minestom.server.entity.damage.DamageType;
|
||||
import net.minestom.server.entity.metadata.LivingEntityMeta;
|
||||
@ -26,6 +28,7 @@ import net.minestom.server.network.player.PlayerConnection;
|
||||
import net.minestom.server.registry.DynamicRegistry;
|
||||
import net.minestom.server.scoreboard.Team;
|
||||
import net.minestom.server.sound.SoundEvent;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.block.BlockIterator;
|
||||
import net.minestom.server.utils.time.Cooldown;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
@ -42,6 +45,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class LivingEntity extends Entity implements EquipmentHandler {
|
||||
|
||||
private static final AttributeModifier SPRINTING_SPEED_MODIFIER = new AttributeModifier(NamespaceID.from("minecraft:sprinting"), 0.3, AttributeOperation.MULTIPLY_TOTAL);
|
||||
|
||||
// ItemStack pickup
|
||||
protected boolean canPickupItem;
|
||||
protected Cooldown itemPickupCooldown = new Cooldown(Duration.of(5, TimeUnit.SERVER_TICK));
|
||||
@ -99,6 +104,18 @@ public class LivingEntity extends Entity implements EquipmentHandler {
|
||||
this.boots = ItemStack.AIR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSprinting(boolean sprinting) {
|
||||
super.setSprinting(sprinting);
|
||||
|
||||
// We must set the sprinting attribute serverside because when we resend modifiers it overwrites what
|
||||
// the client has, meaning if they are sprinting and we send no modifiers, they will no longer be
|
||||
// getting the speed boost of sprinting.
|
||||
final AttributeInstance speed = getAttribute(Attribute.GENERIC_MOVEMENT_SPEED);
|
||||
if (sprinting) speed.addModifier(SPRINTING_SPEED_MODIFIER);
|
||||
else speed.removeModifier(SPRINTING_SPEED_MODIFIER);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ItemStack getItemInMainHand() {
|
||||
|
Loading…
Reference in New Issue
Block a user