Skip to content

Commit

Permalink
Initial work moving create compat mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
StewStrong committed Oct 16, 2023
1 parent 71c5986 commit dad25fc
Show file tree
Hide file tree
Showing 66 changed files with 4,089 additions and 2 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ subprojects {
maven { url = "https://maven.tterrag.com/" } // Registrate, Forge Create and Flywheel
maven { url = "https://maven.cafeteria.dev/releases" } // Fake Player API
maven { url = "https://maven.jamieswhiteshirt.com/libs-release" } // Reach Entity Attributes
maven {
url = "https://maven.realrobotix.me/createbigcannons/" // Create Big Cannons
content {
includeGroup "com.rbasamoyai"
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ dependencies {
//We just use a version from a platform and hope the classes exist on both versions and mixins apply correctly
modCompileOnly("com.simibubi.create:create-fabric-${minecraft_version}:${create_fabric_version}")
{ exclude group: 'com.github.AlphaMode', module: 'fakeconfigtoml' }
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${fabric_api_version}")
modCompileOnly("com.jozufozu.flywheel:flywheel-fabric-${minecraft_version}:${flywheel_version_fabric}")
modCompileOnly("io.github.fabricators_of_create:Porting-Lib:${port_lib_version}+${minecraft_version}")
modCompileOnly("com.rbasamoyai:createbigcannons-fabric-${minecraft_version}:${createbigcannons_version}")
}

architectury {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.valkyrienskies.mod.mixin.mod_compat.create;

import com.simibubi.create.content.kinetics.deployer.DeployerHandler;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(DeployerHandler.class)
public interface IMixinDeployerHandler {
@Invoker("shouldActivate")
static boolean invokeShouldActivate(ItemStack held, Level world, BlockPos targetPos, @Nullable Direction facing){
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.valkyrienskies.mod.mixin.mod_compat.create;

import com.simibubi.create.content.contraptions.behaviour.MovementContext;
import com.simibubi.create.content.kinetics.deployer.DeployerMovementBehaviour;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(DeployerMovementBehaviour.class)
public interface IMixinDeployerMovementBehaviour {
@Invoker("tryGrabbingItem")
void invokeTryGrabbingItem(MovementContext movementContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.valkyrienskies.mod.mixin.mod_compat.create;

import com.simibubi.create.content.kinetics.fan.AirCurrent;
import com.simibubi.create.content.kinetics.fan.IAirCurrentSource;
import com.simibubi.create.foundation.utility.VecHelper;
import java.util.Iterator;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.joml.primitives.AABBd;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;
import org.valkyrienskies.mod.common.world.RaycastUtilsKt;
import org.valkyrienskies.mod.mixinducks.mod_compat.create.IExtendedAirCurrentSource;

@Mixin(AirCurrent.class)
public abstract class MixinAirCurrent {

@Unique
private final float maxAcceleration = 5;
@Shadow
@Final
public IAirCurrentSource source;
@Unique
private Vec3 transformedFlow = Vec3.ZERO;
@Unique
private float acceleration;

@Unique
private Ship getShip() {
if (source instanceof IExtendedAirCurrentSource se)
return se.getShip();
else if (source.getAirCurrentWorld() != null)
return VSGameUtilsKt.getShipManagingPos(source.getAirCurrentWorld(), source.getAirCurrentPos());
else
return null;
}

@Inject(method = "getFlowLimit", at = @At("HEAD"), cancellable = true)
private static void clipFlowLimit(Level level, BlockPos start, float max, Direction facing, CallbackInfoReturnable<Float> cir) {
Ship ship = VSGameUtilsKt.getShipManagingPos(level, start);
if (ship != null) {
Vector3d startVec = ship.getTransform().getShipToWorld().transformPosition(new Vector3d(start.getX() + 0.5, start.getY() + 0.5, start.getZ() + 0.5));
Vector3d direction = ship.getTransform().getShipToWorld().transformDirection(VectorConversionsMCKt.toJOMLD(facing.getNormal()));
startVec.add(direction.x, direction.y, direction.z);
direction.mul(max);
Vec3 mcStart = VectorConversionsMCKt.toMinecraft(startVec);
BlockHitResult result = RaycastUtilsKt.clipIncludeShips(level,
new ClipContext(
mcStart,
VectorConversionsMCKt.toMinecraft(startVec.add(direction.x, direction.y, direction.z)),
ClipContext.Block.OUTLINE,
ClipContext.Fluid.NONE,
null));

// Distance from start to end but, its not squared so, slow -_-
cir.setReturnValue((float) result.getLocation().distanceTo(mcStart));
} else {
BlockPos end = start.relative(facing, (int) max);
if (VSGameUtilsKt.getShipsIntersecting(level,
new AABB(start.getX(), start.getY(), start.getZ(),
end.getX() + 1.0, end.getY() + 1.0, end.getZ() + 1.0)).iterator().hasNext()) {
Vec3 centerStart = Vec3.atCenterOf(start);
BlockHitResult result = RaycastUtilsKt.clipIncludeShips(level,
new ClipContext(
centerStart.add(facing.getStepX(), facing.getStepY(), facing.getStepZ()),
Vec3.atCenterOf(end),
ClipContext.Block.OUTLINE,
ClipContext.Fluid.NONE,
null));

// Distance from start to end but, its not squared so, slow -_-
cir.setReturnValue((float) result.getLocation().distanceTo(centerStart));
}
}
}

@Redirect(method = "tickAffectedEntities", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/phys/AABB;intersects(Lnet/minecraft/world/phys/AABB;)Z"))
private boolean redirectIntersects(AABB instance, AABB other) {
Ship ship = getShip();
if (ship != null) {
AABBd thisAABB = VectorConversionsMCKt.toJOML(instance);
thisAABB.transform(ship.getWorldToShip());
return other.intersects(thisAABB.minX, thisAABB.minY, thisAABB.minZ, thisAABB.maxX, thisAABB.maxY, thisAABB.maxZ);
} else return instance.intersects(other);
}

@Inject(
method = "tickAffectedEntities",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/Entity;getDeltaMovement()Lnet/minecraft/world/phys/Vec3;"
),
locals = LocalCapture.CAPTURE_FAILHARD
)
private void harvester(Level world, Direction facing, CallbackInfo ci, Iterator<Entity> iterator, Entity entity, Vec3 center, Vec3i flow, float sneakModifier, float speed, double entityDistance, float acceleration) {
Ship ship = getShip();
if (ship != null) {
Vector3d tempVec = new Vector3d();
ship.getTransform().getShipToWorld().transformDirection(flow.getX(), flow.getY(), flow.getZ(), tempVec);
transformedFlow = VectorConversionsMCKt.toMinecraft(tempVec);
}
this.acceleration = acceleration;
}

@Redirect(method = "tickAffectedEntities",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;setDeltaMovement(Lnet/minecraft/world/phys/Vec3;)V")
)
private void redirectSetDeltaMovement(Entity instance, Vec3 motion) {
Ship ship = getShip();
if (ship != null) {
Vec3 previousMotion = instance.getDeltaMovement();
double xIn = Mth.clamp(transformedFlow.x * acceleration - previousMotion.x, -maxAcceleration, maxAcceleration);
double yIn = Mth.clamp(transformedFlow.y * acceleration - previousMotion.y, -maxAcceleration, maxAcceleration);
double zIn = Mth.clamp(transformedFlow.z * acceleration - previousMotion.z, -maxAcceleration, maxAcceleration);
instance.setDeltaMovement(previousMotion.add(new Vec3(xIn, yIn, zIn).scale(1 / 8f)));
} else {
instance.setDeltaMovement(motion);
}
}

@Redirect(method = "tickAffectedEntities", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/foundation/utility/VecHelper;getCenterOf(Lnet/minecraft/core/Vec3i;)Lnet/minecraft/world/phys/Vec3;"), allow = 1)
private Vec3 redirectGetCenterOf(Vec3i pos) {
Ship ship = getShip();
Vec3 result = VecHelper.getCenterOf(pos);
if (ship != null && this.source.getAirCurrentWorld() != null) {
Vector3d tempVec = new Vector3d();
ship.getTransform().getShipToWorld().transformPosition(result.x, result.y, result.z, tempVec);
result = VectorConversionsMCKt.toMinecraft(tempVec);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.valkyrienskies.mod.mixin.mod_compat.create;

import com.simibubi.create.content.kinetics.fan.AirCurrent;
import com.simibubi.create.content.kinetics.fan.AirFlowParticle;
import com.simibubi.create.content.kinetics.fan.IAirCurrentSource;
import com.simibubi.create.foundation.utility.VecHelper;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.SimpleAnimatedParticle;
import net.minecraft.client.particle.SpriteSet;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;
import org.valkyrienskies.mod.mixinducks.mod_compat.create.IExtendedAirCurrentSource;

@Mixin(AirFlowParticle.class)
public abstract class MixinAirFlowParticle extends SimpleAnimatedParticle {

@Shadow
@Final
private IAirCurrentSource source;

protected MixinAirFlowParticle(ClientLevel level, double x, double y, double z, SpriteSet sprites, float gravity) {
super(level, x, y, z, sprites, gravity);
}

@Unique
private Ship getShip() {
if (source instanceof IExtendedAirCurrentSource se)
return se.getShip();
else if (source.getAirCurrentWorld() != null)
return VSGameUtilsKt.getShipManagingPos(source.getAirCurrentWorld(), source.getAirCurrentPos());
else
return null;
}

@Redirect(method = "tick", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/phys/AABB;contains(DDD)Z"
))
private boolean redirectBounds(AABB instance, double x, double y, double z) {
AirCurrent current = source.getAirCurrent();
Level level = source.getAirCurrentWorld();
if (current != null && level != null) {
return VSGameUtilsKt.transformAabbToWorld(level, instance).contains(x, y, z);
}

return instance.contains(x, y, z);
}


@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/foundation/utility/VecHelper;getCenterOf(Lnet/minecraft/core/Vec3i;)Lnet/minecraft/world/phys/Vec3;"), allow = 1)
private Vec3 redirectGetCenterOf(Vec3i pos) {
Ship ship = getShip();
Vec3 result = VecHelper.getCenterOf(pos);
if (ship != null) {
Vector3d tempVec = new Vector3d();
ship.getTransform().getShipToWorld().transformPosition(result.x, result.y, result.z, tempVec);
result = VectorConversionsMCKt.toMinecraft(tempVec);
}
return result;
}

@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/phys/Vec3;atLowerCornerOf(Lnet/minecraft/core/Vec3i;)Lnet/minecraft/world/phys/Vec3;"), allow = 1)
private Vec3 redirectToLowerCorner(Vec3i pos) {
Vec3 result = Vec3.atLowerCornerOf(pos);
Ship ship = getShip();
if (ship != null) {
Vector3d tempVec = new Vector3d();
ship.getTransform().getShipToWorld().transformDirection(result.x, result.y, result.z, tempVec);
result = VectorConversionsMCKt.toMinecraft(tempVec);
}
return result;
}
}
Loading

0 comments on commit dad25fc

Please sign in to comment.