Skip to content

Commit 4b93548

Browse files
committed
Add all combat hooks.
1 parent 3b892f1 commit 4b93548

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

src/main/java/com/provismet/CombatPlusCore/CPCMain.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
import com.provismet.CombatPlusCore.interfaces.mixin.IMixinItemStack;
7+
import com.provismet.CombatPlusCore.utility.CPCEnchantmentHelper;
8+
69
import net.fabricmc.api.ModInitializer;
10+
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityCombatEvents;
11+
import net.minecraft.entity.LivingEntity;
712
import net.minecraft.util.Identifier;
813

914
public class CPCMain implements ModInitializer {
@@ -16,7 +21,11 @@ public static Identifier identifier (String path) {
1621

1722
@Override
1823
public void onInitialize () {
19-
24+
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((world, entity, target) -> {
25+
if (entity instanceof LivingEntity user) {
26+
((IMixinItemStack)(Object)user.getMainHandStack()).CPC_postKill(user, target);
27+
CPCEnchantmentHelper.postKill(user, target);
28+
}
29+
});
2030
}
21-
2231
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.provismet.CombatPlusCore.mixin.interfaces;
1+
package com.provismet.CombatPlusCore.interfaces.mixin;
22

33
import net.minecraft.entity.LivingEntity;
44

@@ -7,5 +7,5 @@ public interface IMixinItemStack {
77

88
public void CPC_postCriticalHit (LivingEntity user, LivingEntity target);
99

10-
public void CPC_postKill (LivingEntity user);
10+
public void CPC_postKill (LivingEntity user, LivingEntity target);
1111
}

src/main/java/com/provismet/CombatPlusCore/mixin/ItemStackMixin.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.spongepowered.asm.mixin.Shadow;
55

66
import com.provismet.CombatPlusCore.interfaces.MeleeWeapon;
7-
import com.provismet.CombatPlusCore.mixin.interfaces.IMixinItemStack;
7+
import com.provismet.CombatPlusCore.interfaces.mixin.IMixinItemStack;
88

99
import net.minecraft.entity.LivingEntity;
1010
import net.minecraft.item.Item;
@@ -26,7 +26,7 @@ public void CPC_postCriticalHit (LivingEntity user, LivingEntity target) {
2626
}
2727

2828
@Override
29-
public void CPC_postKill (LivingEntity user) {
30-
if (this.getItem() instanceof MeleeWeapon weapon) weapon.postKill(user);
29+
public void CPC_postKill (LivingEntity user, LivingEntity target) {
30+
if (this.getItem() instanceof MeleeWeapon weapon) weapon.postKill(user, target);
3131
}
3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.provismet.CombatPlusCore.mixin;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
import org.spongepowered.asm.mixin.injection.At;
5+
import org.spongepowered.asm.mixin.injection.Inject;
6+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
7+
8+
import com.provismet.CombatPlusCore.interfaces.mixin.IMixinItemStack;
9+
import com.provismet.CombatPlusCore.utility.CPCEnchantmentHelper;
10+
11+
import net.minecraft.entity.Entity;
12+
import net.minecraft.entity.EntityType;
13+
import net.minecraft.entity.LivingEntity;
14+
import net.minecraft.entity.mob.MobEntity;
15+
import net.minecraft.world.World;
16+
17+
@Mixin(MobEntity.class)
18+
public abstract class MobEntityMixin extends LivingEntity {
19+
protected MobEntityMixin (EntityType<? extends LivingEntity> entityType, World world) {
20+
super(entityType, world);
21+
}
22+
23+
@Inject(method="tryAttack", at=@At(value="INVOKE", target="Lnet/minecraft/enchantment/EnchantmentHelper;getKnockback(Lnet/minecraft/entity/LivingEntity;)I", shift=At.Shift.AFTER))
24+
public void onHit (Entity target, CallbackInfoReturnable<Boolean> cir) {
25+
if (target instanceof LivingEntity living) {
26+
((IMixinItemStack)(Object)this.getMainHandStack()).CPC_postChargedHit(this, living);
27+
CPCEnchantmentHelper.postChargedHit(this, living);
28+
}
29+
}
30+
}

src/main/java/com/provismet/CombatPlusCore/mixin/PlayerEntityMixin.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import org.spongepowered.asm.mixin.Mixin;
44
import org.spongepowered.asm.mixin.injection.At;
55
import org.spongepowered.asm.mixin.injection.Inject;
6+
import org.spongepowered.asm.mixin.injection.Redirect;
67
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
78

8-
import com.provismet.CombatPlusCore.mixin.interfaces.IMixinItemStack;
9+
import com.provismet.CombatPlusCore.interfaces.mixin.IMixinItemStack;
910
import com.provismet.CombatPlusCore.utility.CPCEnchantmentHelper;
1011

12+
import net.minecraft.enchantment.EnchantmentHelper;
1113
import net.minecraft.entity.Entity;
14+
import net.minecraft.entity.EntityGroup;
1215
import net.minecraft.entity.EntityType;
1316
import net.minecraft.entity.LivingEntity;
1417
import net.minecraft.entity.player.PlayerEntity;
18+
import net.minecraft.item.ItemStack;
1519
import net.minecraft.world.World;
1620

1721
@Mixin(PlayerEntity.class)
@@ -20,7 +24,7 @@ protected PlayerEntityMixin (EntityType<? extends LivingEntity> entityType, Worl
2024
super(entityType, world);
2125
}
2226

23-
@Inject(method="attack", at=@At(value="INVOKE", target="Lnet/minecraft/entity/player/PlayerEntity;addCritParticles(Lnet/minecraft/entity/Entity;)V", shift = At.Shift.AFTER))
27+
@Inject(method="attack", at=@At(value="INVOKE", target="Lnet/minecraft/entity/player/PlayerEntity;addCritParticles(Lnet/minecraft/entity/Entity;)V", shift=At.Shift.AFTER))
2428
public void onCriticalHit (Entity entity, CallbackInfo info) {
2529
if (entity instanceof LivingEntity target) {
2630
((IMixinItemStack)(Object)this.getMainHandStack()).CPC_postCriticalHit(this, target);
@@ -35,4 +39,12 @@ public void postChargedHit (Entity entity, CallbackInfo info) {
3539
CPCEnchantmentHelper.postChargedHit(this, target);
3640
}
3741
}
42+
43+
@Redirect(method="attack", at=@At(value="INVOKE", target="Lnet/minecraft/enchantment/EnchantmentHelper;getAttackDamage(Lnet/minecraft/item/ItemStack;Lnet/minecraft/entity/EntityGroup;)F"))
44+
public float redirectVanillaEnchantments (ItemStack itemStack, EntityGroup entityGroup, Entity target) {
45+
if (target instanceof LivingEntity living) {
46+
return CPCEnchantmentHelper.getAttackDamage(this, living);
47+
}
48+
return EnchantmentHelper.getAttackDamage(itemStack, entityGroup);
49+
}
3850
}

src/main/resources/combat-plus.mixins.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"compatibilityLevel": "JAVA_17",
55
"mixins": [
66
"ItemStackMixin",
7-
"PlayerEntityMixin"
7+
"PlayerEntityMixin",
8+
"MobEntityMixin"
89
],
910
"injectors": {
1011
"defaultRequire": 1

0 commit comments

Comments
 (0)