Skip to content

Commit 54fde73

Browse files
committed
rename portal warp methods to preparePortalWarp & activePortalWarp
as well as other clean ups in the warp logic
1 parent 529cc39 commit 54fde73

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataWarpingPlugin.java‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ public final MethodResult distanceToPoint(String name) {
198198
* facing = "north", -- where will the turtle face after teleport
199199
* costs = 10000, -- the costs to cross the portal
200200
* canSpawn = true, -- if the target position is not blocked and turtle are able to spawn there
201-
* shipId = "xxx", -- the random id used for ship the portal. You have to call portalShipActive(shipId) if needed.
201+
* warpId = "xxx", -- the random id used for ship the portal. You have to call activePortalWarp(warpId) if needed.
202202
* }
203203
* </code>
204204
* </pre>
205205
*/
206206
@LuaFunction
207-
public final MethodResult portalShipPrepare(IArguments arguments) throws LuaException {
207+
public final MethodResult preparePortalWarp(IArguments arguments) throws LuaException {
208208
Direction direction;
209209
switch (arguments.optString(0).orElse("").toLowerCase(Locale.ROOT)) {
210210
case "up", "top" -> direction = Direction.UP;
@@ -218,20 +218,20 @@ public final MethodResult portalShipPrepare(IArguments arguments) throws LuaExce
218218
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
219219
ITurtleAccess turtle = owner.getTurtle();
220220
TurtleEnderPearl shipPearl = new TurtleEnderPearl(turtle, direction);
221-
String shipId = shipPearl.getStringUUID();
221+
String warpId = shipPearl.getStringUUID();
222222
ServerWorker.add(() -> {
223223
MethodResult res;
224224
try {
225225
res = automataCore.withOperation(PREPARE_PORTAL, new SingleOperationContext(1, 1), context -> {
226-
shipPearl.setCallback(pearl -> {
226+
shipPearl.setCallbacks(pearl -> {
227227
if (pearl == null || pearl.isRemoved()) {
228-
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, shipId, "PEARL_GONE");
229-
shipPearls.remove(shipId);
228+
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, warpId, "PEARL_GONE");
229+
shipPearls.remove(warpId);
230230
return;
231231
}
232232
Level level = pearl.level();
233233
if (level == turtle.getLevel()) {
234-
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, shipId, "NO_PORTAL_FOUND");
234+
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, warpId, "NO_PORTAL_FOUND");
235235
pearl.discard();
236236
return;
237237
}
@@ -242,11 +242,12 @@ public final MethodResult portalShipPrepare(IArguments arguments) throws LuaExce
242242
"facing", pearl.getDirection().getName(),
243243
"costs", getCostsToLevel(level.dimension()),
244244
"canSpawn", owner.isMovementPossible(level, pos),
245-
"shipId", shipId
245+
"warpId", warpId
246246
);
247-
shipPearls.put(shipId, pearl);
247+
shipPearls.values().removeIf(TurtleEnderPearl::isRemoved);
248+
shipPearls.put(warpId, pearl);
248249
automataCore.queueEvent(PortalPrepareCallback.EVENT_ID, data);
249-
});
250+
}, shipPearls::remove);
250251
turtle.getLevel().addFreshEntity(shipPearl);
251252
return null;
252253
}, null);
@@ -255,18 +256,18 @@ public final MethodResult portalShipPrepare(IArguments arguments) throws LuaExce
255256
}
256257
if (res != null) {
257258
Object err = res.getResult()[1];
258-
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, shipId, err);
259+
automataCore.queueEvent(PortalPrepareCallback.FAILED_EVENT_ID, warpId, err);
259260
}
260261
});
261-
return new PortalPrepareCallback(shipId).pull;
262+
return new PortalPrepareCallback(warpId).pull;
262263
}
263264

264265
/**
265-
* @param id the random shipId, one of the result from portalShipPrepare()
266+
* @param id the random warpId from the result of preparePortalWarp()
266267
* @return true | nil, string
267268
*/
268269
@LuaFunction(mainThread = true)
269-
public final MethodResult portalShipActive(String id) throws LuaException {
270+
public final MethodResult activePortalWarp(String id) throws LuaException {
270271
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
271272
TurtleEnderPearl shipPearl = shipPearls.get(id);
272273
if (shipPearl == null) {
@@ -279,12 +280,12 @@ public final MethodResult portalShipActive(String id) throws LuaException {
279280
Level level = shipPearl.level();
280281
BlockPos newPosition = shipPearl.blockPosition();
281282
return automataCore.withOperation(ACTIVE_PORTAL, new SingleOperationContext(getCostsToLevel(level.dimension()), 1), context -> {
283+
shipPearls.remove(id);
282284
shipPearl.discard();
283285
boolean result = owner.move(level, newPosition);
284286
if (!result) {
285287
return MethodResult.of(null, "Cannot teleport to location");
286288
}
287-
shipPearls.remove(id);
288289
return MethodResult.of(true);
289290
}, context -> {
290291
if (!owner.isMovementPossible(level, newPosition)) {
@@ -330,7 +331,7 @@ public MethodResult resume(Object[] datas) {
330331
if (!EVENT_ID.equals(datas[0]) || datas.length != 2) {
331332
return pull;
332333
}
333-
if (!(datas[1] instanceof Map<?, ?> data) || !id.equals(data.get("shipId"))) {
334+
if (!(datas[1] instanceof Map<?, ?> data) || !id.equals(data.get("warpId"))) {
334335
return pull;
335336
}
336337
return MethodResult.of(data);

‎src/main/java/de/srendi/advancedperipherals/common/entity/TurtleEnderPearl.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class TurtleEnderPearl extends ThrowableProjectile {
3333
private ITurtleAccess turtle = null;
3434
private BlockPos spawnPos = null;
3535
private Consumer<TurtleEnderPearl> callback = null;
36+
private Consumer<String> onExpire = null;
3637
private int life = 20;
3738
private boolean changedDim = false;
3839

@@ -58,8 +59,9 @@ public ITurtleAccess getTurtle() {
5859
return turtle;
5960
}
6061

61-
public void setCallback(Consumer<TurtleEnderPearl> callback) {
62+
public void setCallbacks(Consumer<TurtleEnderPearl> callback, Consumer<String> onExpire) {
6263
this.callback = callback;
64+
this.onExpire = onExpire;
6365
}
6466

6567
@Override
@@ -88,6 +90,9 @@ public void tick() {
8890
// clean after 5s
8991
if (this.life < -100) {
9092
this.discard();
93+
if (this.onExpire != null) {
94+
this.onExpire.accept(this.getStringUUID());
95+
}
9196
return;
9297
}
9398
return;

‎src/main/java/de/srendi/advancedperipherals/common/setup/APItems.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import de.srendi.advancedperipherals.lib.annotation.DefaultTranslation;
1919
import net.minecraft.world.item.ArmorMaterials;
2020
import net.minecraft.world.item.Item;
21-
import net.minecraft.world.item.Items;
2221
import net.neoforged.neoforge.registries.DeferredHolder;
2322

2423
public class APItems {

0 commit comments

Comments
 (0)