Skip to content
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 @@ -13,7 +13,8 @@ public enum SingleOperation implements IPeripheralOperation<SingleOperationConte
SUCK(1000, 1),
USE_ON_ANIMAL(2500, 10),
CAPTURE_ANIMAL(50_000, 100),
WARP(1000, DistancePolicy.IGNORED, CountPolicy.MULTIPLY, 1, DistancePolicy.SQRT, CountPolicy.MULTIPLY);
WARP(1000, DistancePolicy.IGNORED, CountPolicy.MULTIPLY, 1, DistancePolicy.SQRT, CountPolicy.MULTIPLY),
ACCURE_PLACE(1000, DistancePolicy.IGNORED, CountPolicy.MULTIPLY, 1, DistancePolicy.LINEAR, CountPolicy.MULTIPLY);

private final int defaultCooldown;
private final DistancePolicy distanceCooldownPolicy;
Expand Down Expand Up @@ -74,6 +75,7 @@ public void addToConfig(ForgeConfigSpec.Builder builder) {

public enum DistancePolicy {
IGNORED(d -> 1),
LINEAR(d -> d),
SQRT(d -> (int) Math.sqrt(d));

private final Function<Integer, Integer> factorFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ public Direction getFacing() {
return turtle.getDirection();
}

/**
* Not used for turtles
*/
@NotNull
@Override
public FrontAndTop getOrientation() {
return FrontAndTop.NORTH_UP;
return FrontAndTop.fromFrontAndTop(getFacing(), Direction.UP);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.common.events.Events;
import de.srendi.advancedperipherals.common.util.CoordUtil;
import de.srendi.advancedperipherals.common.util.StringUtil;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralFunction;
import de.srendi.advancedperipherals.network.APNetworking;
Expand Down Expand Up @@ -121,9 +122,9 @@ public final MethodResult sendFormattedMessage(@NotNull IArguments arguments) th
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(1, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(1, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(2, "[]"),
arguments.optString(3, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(3, ""))
).append(component);
for (ServerPlayer player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers()) {
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
Expand All @@ -147,9 +148,9 @@ public final MethodResult sendMessage(@NotNull IArguments arguments) throws LuaE
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(1, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(1, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(2, "[]"),
arguments.optString(3, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(3, ""))
).append(message);
for (ServerPlayer player : ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers()) {
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
Expand Down Expand Up @@ -182,9 +183,9 @@ public final MethodResult sendFormattedMessageToPlayer(@NotNull IArguments argum
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(2, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(2, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(3, "[]"),
arguments.optString(4, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(4, ""))
).append(component);
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
return MethodResult.of(false, "NOT_SAME_DIMENSION");
Expand Down Expand Up @@ -222,9 +223,9 @@ public final MethodResult sendFormattedToastToPlayer(@NotNull IArguments argumen
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ,,,)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(3, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(3, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(4, "[]"),
arguments.optString(5, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(5, ""))
).append(messageComponent);

if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
Expand Down Expand Up @@ -256,9 +257,9 @@ public final MethodResult sendMessageToPlayer(@NotNull IArguments arguments) thr
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(2, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(2, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(3, "[]"),
arguments.optString(4, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(4, ""))
).append(message);
if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
return MethodResult.of(false, "NOT_SAME_DIMENSION");
Expand Down Expand Up @@ -287,9 +288,9 @@ public final MethodResult sendToastToPlayer(@NotNull IArguments arguments) throw
return MethodResult.of(null, "incorrect bracket string (e.g. [], {}, <>, ...)");

MutableComponent preparedMessage = appendPrefix(
arguments.optString(3, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get()).replaceAll("&", "\u00a7"),
StringUtil.convertAndToSectionMark(arguments.optString(3, APConfig.PERIPHERALS_CONFIG.defaultChatBoxPrefix.get())),
arguments.optString(4, "[]"),
arguments.optString(5, "").replaceAll("&", "\u00a7")
StringUtil.convertAndToSectionMark(arguments.optString(5, ""))
).append(message);

if (!APConfig.PERIPHERALS_CONFIG.chatBoxMultiDimensional.get() && player.getLevel().dimension() != dimension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public class CompassPeripheral extends BasePeripheral<TurtlePeripheralOwner> {

public static final String PERIPHERAL_TYPE = "compass";

protected CompassPeripheral(TurtlePeripheralOwner owner) {
super(PERIPHERAL_TYPE, owner);
}

public CompassPeripheral(ITurtleAccess turtle, TurtleSide side) {
super(PERIPHERAL_TYPE, new TurtlePeripheralOwner(turtle, side));
this(new TurtlePeripheralOwner(turtle, side));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,47 @@
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.core.apis.TableHelper;
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.CompassPeripheral;
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.common.util.Pair;
import de.srendi.advancedperipherals.common.util.StringUtil;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SignItem;
import net.minecraft.world.item.context.DirectionalPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.DIG;
import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.USE_ON_BLOCK;
import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.ACCURE_PLACE;

public class AutomataBlockHandPlugin extends AutomataCorePlugin {

Expand Down Expand Up @@ -71,4 +96,139 @@ public final MethodResult useOnBlock(@NotNull IArguments arguments) throws LuaEx
});
}

/**
* placeBlock method will let turtle place a block with more details when compass has equipped.
* It should not able to place fluids / use any item, because compass do not recognize them.
*
* @param options A table contains how to place the block:
* x: the x offset relative to the turtle. Default 0
* y: the y offset relative to the turtle. Default 0
* z: the z offset relative to the turtle. Default 0
* anchor: the direction the block is going to hanging on. Default is the direction of the turtle
* front: the direction the block is going to facing. Default is same as anchor
* top: the direction the block's top is going to facing. Default is TOP
* text: the text going to write on the sign. Default is null
*/
@LuaFunction(mainThread = true)
public MethodResult placeBlock(@NotNull Map<?, ?> options) throws LuaException {
ITurtleAccess turtle = automataCore.getPeripheralOwner().getTurtle();
CompassPeripheral compassPeripheral = Stream.of(TurtleSide.values()).map(side -> turtle.getPeripheral(side) instanceof CompassPeripheral compass ? compass : null).filter(peripheral -> peripheral != null).findFirst().orElse(null);
if (compassPeripheral == null || !compassPeripheral.isEnabled()) {
return MethodResult.of(false, "COMPASS_NOT_EQUIPPED");
}
int x = TableHelper.optIntField(options, "x", 0);
int y = TableHelper.optIntField(options, "y", 0);
int z = TableHelper.optIntField(options, "z", 0);
final int maxDist = APConfig.PERIPHERALS_CONFIG.compassAccurePlaceRadius.get();
final int freeDist = APConfig.PERIPHERALS_CONFIG.compassAccurePlaceFreeRadius.get();
if (Math.abs(x) > maxDist || Math.abs(y) > maxDist || Math.abs(z) > maxDist) {
return MethodResult.of(null, "OUT_OF_RANGE");
}
String anchor = TableHelper.optStringField(options, "anchor", null);
String front = TableHelper.optStringField(options, "front", null);
String top = TableHelper.optStringField(options, "top", null);
Direction anchorDir = anchor != null ? automataCore.validateSide(anchor) : null;
Direction frontDir = front != null ? automataCore.validateSide(front) : null;
Direction topDir = top != null ? automataCore.validateSide(top) : null;

int distance =
Math.max(0, Math.abs(x) - freeDist) +
Math.max(0, Math.abs(y) - freeDist) +
Math.max(0, Math.abs(z) - freeDist);
return automataCore.withOperation(ACCURE_PLACE, new SingleOperationContext(1, distance), context -> {
ItemStack stack = turtle.getInventory().getItem(turtle.getSelectedSlot());
if (stack.isEmpty()) {
return MethodResult.of(null, "EMPTY_SLOT");
}
BlockPos position = turtle.getPosition().offset(x, y, z);
String err = deployOn(stack, position, anchorDir, frontDir, topDir, options);
if (err != null) {
return MethodResult.of(null, err);
}
return MethodResult.of(true);
}, null);
}

/**
* @return A nullable string of the error. <code>null</code> means the operation is successful
*/
@Nullable
private String deployOn(ItemStack stack, BlockPos position, Direction anchor, Direction front, Direction top, Map<?, ?> options) throws LuaException {
ITurtleAccess turtle = automataCore.getPeripheralOwner().getTurtle();
Level world = turtle.getLevel();
if (anchor == null) {
anchor = turtle.getDirection();
}
if (front == null) {
front = anchor;
}
if (top == null) {
top = Direction.UP;
}
TurtlePlayer turtlePlayer = TurtlePlayer.getWithPosition(turtle, position, front.getOpposite());
BlockHitResult hit = BlockHitResult.miss(Vec3.atCenterOf(position), top, position);
AdvanceDirectionalPlaceContext context = new AdvanceDirectionalPlaceContext(world, position, anchor, front, stack, top);
PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock(turtlePlayer, InteractionHand.MAIN_HAND, position, hit);
if (event.isCanceled()) {
return "EVENT_CANCELED";
}
Item item = stack.getItem();
if (!(item instanceof BlockItem)) {
return "NOT_BLOCK";
}
BlockItem block = (BlockItem) item;
InteractionResult res = block.place(context);
if (!res.consumesAction()) {
return "CANNOT_PLACE";
}
if (block instanceof SignItem) {
BlockEntity blockEntity = world.getBlockEntity(position);
if (blockEntity instanceof SignBlockEntity sign) {
String text = StringUtil.convertAndToSectionMark(TableHelper.optStringField(options, "text", null));
setSignText(world, sign, text);
}
}
return null;
}

private static void setSignText(Level world, SignBlockEntity sign, String text) {
if (text == null) {
for (int i = 0; i < SignBlockEntity.LINES; i++) {
sign.setMessage(i, Component.literal(""));
}
} else {
String[] lines = text.split("\n");
for (int i = 0; i < SignBlockEntity.LINES; i++) {
sign.setMessage(i, Component.literal(i < lines.length ? lines[i] : ""));
}
}
sign.setChanged();
world.sendBlockUpdated(sign.getBlockPos(), sign.getBlockState(), sign.getBlockState(), Block.UPDATE_ALL);
}

private static class AdvanceDirectionalPlaceContext extends DirectionalPlaceContext {
private final Direction anchor;

AdvanceDirectionalPlaceContext(Level world, BlockPos pos, Direction anchor, Direction front, ItemStack stack, Direction top) {
super(world, pos, front, stack, top);
this.anchor = anchor;
}

@Override
public Direction getNearestLookingDirection() {
return this.anchor;
}

@Override
public Direction[] getNearestLookingDirections() {
return switch (this.anchor) {
case DOWN -> new Direction[]{Direction.DOWN, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.UP};
case UP -> new Direction[]{Direction.UP, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.DOWN};
case NORTH -> new Direction[]{Direction.NORTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.DOWN, Direction.SOUTH};
case SOUTH -> new Direction[]{Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.DOWN, Direction.NORTH};
case WEST -> new Direction[]{Direction.WEST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.DOWN, Direction.EAST};
case EAST -> new Direction[]{Direction.EAST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.DOWN, Direction.WEST};
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class GeneralConfig implements IAPConfig {

LibConfig.build(builder);

builder.pop();
builder.push("Unsafe");

UnsafeConfig.build(builder);

builder.pop();

configSpec = builder.build();
Expand Down
Loading