From 2dfe88b0e761415997492b9220de791cb29526cb Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Fri, 29 Nov 2024 02:19:19 +0100 Subject: [PATCH] Reimplement kb resistance in modern (#1448) Signed-off-by: Pablo Herrera --- .../platform/modern/impl/ModernPlayerUtils.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/impl/ModernPlayerUtils.java b/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/impl/ModernPlayerUtils.java index 705b6cdd75..1a76eb64f1 100644 --- a/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/impl/ModernPlayerUtils.java +++ b/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/impl/ModernPlayerUtils.java @@ -5,7 +5,10 @@ import com.mojang.authlib.GameProfile; import java.util.Optional; import java.util.UUID; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; +import net.minecraft.world.entity.ai.attributes.AttributeModifier; +import net.minecraft.world.entity.ai.attributes.Attributes; import org.bukkit.Location; import org.bukkit.attribute.Attribute; import org.bukkit.craftbukkit.entity.CraftPlayer; @@ -79,15 +82,23 @@ public void setCollidesWithEntities(Player player, boolean collides) { player.setCollidable(collides); } + private final ResourceLocation KB_REDUCT = + ResourceLocation.fromNamespaceAndPath("pgm", "custom_kb_reduction"); + @Override public void setKnockbackReduction(Player player, float reduction) { - // TODO: PLATFORM 1.20 does not support kb reduction + // Use NMS to have access to addOrUpdateTransientModifier + var nmsPlayer = ((CraftPlayer) player).getHandle(); + var attr = nmsPlayer.getAttributes().getInstance(Attributes.KNOCKBACK_RESISTANCE); + if (attr == null) return; + attr.addOrUpdateTransientModifier( + new AttributeModifier(KB_REDUCT, reduction, AttributeModifier.Operation.ADD_VALUE)); } @Override public float getKnockbackReduction(Player player) { - // TODO: PLATFORM 1.20 does not support kb reduction - return 0; + var attr = player.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE); + return attr != null ? (float) attr.getValue() : 0; } @Override