From ff96023b56371d6734fa98fac5a40fa17481c437 Mon Sep 17 00:00:00 2001 From: Andrew71 Date: Sat, 28 Jan 2023 15:51:12 +0300 Subject: [PATCH] Infinite Potential --- .../cctutorial/cctutorial/CCPeripheral.java | 145 +++++++++++++----- 1 file changed, 104 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/srendi/cctutorial/cctutorial/CCPeripheral.java b/src/main/java/de/srendi/cctutorial/cctutorial/CCPeripheral.java index d90f78f..521d0c1 100644 --- a/src/main/java/de/srendi/cctutorial/cctutorial/CCPeripheral.java +++ b/src/main/java/de/srendi/cctutorial/cctutorial/CCPeripheral.java @@ -3,31 +3,33 @@ package de.srendi.cctutorial.cctutorial; import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IPeripheral; -import net.minecraft.network.chat.Component; -import net.minecraftforge.server.ServerLifecycleHooks; +import dan200.computercraft.api.lua.ObjectLuaTable; +import dan200.computercraft.api.lua.LuaException; +import net.minecraft.world.level.Level; + + +// TODO: Fabric and Forge diffirence? (Bottom: Fabric) import com.swdteam.tardim.TardimData; import com.swdteam.tardim.TardimManager; +import com.swdteam.tardim.TardimData.Location; +//import com.swdteam.tardim.tardim.TardimManager; +//import com.swdteam.tardim.tardim.TardimData; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * Our peripheral class, this is the class where we will register functions for our block. */ public class CCPeripheral implements IPeripheral { - /** - * A list of all our connected computers. We need this for event usages. - */ - private final List connectedComputers = new ArrayList<>(); - - /** - * This is our tile entity, we set the tile entity when we create a new peripheral. We use this tile entity to access the block or the world - */ - private final CCTileEntity tileEntity; + private final List connectedComputers = new ArrayList<>(); // List of computers connected to the peripheral + private final CCTileEntity tileEntity; // Peripheral's BlockEntity, used for accessing coordinates /** * @param tileEntity the tile entity of this peripheral @@ -36,67 +38,128 @@ public class CCPeripheral implements IPeripheral { this.tileEntity = tileEntity; } - /** - * We use getType to set the name for our peripheral. A modem would wrap our block as "test_n" - * - * @return the name of our peripheral - */ + // Setting name for the peripheral. A computer will see it as "digital_tardim_interface_n" @Nonnull @Override - public String getType() { - return "test"; - } + public String getType() { return "digital_tardim_interface"; } - /** - * CC use this method to check, if the peripheral in front of the modem is our peripheral - */ + // Apparently CC uses this to check if the peripheral in front of a modem is this one @Override - public boolean equals(@Nullable IPeripheral iPeripheral) { - return this == iPeripheral; - } + public boolean equals(@Nullable IPeripheral iPeripheral) { return this == iPeripheral; } - /** - * Will be called when a computer disconnects from our block - */ + // Called when a computer disconnects from the peripheral @Override - public void detach(@Nonnull IComputerAccess computer) { - connectedComputers.remove(computer); - } + public void detach(@Nonnull IComputerAccess computer) { connectedComputers.remove(computer); } - /** - * Will be called when a computer connects to our block - */ + // Called when a computer connects to the peripheral + // TODO: add a sound effect? Like a simple TARDIS beep? @Override - public void attach(@Nonnull IComputerAccess computer) { - connectedComputers.add(computer); - } + public void attach(@Nonnull IComputerAccess computer) { connectedComputers.add(computer); } public CCTileEntity getTileEntity() { return tileEntity; } + // 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()); } - /** - * To register functions for our block, wee need to create final methods with the {@link LuaFunction} annotation - * This function will send a message to every player on the Server - */ + // Peripheral methods =============================================================== + // Get amount of fuel we have (Out of 100) @LuaFunction(mainThread = true) public final double get_fuel() { return getTardimData().getFuel(); } + @LuaFunction(mainThread = true) + public final double calculate_fuel() { + TardimData data = getTardimData(); + + if (data.getTravelLocation() == null) return 0; + + Location curr = data.getCurrentLocation(); + Location dest = data.getTravelLocation(); + + double fuel = 0.0; + + if (curr.getLevel() != dest.getLevel()) + { + fuel = 10.0; + } + + return 100; //data.calculateFuelForJourney(((Level) curr.getLevel()), dest.getLevel().location(), curr.getPos(), dest.getPos()); + } + + // Check whether the TARDIM is locked @LuaFunction(mainThread = true) public final boolean is_locked() { return getTardimData().isLocked(); } + // Check whether the TARDIM is in flight + @LuaFunction(mainThread = true) + public final boolean is_in_flight() { return getTardimData().isInFlight(); } + + // Supposedly gets UNIX timestamp of when we entered flight + @LuaFunction(mainThread = true) + public final long get_time_entered_flight() { + TardimData data = getTardimData(); + if (!data.isInFlight()) { + return -1; + // ???? + } + return data.getTimeEnteredFlight(); + } + + // Get username of the TARDIM's owner + @LuaFunction(mainThread = true) + public final String get_owner_name() { + TardimData data = getTardimData(); + return data.getOwnerName(); + } + + // Lock/Unlock the TARDIM @LuaFunction(mainThread = true) public final void set_locked(boolean locked) { getTardimData().setLocked(locked); } + + // Returns table with current TARDIM location + @LuaFunction(mainThread = true) + public final ObjectLuaTable get_current_location() { + Location loc = getTardimData().getCurrentLocation(); + return new ObjectLuaTable(Map.of( + "dimension", loc.getLevel().location().toString(), + "pos", new ObjectLuaTable(Map.of( + "x", loc.getPos().getX(), + "y", loc.getPos().getY(), + "z", loc.getPos().getZ() + )), + "facing", loc.getFacing().toString() + )); + } + + // Returns flight destination (or null if there isn't one) + @LuaFunction(mainThread = true) + public final ObjectLuaTable get_flight_location() { + TardimData data = getTardimData(); + if (data.getTravelLocation() != null) { + Location loc = data.getTravelLocation(); + return new ObjectLuaTable(Map.of( + "dimension", loc.getLevel().location().toString(), + "pos", new ObjectLuaTable(Map.of( + "x", loc.getPos().getX(), + "y", loc.getPos().getY(), + "z", loc.getPos().getZ() + )), + "facing", loc.getFacing().toString() + )); + } else { + return null; + } + } }