Skip to content

Commit

Permalink
feat(mixin): mixin projectile to get shotting position
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Sep 17, 2022
1 parent 18fd871 commit 444780c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
@@ -0,0 +1,7 @@
package org.auioc.mcmod.arnicalib.api.mixin.common;

import org.auioc.mcmod.arnicalib.api.game.entity.IHProjectile;

public interface IMixinProjectile extends IHProjectile {

}
@@ -0,0 +1,65 @@
package org.auioc.mcmod.arnicalib.mixin.common;

import javax.annotation.Nullable;
import org.auioc.mcmod.arnicalib.api.mixin.common.IMixinProjectile;
import org.auioc.mcmod.arnicalib.utils.game.NbtUtils;
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 net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.phys.Vec3;

@Mixin(value = Projectile.class)
public class MixinProjectile implements IMixinProjectile {

@Nullable
private Vec3 shootingPosition;

@Nullable
@Override
public Vec3 getShootingPosition() {
return this.shootingPosition;
}

@Inject(
method = "Lnet/minecraft/world/entity/projectile/Projectile;tick()V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/projectile/Projectile;gameEvent(Lnet/minecraft/world/level/gameevent/GameEvent;Lnet/minecraft/world/entity/Entity;Lnet/minecraft/core/BlockPos;)V",
ordinal = 0
),
require = 1,
allow = 1
)
private void tick(CallbackInfo ci) {
var pos = ((Projectile) (Object) this).position();
this.shootingPosition = new Vec3(pos.x, pos.y, pos.z);
}

@Inject(
method = "Lnet/minecraft/world/entity/projectile/Projectile;readAdditionalSaveData(Lnet/minecraft/nbt/CompoundTag;)V",
at = @At(value = "TAIL"),
require = 1,
allow = 1
)
private void readAdditionalSaveData(CompoundTag p_37262_, CallbackInfo ci) {
if (p_37262_.contains("ShootingPosition", 6)) {
this.shootingPosition = NbtUtils.readVec3(p_37262_.getList("ShootingPosition", 6));
}
}

@Inject(
method = "Lnet/minecraft/world/entity/projectile/Projectile;addAdditionalSaveData(Lnet/minecraft/nbt/CompoundTag;)V",
at = @At(value = "TAIL"),
require = 1,
allow = 1
)
private void addAdditionalSaveData(CompoundTag p_37265_, CallbackInfo ci) {
if (this.shootingPosition != null) {
p_37265_.put("ShootingPosition", NbtUtils.writeVec3(this.shootingPosition));
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/arnicalib.mixin.json
Expand Up @@ -8,6 +8,7 @@
"common.MixinCommandSourceStack",
"common.MixinCreativeModeTab",
"common.MixinItemStack",
"common.MixinProjectile",
"common.MixinArrow",
"server.MixinServerLifecycleHooks",
"server.MixinLootContext",
Expand Down

0 comments on commit 444780c

Please sign in to comment.