Skip to content
Open
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
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ repositories {
includeModule("thedarkcolour", "kotlinforforge")
}
}
maven {
name = "RyanHCode Maven"
url = "https://maven.ryanhcode.dev/releases"
}
exclusiveContent {
forRepository {
maven {
Expand Down Expand Up @@ -249,6 +253,13 @@ dependencies {

// Minimal requirements end

// Sable Companion
jarJar(api("dev.ryanhcode.sable-companion:sable-companion-common-1.21.1:[${sable_companion_version},)")) {
version {
prefer project.sable_companion_version
}
}

compileOnly "com.refinedmods.refinedstorage:refinedstorage-neoforge:${refinedstorage_version}"
if (!minimumRuntime) {
runtimeOnly("com.refinedmods.refinedstorage:refinedstorage-neoforge:${refinedstorage_version}") {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ patchouli_version=1.21-88
powah_version=6143661
refinedstorage_mekanism_version=1.1.0
refinedstorage_version=2.0.0
sable_companion_version=1.6.0

# Mod dependencies which are needed for other mods
# dimstorage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum APAddon {
PATCHOULI("patchouli"),
POWAH("powah"),
REFINEDSTORAGE("refinedstorage"),
REFINEDSTORAGE_MEKANISM("refinedstorage_mekanism_integration");
REFINEDSTORAGE_MEKANISM("refinedstorage_mekanism_integration"),
SABLE("sable");

private final String modId;
private boolean loaded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IPeripheral;
import de.srendi.advancedperipherals.common.addons.sable.SableHelper;
import de.srendi.advancedperipherals.common.util.StringUtil;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
Expand Down Expand Up @@ -62,22 +63,23 @@ default Vec3 getCenterPos() {

@NotNull
default Vec3 getPhysicsPos() {
Level level = this.getLevel();
Vec3 pos = this.getCenterPos();
return pos;
// if (!APAddons.vs2Loaded) {
// return pos;
// }
// return ValkyrienSkies.transformToWorldPos(getLevel(), getPos(), pos);
if (level == null || pos == null) {
return pos;
}
return SableHelper.projectOutOfSubLevel(level, pos);
}

@NotNull
default Vec3 getDirection() {
Vec3 dir = Vec3.atLowerCornerOf(getFacing().getNormal());
return dir;
// if (!APAddons.vs2Loaded) {
// return dir;
// }
// return ValkyrienSkies.transformToWorldDir(getLevel(), getPos(), dir);
Level level = this.getLevel();
Vec3 center = this.getCenterPos();
if (level == null || center == null) {
return dir;
}
return SableHelper.transformDirectionOutOfSubLevel(level, getPos(), dir);
}

@NotNull Direction getFacing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,6 @@ private IColony getColonyOrThrow() throws LuaException {
@Nullable
private IColony getColonyWithoutPermission() {
IMinecoloniesAPI api = IMinecoloniesAPI.getInstance();
return api.getColonyManager().getColonyByPosFromWorld(getLevel(), getPos());
return api.getColonyManager().getColonyByPosFromWorld(getLevel(), getPhysicsBlockPos());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,23 @@ public final List<String> getPlayersInCoords(Map<?, ?> firstCoord, Map<?, ?> sec
BlockPos secondPos = LuaConverter.convertToBlockPos(secondCoord);

return getPlayers()
.filter(player -> CoordUtil.isInRange(getCenterPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE))
.filter(player -> CoordUtil.isInRange(getPhysicsPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE))
.map(player -> player.getGameProfile().getName())
.toList();
}

@LuaFunction(mainThread = true)
public final List<String> getPlayersInCubic(int x, int y, int z) {
return getPlayers()
.filter(player -> CoordUtil.isInRange(getCenterPos(), getLevel(), player, x, y, z, MAX_RANGE))
.filter(player -> CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, x, y, z, MAX_RANGE))
.map(player -> player.getGameProfile().getName())
.toList();
}

@LuaFunction(mainThread = true)
public final List<String> getPlayersInRange(int range) {
return getPlayers()
.filter(player -> CoordUtil.isInRange(getCenterPos(), getLevel(), player, range, MAX_RANGE))
.filter(player -> CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, range, MAX_RANGE))
.map(player -> player.getGameProfile().getName())
.toList();
}
Expand All @@ -124,19 +124,19 @@ public final boolean isPlayersInCoords(Map<?, ?> firstCoord, Map<?, ?> secondCoo
BlockPos secondPos = LuaConverter.convertToBlockPos(secondCoord);

return getPlayers()
.anyMatch(player -> CoordUtil.isInRange(getCenterPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE));
.anyMatch(player -> CoordUtil.isInRange(getPhysicsPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE));
}

@LuaFunction(mainThread = true)
public final boolean isPlayersInCubic(int x, int y, int z) {
return getPlayers()
.anyMatch(player -> CoordUtil.isInRange(getCenterPos(), getLevel(), player, x, y, z, MAX_RANGE));
.anyMatch(player -> CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, x, y, z, MAX_RANGE));
}

@LuaFunction(mainThread = true)
public final boolean isPlayersInRange(int range) {
return getPlayers()
.anyMatch(player -> CoordUtil.isInRange(getCenterPos(), getLevel(), player, range, MAX_RANGE));
.anyMatch(player -> CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, range, MAX_RANGE));
}

@LuaFunction(mainThread = true)
Expand All @@ -145,19 +145,19 @@ public final boolean isPlayerInCoords(Map<?, ?> firstCoord, Map<?, ?> secondCoor
BlockPos secondPos = LuaConverter.convertToBlockPos(secondCoord);

ServerPlayer player = getPlayer(username);
return player != null && CoordUtil.isInRange(getCenterPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE);
return player != null && CoordUtil.isInRange(getPhysicsPos(), player, getLevel(), firstPos, secondPos, MAX_RANGE);
}

@LuaFunction(mainThread = true)
public final boolean isPlayerInCubic(int x, int y, int z, String username) {
ServerPlayer player = getPlayer(username);
return player != null && CoordUtil.isInRange(getCenterPos(), getLevel(), player, x, y, z, MAX_RANGE);
return player != null && CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, x, y, z, MAX_RANGE);
}

@LuaFunction(mainThread = true)
public final boolean isPlayerInRange(int range, String username) {
ServerPlayer player = getPlayer(username);
return player != null && CoordUtil.isInRange(getCenterPos(), getLevel(), player, range, MAX_RANGE);
return player != null && CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, range, MAX_RANGE);
}

@LuaFunction(value = "getPlayer", mainThread = true)
Expand All @@ -168,7 +168,7 @@ public final Map<String, Object> getPlayerLua(String playerName, Optional<Boolea
if (player == null) {
return null;
}
if (MAX_RANGE != -1 && !CoordUtil.isInRange(getCenterPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
if (MAX_RANGE != -1 && !CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
return null;
}
return getPlayerInfo(player, player == owner.getHoldingEntity(), detailed.orElse(false));
Expand Down Expand Up @@ -248,7 +248,7 @@ public final MethodResult getPlayerStat(IArguments arguments) throws LuaExceptio
if (player == null) {
return Errors.PLAYER_NOT_EXISTS_RESULT;
}
if (MAX_RANGE != -1 && !CoordUtil.isInRange(getCenterPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
if (MAX_RANGE != -1 && !CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
return Errors.PLAYER_NOT_EXISTS_RESULT;
}

Expand Down Expand Up @@ -353,7 +353,7 @@ public void update() {
lastConsumedMessage = Events.traversePlayerMessages(lastConsumedMessage, message -> {
if (message.restrictedRange() && MAX_RANGE != -1) {
ServerPlayer player = level.getServer().getPlayerList().getPlayer(message.playerId());
if (player == null || !CoordUtil.isInRange(getCenterPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
if (player == null || !CoordUtil.isInRange(getPhysicsPos(), getLevel(), player, MAX_RANGE, MAX_RANGE)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.srendi.advancedperipherals.common.addons.sable;

import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.EnvironmentDetectorPeripheral;

public class Integration implements Runnable {

@Override
public void run() {
EnvironmentDetectorPeripheral.addIntegrationPlugin(ShipScannerPlugin::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package de.srendi.advancedperipherals.common.addons.sable;

import dev.ryanhcode.sable.companion.SableCompanion;
import dev.ryanhcode.sable.companion.SubLevelAccess;
import dev.ryanhcode.sable.companion.math.BoundingBox3d;
import dev.ryanhcode.sable.companion.math.BoundingBox3dc;
import dev.ryanhcode.sable.companion.math.Pose3dc;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Position;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.joml.Quaterniondc;
import org.joml.Vector3d;
import org.joml.Vector3dc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class SableHelper {

private SableHelper() {}

public static Vec3 projectOutOfSubLevel(Level level, Vec3 pos) {
return SableCompanion.INSTANCE.projectOutOfSubLevel(level, (Position) pos);
}

public static Vec3 transformDirectionOutOfSubLevel(Level level, BlockPos pos, Vec3 direction) {
SubLevelAccess subLevel = SableCompanion.INSTANCE.getContaining(level, pos);
if (subLevel == null) {
return direction;
}
return subLevel.logicalPose().transformNormal(direction);
}

public static boolean isOnSubLevel(Level level, BlockPos pos) {
return SableCompanion.INSTANCE.getContaining(level, pos) != null;
}

public static Map<String, Object> getContainingSubLevel(Level level, BlockPos pos, Vec3 center) {
SubLevelAccess subLevel = SableCompanion.INSTANCE.getContaining(level, pos);
if (subLevel == null) {
return null;
}
return subLevelToObject(level, subLevel, center);
}

public static List<Map<String, Object>> scanSubLevels(Level level, Vec3 center, double radius) {
BoundingBox3d bounds = new BoundingBox3d(
center.x - radius, center.y - radius, center.z - radius,
center.x + radius, center.y + radius, center.z + radius);

List<Map<String, Object>> result = new ArrayList<>();
for (SubLevelAccess subLevel : SableCompanion.INSTANCE.getAllIntersecting(level, bounds)) {
result.add(subLevelToObject(level, subLevel, center));
}
return result;
}

private static Map<String, Object> subLevelToObject(Level level, SubLevelAccess subLevel, Vec3 center) {
Pose3dc pose = subLevel.logicalPose();
Vector3dc pos = pose.position();
Quaterniondc rot = pose.orientation();
BoundingBox3dc box = subLevel.boundingBox();
Vector3d velocity = SableCompanion.INSTANCE.getVelocity(level, new Vector3d(pos));

Map<String, Object> data = new HashMap<>();
data.put("id", subLevel.getUniqueId().toString());
String name = subLevel.getName();
if (name != null) {
data.put("name", name);
}
data.put("x", pos.x() - center.x);
data.put("y", pos.y() - center.y);
data.put("z", pos.z() - center.z);
data.put("rotate", Map.of("x", rot.x(), "y", rot.y(), "z", rot.z(), "w", rot.w()));
data.put("size", Map.of("x", box.maxX() - box.minX(), "y", box.maxY() - box.minY(), "z", box.maxZ() - box.minZ()));
data.put("velocity", Map.of("x", velocity.x(), "y", velocity.y(), "z", velocity.z()));
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.srendi.advancedperipherals.common.addons.sable;

import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperationContext;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheralPlugin;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;

import java.util.Map;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SphereOperation.SCAN_SHIPS;

public class ShipScannerPlugin extends BasePeripheralPlugin {

public ShipScannerPlugin(IPeripheralOwner owner) {
super(owner);
}

@Override
public IPeripheralOperation<?>[] getOperations() {
return new IPeripheralOperation[]{SCAN_SHIPS};
}

@LuaFunction(mainThread = true)
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 -> {
return MethodResult.of(SableHelper.scanSubLevels(owner.getLevel(), owner.getPhysicsPos(), context.getRadius()));
}, null);
}

@LuaFunction(mainThread = true)
public final Map<String, Object> getShip() throws LuaException {
Map<String, Object> ship = SableHelper.getContainingSubLevel(owner.getLevel(), owner.getPos(), owner.getPhysicsPos());
if (ship == null) {
throw new LuaException("There is no ship here");
}
return ship;
}

@LuaFunction
public final MethodResult scanShipCost(int radius) {
int estimatedCost = estimateShipCost(radius);
if (estimatedCost < 0) {
return MethodResult.of(null, "Radius exceeds max value");
}
return MethodResult.of(estimatedCost);
}

private static int estimateShipCost(int radius) {
if (radius <= SCAN_SHIPS.getMaxFreeRadius()) {
return 0;
}
if (radius > SCAN_SHIPS.getMaxCostRadius()) {
return -1;
}
return SCAN_SHIPS.getCost(SphereOperationContext.of(radius));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.srendi.advancedperipherals.common.addons.computercraft.owner.IPeripheralOwner;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.OperationAbility;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.PeripheralOwnerAbility;
import de.srendi.advancedperipherals.common.addons.sable.SableHelper;
import de.srendi.advancedperipherals.common.util.CoordUtil;
import de.srendi.advancedperipherals.common.util.inventory.ChemicalUtil;
import de.srendi.advancedperipherals.common.util.inventory.FluidUtil;
Expand Down Expand Up @@ -152,8 +153,7 @@ public Vec3 getCenterPos() {
}

public boolean isOnShip() {
return false;
// return APAddons.isBlockOnShip(owner.getLevel(), owner.getPos());
return SableHelper.isOnSubLevel(owner.getLevel(), owner.getPos());
}

public Vec3 getPhysicsPos() {
Expand Down