Skip to content

Commit bb50c18

Browse files
authored
Damage utils refactor (#4083)
1 parent 3d05485 commit bb50c18

27 files changed

+993
-374
lines changed

src/main/java/meteordevelopment/meteorclient/events/entity/DamageEvent.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ public class DamageEvent {
1313

1414
public LivingEntity entity;
1515
public DamageSource source;
16-
public float amount;
1716

18-
public static DamageEvent get(LivingEntity entity, DamageSource source, float amount) {
17+
public static DamageEvent get(LivingEntity entity, DamageSource source) {
1918
INSTANCE.entity = entity;
2019
INSTANCE.source = source;
21-
INSTANCE.amount = amount;
2220
return INSTANCE;
2321
}
2422
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixin;
7+
8+
import meteordevelopment.meteorclient.mixininterface.IAttributeContainer;
9+
import meteordevelopment.meteorclient.mixininterface.IEntityAttributeInstance;
10+
import net.minecraft.entity.attribute.AttributeContainer;
11+
import net.minecraft.entity.attribute.EntityAttribute;
12+
import net.minecraft.entity.attribute.EntityAttributeInstance;
13+
import org.jetbrains.annotations.Nullable;
14+
import org.spongepowered.asm.mixin.Final;
15+
import org.spongepowered.asm.mixin.Mixin;
16+
import org.spongepowered.asm.mixin.Shadow;
17+
18+
import java.util.Map;
19+
import java.util.Set;
20+
21+
@Mixin(AttributeContainer.class)
22+
public abstract class AttributeContainerMixin implements IAttributeContainer {
23+
@Shadow @Final private Map<EntityAttribute, EntityAttributeInstance> custom;
24+
@Shadow @Final private Set<EntityAttributeInstance> tracked;
25+
26+
@Override
27+
public void meteor$copyFrom(AttributeContainer other) {
28+
for (var otherInstance : ((AttributeContainerMixin) (Object) other).custom.values()) {
29+
@Nullable EntityAttributeInstance instance = custom.get(otherInstance.getAttribute());
30+
if (instance != null) {
31+
((IEntityAttributeInstance) instance).meteor$copyFrom(otherInstance);
32+
} else {
33+
custom.put(otherInstance.getAttribute(), otherInstance);
34+
if (otherInstance.getAttribute().isTracked()) tracked.add(otherInstance);
35+
}
36+
}
37+
}
38+
}

src/main/java/meteordevelopment/meteorclient/mixin/ClientPlayerEntityMixin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void onPushOutOfBlocks(double x, double d, CallbackInfo info) {
7474

7575
@Inject(method = "damage", at = @At("HEAD"))
7676
private void onDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
77-
if (Utils.canUpdate() && getWorld().isClient && canTakeDamage()) MeteorClient.EVENT_BUS.post(DamageEvent.get(this, source, amount));
77+
if (Utils.canUpdate() && getWorld().isClient && canTakeDamage()) MeteorClient.EVENT_BUS.post(DamageEvent.get(this, source));
7878
}
7979

8080
@ModifyConstant(method = "canSprint", constant = @Constant(floatValue = 6.0f))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixin;
7+
8+
import meteordevelopment.meteorclient.mixininterface.IEntityAttributeInstance;
9+
import net.minecraft.entity.attribute.EntityAttributeInstance;
10+
import net.minecraft.entity.attribute.EntityAttributeModifier;
11+
import org.jetbrains.annotations.Nullable;
12+
import org.spongepowered.asm.mixin.Final;
13+
import org.spongepowered.asm.mixin.Mixin;
14+
import org.spongepowered.asm.mixin.Shadow;
15+
16+
import java.util.Map;
17+
import java.util.Set;
18+
import java.util.UUID;
19+
20+
@Mixin(EntityAttributeInstance.class)
21+
public abstract class EntityAttributeInstanceMixin implements IEntityAttributeInstance {
22+
@Shadow @Final private Map<UUID, EntityAttributeModifier> idToModifiers;
23+
@Shadow @Final private Set<EntityAttributeModifier> persistentModifiers;
24+
@Shadow public abstract Set<EntityAttributeModifier> getModifiers(EntityAttributeModifier.Operation operation);
25+
@Shadow protected abstract void onUpdate();
26+
27+
@Override
28+
public void meteor$copyFrom(EntityAttributeInstance other) {
29+
for (var modifier : other.getModifiers()) {
30+
@Nullable EntityAttributeModifier old = idToModifiers.put(modifier.getId(), modifier);
31+
if (old != null) {
32+
getModifiers(old.getOperation()).remove(old);
33+
persistentModifiers.remove(old);
34+
}
35+
getModifiers(modifier.getOperation()).add(modifier);
36+
}
37+
onUpdate();
38+
}
39+
}

src/main/java/meteordevelopment/meteorclient/mixin/LivingEntityAccessor.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package meteordevelopment.meteorclient.mixin;
77

88
import net.minecraft.entity.LivingEntity;
9+
import net.minecraft.entity.data.TrackedData;
910
import net.minecraft.fluid.Fluid;
1011
import net.minecraft.registry.tag.TagKey;
1112
import org.spongepowered.asm.mixin.Mixin;
@@ -25,4 +26,14 @@ public interface LivingEntityAccessor {
2526

2627
@Accessor("jumpingCooldown")
2728
void setJumpCooldown(int cooldown);
29+
30+
@Accessor("POTION_SWIRLS_COLOR")
31+
static TrackedData<Integer> meteor$getPotionSwirlsColor() {
32+
throw new AssertionError();
33+
}
34+
35+
@Accessor("POTION_SWIRLS_AMBIENT")
36+
static TrackedData<Boolean> meteor$getPotionSwirlsAmbient() {
37+
throw new AssertionError();
38+
}
2839
}

src/main/java/meteordevelopment/meteorclient/mixin/LivingEntityMixin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public LivingEntityMixin(EntityType<?> type, World world) {
5454
@Inject(method = "damage", at = @At("HEAD"))
5555
private void onDamageHead(DamageSource source, float amount, CallbackInfoReturnable<Boolean> info) {
5656
if (Utils.canUpdate() && getWorld().isClient)
57-
MeteorClient.EVENT_BUS.post(DamageEvent.get((LivingEntity) (Object) this, source, amount));
57+
MeteorClient.EVENT_BUS.post(DamageEvent.get((LivingEntity) (Object) this, source));
5858
}
5959

6060
@ModifyReturnValue(method = "canWalkOnFluid", at = @At("RETURN"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixin;
7+
8+
import net.minecraft.entity.attribute.EntityAttributeModifier;
9+
import net.minecraft.entity.data.TrackedData;
10+
import net.minecraft.entity.mob.ShulkerEntity;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.gen.Accessor;
13+
14+
@Mixin(ShulkerEntity.class)
15+
public interface ShulkerEntityAccessor {
16+
@Accessor("PEEK_AMOUNT")
17+
static TrackedData<Byte> meteor$getPeekAmount() {
18+
throw new AssertionError();
19+
}
20+
21+
@Accessor("COVERED_ARMOR_BONUS")
22+
static EntityAttributeModifier meteor$getCoveredArmorBonus() {
23+
throw new AssertionError();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixininterface;
7+
8+
import net.minecraft.entity.attribute.AttributeContainer;
9+
import net.minecraft.entity.attribute.EntityAttributeInstance;
10+
11+
public interface IAttributeContainer {
12+
/**
13+
* Copy the {@link EntityAttributeInstance} of the other {@link AttributeContainer} into this one, copying their modifiers if there's a duplicate
14+
*/
15+
void meteor$copyFrom(AttributeContainer other);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.mixininterface;
7+
8+
import net.minecraft.entity.attribute.EntityAttributeInstance;
9+
10+
public interface IEntityAttributeInstance {
11+
/**
12+
* Adds the modifiers of the other {@link EntityAttributeInstance} to this one, replacing them if there's a duplicate
13+
*/
14+
void meteor$copyFrom(EntityAttributeInstance other);
15+
}

src/main/java/meteordevelopment/meteorclient/systems/modules/combat/AnchorAura.java

+49-42
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import meteordevelopment.meteorclient.settings.*;
1212
import meteordevelopment.meteorclient.systems.modules.Categories;
1313
import meteordevelopment.meteorclient.systems.modules.Module;
14+
import meteordevelopment.meteorclient.utils.entity.DamageUtils;
1415
import meteordevelopment.meteorclient.utils.entity.EntityUtils;
1516
import meteordevelopment.meteorclient.utils.entity.SortPriority;
1617
import meteordevelopment.meteorclient.utils.entity.TargetUtils;
@@ -20,13 +21,13 @@
2021
import meteordevelopment.orbit.EventHandler;
2122
import net.minecraft.block.Blocks;
2223
import net.minecraft.entity.player.PlayerEntity;
23-
import net.minecraft.fluid.FlowableFluid;
2424
import net.minecraft.item.Items;
2525
import net.minecraft.util.Hand;
2626
import net.minecraft.util.hit.BlockHitResult;
2727
import net.minecraft.util.math.BlockPos;
2828
import net.minecraft.util.math.Direction;
2929
import net.minecraft.util.math.Vec3d;
30+
import org.jetbrains.annotations.Nullable;
3031

3132
public class AnchorAura extends Module {
3233
private final SettingGroup sgGeneral = settings.getDefaultGroup();
@@ -225,6 +226,7 @@ public class AnchorAura extends Module {
225226
private int placeDelayLeft;
226227
private int breakDelayLeft;
227228
private PlayerEntity target;
229+
private final BlockPos.Mutable mutable = new BlockPos.Mutable();
228230

229231
public AnchorAura() {
230232
super(Categories.Combat, "anchor-aura", "Automatically places and breaks Respawn Anchors to harm entities.");
@@ -264,7 +266,8 @@ private void onTick(TickEvent.Post event) {
264266
breakDelayLeft = 0;
265267

266268
if (rotationMode.get() == RotationMode.Both || rotationMode.get() == RotationMode.Break) {
267-
Rotations.rotate(Rotations.getYaw(breakPos), Rotations.getPitch(breakPos), 50, () -> breakAnchor(breakPos, anchor, glowStone));
269+
BlockPos immutableBreakPos = breakPos.toImmutable();
270+
Rotations.rotate(Rotations.getYaw(breakPos), Rotations.getPitch(breakPos), 50, () -> breakAnchor(immutableBreakPos, anchor, glowStone));
268271
} else breakAnchor(breakPos, anchor, glowStone);
269272
}
270273
}
@@ -274,7 +277,7 @@ private void onTick(TickEvent.Post event) {
274277

275278
if (placePos != null) {
276279
placeDelayLeft = 0;
277-
BlockUtils.place(placePos, anchor, (rotationMode.get() == RotationMode.Place || rotationMode.get() == RotationMode.Both), 50);
280+
BlockUtils.place(placePos.toImmutable(), anchor, (rotationMode.get() == RotationMode.Place || rotationMode.get() == RotationMode.Both), 50);
278281
}
279282
}
280283

@@ -301,48 +304,50 @@ private void onRender(Render3DEvent event) {
301304
}
302305
}
303306

307+
@Nullable
304308
private BlockPos findPlacePos(BlockPos targetPlacePos) {
305309
switch (placePositions.get()) {
306-
case All:
307-
if (isValidPlace(targetPlacePos.down())) return targetPlacePos.down();
308-
else if (isValidPlace(targetPlacePos.up(2))) return targetPlacePos.up(2);
309-
else if (isValidPlace(targetPlacePos.add(1, 0, 0))) return targetPlacePos.add(1, 0, 0);
310-
else if (isValidPlace(targetPlacePos.add(-1, 0, 0))) return targetPlacePos.add(-1, 0, 0);
311-
else if (isValidPlace(targetPlacePos.add(0, 0, 1))) return targetPlacePos.add(0, 0, 1);
312-
else if (isValidPlace(targetPlacePos.add(0, 0, -1))) return targetPlacePos.add(0, 0, -1);
313-
else if (isValidPlace(targetPlacePos.add(1, 1, 0))) return targetPlacePos.add(1, 1, 0);
314-
else if (isValidPlace(targetPlacePos.add(-1, -1, 0))) return targetPlacePos.add(-1, -1, 0);
315-
else if (isValidPlace(targetPlacePos.add(0, 1, 1))) return targetPlacePos.add(0, 1, 1);
316-
else if (isValidPlace(targetPlacePos.add(0, 0, -1))) return targetPlacePos.add(0, 0, -1);
317-
break;
318-
case Above:
319-
if (isValidPlace(targetPlacePos.up(2))) return targetPlacePos.up(2);
320-
break;
321-
case AboveAndBelow:
322-
if (isValidPlace(targetPlacePos.down())) return targetPlacePos.down();
323-
else if (isValidPlace(targetPlacePos.up(2))) return targetPlacePos.up(2);
324-
break;
325-
case Around:
326-
if (isValidPlace(targetPlacePos.north())) return targetPlacePos.north();
327-
else if (isValidPlace(targetPlacePos.east())) return targetPlacePos.east();
328-
else if (isValidPlace(targetPlacePos.west())) return targetPlacePos.west();
329-
else if (isValidPlace(targetPlacePos.south())) return targetPlacePos.south();
330-
break;
310+
case All -> {
311+
if (isValidPlace(targetPlacePos, 0, -1, 0)) return mutable;
312+
else if (isValidPlace(targetPlacePos, 0, 2, 0)) return mutable;
313+
else if (isValidPlace(targetPlacePos, 1, 0, 0)) return mutable;
314+
else if (isValidPlace(targetPlacePos, -1, 0, 0)) return mutable;
315+
else if (isValidPlace(targetPlacePos, 0, 0, 1)) return mutable;
316+
else if (isValidPlace(targetPlacePos, 0, 0, -1)) return mutable;
317+
else if (isValidPlace(targetPlacePos, 1, 1, 0)) return mutable;
318+
else if (isValidPlace(targetPlacePos, -1, -1, 0)) return mutable;
319+
else if (isValidPlace(targetPlacePos, 0, 1, 1)) return mutable;
320+
else if (isValidPlace(targetPlacePos, 0, 0, -1)) return mutable;
321+
}
322+
case Above -> {
323+
if (isValidPlace(targetPlacePos, 0, 2, 0)) return mutable;
324+
}
325+
case AboveAndBelow -> {
326+
if (isValidPlace(targetPlacePos, 0, -1, 0)) return mutable;
327+
else if (isValidPlace(targetPlacePos, 0, 2, 0)) return mutable;
328+
}
329+
case Around -> {
330+
if (isValidPlace(targetPlacePos, 0, 0, -1)) return mutable;
331+
else if (isValidPlace(targetPlacePos, 1, 0, 0)) return mutable;
332+
else if (isValidPlace(targetPlacePos, -1, 0, 0)) return mutable;
333+
else if (isValidPlace(targetPlacePos, 0, 0, 1)) return mutable;
334+
}
331335
}
332336
return null;
333337
}
334338

339+
@Nullable
335340
private BlockPos findBreakPos(BlockPos targetPos) {
336-
if (isValidBreak(targetPos.down())) return targetPos.down();
337-
else if (isValidBreak(targetPos.up(2))) return targetPos.up(2);
338-
else if (isValidBreak(targetPos.add(1, 0, 0))) return targetPos.add(1, 0, 0);
339-
else if (isValidBreak(targetPos.add(-1, 0, 0))) return targetPos.add(-1, 0, 0);
340-
else if (isValidBreak(targetPos.add(0, 0, 1))) return targetPos.add(0, 0, 1);
341-
else if (isValidBreak(targetPos.add(0, 0, -1))) return targetPos.add(0, 0, -1);
342-
else if (isValidBreak(targetPos.add(1, 1, 0))) return targetPos.add(1, 1, 0);
343-
else if (isValidBreak(targetPos.add(-1, -1, 0))) return targetPos.add(-1, -1, 0);
344-
else if (isValidBreak(targetPos.add(0, 1, 1))) return targetPos.add(0, 1, 1);
345-
else if (isValidBreak(targetPos.add(0, 0, -1))) return targetPos.add(0, 0, -1);
341+
if (isValidBreak(targetPos, 0, -1, 0)) return mutable;
342+
else if (isValidBreak(targetPos, 0, 2, 0)) return mutable;
343+
else if (isValidBreak(targetPos, 1, 0, 0)) return mutable;
344+
else if (isValidBreak(targetPos, -1, 0, 0)) return mutable;
345+
else if (isValidBreak(targetPos, 0, 0, 1)) return mutable;
346+
else if (isValidBreak(targetPos, 0, 0, -1)) return mutable;
347+
else if (isValidBreak(targetPos, 1, 1, 0)) return mutable;
348+
else if (isValidBreak(targetPos, -1, -1, 0)) return mutable;
349+
else if (isValidBreak(targetPos, 0, 1, 1)) return mutable;
350+
else if (isValidBreak(targetPos, 0, 0, -1)) return mutable;
346351
return null;
347352
}
348353

@@ -354,12 +359,14 @@ private boolean getDamageBreak(BlockPos pos) {
354359
return breakMode.get() == Safety.Suicide || DamageUtils.anchorDamage(mc.player, pos.toCenterPos()) <= maxDamage.get();
355360
}
356361

357-
private boolean isValidPlace(BlockPos pos) {
358-
return Math.sqrt(mc.player.getBlockPos().getSquaredDistance(pos)) <= placeRange.get() && getDamagePlace(pos) && BlockUtils.canPlace(pos, true);
362+
private boolean isValidPlace(BlockPos origin, int xOffset, int yOffset, int zOffset) {
363+
BlockUtils.mutateAround(mutable, origin, xOffset, yOffset, zOffset);
364+
return Math.sqrt(mc.player.getBlockPos().getSquaredDistance(mutable)) <= placeRange.get() && getDamagePlace(mutable) && BlockUtils.canPlace(mutable);
359365
}
360366

361-
private boolean isValidBreak(BlockPos pos) {
362-
return mc.world.getBlockState(pos).getBlock() == Blocks.RESPAWN_ANCHOR && Math.sqrt(mc.player.getBlockPos().getSquaredDistance(pos)) <= breakRange.get() && getDamageBreak(pos);
367+
private boolean isValidBreak(BlockPos origin, int xOffset, int yOffset, int zOffset) {
368+
BlockUtils.mutateAround(mutable, origin, xOffset, yOffset, zOffset);
369+
return mc.world.getBlockState(mutable).getBlock() == Blocks.RESPAWN_ANCHOR && Math.sqrt(mc.player.getBlockPos().getSquaredDistance(mutable)) <= breakRange.get() && getDamageBreak(mutable);
363370
}
364371

365372
private void breakAnchor(BlockPos pos, FindItemResult anchor, FindItemResult glowStone) {

src/main/java/meteordevelopment/meteorclient/systems/modules/combat/BedAura.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import meteordevelopment.meteorclient.systems.modules.Categories;
1313
import meteordevelopment.meteorclient.systems.modules.Module;
1414
import meteordevelopment.meteorclient.utils.Utils;
15+
import meteordevelopment.meteorclient.utils.entity.DamageUtils;
1516
import meteordevelopment.meteorclient.utils.entity.EntityUtils;
1617
import meteordevelopment.meteorclient.utils.entity.SortPriority;
1718
import meteordevelopment.meteorclient.utils.entity.TargetUtils;
@@ -258,8 +259,8 @@ private BlockPos findPlace(PlayerEntity target) {
258259

259260
BlockPos centerPos = target.getBlockPos().up(i);
260261

261-
double headSelfDamage = DamageUtils.bedDamage(mc.player, Utils.vec3d(centerPos));
262-
double offsetSelfDamage = DamageUtils.bedDamage(mc.player, Utils.vec3d(centerPos.offset(dir.toDirection())));
262+
float headSelfDamage = DamageUtils.bedDamage(mc.player, Utils.vec3d(centerPos));
263+
float offsetSelfDamage = DamageUtils.bedDamage(mc.player, Utils.vec3d(centerPos.offset(dir.toDirection())));
263264

264265
if (mc.world.getBlockState(centerPos).isReplaceable()
265266
&& BlockUtils.canPlace(centerPos.offset(dir.toDirection()))

0 commit comments

Comments
 (0)