TARDIM factory closed due to supply chain issues

This commit is contained in:
Andrey Nikitin 2023-01-28 17:01:23 +03:00
parent ff96023b56
commit 3922140f13
2 changed files with 82 additions and 41 deletions

View file

@ -6,6 +6,7 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.lua.ObjectLuaTable;
import dan200.computercraft.api.lua.LuaException;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
@ -61,22 +62,75 @@ public class CCPeripheral implements IPeripheral {
}
// Get TARDIM's data, which we need for *every* function
// TODO: How do we check if there is no TARDIM?!
public TardimData getTardimData() {
return TardimManager.getFromPos(getTileEntity().getPos());
/* Get TARDIM's data, which we need for *every* function
*
* We can't do a simple
* TardimManager.getFromPos(getTileEntity().getPos())
* because if someone attempts to call a method outside a TARDIM, this would create a new TARDIM/Point to the one with ID of 0 (Due to the way TardimSaveHandler.loadTardisData works).
* Which is obviously not what we want.
*
* So instead we use this, and recieve ability to give user a LuaException if they think that fiddling with time devices is funny
* This is mostly a copy of getIDForXZ function */
public TardimData getTardimData() throws LuaException {
int X = getTileEntity().getPos().getX(), Z = getTileEntity().getPos().getZ();
int index = 0;
int x = 0;
int y = 0;
int dx = 0;
int dy = 1;
int segment_length = 1;
int segment_passed = 0;
boolean found = false;
long timecheck = System.currentTimeMillis();
while(true) {
if (System.currentTimeMillis() - timecheck > 10000L) {
System.out.println("Finding ID from XZ Coordinates is taking too long!");
break;
}
if (X >= x * TardimManager.INTERIOR_BOUNDS
&& X <= TardimManager.INTERIOR_BOUNDS + x * TardimManager.INTERIOR_BOUNDS
&& Z >= y * TardimManager.INTERIOR_BOUNDS
&& Z <= TardimManager.INTERIOR_BOUNDS + y * TardimManager.INTERIOR_BOUNDS) {
found = true;
break;
}
x += dx;
y += dy;
if (++segment_passed == segment_length) {
segment_passed = 0;
int buffer = dy;
dy = -dx;
dx = buffer;
if (buffer == 0) {
++segment_length;
}
}
++index;
}
// We really don't want to create a new TARDIM if we are not inside one, do we?
if (!found) {
throw new LuaException("Peripheral is not inside a TARDIM");
}
return TardimManager.getTardim(index);
}
// Peripheral methods ===============================================================
// Get amount of fuel we have (Out of 100)
@LuaFunction(mainThread = true)
public final double get_fuel() {
public final double get_fuel() throws LuaException {
return getTardimData().getFuel();
}
@LuaFunction(mainThread = true)
public final double calculate_fuel() {
public final double calculate_fuel() throws LuaException {
TardimData data = getTardimData();
if (data.getTravelLocation() == null) return 0;
@ -96,17 +150,17 @@ public class CCPeripheral implements IPeripheral {
// Check whether the TARDIM is locked
@LuaFunction(mainThread = true)
public final boolean is_locked() {
public final boolean is_locked() throws LuaException {
return getTardimData().isLocked();
}
// Check whether the TARDIM is in flight
@LuaFunction(mainThread = true)
public final boolean is_in_flight() { return getTardimData().isInFlight(); }
public final boolean is_in_flight() throws LuaException { return getTardimData().isInFlight(); }
// Supposedly gets UNIX timestamp of when we entered flight
@LuaFunction(mainThread = true)
public final long get_time_entered_flight() {
public final long get_time_entered_flight() throws LuaException {
TardimData data = getTardimData();
if (!data.isInFlight()) {
return -1;
@ -117,20 +171,20 @@ public class CCPeripheral implements IPeripheral {
// Get username of the TARDIM's owner
@LuaFunction(mainThread = true)
public final String get_owner_name() {
public final String get_owner_name() throws LuaException {
TardimData data = getTardimData();
return data.getOwnerName();
}
// Lock/Unlock the TARDIM
@LuaFunction(mainThread = true)
public final void set_locked(boolean locked) {
public final void set_locked(boolean locked) throws LuaException {
getTardimData().setLocked(locked);
}
// Returns table with current TARDIM location
@LuaFunction(mainThread = true)
public final ObjectLuaTable get_current_location() {
public final ObjectLuaTable get_current_location() throws LuaException {
Location loc = getTardimData().getCurrentLocation();
return new ObjectLuaTable(Map.of(
"dimension", loc.getLevel().location().toString(),
@ -145,7 +199,7 @@ public class CCPeripheral implements IPeripheral {
// Returns flight destination (or null if there isn't one)
@LuaFunction(mainThread = true)
public final ObjectLuaTable get_flight_location() {
public final ObjectLuaTable get_flight_location() throws LuaException {
TardimData data = getTardimData();
if (data.getTravelLocation() != null) {
Location loc = data.getTravelLocation();