Skip to content

Add getPositionRemote, addPeripheral APIs and fix direction handling after reload - #66

Open
vladaliii wants to merge 1 commit into
SirEdvin:1.20from
vladaliii:feature/PosAPI
Open

Add getPositionRemote, addPeripheral APIs and fix direction handling after reload#66
vladaliii wants to merge 1 commit into
SirEdvin:1.20from
vladaliii:feature/PosAPI

Conversation

@vladaliii

Copy link
Copy Markdown

While playing with this mod, I felt that as a mod designed to enhance the CC:T experience, it could potentially offer a higher degree of automation efficiency by making the peripheral binding process fully self-contained within CC:T. Since binding a peripheral is already cost-free, I believe this does not affect the mod's balance.

Overview

This PR introduces two new Lua APIs for the Peripheral Proxy (getPositionRemote and addPeripheral) and fixes a bug where bound peripherals would always connect from the north after a world reload.

Changes

1. New API: getPositionRemote(name)

Location: PeripheralProxyPeripheral.kt ([line 25]
Allows querying the position and facing direction of a remote peripheral by its assigned name.

Usage:

local info = proxy.getPositionRemote("monitor_1")
Returns:

If the peripheral exists:

lua
{
    x = 100,
    y = 64,
    z = -20,
    direction = "north"
}
If the peripheral name is not registered: nil

This is a purely additive API and does not change any existing behavior.

2. New API: addPeripheral(position, direction)
Location: PeripheralProxyPeripheral.kt (same file as above)

Programmatically binds a peripheral at the given coordinates and facing direction. It reuses the core logic of the existing Configurator binding flow.

Usage:

lua
local success, result = proxy.addPeripheral(
    { x = 100, y = 64, z = -20 },
    "north"
)
Returns:

On success: true, "peripheral_name"

On failure: false, "failure_reason"

Checks performed internally:

Distance check

Capacity check

Forbidden binding tag check

Chunk loading check

Directional peripheral existence check

Synchronisation, persistence and mounting

This API only has an effect when called explicitly; it does not automatically alter any existing peripherals.

3. Fix: Direction handling after world reload
Location: PeripheralProxyBlockEntity.kt ([line 212]
Previous behavior:
When the world was reloaded, the code always attempted to fetch the peripheral from the north side:

kotlin
Direction.NORTH
New behavior:
The stored direction from the binding record is now used:

kotlin
record.direction
This is a bug fix. For blocks that expose a CC peripheral only on a specific side, connections will now be correctly restored after a reload.

Expected behavioral change:
A small number of bindings that were previously (incorrectly) connected from the north will now connect according to their actual saved direction. This is the intended and correct behavior.

**Motivation and Context**
The new APIs give players and programs more flexibility to inspect and manage remote peripheral bindings at runtime.

The direction bug prevented proxies from working reliably with directional peripherals across world reloads or dimension changes.

**How Has This Been Tested?**
getPositionRemote returns correct coordinates and direction for existing bindings, and nil for non-existent names.

addPeripheral successfully binds valid peripherals, and returns descriptive errors for invalid input (out of range, missing peripheral, blocked tag, etc.).

Existing bindings are unaffected unless explicitly modified via the new API.

After reloading the world, a proxy bound to a peripheral on the south side no longer incorrectly attempts to connect from the north; it reconnects using the saved direction.

**Checklist**
Code compiles and passes existing tests

New Lua APIs are documented (if applicable)

No breaking changes to existing APIs

@vladaliii

Copy link
Copy Markdown
Author

I am sorry for my description format. I forgot to close the code block : (

@SirEdvin

Copy link
Copy Markdown
Owner

Hey, I am sorry for such long review, I will merge it, but I will move it to upgraded version of peripheral proxy for gameplay reasons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants