From 0573cb390de7fc51f55f4c28a7373c5cf87cb8f3 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Mon, 1 Jan 2024 19:16:29 -0800 Subject: [PATCH] improved on damage script --- .../events/client/EntityDamageEvent.java | 31 +++++++++++++++++++ .../events/events/client/TakeDamageEvent.java | 21 ------------- .../mixins/MixinClientConnection.java | 5 +++ .../mixins/MixinPlayerEntity.java | 8 ----- .../modules/modules/ScriptedModule.java | 6 ++-- .../util/minecraft/VectorParser.java | 23 ++++++++------ 6 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 src/main/java/io/github/itzispyder/clickcrystals/events/events/client/EntityDamageEvent.java delete mode 100644 src/main/java/io/github/itzispyder/clickcrystals/events/events/client/TakeDamageEvent.java diff --git a/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/EntityDamageEvent.java b/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/EntityDamageEvent.java new file mode 100644 index 00000000..4dad94f5 --- /dev/null +++ b/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/EntityDamageEvent.java @@ -0,0 +1,31 @@ +package io.github.itzispyder.clickcrystals.events.events.client; + +import io.github.itzispyder.clickcrystals.events.Event; +import io.github.itzispyder.clickcrystals.util.minecraft.PlayerUtils; +import net.minecraft.entity.Entity; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.network.packet.s2c.play.EntityDamageS2CPacket; + +public class EntityDamageEvent extends Event { + + private final DamageSource source; + private final Entity entity; + + public EntityDamageEvent(EntityDamageS2CPacket packet) { + var world = PlayerUtils.getWorld(); + this.source = packet.createDamageSource(world); + this.entity = world.getEntityById(packet.entityId()); + } + + public boolean isSelf() { + return PlayerUtils.playerNotNull() && entity.getId() == PlayerUtils.player().getId(); + } + + public DamageSource getSource() { + return source; + } + + public float getCurrentHealth() { + return PlayerUtils.playerNull() ? 0.0F : PlayerUtils.player().getHealth(); + } +} diff --git a/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/TakeDamageEvent.java b/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/TakeDamageEvent.java deleted file mode 100644 index f0a203b1..00000000 --- a/src/main/java/io/github/itzispyder/clickcrystals/events/events/client/TakeDamageEvent.java +++ /dev/null @@ -1,21 +0,0 @@ -package io.github.itzispyder.clickcrystals.events.events.client; - -import io.github.itzispyder.clickcrystals.events.Event; -import io.github.itzispyder.clickcrystals.util.minecraft.PlayerUtils; - -public class TakeDamageEvent extends Event { - - private final float amount; - - public TakeDamageEvent(float amount) { - this.amount = amount; - } - - public float getCurrentHealth() { - return PlayerUtils.playerNull() ? 0.0F : PlayerUtils.player().getHealth(); - } - - public float getAmount() { - return amount; - } -} diff --git a/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinClientConnection.java b/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinClientConnection.java index 86a353e6..4b21705c 100644 --- a/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinClientConnection.java +++ b/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinClientConnection.java @@ -1,6 +1,7 @@ package io.github.itzispyder.clickcrystals.mixins; import io.github.itzispyder.clickcrystals.Global; +import io.github.itzispyder.clickcrystals.events.events.client.EntityDamageEvent; import io.github.itzispyder.clickcrystals.events.events.networking.GameJoinEvent; import io.github.itzispyder.clickcrystals.events.events.networking.PacketReceiveEvent; import io.github.itzispyder.clickcrystals.events.events.networking.PacketSendEvent; @@ -9,6 +10,7 @@ import net.minecraft.network.listener.PacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.login.LoginSuccessS2CPacket; +import net.minecraft.network.packet.s2c.play.EntityDamageS2CPacket; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -35,6 +37,9 @@ private static void onPacketRead(Packet packet, PacketListener listener, Call if (packet instanceof LoginSuccessS2CPacket) { system.eventBus.pass(new GameJoinEvent()); } + if (packet instanceof EntityDamageS2CPacket p) { + system.eventBus.pass(new EntityDamageEvent(p)); + } system.eventBus.passWithCallbackInfo(ci, new PacketReceiveEvent(packet)); } } diff --git a/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinPlayerEntity.java b/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinPlayerEntity.java index 33e8a842..9919904a 100644 --- a/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinPlayerEntity.java +++ b/src/main/java/io/github/itzispyder/clickcrystals/mixins/MixinPlayerEntity.java @@ -2,15 +2,12 @@ import io.github.itzispyder.clickcrystals.Global; import io.github.itzispyder.clickcrystals.events.events.client.PlayerAttackEntityEvent; -import io.github.itzispyder.clickcrystals.events.events.client.TakeDamageEvent; import net.minecraft.entity.Entity; -import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; 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.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(PlayerEntity.class) public abstract class MixinPlayerEntity implements Global { @@ -20,9 +17,4 @@ public void attack(Entity target, CallbackInfo ci) { PlayerAttackEntityEvent event = new PlayerAttackEntityEvent(mc.player, target, mc.crosshairTarget); system.eventBus.passWithCallbackInfo(ci, event); } - - @Inject(method = "damage", at = @At("HEAD")) - public void attack(DamageSource source, float amount, CallbackInfoReturnable cir) { - system.eventBus.pass(new TakeDamageEvent(amount)); - } } diff --git a/src/main/java/io/github/itzispyder/clickcrystals/modules/modules/ScriptedModule.java b/src/main/java/io/github/itzispyder/clickcrystals/modules/modules/ScriptedModule.java index 03e1d454..a8dc85ea 100644 --- a/src/main/java/io/github/itzispyder/clickcrystals/modules/modules/ScriptedModule.java +++ b/src/main/java/io/github/itzispyder/clickcrystals/modules/modules/ScriptedModule.java @@ -196,8 +196,10 @@ public void onKeyPress(KeyPressEvent e) { } @EventHandler - public void onDamage(TakeDamageEvent e) { - damageListeners.forEach(Runnable::run); + public void onDamage(EntityDamageEvent e) { + if (e.isSelf()) { + damageListeners.forEach(Runnable::run); + } } @EventHandler diff --git a/src/main/java/io/github/itzispyder/clickcrystals/util/minecraft/VectorParser.java b/src/main/java/io/github/itzispyder/clickcrystals/util/minecraft/VectorParser.java index 7461484a..f62a3527 100644 --- a/src/main/java/io/github/itzispyder/clickcrystals/util/minecraft/VectorParser.java +++ b/src/main/java/io/github/itzispyder/clickcrystals/util/minecraft/VectorParser.java @@ -8,6 +8,7 @@ import net.minecraft.world.World; import java.util.function.Function; +import java.util.function.Predicate; public class VectorParser { @@ -15,6 +16,7 @@ public class VectorParser { private static final String REGEX_RELATIVE = "[^0-9 ^~.-]"; private static final Function toNumber = s -> s.replaceAll(REGEX_DECIMAL, "").trim(); private static final Function toRelation = s -> s.replaceAll(REGEX_RELATIVE, "").trim(); + private static final Predicate isRelative = s -> s.startsWith("~") || s.startsWith("^"); private final double x, y, z; public VectorParser(String arg1, String arg2, String arg3) { @@ -84,15 +86,18 @@ public VectorParser(String arg1, String arg2, String arg3, Vec3d relativePos, Ve double argX = 0.0; double argY = 0.0; double argZ = 0.0; + String argNum1 = toNumber.apply(arg1); + String argNum2 = toNumber.apply(arg2); + String argNum3 = toNumber.apply(arg3); - if (!toNumber.apply(arg1).isEmpty()) { - argX = Double.parseDouble(toNumber.apply(arg1)); + if (!argNum1.isEmpty()) { + argX = Double.parseDouble(argNum1); } - if (!toNumber.apply(arg2).isEmpty()) { - argY = Double.parseDouble(toNumber.apply(arg2)); + if (!argNum2.isEmpty()) { + argY = Double.parseDouble(argNum2); } - if (!toNumber.apply(arg3).isEmpty()) { - argZ = Double.parseDouble(toNumber.apply(arg3)); + if (!argNum3.isEmpty()) { + argZ = Double.parseDouble(argNum3); } Vec3d result = relativePos; @@ -122,9 +127,9 @@ public VectorParser(String arg1, String arg2, String arg3, Vec3d relativePos, Ve result = distInFront(result, pitch, yaw, argZ); } - this.x = result.x; - this.y = result.y; - this.z = result.z; + this.x = isRelative.test(arg1) ? result.x : argX; + this.y = isRelative.test(arg2) ? result.y : argY; + this.z = isRelative.test(arg3) ? result.z : argZ; } public static Vec3d distInFront(Vec3d start, Vec3d dir, double dist) {