Skip to content

Commit

Permalink
improved on damage script
Browse files Browse the repository at this point in the history
  • Loading branch information
ItziSpyder committed Jan 2, 2024
1 parent 466dc43 commit 0573cb3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Boolean> cir) {
system.eventBus.pass(new TakeDamageEvent(amount));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import net.minecraft.world.World;

import java.util.function.Function;
import java.util.function.Predicate;

public class VectorParser {

private static final String REGEX_DECIMAL = "[^0-9 .-]";
private static final String REGEX_RELATIVE = "[^0-9 ^~.-]";
private static final Function<String, String> toNumber = s -> s.replaceAll(REGEX_DECIMAL, "").trim();
private static final Function<String, String> toRelation = s -> s.replaceAll(REGEX_RELATIVE, "").trim();
private static final Predicate<String> isRelative = s -> s.startsWith("~") || s.startsWith("^");
private final double x, y, z;

public VectorParser(String arg1, String arg2, String arg3) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0573cb3

Please sign in to comment.