diff --git a/src/main/java/carpet/mixins/DamageTracker_scarpetEventMixin.java b/src/main/java/carpet/mixins/DamageTracker_scarpetEventMixin.java deleted file mode 100644 index 049a80c5..00000000 --- a/src/main/java/carpet/mixins/DamageTracker_scarpetEventMixin.java +++ /dev/null @@ -1,37 +0,0 @@ -package carpet.mixins; - -import carpet.fakes.EntityInterface; -import carpet.script.EntityEventsGroup; -import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.damage.DamageSource; -import net.minecraft.entity.damage.DamageTracker; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import static carpet.script.CarpetEventServer.Event.PLAYER_DEALS_DAMAGE; -import static carpet.script.CarpetEventServer.Event.PLAYER_TAKES_DAMAGE; - -@Mixin(DamageTracker.class) -public abstract class DamageTracker_scarpetEventMixin -{ - @Shadow @Final private LivingEntity entity; - - @Inject(method = "onDamage", at = @At("HEAD")) - private void onDamageTaken(DamageSource damageSource_1, float float_1, float float_2, CallbackInfo ci) - { - ((EntityInterface)entity).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DAMAGE, float_2, damageSource_1); - if (entity instanceof ServerPlayerEntity && PLAYER_TAKES_DAMAGE.isNeeded()) - { - PLAYER_TAKES_DAMAGE.onDamage(entity, float_2, damageSource_1); - } - if (damageSource_1.getAttacker() instanceof ServerPlayerEntity && PLAYER_DEALS_DAMAGE.isNeeded()) - { - PLAYER_DEALS_DAMAGE.onDamage(entity, float_2, damageSource_1); - } - } -} diff --git a/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java index 347beb75..c8e55f26 100644 --- a/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/LivingEntity_scarpetEventsMixin.java @@ -1,5 +1,6 @@ package carpet.mixins; +import carpet.CarpetSettings; import carpet.fakes.EntityInterface; import carpet.fakes.LivingEntityInterface; import carpet.script.EntityEventsGroup; @@ -7,12 +8,17 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.damage.DamageSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; 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.LocalCapture; + +import static carpet.script.CarpetEventServer.Event.PLAYER_DEALS_DAMAGE; +import static carpet.script.CarpetEventServer.Event.PLAYER_TAKES_DAMAGE; @Mixin(LivingEntity.class) public abstract class LivingEntity_scarpetEventsMixin extends Entity implements LivingEntityInterface @@ -33,6 +39,25 @@ public abstract class LivingEntity_scarpetEventsMixin extends Entity implements ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DEATH, damageSource_1.name); } + @Inject(method = "applyDamage", locals = LocalCapture.CAPTURE_FAILHARD, at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/LivingEntity;getHealth()F", + shift = At.Shift.BEFORE + )) + private void entityTakingDamage(DamageSource source, float amount, CallbackInfo ci) + { + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DAMAGE, amount, source); + // this is not applicable since its not a playr for sure + //if (entity instanceof ServerPlayerEntity && PLAYER_TAKES_DAMAGE.isNeeded()) + //{ + // PLAYER_TAKES_DAMAGE.onDamage(entity, float_2, damageSource_1); + //} + if (source.getAttacker() instanceof ServerPlayerEntity && PLAYER_DEALS_DAMAGE.isNeeded()) + { + PLAYER_DEALS_DAMAGE.onDamage(this, amount, source); + } + } + @Override public void doJumpCM() { diff --git a/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java b/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java new file mode 100644 index 00000000..10ed7c2d --- /dev/null +++ b/src/main/java/carpet/mixins/PlayerEntity_scarpetEventsMixin.java @@ -0,0 +1,46 @@ +package carpet.mixins; + +import carpet.CarpetSettings; +import carpet.fakes.EntityInterface; +import carpet.script.EntityEventsGroup; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.damage.DamageSource; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.world.World; +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.LocalCapture; + +import static carpet.script.CarpetEventServer.Event.PLAYER_DEALS_DAMAGE; +import static carpet.script.CarpetEventServer.Event.PLAYER_TAKES_DAMAGE; + +@Mixin(PlayerEntity.class) +public abstract class PlayerEntity_scarpetEventsMixin extends LivingEntity +{ + protected PlayerEntity_scarpetEventsMixin(EntityType type, World world) + { + super(type, world); + } + + @Inject(method = "applyDamage", locals = LocalCapture.CAPTURE_FAILHARD, at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/player/PlayerEntity;addExhaustion(F)V" + )) + private void playerTakingDamage(DamageSource source, float amount, CallbackInfo ci) + { + // version of LivingEntity_scarpetEventsMixin::entityTakingDamage + ((EntityInterface)this).getEventContainer().onEvent(EntityEventsGroup.EntityEventType.ON_DAMAGE, amount, source); + if (PLAYER_TAKES_DAMAGE.isNeeded()) + { + PLAYER_TAKES_DAMAGE.onDamage(this, amount, source); + } + if (source.getAttacker() instanceof ServerPlayerEntity && PLAYER_DEALS_DAMAGE.isNeeded()) + { + PLAYER_DEALS_DAMAGE.onDamage(this, amount, source); + } + } +} diff --git a/src/main/java/carpet/mixins/ServerPlayNetworkHandler_scarpetEventsMixin.java b/src/main/java/carpet/mixins/ServerPlayNetworkHandler_scarpetEventsMixin.java index 9547537c..027236bd 100644 --- a/src/main/java/carpet/mixins/ServerPlayNetworkHandler_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/ServerPlayNetworkHandler_scarpetEventsMixin.java @@ -225,13 +225,13 @@ public class ServerPlayNetworkHandler_scarpetEventsMixin PLAYER_ATTACKS_ENTITY.onEntityAction(player, playerInteractEntityC2SPacket_1.getEntity(player.getServerWorld()), null); } - @Inject(method = "onButtonClick", at = @At("HEAD")) + @Inject(method = "onButtonClick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;updateLastActionTime()V")) private void onItemBeingPickedFromInventory(ButtonClickC2SPacket packet, CallbackInfo ci) { // crafts not int the crafting window //CarpetSettings.LOG.error("Player clicks button "+packet.getButtonId()); } - @Inject(method = "onCraftRequest", at = @At("HEAD")) + @Inject(method = "onCraftRequest", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;updateLastActionTime()V")) private void onRecipeSelectedInRecipeManager(CraftRequestC2SPacket packet, CallbackInfo ci) { if (PLAYER_CHOOSES_RECIPE.isNeeded()) @@ -243,7 +243,7 @@ public class ServerPlayNetworkHandler_scarpetEventsMixin @Inject(method = "onUpdateSelectedSlot", at = @At("HEAD")) private void onUpdatedSelectedSLot(UpdateSelectedSlotC2SPacket packet, CallbackInfo ci) { - if (PLAYER_SWITCHES_SLOT.isNeeded()) + if (PLAYER_SWITCHES_SLOT.isNeeded() && player.getServer() != null && player.getServer().isOnThread()) { PLAYER_SWITCHES_SLOT.onSlotSwitch(player, player.inventory.selectedSlot, packet.getSelectedSlot()); } diff --git a/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java b/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java index ad131366..bc54e158 100644 --- a/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java +++ b/src/main/java/carpet/mixins/ServerPlayerEntity_scarpetEventMixin.java @@ -52,9 +52,9 @@ public abstract class ServerPlayerEntity_scarpetEventMixin extends PlayerEntity { Hand hand = getActiveHand(); ItemStack stack = getActiveItem().copy(); + PLAYER_FINISHED_USING_ITEM.onItemAction((ServerPlayerEntity) (Object)this, hand, stack); // do vanilla super.consumeItem(); - PLAYER_FINISHED_USING_ITEM.onItemAction((ServerPlayerEntity) (Object)this, hand, stack); } else { diff --git a/src/main/java/carpet/mixins/ServerPlayerInteractionManager_scarpetEventsMixin.java b/src/main/java/carpet/mixins/ServerPlayerInteractionManager_scarpetEventsMixin.java index 90b99892..294551b9 100644 --- a/src/main/java/carpet/mixins/ServerPlayerInteractionManager_scarpetEventsMixin.java +++ b/src/main/java/carpet/mixins/ServerPlayerInteractionManager_scarpetEventsMixin.java @@ -28,12 +28,12 @@ public class ServerPlayerInteractionManager_scarpetEventsMixin { @Shadow public ServerPlayerEntity player; - @Inject(method = "tryBreakBlock", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At( + @Inject(method = "tryBreakBlock", locals = LocalCapture.CAPTURE_FAILHARD, at = @At( value = "INVOKE", - target = "Lnet/minecraft/block/Block;onBroken(Lnet/minecraft/world/WorldAccess;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V", + target = "Lnet/minecraft/block/Block;onBreak(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/entity/player/PlayerEntity;)V", shift = At.Shift.BEFORE )) - private void onBlockBroken(BlockPos blockPos_1, CallbackInfoReturnable cir, BlockState blockState_1, BlockEntity be, Block b, boolean boolean_1) + private void onBlockBroken(BlockPos blockPos_1, CallbackInfoReturnable cir, BlockState blockState_1, BlockEntity be, Block b) { PLAYER_BREAK_BLOCK.onBlockBroken(player, blockPos_1, blockState_1); } diff --git a/src/main/resources/carpet.mixins.json b/src/main/resources/carpet.mixins.json index 207d7d71..6a20aac3 100644 --- a/src/main/resources/carpet.mixins.json +++ b/src/main/resources/carpet.mixins.json @@ -163,7 +163,7 @@ "ArmorStandEntity_scarpetMarkerMixin", "Entity_scarpetEventsMixin", "LivingEntity_scarpetEventsMixin", - "DamageTracker_scarpetEventMixin", + "PlayerEntity_scarpetEventsMixin", "ServerPlayerEntity_scarpetEventMixin", "FallingBlockEntity_scarpetEventsMixin", "BlockItem_scarpetEventMixin",