Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up Old Mixins #1040

Merged
merged 7 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
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.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.api.ValkyrienSkies;
import org.valkyrienskies.mod.client.audio.VelocityTickableSoundInstance;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

@Pseudo
@Mixin(SpeakerSound.class)
public abstract class MixinSpeakerSound extends AbstractSoundInstance implements VelocityTickableSoundInstance {
@Unique private SpeakerPosition speakerPosition;
@Unique private Ship ship;

protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2) {
protected MixinSpeakerSound(final ResourceLocation arg, final SoundSource arg2) {
super(arg, arg2);
}

Expand All @@ -32,11 +34,11 @@ protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2) {
at = @At("RETURN"),
remap = false
)
private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
private void isOnShip(final SpeakerPosition position, final CallbackInfo ci) {
this.speakerPosition = position;
this.ship = VSGameUtilsKt.getShipManagingPos(position.level(), position.position());
this.ship = ValkyrienSkies.getShipManagingBlock(position.level(), position.position());
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
final Vec3 worldPos = ValkyrienSkies.positionToWorld(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
Expand All @@ -49,7 +51,7 @@ private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
)
private void updateWorldPos(CallbackInfo ci) {
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
final Vec3 worldPos = ValkyrienSkies.positionToWorld(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
import dan200.computercraft.shared.turtle.core.TurtleBrain;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
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.Matrix4dc;
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.joml.primitives.AABBic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
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.ModifyVariable;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.api.ValkyrienSkies;
import org.valkyrienskies.mod.common.config.VSGameConfig;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;

Expand All @@ -36,7 +30,7 @@ public abstract class MixinTurtleBrain {
public abstract Level getLevel();

@ModifyVariable(
method = "Ldan200/computercraft/shared/turtle/core/TurtleBrain;teleportTo(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;)Z",
method = "teleportTo(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;)Z",
at = @At(value = "HEAD"),
index = 2
)
Expand All @@ -45,71 +39,27 @@ public abstract class MixinTurtleBrain {
final BlockPos oldPos = currentOwner.getBlockPos();
final Level world = getLevel();

final Ship ship = VSGameUtilsKt.getShipManagingPos(world, oldPos);
final Ship ship = ValkyrienSkies.getShipManagingBlock(world, oldPos);
if (ship != null) {
// THERE IS A SHIP
final Direction d = getNewDirection(ship, currentOwner.getDirection());
if (!doesShipContainPoint(ship, pos)) {
final Vector3d transformedDirection = ship.getShipToWorld().transformDirection(
ValkyrienSkies.toJOMLd(currentOwner.getDirection().getNormal())
);
if (!ship.getShipAABB().containsPoint(VectorConversionsMCKt.toJOML(pos))) {
// POSITION IS OUTSIDE THE SHIP'S AABB

currentOwner.setDirection(d);
currentOwner.setDirection(
Direction.getNearest(transformedDirection.x, transformedDirection.y, transformedDirection.z));
setOwner(currentOwner);

if (!isShipScaled(ship)) {
// SHIP IS NOT SCALED

return getWorldPosFromShipPos(ship, pos);
} else if (turtlesLeaveScaledShips()) {
if (ship.getTransform().getShipToWorldScaling().equals(1.000E+0, 1.000E+0, 1.000E+0) &&
VSGameConfig.SERVER.getComputerCraft().getCanTurtlesLeaveScaledShips()) {
// SHIP IS SCALED AND TURTLES CAN LEAVE SCALED SHIPS

return getWorldPosFromShipPos(ship, pos);
return new BlockPos(ValkyrienSkies.positionToWorld(ship, Vec3.atCenterOf(pos)));
}
}
}
return pos;
}

// CUSTOM METHODS

@Unique
private static Direction getNewDirection(final Ship ship, final Direction direction) {
final Matrix4dc matrix = ship.getShipToWorld();
final Vec3i turtleDirectionVector = direction.getNormal();
final Vector3d directionVec =
matrix.transformDirection(turtleDirectionVector.getX(), turtleDirectionVector.getY(),
turtleDirectionVector.getZ(), new Vector3d());
final Direction dir = Direction.getNearest(directionVec.x, directionVec.y, directionVec.z);

return dir;
}

@Unique
private static boolean turtlesLeaveScaledShips() {
return VSGameConfig.SERVER.getComputerCraft().getCanTurtlesLeaveScaledShips();
}

@Unique
private static boolean isShipScaled(final Ship ship) {
final Vector3dc scale = ship.getTransform().getShipToWorldScaling();
final Vector3dc normalScale = new Vector3d(1.000E+0, 1.000E+0, 1.000E+0);
return !scale.equals(normalScale);
}

@Unique
private static boolean doesShipContainPoint(final Ship ship, final BlockPos pos) {
final AABBic shipAABB = ship.getShipAABB();

final AABB t = new AABB(shipAABB.maxX(), shipAABB.maxY(), shipAABB.maxZ(), shipAABB.minX(), shipAABB.minY(),
shipAABB.minZ());
return t.intersects(new AABB(pos));
}

@Unique
private static BlockPos getWorldPosFromShipPos(final Ship ship, final BlockPos pos) {
final Vec3 tPos = VectorConversionsMCKt.toMinecraft(
VSGameUtilsKt.toWorldCoordinates(ship, pos.getX() + 0.5, pos.getY() + 0.5,
pos.getZ() + 0.5));
final BlockPos newPos = new BlockPos(tPos.x, tPos.y, tPos.z);
return newPos;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.valkyrienskies.mod.fabric.mixin.compat.cc_restitched;

import com.google.common.collect.Streams;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.shared.turtle.core.TurtleMoveCommand;
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.joml.Vector3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
Expand All @@ -16,7 +20,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.api.ValkyrienSkies;

@Pseudo
@Mixin(TurtleMoveCommand.class)
Expand All @@ -26,32 +30,24 @@ public abstract class MixinTurtleMoveCommand {
final TurtlePlayer turtlePlayer, final Level world, @Nonnull final BlockPos position,
final CallbackInfoReturnable<TurtleCommandResult> cir) {
if (cir.getReturnValue().isSuccess()) {
final Ship ship = VSGameUtilsKt.getShipManagingPos(world, position);
final Ship ship = ValkyrienSkies.getShipManagingBlock(world, position);
if (ship == null) {
final Ship iShip = VSGameUtilsKt.getShipManagingPos(world, getShipPosFromWorldPos(world, position));
if (iShip != null) {
cir.setReturnValue(TurtleCommandResult.failure("ship"));
final boolean notInAir = Streams
.stream(ValkyrienSkies.positionToNearbyShipsAndWorld(world, position.getX() + 0.5, position.getY() + 0.5, position.getZ() + 0.5, 0.1))
.map(pos -> ValkyrienSkies.getShipManagingBlock(world, pos))
.map(s -> ValkyrienSkies.positionToShip(s, new Vector3d(position.getX() + 0.5, position.getY() + 0.5, position.getZ() + 0.5)))
.map(pos -> world.getBlockState(new BlockPos(ValkyrienSkies.toMinecraft(pos))))
.anyMatch(BlockState::isAir);

if (notInAir) {
cir.setReturnValue(TurtleCommandResult.failure("Movement obstructed by ship"));
}
} else {
final ChunkPos chunk = world.getChunkAt(position).getPos();
if (!ship.getChunkClaim().contains(chunk.x, chunk.z)) {
cir.setReturnValue(TurtleCommandResult.failure("out of ship"));
cir.setReturnValue(TurtleCommandResult.failure("Out of ship chunk"));
}
}
}
}

//CUSTOM METHODS
@Unique
private static Vector3d getShipPosFromWorldPos(final Level world, final BlockPos position) {
final List<Vector3d> detectedShips =
VSGameUtilsKt.transformToNearbyShipsAndWorld(world, position.getX() + 0.5, position.getY() + 0.5,
position.getZ() + 0.5, 0.1);
for (final Vector3d vec : detectedShips) {
if (vec != null) {
return vec;
}
}
return new Vector3d(position.getX(), position.getY(), position.getZ());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.api.ValkyrienSkies;

@Pseudo
@Mixin(WirelessNetwork.class)
Expand All @@ -28,12 +28,8 @@ public class MixinWirelessNetwork {
final Vec3 posOnShip = shipReceiver.getPosition();
final Vec3 posInWorld = shipSender.getPosition();

final double distance =
VSGameUtilsKt.squaredDistanceBetweenInclShips(shipReceiver.getLevel(),
posOnShip.x, posOnShip.y, posOnShip.z,
posInWorld.x, posInWorld.y, posInWorld.z);

return distance;
return ValkyrienSkies.distanceSquared(shipReceiver.getLevel(),
posOnShip.x, posOnShip.y, posOnShip.z, posInWorld.x, posInWorld.y, posInWorld.z);
}

@Inject(at = @At("HEAD"), method = "tryTransmit", remap = false)
Expand Down
2 changes: 1 addition & 1 deletion forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {
modImplementation("curse.maven:coroutil-237749:5008370")

// CC Tweaked
modCompileOnly("curse.maven:cc-tweaked-282001:4061947")
modCompileOnly("curse.maven:cc-tweaked-282001:4630521")

// TIS-3d
modCompileOnly("curse.maven:tis3d-238603:3738437")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
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.callback.CallbackInfo;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.api.ValkyrienSkies;
import org.valkyrienskies.mod.client.audio.VelocityTickableSoundInstance;
import org.valkyrienskies.mod.common.VSGameUtilsKt;

@Pseudo
@Mixin(SpeakerSound.class)
public abstract class MixinSpeakerSound extends AbstractSoundInstance implements VelocityTickableSoundInstance {
@Unique private SpeakerPosition speakerPosition;
@Unique private Ship ship;

protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2) {
protected MixinSpeakerSound(final ResourceLocation arg, final SoundSource arg2) {
super(arg, arg2);
}

Expand All @@ -32,11 +34,11 @@ protected MixinSpeakerSound(ResourceLocation arg, SoundSource arg2) {
at = @At("RETURN"),
remap = false
)
private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
private void isOnShip(final SpeakerPosition position, final CallbackInfo ci) {
this.speakerPosition = position;
this.ship = VSGameUtilsKt.getShipManagingPos(position.level(), position.position());
this.ship = ValkyrienSkies.getShipManagingBlock(position.level(), position.position());
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
final Vec3 worldPos = ValkyrienSkies.positionToWorld(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
Expand All @@ -47,9 +49,9 @@ private void isOnShip(SpeakerPosition position, CallbackInfo ci) {
method = "tick",
at = @At("HEAD")
)
private void updateWorldPos(CallbackInfo ci) {
private void updateWorldPos(final CallbackInfo ci) {
if (this.ship != null) {
Vec3 worldPos = VSGameUtilsKt.toWorldCoordinates(speakerPosition.level(), speakerPosition.position());
final Vec3 worldPos = ValkyrienSkies.positionToWorld(speakerPosition.level(), speakerPosition.position());
x = worldPos.x;
y = worldPos.y;
z = worldPos.z;
Expand Down
Loading
Loading