Infinite Potential
This commit is contained in:
parent
f90500fdd4
commit
ff96023b56
1 changed files with 104 additions and 41 deletions
|
@ -3,31 +3,33 @@ package de.srendi.cctutorial.cctutorial;
|
||||||
import dan200.computercraft.api.lua.LuaFunction;
|
import dan200.computercraft.api.lua.LuaFunction;
|
||||||
import dan200.computercraft.api.peripheral.IComputerAccess;
|
import dan200.computercraft.api.peripheral.IComputerAccess;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import net.minecraft.network.chat.Component;
|
import dan200.computercraft.api.lua.ObjectLuaTable;
|
||||||
import net.minecraftforge.server.ServerLifecycleHooks;
|
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.TardimData;
|
||||||
import com.swdteam.tardim.TardimManager;
|
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.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Our peripheral class, this is the class where we will register functions for our block.
|
* Our peripheral class, this is the class where we will register functions for our block.
|
||||||
*/
|
*/
|
||||||
public class CCPeripheral implements IPeripheral {
|
public class CCPeripheral implements IPeripheral {
|
||||||
|
|
||||||
/**
|
private final List<IComputerAccess> connectedComputers = new ArrayList<>(); // List of computers connected to the peripheral
|
||||||
* A list of all our connected computers. We need this for event usages.
|
private final CCTileEntity tileEntity; // Peripheral's BlockEntity, used for accessing coordinates
|
||||||
*/
|
|
||||||
private final List<IComputerAccess> 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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param tileEntity the tile entity of this peripheral
|
* @param tileEntity the tile entity of this peripheral
|
||||||
|
@ -36,67 +38,128 @@ public class CCPeripheral implements IPeripheral {
|
||||||
this.tileEntity = tileEntity;
|
this.tileEntity = tileEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Setting name for the peripheral. A computer will see it as "digital_tardim_interface_n"
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getType() {
|
public String getType() { return "digital_tardim_interface"; }
|
||||||
return "test";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Apparently CC uses this to check if the peripheral in front of a modem is this one
|
||||||
* CC use this method to check, if the peripheral in front of the modem is our peripheral
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(@Nullable IPeripheral iPeripheral) {
|
public boolean equals(@Nullable IPeripheral iPeripheral) { return this == iPeripheral; }
|
||||||
return this == iPeripheral;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Called when a computer disconnects from the peripheral
|
||||||
* Will be called when a computer disconnects from our block
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void detach(@Nonnull IComputerAccess computer) {
|
public void detach(@Nonnull IComputerAccess computer) { connectedComputers.remove(computer); }
|
||||||
connectedComputers.remove(computer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Called when a computer connects to the peripheral
|
||||||
* Will be called when a computer connects to our block
|
// TODO: add a sound effect? Like a simple TARDIS beep?
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void attach(@Nonnull IComputerAccess computer) {
|
public void attach(@Nonnull IComputerAccess computer) { connectedComputers.add(computer); }
|
||||||
connectedComputers.add(computer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CCTileEntity getTileEntity() {
|
public CCTileEntity getTileEntity() {
|
||||||
return tileEntity;
|
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() {
|
public TardimData getTardimData() {
|
||||||
return TardimManager.getFromPos(getTileEntity().getPos());
|
return TardimManager.getFromPos(getTileEntity().getPos());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Peripheral methods ===============================================================
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
// Get amount of fuel we have (Out of 100)
|
||||||
@LuaFunction(mainThread = true)
|
@LuaFunction(mainThread = true)
|
||||||
public final double get_fuel() {
|
public final double get_fuel() {
|
||||||
return getTardimData().getFuel();
|
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)
|
@LuaFunction(mainThread = true)
|
||||||
public final boolean is_locked() {
|
public final boolean is_locked() {
|
||||||
return getTardimData().isLocked();
|
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)
|
@LuaFunction(mainThread = true)
|
||||||
public final void set_locked(boolean locked) {
|
public final void set_locked(boolean locked) {
|
||||||
getTardimData().setLocked(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue