From bd49e5a8173bc23cda05bd90f942b85364ea5fc8 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Thu, 26 Dec 2024 17:53:00 -0700 Subject: [PATCH 1/6] add mountToShip for automata turtles --- .../operations/SingleOperation.java | 3 +- .../EnvironmentDetectorPeripheral.java | 2 +- .../WeakAutomataCorePeripheral.java | 17 ++++ .../plugins/AutomataVSMountPlugin.java | 96 +++++++++++++++++++ .../valkyrienskies/ShipScannerPlugin.java | 22 +---- .../addons/valkyrienskies/ValkyrienSkies.java | 34 +++++++ 6 files changed, 153 insertions(+), 21 deletions(-) create mode 100644 src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java create mode 100644 src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ValkyrienSkies.java diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java index 333eb905f..f2075d2a4 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java @@ -16,7 +16,8 @@ public enum SingleOperation implements IPeripheralOperation { public static final String PERIPHERAL_TYPE = "environment_detector"; - private static final List> PERIPHERAL_PLUGINS = new LinkedList<>(); + private static final List> PERIPHERAL_PLUGINS = new ArrayList<>(); protected EnvironmentDetectorPeripheral(IPeripheralOwner owner) { super(PERIPHERAL_TYPE, owner); diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java index 64bd72511..bc7fa0e6a 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java @@ -2,14 +2,28 @@ import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; +import de.srendi.advancedperipherals.common.addons.APAddons; import de.srendi.advancedperipherals.common.addons.computercraft.operations.AutomataCoreTier; import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins.*; import de.srendi.advancedperipherals.common.configuration.APConfig; import de.srendi.advancedperipherals.lib.metaphysics.IAutomataCoreTier; import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + public class WeakAutomataCorePeripheral extends AutomataCorePeripheral { public static final String TYPE = "weak_automata"; + private static final List> PERIPHERAL_PLUGINS = new ArrayList<>(); + + static { + PERIPHERAL_PLUGINS.add(AutomataItemSuckPlugin::new); + PERIPHERAL_PLUGINS.add(AutomataLookPlugin::new); + PERIPHERAL_PLUGINS.add(AutomataBlockHandPlugin::new); + PERIPHERAL_PLUGINS.add(AutomataSoulFeedingPlugin::new); + PERIPHERAL_PLUGINS.add(AutomataChargingPlugin::new); + } public WeakAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { this(TYPE, turtle, side, AutomataCoreTier.TIER1); @@ -22,6 +36,9 @@ protected WeakAutomataCorePeripheral(String type, ITurtleAccess turtle, TurtleSi addPlugin(new AutomataBlockHandPlugin(this)); addPlugin(new AutomataSoulFeedingPlugin(this)); addPlugin(new AutomataChargingPlugin(this)); + if (APAddons.vs2Loaded) { + addPlugin(new AutomataVSMountPlugin(this)); + } } @Override diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java new file mode 100644 index 000000000..584214e77 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java @@ -0,0 +1,96 @@ +package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins; + +import dan200.computercraft.api.lua.IArguments; +import dan200.computercraft.api.lua.LuaException; +import dan200.computercraft.api.lua.LuaFunction; +import dan200.computercraft.api.lua.MethodResult; +import de.srendi.advancedperipherals.common.addons.APAddons; +import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext; +import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner; +import de.srendi.advancedperipherals.common.addons.valkyrienskies.ValkyrienSkies; +import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.Level; +import net.minecraft.world.phys.Vec3; +import org.joml.Vector3d; +import org.valkyrienskies.core.api.ships.ServerShip; +import org.valkyrienskies.core.api.ships.Ship; + +import java.util.List; +import java.util.stream.Collectors; + +import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.MOUNT_SHIP; + +public class AutomataVSMountPlugin extends AutomataCorePlugin { + + public AutomataVSMountPlugin(AutomataCorePeripheral automataCore) { + super(automataCore); + } + + @LuaFunction(mainThread = true) + public final boolean isOnShip() { + IPeripheralOwner owner = this.automataCore.getPeripheralOwner(); + return APAddons.isBlockOnShip(owner.getLevel(), owner.getPos()); + } + + @LuaFunction(mainThread = true) + public final MethodResult canMountToShip() { + List ships = this.getMountableShips(); + if (ships.size() == 0) { + return MethodResult.of(); + } + List shipNames = ships.stream().map(s -> s.getSlug()).collect(Collectors.toList()); + return MethodResult.of(shipNames); + } + + @LuaFunction(mainThread = true) + public final MethodResult mountToShip(IArguments args) throws LuaException { + String name = args.optString(0).orElse(null); + List ships = this.getMountableShips(); + if (ships.size() == 0) { + return MethodResult.of(false, "no mountable ship detected"); + } + ServerShip targetShip = null; + if (name == null) { + targetShip = ships.get(0); + } else { + for (ServerShip s : ships) { + if (s.getSlug().equals(name)) { + targetShip = s; + break; + } + } + } + if (targetShip == null) { + return MethodResult.of(false, "target ship not found"); + } + IPeripheralOwner owner = this.automataCore.getPeripheralOwner(); + Level level = owner.getLevel(); + Vec3 pos = this.getMountDetectPosition(); + Vector3d targetPos = targetShip.getWorldToShip().transformPosition(new Vector3d(pos.x, pos.y, pos.z)); + BlockPos newPosition = new BlockPos(targetPos.x, targetPos.y, targetPos.z); + return this.automataCore.withOperation(MOUNT_SHIP, new SingleOperationContext(1, 1), context -> { + boolean result = owner.move(level, newPosition); + if (!result) { + return MethodResult.of(false, "cannot mount to ship"); + } + return MethodResult.of(true); + }, context -> { + if (!owner.isMovementPossible(level, newPosition)) { + return MethodResult.of(false, "move forbidden"); + } + return null; + }); + } + + protected Vec3 getMountDetectPosition() { + IPeripheralOwner owner = this.automataCore.getPeripheralOwner(); + return owner.getCenterPos().add(Vec3.atLowerCornerOf(owner.getFacing().getNormal())); + } + + protected List getMountableShips() { + IPeripheralOwner owner = this.automataCore.getPeripheralOwner(); + return ValkyrienSkies.getNearbyShips((ServerLevel) owner.getLevel(), this.getMountDetectPosition(), 0.5); + } +} diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java index eb4ea5749..6450eec51 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java @@ -3,23 +3,19 @@ import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.lua.MethodResult; -import de.srendi.advancedperipherals.common.addons.APAddons; import de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperationContext; import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner; import de.srendi.advancedperipherals.common.util.LuaConverter; import de.srendi.advancedperipherals.lib.peripherals.BasePeripheralPlugin; import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation; -import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.phys.Vec3; -import org.joml.Vector3d; import org.valkyrienskies.core.api.ships.ServerShip; -import org.valkyrienskies.core.api.ships.Ship; -import org.valkyrienskies.mod.common.VSGameUtilsKt; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperation.SCAN_SHIPS; @@ -38,21 +34,9 @@ public final MethodResult scanShips(int radius) throws LuaException { return withOperation(SCAN_SHIPS, new SphereOperationContext(radius), context -> { return context.getRadius() > SCAN_SHIPS.getMaxCostRadius() ? MethodResult.of(null, "Radius exceeds max value") : null; }, context -> { - ServerLevel level = (ServerLevel) this.owner.getLevel(); Vec3 pos = this.owner.getCenterPos(); - Ship ship = APAddons.getVS2Ship(level, new BlockPos(pos)); - if (ship != null) { - Vector3d newPos = ship.getShipToWorld().transformPosition(new Vector3d(pos.x, pos.y, pos.z)); - pos = new Vec3(newPos.x, newPos.y, newPos.z); - } - List shipPoses = VSGameUtilsKt.transformToNearbyShipsAndWorld(level, pos.x, pos.y, pos.z, context.getRadius()); - List> shipDatas = new ArrayList<>(shipPoses.size()); - for (Vector3d p : shipPoses) { - ServerShip s = VSGameUtilsKt.getShipManagingPos(level, p.x, p.y, p.z); - if (ship == null || s.getId() != ship.getId()) { - shipDatas.add(LuaConverter.shipToObject(s, pos)); - } - } + List ships = ValkyrienSkies.getNearbyShips((ServerLevel) this.owner.getLevel(), pos, context.getRadius()); + List> shipDatas = ships.stream().map(s -> LuaConverter.shipToObject(s, pos)).collect(Collectors.toList()); return MethodResult.of(shipDatas); }, null); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ValkyrienSkies.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ValkyrienSkies.java new file mode 100644 index 000000000..61d084da1 --- /dev/null +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ValkyrienSkies.java @@ -0,0 +1,34 @@ +package de.srendi.advancedperipherals.common.addons.valkyrienskies; + +import de.srendi.advancedperipherals.common.addons.APAddons; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.phys.Vec3; +import org.joml.Vector3d; +import org.valkyrienskies.core.api.ships.ServerShip; +import org.valkyrienskies.core.api.ships.Ship; +import org.valkyrienskies.mod.common.VSGameUtilsKt; + +import java.util.ArrayList; +import java.util.List; + +public final class ValkyrienSkies { + private ValkyrienSkies() {} + + public static List getNearbyShips(ServerLevel level, Vec3 pos, double radius) { + Ship ship = APAddons.getVS2Ship(level, new BlockPos(pos)); + if (ship != null) { + Vector3d newPos = ship.getShipToWorld().transformPosition(new Vector3d(pos.x, pos.y, pos.z)); + pos = new Vec3(newPos.x, newPos.y, newPos.z); + } + List shipPoses = VSGameUtilsKt.transformToNearbyShipsAndWorld(level, pos.x, pos.y, pos.z, radius); + List ships = new ArrayList<>(shipPoses.size()); + for (Vector3d p : shipPoses) { + ServerShip s = VSGameUtilsKt.getShipManagingPos(level, p.x, p.y, p.z); + if (ship == null || s.getId() != ship.getId()) { + ships.add(s); + } + } + return ships; + } +} From 83556a8ded5790f80662db077dc633fd1f0d90a4 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Tue, 31 Dec 2024 23:37:00 -0700 Subject: [PATCH 2/6] fix automata turtle interact with vs2 ship --- .../WeakAutomataCorePeripheral.java | 24 +++---- .../plugins/AutomataLookPlugin.java | 18 ++++-- .../AutomataVSMountPlugin.java | 4 +- .../addons/valkyrienskies/Integration.java | 2 + .../common/util/HitResultUtil.java | 2 +- .../common/util/fakeplayer/APFakePlayer.java | 64 +++++++------------ .../fakeplayer/FakePlayerProviderTurtle.java | 36 +++++++++-- .../lib/peripherals/BasePeripheral.java | 12 ++-- 8 files changed, 88 insertions(+), 74 deletions(-) rename src/main/java/de/srendi/advancedperipherals/common/addons/{computercraft/peripheral/plugins => valkyrienskies}/AutomataVSMountPlugin.java (95%) diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java index bc7fa0e6a..dc0ac06d7 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java @@ -8,6 +8,7 @@ import de.srendi.advancedperipherals.common.configuration.APConfig; import de.srendi.advancedperipherals.lib.metaphysics.IAutomataCoreTier; import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral; +import de.srendi.advancedperipherals.lib.peripherals.IPeripheralPlugin; import java.util.ArrayList; import java.util.List; @@ -18,11 +19,11 @@ public class WeakAutomataCorePeripheral extends AutomataCorePeripheral { private static final List> PERIPHERAL_PLUGINS = new ArrayList<>(); static { - PERIPHERAL_PLUGINS.add(AutomataItemSuckPlugin::new); - PERIPHERAL_PLUGINS.add(AutomataLookPlugin::new); - PERIPHERAL_PLUGINS.add(AutomataBlockHandPlugin::new); - PERIPHERAL_PLUGINS.add(AutomataSoulFeedingPlugin::new); - PERIPHERAL_PLUGINS.add(AutomataChargingPlugin::new); + addIntegrationPlugin(AutomataItemSuckPlugin::new); + addIntegrationPlugin(AutomataLookPlugin::new); + addIntegrationPlugin(AutomataBlockHandPlugin::new); + addIntegrationPlugin(AutomataSoulFeedingPlugin::new); + addIntegrationPlugin(AutomataChargingPlugin::new); } public WeakAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { @@ -31,16 +32,15 @@ public WeakAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { protected WeakAutomataCorePeripheral(String type, ITurtleAccess turtle, TurtleSide side, IAutomataCoreTier tier) { super(type, turtle, side, tier); - addPlugin(new AutomataItemSuckPlugin(this)); - addPlugin(new AutomataLookPlugin(this)); - addPlugin(new AutomataBlockHandPlugin(this)); - addPlugin(new AutomataSoulFeedingPlugin(this)); - addPlugin(new AutomataChargingPlugin(this)); - if (APAddons.vs2Loaded) { - addPlugin(new AutomataVSMountPlugin(this)); + for (Function plugin : PERIPHERAL_PLUGINS) { + addPlugin(plugin.apply(this)); } } + public static void addIntegrationPlugin(Function plugin) { + PERIPHERAL_PLUGINS.add(plugin); + } + @Override public boolean isEnabled() { return APConfig.METAPHYSICS_CONFIG.enableWeakAutomataCore.get(); diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java index a3b5446ac..6bf4759ad 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java @@ -14,6 +14,7 @@ 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.jetbrains.annotations.NotNull; @@ -36,16 +37,21 @@ public final MethodResult lookAtBlock(@NotNull IArguments arguments) throws LuaE automataCore.addRotationCycle(); TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); HitResult result = owner.withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(true, false))); - if (result.getType() == HitResult.Type.MISS) + if (result.getType() == HitResult.Type.MISS) { return MethodResult.of(null, "No block find"); + } BlockHitResult blockHit = (BlockHitResult) result; BlockState state = owner.getLevel().getBlockState(blockHit.getBlockPos()); Map data = new HashMap<>(); ResourceLocation blockName = ForgeRegistries.BLOCKS.getKey(state.getBlock()); - if (blockName != null) - data.put("name", blockName.toString()); + data.put("name", blockName == null ? null : blockName.toString()); data.put("tags", LuaConverter.tagsToList(() -> state.getBlock().builtInRegistryHolder().tags())); + Vec3 pos = blockHit.getLocation(); + Vec3 origin = automataCore.getWorldPos(); + data.put("x", pos.x - origin.x); + data.put("y", pos.y - origin.y); + data.put("z", pos.z - origin.z); return MethodResult.of(data); } @@ -57,11 +63,13 @@ public final MethodResult lookAtEntity(@NotNull IArguments arguments) throws Lua automataCore.addRotationCycle(); HitResult result = automataCore.getPeripheralOwner().withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(false, true))); - if (result.getType() == HitResult.Type.MISS) + if (result.getType() == HitResult.Type.MISS) { return MethodResult.of(null, "No entity find"); + } EntityHitResult entityHit = (EntityHitResult) result; - return MethodResult.of(LuaConverter.entityToLua(entityHit.getEntity(), true)); + Vec3 origin = automataCore.getWorldPos(); + return MethodResult.of(LuaConverter.completeEntityWithPositionToLua(entityHit.getEntity(), origin, true)); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java similarity index 95% rename from src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java rename to src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java index 584214e77..e6699476b 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataVSMountPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java @@ -1,4 +1,4 @@ -package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins; +package de.srendi.advancedperipherals.common.addons.valkyrienskies; import dan200.computercraft.api.lua.IArguments; import dan200.computercraft.api.lua.LuaException; @@ -7,7 +7,7 @@ import de.srendi.advancedperipherals.common.addons.APAddons; import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext; import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner; -import de.srendi.advancedperipherals.common.addons.valkyrienskies.ValkyrienSkies; +import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins.AutomataCorePlugin; import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/Integration.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/Integration.java index a416cbbea..b54a20c27 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/Integration.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/Integration.java @@ -1,11 +1,13 @@ package de.srendi.advancedperipherals.common.addons.valkyrienskies; import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.EnvironmentDetectorPeripheral; +import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.metaphysics.WeakAutomataCorePeripheral; public class Integration implements Runnable { @Override public void run() { EnvironmentDetectorPeripheral.addIntegrationPlugin(ShipScannerPlugin::new); + WeakAutomataCorePeripheral.addIntegrationPlugin(AutomataVSMountPlugin::new); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java index 618cf90b2..95ea8d085 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java @@ -128,7 +128,7 @@ public static EntityHitResult getEntityHitResult(Vec3 to, Vec3 from, Level level */ @NotNull public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreNoOccluded) { - return getBlockHitResult(to, from, level, ignoreNoOccluded); + return getBlockHitResult(to, from, level, ignoreNoOccluded, null); } /** diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java index a947a4c53..4068709db 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java @@ -3,6 +3,7 @@ import com.mojang.authlib.GameProfile; import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.util.Pair; +import de.srendi.advancedperipherals.common.util.HitResultUtil; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket; @@ -33,6 +34,7 @@ import net.minecraft.world.level.block.entity.SignBlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.*; +import net.minecraft.world.phys.shapes.Shapes; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.ForgeMod; import net.minecraftforge.common.util.FakePlayer; @@ -55,38 +57,33 @@ public class APFakePlayer extends FakePlayer { public static final GameProfile PROFILE = new GameProfile(UUID.fromString("6e483f02-30db-4454-b612-3a167614b276"), "[" + AdvancedPeripherals.MOD_ID + "]"); private static final Predicate collidablePredicate = EntitySelector.NO_SPECTATORS; - private final WeakReference owner; - - private BlockPos digPosition; - private Block digBlock; - + private BlockPos source = null; + private BlockPos digPosition = null; + private Block digBlock = null; private float currentDamage = 0; public APFakePlayer(ServerLevel world, Entity owner, GameProfile profile) { super(world, profile != null && profile.isComplete() ? profile : PROFILE); if (owner != null) { setCustomName(owner.getName()); - this.owner = new WeakReference<>(owner); - } else { - this.owner = null; } } + public void setSourceBlock(BlockPos pos) { + this.source = pos; + } + @Override public void awardStat(@NotNull Stat stat) { MinecraftServer server = level.getServer(); if (server != null && getGameProfile() != PROFILE) { Player player = server.getPlayerList().getPlayer(getUUID()); - if (player != null) + if (player != null) { player.awardStat(stat); + } } } - @Override - public boolean canAttack(@NotNull LivingEntity livingEntity) { - return true; - } - @Override public void openTextEdit(@NotNull SignBlockEntity sign) { } @@ -101,7 +98,6 @@ public void playSound(@NotNull SoundEvent soundIn, float volume, float pitch) { } private void setState(Block block, BlockPos pos) { - if (digPosition != null) { gameMode.handleBlockBreakAction(digPosition, ServerboundPlayerActionPacket.Action.ABORT_DESTROY_BLOCK, Direction.EAST, 320, 1); } @@ -121,12 +117,12 @@ public static Function wrapActionWithRot(float yaw, float p } public T doActionWithRot(float yaw, float pitch, Function action) { - final float oldRot = this.getYRot(); - this.setRot(oldRot + yaw, pitch); + final float yRot = this.getYRot(), xRot = this.getXRot(); + this.setRot(yRot + yaw, xRot + pitch); try { return action.apply(this); } finally { - this.setRot(oldRot, 0); + this.setRot(yRot, xRot); } } @@ -144,11 +140,6 @@ public T doActionWithShiftKey(boolean shift, Function actio } } - @Deprecated(forRemoval = true) - public Pair digBlock(Direction direction) { - return doActionWithRot(direction.toYRot() - this.getYRot(), direction == Direction.DOWN ? 90 : direction == Direction.UP ? -90 : 0, APFakePlayer::digBlock); - } - public Pair digBlock() { Level world = getLevel(); HitResult hit = findHit(true, false); @@ -290,33 +281,27 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock) { @NotNull public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predicate entityFilter) { - AttributeInstance reachAttribute = getAttribute(ForgeMod.REACH_DISTANCE.get()); + AttributeInstance reachAttribute = this.getAttribute(ForgeMod.REACH_DISTANCE.get()); if (reachAttribute == null) throw new IllegalArgumentException("How did this happened?"); double range = reachAttribute.getValue(); - Vec3 origin = new Vec3(getX(), getY(), getZ()); - Vec3 look = getLookAngle(); + Vec3 origin = new Vec3(this.getX(), this.getY(), this.getZ()); + Vec3 look = this.getLookAngle(); Vec3 target = new Vec3(origin.x + look.x * range, origin.y + look.y * range, origin.z + look.z * range); - ClipContext traceContext = new ClipContext(origin, target, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this); - Vec3 directionVec = traceContext.getFrom().subtract(traceContext.getTo()); - Direction traceDirection = Direction.getNearest(directionVec.x, directionVec.y, directionVec.z); HitResult blockHit; if (skipBlock) { - blockHit = BlockHitResult.miss(traceContext.getTo(), traceDirection, new BlockPos(traceContext.getTo())); + Direction traceDirection = Direction.getNearest(look.x, look.y, look.z); + blockHit = BlockHitResult.miss(target, traceDirection, new BlockPos(target)); } else { - blockHit = BlockGetter.traverseBlocks(traceContext.getFrom(), traceContext.getTo(), traceContext, (rayTraceContext, blockPos) -> { - if (level.isEmptyBlock(blockPos) || blockPos.equals(blockPosition())) { - return null; - } - return new BlockHitResult(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), traceDirection, blockPos, false); - }, rayTraceContext -> BlockHitResult.miss(rayTraceContext.getTo(), traceDirection, new BlockPos(rayTraceContext.getTo()))); + blockHit = HitResultUtil.getBlockHitResult(target, origin, level, false, this.source); } - if (skipEntity) + if (skipEntity) { return blockHit; + } - List entities = level.getEntities(this, getBoundingBox().expandTowards(look.x * range, look.y * range, look.z * range).inflate(1), collidablePredicate); + List entities = level.getEntities(this, this.getBoundingBox().expandTowards(look.x * range, look.y * range, look.z * range).inflate(1), collidablePredicate); LivingEntity closestEntity = null; Vec3 closestVec = null; @@ -330,9 +315,6 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic continue; } - // Removed a lot logic here to make Automata cores interact like a player. - // However, the results for some edge cases may change. Need more review and tests. - // Hit vehicle before passenger if (entity.isPassenger()) { continue; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java index 03696de42..60946c0b2 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java @@ -3,6 +3,8 @@ import com.mojang.authlib.GameProfile; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.shared.util.WorldUtil; +import de.srendi.advancedperipherals.common.addons.APAddons; +import net.minecraft.commands.arguments.EntityAnchorArgument; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; @@ -11,8 +13,12 @@ import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.phys.Vec3;; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.wrapper.InvWrapper; +import org.joml.Matrix4dc; +import org.joml.Vector3d; +import org.valkyrienskies.core.api.ships.Ship; import java.util.WeakHashMap; import java.util.function.Function; @@ -32,11 +38,27 @@ public static APFakePlayer getPlayer(ITurtleAccess turtle, GameProfile profile) } public static void load(APFakePlayer player, ITurtleAccess turtle) { - Direction direction = turtle.getDirection(); - player.setLevel((ServerLevel) turtle.getLevel()); - // Player position - BlockPos position = turtle.getPosition(); - player.moveTo(position.getX() + 0.5, position.getY() + 0.5, position.getZ() + 0.5, direction.toYRot(), 0); + ServerLevel level = (ServerLevel) turtle.getLevel(); + player.setLevel(level); + + BlockPos pos = turtle.getPosition(); + player.setSourceBlock(pos); + + Vec3 direction = Vec3.atLowerCornerOf(turtle.getDirection().getNormal()); + Vec3 position = Vec3.atCenterOf(pos); + if (APAddons.vs2Loaded) { + Ship ship = APAddons.getVS2Ship(level, pos); + if (ship != null) { + Matrix4dc matrix = ship.getShipToWorld(); + Vector3d newPos = matrix.transformPosition(new Vector3d(position.x, position.y, position.z)); + Vector3d newDir = matrix.transformDirection(new Vector3d(direction.x, direction.y, direction.z)); + position = new Vec3(newPos.x, newPos.y, newPos.z); + direction = new Vec3(newDir.x, newDir.y, newDir.z); + } + } + player.lookAt(EntityAnchorArgument.Anchor.FEET, position.add(direction)); + player.moveTo(position.x, position.y, position.z, player.getYRot(), player.getXRot()); + // Player inventory Inventory playerInventory = player.getInventory(); playerInventory.selected = 0; @@ -55,9 +77,9 @@ public static void load(APFakePlayer player, ITurtleAccess turtle) { // Add properties ItemStack activeStack = player.getItemInHand(InteractionHand.MAIN_HAND); - if (!activeStack.isEmpty()) + if (!activeStack.isEmpty()) { player.getAttributes().addTransientAttributeModifiers(activeStack.getAttributeModifiers(EquipmentSlot.MAINHAND)); - + } } public static void unload(APFakePlayer player, ITurtleAccess turtle) { diff --git a/src/main/java/de/srendi/advancedperipherals/lib/peripherals/BasePeripheral.java b/src/main/java/de/srendi/advancedperipherals/lib/peripherals/BasePeripheral.java index 342ffb10e..5ba11f4fd 100644 --- a/src/main/java/de/srendi/advancedperipherals/lib/peripherals/BasePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/lib/peripherals/BasePeripheral.java @@ -132,23 +132,23 @@ public final Map getConfiguration() { return getPeripheralConfiguration(); } - protected BlockPos getPos() { + public BlockPos getPos() { return owner.getPos(); } - protected Vec3 getCenterPos() { + public Vec3 getCenterPos() { return owner.getCenterPos(); } - protected Level getLevel() { + public Level getLevel() { return owner.getLevel(); } - protected boolean isOnShip() { + public boolean isOnShip() { return APAddons.vs2Loaded && APAddons.isBlockOnShip(owner.getLevel(), owner.getPos()); } - protected Vec3 getWorldPos() { + public Vec3 getWorldPos() { Vec3 pos = this.getCenterPos(); if (!APAddons.vs2Loaded) { return pos; @@ -161,7 +161,7 @@ protected Vec3 getWorldPos() { return new Vec3(newPos.x, newPos.y, newPos.z); } - protected final BlockPos getWorldBlockPos() { + public final BlockPos getWorldBlockPos() { return new BlockPos(this.getWorldPos()); } From 00f327680e80a91455b6577d78489971e0ca4efc Mon Sep 17 00:00:00 2001 From: zyxkad Date: Tue, 31 Dec 2024 23:43:41 -0700 Subject: [PATCH 3/6] remove unused import --- .../de/srendi/advancedperipherals/common/util/LuaConverter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java index 51ea7661a..f595127e7 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java @@ -29,7 +29,6 @@ import org.joml.Vector3d; import org.joml.Vector3dc; import org.valkyrienskies.core.api.ships.ServerShip; -import org.valkyrienskies.core.api.ships.Ship; import org.valkyrienskies.core.api.ships.properties.ShipInertiaData; import org.valkyrienskies.core.api.ships.properties.ShipTransform; From aa1e2bfe238e7502f607fd8d9a5cd0b4998dadd2 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Tue, 31 Dec 2024 23:47:15 -0700 Subject: [PATCH 4/6] update github workflow --- .github/workflows/build-and-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index f3c7b7f7e..f4379176b 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -3,7 +3,7 @@ name: Build and Test on: workflow_dispatch: push: - pull_request_target: + pull_request: types: - opened - synchronize From 263d02c3a80779075da173e0d3904e414cd1b784 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Tue, 31 Dec 2024 23:51:57 -0700 Subject: [PATCH 5/6] remove more unused imports --- .../peripheral/metaphysics/WeakAutomataCorePeripheral.java | 1 - .../common/addons/valkyrienskies/AutomataVSMountPlugin.java | 1 - .../common/addons/valkyrienskies/ShipScannerPlugin.java | 1 - .../common/util/fakeplayer/APFakePlayer.java | 4 ---- .../common/util/fakeplayer/FakePlayerProviderTurtle.java | 3 +-- 5 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java index dc0ac06d7..ea46ec4fa 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java @@ -2,7 +2,6 @@ import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; -import de.srendi.advancedperipherals.common.addons.APAddons; import de.srendi.advancedperipherals.common.addons.computercraft.operations.AutomataCoreTier; import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins.*; import de.srendi.advancedperipherals.common.configuration.APConfig; diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java index e6699476b..fa99b51ca 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java @@ -15,7 +15,6 @@ import net.minecraft.world.phys.Vec3; import org.joml.Vector3d; import org.valkyrienskies.core.api.ships.ServerShip; -import org.valkyrienskies.core.api.ships.Ship; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java index 6450eec51..ee0786c76 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/ShipScannerPlugin.java @@ -12,7 +12,6 @@ import net.minecraft.world.phys.Vec3; import org.valkyrienskies.core.api.ships.ServerShip; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java index 4068709db..3f3e66887 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java @@ -24,8 +24,6 @@ import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; -import net.minecraft.world.level.BlockGetter; -import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -34,7 +32,6 @@ import net.minecraft.world.level.block.entity.SignBlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.*; -import net.minecraft.world.phys.shapes.Shapes; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.ForgeMod; import net.minecraftforge.common.util.FakePlayer; @@ -44,7 +41,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.lang.ref.WeakReference; import java.util.List; import java.util.UUID; import java.util.function.Function; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java index 60946c0b2..26fd8d024 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java @@ -6,14 +6,13 @@ import de.srendi.advancedperipherals.common.addons.APAddons; import net.minecraft.commands.arguments.EntityAnchorArgument; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.phys.Vec3;; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.wrapper.InvWrapper; import org.joml.Matrix4dc; From 087f783678f7e93888d86b7b9559fd41389babbe Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 1 Jan 2025 11:16:20 -0700 Subject: [PATCH 6/6] add getCurrentShip method --- .../plugins/AutomataLookPlugin.java | 13 ++++++++- .../valkyrienskies/AutomataVSMountPlugin.java | 15 +++++++++- .../common/util/LuaConverter.java | 28 +++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java index 6bf4759ad..3aa10fd87 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java @@ -5,10 +5,12 @@ 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; @@ -16,6 +18,7 @@ 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; @@ -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 data = new HashMap<>(); ResourceLocation blockName = ForgeRegistries.BLOCKS.getKey(state.getBlock()); data.put("name", blockName == null ? null : blockName.toString()); @@ -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); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java index fa99b51ca..758b46a85 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/valkyrienskies/AutomataVSMountPlugin.java @@ -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; @@ -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; @@ -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 data = LuaConverter.shipToObjectOnShip(ship, this.automataCore.getCenterPos()); + return MethodResult.of(data); + } + @LuaFunction(mainThread = true) public final MethodResult canMountToShip() { List ships = this.getMountableShips(); @@ -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 getMountableShips() { diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java index f595127e7..2429d3b36 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java @@ -292,6 +292,10 @@ public static Object effectToObject(MobEffectInstance effect) { return map; } + public static Map shipToObject(ServerShip ship) { + return shipToObject(ship, null); + } + public static Map shipToObject(ServerShip ship, Vec3 pos) { Map map = new HashMap<>(); @@ -300,11 +304,13 @@ public static Map 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)); @@ -327,8 +333,20 @@ public static Map 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 shipToObjectOnShip(ServerShip ship, Vec3 pos) { + Map 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; } }