From d410ca6a949639f668937b2c6a8d513cd0b94707 Mon Sep 17 00:00:00 2001 From: soup587 Date: Fri, 28 Jun 2024 19:39:11 +0100 Subject: [PATCH 1/3] damage event!! --- .../java/org/figuramc/figura/avatar/Avatar.java | 4 ++++ .../figuramc/figura/lua/api/event/EventsAPI.java | 4 ++++ .../figuramc/figura/mixin/LivingEntityMixin.java | 16 ++++++++++++++++ .../main/resources/assets/figura/lang/en_us.json | 1 + 4 files changed, 25 insertions(+) diff --git a/common/src/main/java/org/figuramc/figura/avatar/Avatar.java b/common/src/main/java/org/figuramc/figura/avatar/Avatar.java index 6a2855545..30d520cb6 100644 --- a/common/src/main/java/org/figuramc/figura/avatar/Avatar.java +++ b/common/src/main/java/org/figuramc/figura/avatar/Avatar.java @@ -430,6 +430,10 @@ public void resourceReloadEvent() { if (loaded) run("RESOURCE_RELOAD", tick); } + public void damageEvent(String sourceType, EntityAPI sourceCause, EntityAPI sourceDirect, FiguraVec3 sourcePosition) { + if (loaded) run("DAMAGE", tick, sourceType, sourceCause, sourceDirect, sourcePosition); + } + // -- host only events -- // public String chatSendMessageEvent(String message) { // piped event diff --git a/common/src/main/java/org/figuramc/figura/lua/api/event/EventsAPI.java b/common/src/main/java/org/figuramc/figura/lua/api/event/EventsAPI.java index f5594cb3f..175d15af4 100644 --- a/common/src/main/java/org/figuramc/figura/lua/api/event/EventsAPI.java +++ b/common/src/main/java/org/figuramc/figura/lua/api/event/EventsAPI.java @@ -87,6 +87,9 @@ public class EventsAPI { @LuaWhitelist @LuaFieldDoc("events.resource_reload") public final LuaEvent RESOURCE_RELOAD = new LuaEvent(); + @LuaWhitelist + @LuaFieldDoc("events.damage") + public final LuaEvent DAMAGE = new LuaEvent(); private final Map events = new HashMap<>(); @@ -112,6 +115,7 @@ public EventsAPI() { events.put("ITEM_RENDER", ITEM_RENDER); events.put("ON_PLAY_SOUND", ON_PLAY_SOUND); events.put("RESOURCE_RELOAD", RESOURCE_RELOAD); + events.put("DAMAGE", DAMAGE); for (FiguraEvent entrypoint : ENTRYPOINTS) { String ID = entrypoint.getID().toUpperCase(Locale.US); diff --git a/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java b/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java index 3b0a14090..2f296b68d 100644 --- a/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java +++ b/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java @@ -1,5 +1,6 @@ package org.figuramc.figura.mixin; +import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; @@ -7,7 +8,9 @@ import net.minecraft.world.level.Level; import org.figuramc.figura.avatar.Avatar; import org.figuramc.figura.avatar.AvatarManager; +import org.figuramc.figura.lua.api.entity.EntityAPI; import org.figuramc.figura.lua.api.world.ItemStackAPI; +import org.figuramc.figura.math.vector.FiguraVec3; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -26,4 +29,17 @@ private void triggerItemUseEffects(ItemStack stack, int particleCount, CallbackI if (avatar != null && avatar.useItemEvent(ItemStackAPI.verify(stack), stack.getUseAnimation().name(), particleCount)) ci.cancel(); } + + @Inject(at = @At("TAIL"), method = "handleDamageEvent") + private void handleDamageEvent(DamageSource source, CallbackInfo ci) { + //Avatar avatar = AvatarManager.getAvatarForPlayer(FiguraMod.getLocalPlayerUUID()); + Avatar avatar = AvatarManager.getAvatar(this); + if (avatar == null) return; + avatar.damageEvent( + source.typeHolder().unwrapKey().get().location().toString(), + EntityAPI.wrap(source.getEntity()), + EntityAPI.wrap(source.getDirectEntity()), + source.getSourcePosition() != null ? FiguraVec3.fromVec3(source.getSourcePosition()) : null + ); + } } diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index d7bbefb60..3f963913f 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -1060,6 +1060,7 @@ "figura.docs.events.item_render": "Called on every one of your items that is being rendered\nIt takes six arguments: the item being rendered, the rendering mode, the position, rotation, and scale that would be applied to the item, and if it's being rendered in the left hand\nReturning a ModelPart parented to Item stops the rendering of this item and will render the returned part instead", "figura.docs.events.on_play_sound": "Called every time a new sound is played\nTakes the following as arguments: the sound's ID, its world position, volume, pitch, if the sound should loop, the sound's category, and the sound's file path\nReturn true to prevent this sound from playing", "figura.docs.events.resource_reload": "Called every time that the client resources are reloaded, allowing you to re-create or update resource texture references", + "figura.docs.events.damage": "Called every time you take damage\nTakes four arguments, the damage type as a string, the entity that dealt the damage, the attacking entity, and the damage position\nThe last three arguments may return nil if there is no direct damage source", "figura.docs.events.get_events": "Returns a table with all events types", "figura.docs.event": "A hook for a certain event in Minecraft\nYou may register functions to one, and those functions will be called when the event occurs", "figura.docs.event.register": "Register a function on this event\nFunctions are run in registration order\nAn optional string argument can be given, grouping functions under that name, for an easier management later on", From fd20341e32978b7318d409ce6d4f4768fc0469d6 Mon Sep 17 00:00:00 2001 From: soup587 Date: Fri, 28 Jun 2024 21:13:27 +0100 Subject: [PATCH 2/3] Update en_us (Replace comma with colon, thanks Riftlight) --- common/src/main/resources/assets/figura/lang/en_us.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/resources/assets/figura/lang/en_us.json b/common/src/main/resources/assets/figura/lang/en_us.json index 3f963913f..9569dc36a 100644 --- a/common/src/main/resources/assets/figura/lang/en_us.json +++ b/common/src/main/resources/assets/figura/lang/en_us.json @@ -1060,7 +1060,7 @@ "figura.docs.events.item_render": "Called on every one of your items that is being rendered\nIt takes six arguments: the item being rendered, the rendering mode, the position, rotation, and scale that would be applied to the item, and if it's being rendered in the left hand\nReturning a ModelPart parented to Item stops the rendering of this item and will render the returned part instead", "figura.docs.events.on_play_sound": "Called every time a new sound is played\nTakes the following as arguments: the sound's ID, its world position, volume, pitch, if the sound should loop, the sound's category, and the sound's file path\nReturn true to prevent this sound from playing", "figura.docs.events.resource_reload": "Called every time that the client resources are reloaded, allowing you to re-create or update resource texture references", - "figura.docs.events.damage": "Called every time you take damage\nTakes four arguments, the damage type as a string, the entity that dealt the damage, the attacking entity, and the damage position\nThe last three arguments may return nil if there is no direct damage source", + "figura.docs.events.damage": "Called every time you take damage\nTakes four arguments: the damage type as a string, the entity that dealt the damage, the attacking entity, and the damage position\\nThe last three arguments may return nil if there is no direct damage source", "figura.docs.events.get_events": "Returns a table with all events types", "figura.docs.event": "A hook for a certain event in Minecraft\nYou may register functions to one, and those functions will be called when the event occurs", "figura.docs.event.register": "Register a function on this event\nFunctions are run in registration order\nAn optional string argument can be given, grouping functions under that name, for an easier management later on", From cedabfddbebe224c8f1f08d1da0efa7dd78f336c Mon Sep 17 00:00:00 2001 From: soup587 Date: Fri, 28 Jun 2024 22:12:51 +0100 Subject: [PATCH 3/3] Remove commented out code --- .../main/java/org/figuramc/figura/mixin/LivingEntityMixin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java b/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java index 2f296b68d..98f58ef09 100644 --- a/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java +++ b/common/src/main/java/org/figuramc/figura/mixin/LivingEntityMixin.java @@ -32,7 +32,6 @@ private void triggerItemUseEffects(ItemStack stack, int particleCount, CallbackI @Inject(at = @At("TAIL"), method = "handleDamageEvent") private void handleDamageEvent(DamageSource source, CallbackInfo ci) { - //Avatar avatar = AvatarManager.getAvatarForPlayer(FiguraMod.getLocalPlayerUUID()); Avatar avatar = AvatarManager.getAvatar(this); if (avatar == null) return; avatar.damageEvent(