Skip to content

Commit

Permalink
add getCurrentShip method
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jan 1, 2025
1 parent 263d02c commit 087f783
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.APAddons;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.registries.ForgeRegistries;
import org.valkyrienskies.core.api.ships.Ship;

import org.jetbrains.annotations.NotNull;
import java.util.Collections;
Expand All @@ -42,7 +45,8 @@ public final MethodResult lookAtBlock(@NotNull IArguments arguments) throws LuaE
}

BlockHitResult blockHit = (BlockHitResult) result;
BlockState state = owner.getLevel().getBlockState(blockHit.getBlockPos());
BlockPos blockPos = blockHit.getBlockPos();
BlockState state = owner.getLevel().getBlockState(blockPos);
Map<String, Object> data = new HashMap<>();
ResourceLocation blockName = ForgeRegistries.BLOCKS.getKey(state.getBlock());
data.put("name", blockName == null ? null : blockName.toString());
Expand All @@ -52,6 +56,13 @@ public final MethodResult lookAtBlock(@NotNull IArguments arguments) throws LuaE
data.put("x", pos.x - origin.x);
data.put("y", pos.y - origin.y);
data.put("z", pos.z - origin.z);
if (APAddons.vs2Loaded) {
Ship ship = APAddons.getVS2Ship(automataCore.getLevel(), blockPos);
if (ship != null) {
data.put("shipId", ship.getId());
data.put("shipName", ship.getSlug());
}
}
return MethodResult.of(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins.AutomataCorePlugin;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -17,6 +18,7 @@
import org.valkyrienskies.core.api.ships.ServerShip;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.MOUNT_SHIP;
Expand All @@ -33,6 +35,17 @@ public final boolean isOnShip() {
return APAddons.isBlockOnShip(owner.getLevel(), owner.getPos());
}

@LuaFunction(mainThread = true)
public final MethodResult getCurrentShip() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
ServerShip ship = (ServerShip) APAddons.getVS2Ship(owner.getLevel(), owner.getPos());
if (ship == null) {
return MethodResult.of();
}
Map<String, Object> data = LuaConverter.shipToObjectOnShip(ship, this.automataCore.getCenterPos());
return MethodResult.of(data);
}

@LuaFunction(mainThread = true)
public final MethodResult canMountToShip() {
List<ServerShip> ships = this.getMountableShips();
Expand Down Expand Up @@ -85,7 +98,7 @@ public final MethodResult mountToShip(IArguments args) throws LuaException {

protected Vec3 getMountDetectPosition() {
IPeripheralOwner owner = this.automataCore.getPeripheralOwner();
return owner.getCenterPos().add(Vec3.atLowerCornerOf(owner.getFacing().getNormal()));
return owner.getCenterPos();
}

protected List<ServerShip> getMountableShips() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ public static Object effectToObject(MobEffectInstance effect) {
return map;
}

public static Map<String, Object> shipToObject(ServerShip ship) {
return shipToObject(ship, null);
}

public static Map<String, Object> shipToObject(ServerShip ship, Vec3 pos) {
Map<String, Object> map = new HashMap<>();

Expand All @@ -300,11 +304,13 @@ public static Map<String, Object> shipToObject(ServerShip ship, Vec3 pos) {

ShipTransform tf = ship.getTransform();

Vector3dc worldPos = tf.getShipPositionInWorldCoordinates();
Vector3dc shipPos = tf.getShipPositionInShipCoordinates();
map.put("x", worldPos.x() - pos.x);
map.put("y", worldPos.y() - pos.y);
map.put("z", worldPos.z() - pos.z);
if (pos != null) {
Vector3dc worldPos = tf.getShipPositionInWorldCoordinates();
map.put("x", worldPos.x() - pos.x);
map.put("y", worldPos.y() - pos.y);
map.put("z", worldPos.z() - pos.z);
}
Quaterniondc rot = tf.getShipToWorldRotation();
final double rotX = rot.x(), rotY = rot.y(), rotZ = rot.z(), rotW = rot.w();
map.put("rotate", Map.of("x", rotX, "y", rotY, "z", rotZ, "w", rotW));
Expand All @@ -327,8 +333,20 @@ public static Map<String, Object> shipToObject(ServerShip ship, Vec3 pos) {
ShipInertiaData data = ship.getInertiaData();
map.put("mass", data.getMass());
Vector3d com = tf.getShipToWorld().transformPosition(data.getCenterOfMassInShipSpace(), new Vector3d());
map.put("centerOfMass", Map.of("x", com.x - pos.x, "y", com.y - pos.y, "z", com.z - pos.z));
if (pos != null) {
map.put("centerOfMass", Map.of("x", com.x - pos.x, "y", com.y - pos.y, "z", com.z - pos.z));
}
return map;
}

public static Map<String, Object> shipToObjectOnShip(ServerShip ship, Vec3 pos) {
Map<String, Object> map = shipToObject(ship);
Vector3dc shipPos = ship.getTransform().getShipPositionInShipCoordinates();
map.put("x", shipPos.x() - pos.x);
map.put("y", shipPos.y() - pos.y);
map.put("z", shipPos.z() - pos.z);
Vector3dc com = ship.getInertiaData().getCenterOfMassInShipSpace();
map.put("centerOfMass", Map.of("x", com.x() - pos.x, "y", com.y() - pos.y, "z", com.z() - pos.z));
return map;
}
}

0 comments on commit 087f783

Please sign in to comment.