Skip to content

Commit

Permalink
Reimplement kb resistance in modern (#1448)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Nov 29, 2024
1 parent 79dfcfc commit 2dfe88b
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2dfe88b

Please sign in to comment.