From e1195256c669fa94566cd8229c9612c4580be60b Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 6 Dec 2022 22:52:24 +0000 Subject: [PATCH] Switch to using entity-reach-attributes for changing the player's reach distance. Closes #82 --- build.gradle | 9 ++++++ gradle.properties | 1 + .../unicopia/entity/UEntityAttributes.java | 4 ++- .../unicopia/entity/player/Pony.java | 5 --- .../unicopia/item/PolearmItem.java | 1 + .../unicopia/mixin/MixinReachDistanceFix.java | 28 ----------------- .../MixinServerPlayerInteractionManager.java | 31 ------------------- .../MixinClientPlayerInteractionManager.java | 30 ------------------ .../mixin/client/MixinGameRenderer.java | 11 ------- src/main/resources/unicopia.mixin.json | 3 -- 10 files changed, 14 insertions(+), 109 deletions(-) delete mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/MixinReachDistanceFix.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/MixinServerPlayerInteractionManager.java delete mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/client/MixinClientPlayerInteractionManager.java diff --git a/build.gradle b/build.gradle index 754fdb46..d0075b6d 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,12 @@ repositories { name = 'modmenu' url = 'https://maven.terraformersmc.com/releases' } + maven { + url "https://maven.jamieswhiteshirt.com/libs-release" + content { + includeGroup "com.jamieswhiteshirt" + } + } maven { name = 'minelp' url = 'https://repo.minelittlepony-mod.com/maven/snapshot' @@ -60,6 +66,9 @@ dependencies { modApi "com.minelittlepony:kirin:${project.kirin_version}" include "com.minelittlepony:kirin:${project.kirin_version}" + modApi "com.jamieswhiteshirt:reach-entity-attributes:${project.reach_attributes_version}" + include "com.jamieswhiteshirt:reach-entity-attributes:${project.reach_attributes_version}" + modCompileOnly("com.terraformersmc:modmenu:${project.modmenu_version}") // implementation 'org.jetbrains:intellij-fernflower:1.2.1.16' modCompileOnly("net.caffienemc.sodium:sodium-fabric-mc1.19.2:0.4.4+build.18") diff --git a/gradle.properties b/gradle.properties index e2e2e51b..a19b8597 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,3 +18,4 @@ org.gradle.daemon=false modmenu_version=4.0.6 minelp_version=4.6.2-beta.1 kirin_version=1.12.0-beta.4 + reach_attributes_version=2.3.0 \ No newline at end of file diff --git a/src/main/java/com/minelittlepony/unicopia/entity/UEntityAttributes.java b/src/main/java/com/minelittlepony/unicopia/entity/UEntityAttributes.java index 6fb66d94..dad58c6c 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/UEntityAttributes.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/UEntityAttributes.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity; import java.util.ArrayList; import java.util.List; +import com.jamieswhiteshirt.reachentityattributes.ReachEntityAttributes; import com.minelittlepony.unicopia.Unicopia; import net.minecraft.entity.attribute.ClampedEntityAttribute; @@ -12,7 +13,8 @@ import net.minecraft.util.registry.Registry; public interface UEntityAttributes { List REGISTRY = new ArrayList<>(); - EntityAttribute EXTENDED_REACH_DISTANCE = register("pegasus.reach", new ClampedEntityAttribute("player.reachDistance", 0, 0, 10).setTracked(true)); + EntityAttribute EXTENDED_REACH_DISTANCE = ReachEntityAttributes.REACH; + EntityAttribute EXTENDED_ATTACK_DISTANCE = ReachEntityAttributes.ATTACK_RANGE; EntityAttribute EXTRA_MINING_SPEED = register("earth.mining_speed", new ClampedEntityAttribute("player.miningSpeed", 1, 0, 5).setTracked(true)); EntityAttribute ENTITY_GRAVTY_MODIFIER = register("player.gravity", (new EntityAttribute("player.gravityModifier", 1) {}).setTracked(true)); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index a96dbf54..df11d9fd 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -123,7 +123,6 @@ public class Pony extends Living implements Transmittable, Copieab } public static void registerAttributes(DefaultAttributeContainer.Builder builder) { - builder.add(UEntityAttributes.EXTENDED_REACH_DISTANCE); builder.add(UEntityAttributes.EXTRA_MINING_SPEED); builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER); } @@ -266,10 +265,6 @@ public class Pony extends Living implements Transmittable, Copieab return gravity; } - public float getExtendedReach() { - return (float)entity.getAttributeInstance(UEntityAttributes.EXTENDED_REACH_DISTANCE).getValue(); - } - public float getBlockBreakingSpeed() { return (float)entity.getAttributeInstance(UEntityAttributes.EXTRA_MINING_SPEED).getValue(); } diff --git a/src/main/java/com/minelittlepony/unicopia/item/PolearmItem.java b/src/main/java/com/minelittlepony/unicopia/item/PolearmItem.java index e0a4b0c0..b6d81dd9 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/PolearmItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/PolearmItem.java @@ -27,6 +27,7 @@ public class PolearmItem extends SwordItem { ImmutableMultimap.Builder builder = ImmutableMultimap.builder(); builder.putAll(super.getAttributeModifiers(EquipmentSlot.MAINHAND)); builder.put(UEntityAttributes.EXTENDED_REACH_DISTANCE, new EntityAttributeModifier(ATTACK_RANGE_MODIFIER_ID, "Weapon modifier", attackRange, EntityAttributeModifier.Operation.ADDITION)); + builder.put(UEntityAttributes.EXTENDED_ATTACK_DISTANCE, new EntityAttributeModifier(ATTACK_RANGE_MODIFIER_ID, "Weapon modifier", attackRange, EntityAttributeModifier.Operation.ADDITION)); attributeModifiers = builder.build(); } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinReachDistanceFix.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinReachDistanceFix.java deleted file mode 100644 index d2014620..00000000 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinReachDistanceFix.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.minelittlepony.unicopia.mixin; - -import org.objectweb.asm.Opcodes; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.*; -import com.minelittlepony.unicopia.entity.player.Pony; - -import net.minecraft.server.network.*; - -@Mixin(ServerPlayNetworkHandler.class) -abstract class MixinReachDistanceFix { - @Redirect( - method = { - "onPlayerInteractBlock", - "onPlayerInteractEntity" - }, - at = @At( - value = "FIELD", - target = "net/minecraft/server/network/ServerPlayNetworkHandler.MAX_BREAK_SQUARED_DISTANCE:D", - opcode = Opcodes.GETSTATIC - ), - require = 0 - ) - private double bgetMaxBreakSquaredDistance() { - double reach = 6 + Pony.of(((ServerPlayNetworkHandler)(Object)this).getPlayer()).getExtendedReach(); - return reach * reach; - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/MixinServerPlayerInteractionManager.java b/src/main/java/com/minelittlepony/unicopia/mixin/MixinServerPlayerInteractionManager.java deleted file mode 100644 index 47b98398..00000000 --- a/src/main/java/com/minelittlepony/unicopia/mixin/MixinServerPlayerInteractionManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.minelittlepony.unicopia.mixin; - -import org.objectweb.asm.Opcodes; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -import com.minelittlepony.unicopia.entity.player.Pony; - -import net.minecraft.server.network.*; - -@Mixin(ServerPlayerInteractionManager.class) -abstract class MixinServerPlayerInteractionManager { - @Accessor - public abstract ServerPlayerEntity getPlayer(); - - @Redirect( - method = "processBlockBreakingAction", - at = @At( - value = "FIELD", - target = "net/minecraft/server/network/ServerPlayNetworkHandler.MAX_BREAK_SQUARED_DISTANCE:D", - opcode = Opcodes.GETSTATIC - ), - require = 0 - ) - private double bgetMaxBreakSquaredDistance() { - double reach = 6 + Pony.of(getPlayer()).getExtendedReach(); - return reach * reach; - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinClientPlayerInteractionManager.java b/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinClientPlayerInteractionManager.java deleted file mode 100644 index 3ab60ef1..00000000 --- a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinClientPlayerInteractionManager.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.minelittlepony.unicopia.mixin.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.minelittlepony.unicopia.entity.player.Pony; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayerInteractionManager; - -@Mixin(ClientPlayerInteractionManager.class) -abstract class MixinClientPlayerInteractionManager { - @Inject(method = "getReachDistance()F", at = @At("RETURN"), cancellable = true) - private void onGetReachDistance(CallbackInfoReturnable info) { - Pony player = Pony.of(MinecraftClient.getInstance().player); - - if (player != null) { - info.setReturnValue(player.getExtendedReach() + info.getReturnValueF()); - } - } - - @Inject(method = "hasExtendedReach", at = @At("HEAD"), cancellable = true) - private void onHasExtendedReach(CallbackInfoReturnable info) { - if (!info.getReturnValueZ() && Pony.of(MinecraftClient.getInstance().player).getExtendedReach() > 0) { - info.setReturnValue(true); - } - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinGameRenderer.java b/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinGameRenderer.java index 3b873701..16709a60 100644 --- a/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinGameRenderer.java +++ b/src/main/java/com/minelittlepony/unicopia/mixin/client/MixinGameRenderer.java @@ -24,17 +24,6 @@ abstract class MixinGameRenderer implements AutoCloseable, SynchronousResourceRe @Shadow private @Final MinecraftClient client; - @ModifyConstant( - method = "updateTargetedEntity", - constant = @Constant(doubleValue = 6), - require = 0 - /* This injection is only here to fix reach distance in creative. If it fails, another mod is probably doing the same thing as us. */ - // TODO: Find a better way of doing this - ) - private double onUpdateTargetedEntity(double initial) { - return Math.max(initial, client.interactionManager.getReachDistance()); - } - @Inject(method = "getFov(Lnet/minecraft/client/render/Camera;FZ)D", at = @At("RETURN"), cancellable = true) diff --git a/src/main/resources/unicopia.mixin.json b/src/main/resources/unicopia.mixin.json index f9eb0627..f8cfe9bd 100644 --- a/src/main/resources/unicopia.mixin.json +++ b/src/main/resources/unicopia.mixin.json @@ -25,9 +25,7 @@ "MixinPlayerEntity", "MixinPowderSnowBlock", "MixinProjectileEntity", - "MixinReachDistanceFix", "MixinServerPlayerEntity", - "MixinServerPlayerInteractionManager", "MixinServerPlayNetworkHandler", "MixinServerWorld", "MixinSheepEntity", @@ -47,7 +45,6 @@ "client.MixinAnimalModel", "client.MixinArmorFeatureRenderer", "client.MixinCamera", - "client.MixinClientPlayerInteractionManager", "client.MixinEntityRenderDispatcher", "client.MixinGameRenderer", "client.MixinInGameHud",