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

Add inspect method to robot HLAPI that returns name of block #108

Open
wants to merge 1 commit into
base: 1.18-forge
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions src/main/java/li/cil/oc2/common/entity/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import li.cil.oc2.api.bus.device.object.Parameter;
import li.cil.oc2.api.bus.device.provider.ItemDeviceQuery;
import li.cil.oc2.api.capabilities.TerminalUserProvider;
import li.cil.oc2.api.util.RobotOperationSide;
import li.cil.oc2.common.Config;
import li.cil.oc2.common.bus.AbstractDeviceBusElement;
import li.cil.oc2.common.bus.CommonDeviceBusController;
Expand Down Expand Up @@ -866,6 +867,17 @@ public ItemStack getStackInSlot(@Parameter("slot") final int slot) {
return inventory.getStackInSlot(slot);
}

@Callback(synchronize = false)
public String inspect(@Parameter("side") @Nullable final RobotOperationSide side) {
if (side == null) throw new IllegalArgumentException();
final Level level = Robot.this.level;
if (!(level instanceof final ServerLevel serverLevel)) {
throw new IllegalArgumentException();
}
final BlockPos pos = Robot.this.blockPosition();
return level.getBlockState(pos.relative(RobotOperationSide.toGlobal(Robot.this, side))).getBlock().getDescriptionId();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we maybe want to explicitly check if the block is "air" and return null instead if that's the case?

}

@Callback(synchronize = false)
public boolean move(@Parameter("direction") @Nullable final MovementDirection direction) {
if (direction == null) throw new IllegalArgumentException();
Expand Down
5 changes: 5 additions & 0 deletions src/main/scripts/lib/lua/robot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ M.turnAsync = function(direction)
end
end

M.inspect = function(direction)
direction = assert(direction, "no direction specified")
return robot:inspect(direction)
end

return M
5 changes: 5 additions & 0 deletions src/main/scripts/lib/micropython/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def turn_async(self, direction):
while not self.device.turn(direction):
time.sleep(1)

def inspect(self, direction):
if not direction:
raise Exception("no direction specified")
return self.device.inspect(direction)

def _wait_for_last_action(self):
id = self.device.getLastActionId()
result = self.device.getActionResult(id)
Expand Down