JavaDoc is an illusion, and pain is the magician

This commit is contained in:
Andrey Nikitin 2023-01-29 19:17:29 +03:00
parent 79ad4777b3
commit 0f377893d7
7 changed files with 244 additions and 47 deletions

View file

@ -12,9 +12,25 @@ This mod is an addon for the [TARDIM mod](https://www.curseforge.com/minecraft/m
All the methods can be found in the [API documentation](https:/google.com), and there are some examples in the [examples folder](examples) All the methods can be found in the [API documentation](https:/google.com), and there are some examples in the [examples folder](examples)
### Example usecases ### Example usecases
* Monitor to display fuel levels, current location, and other information on a screen in a nice way. * Monitor fuel levels, current location, and other information on a screen in a nice way.
* Way to remotely summon your TARDIM to you, or to a specific location. (Using chunkloaders) * Way to remotely summon your TARDIM to you, or to a specific location. (Using chunkloaders)
* Refined control of your TARDIM, such as saving and loading locations, or setting a destination in a GUI. * Refined control of your TARDIM, such as saving and loading locations, or setting a destination in a GUI.
* Visual effects that activate during flight. * Visual effects that activate during flight.
The possibilities are endless, the only limit is your imagination! (And coding skills) The possibilities are endless, the only limit is your imagination! (And coding skills)
### FAQ
Is this for Fabric or Forge?
: As a Fabric player who recognises Forge's large playerbase, I intend to support both major modloaders.
However a version for one of them could be released a bit later than the other one's.
Can I use this in my modpack?
: Sure, as long as you credit me and link to this page.
Will there be a 1.19.3 version and beyond?
: Yes, I will try my best to update to later versions as soon as ComputerCraft: Tweaked and TARDIM receive stable versions for them.
Will you support earlier versions?
: I am not very familiar with those and therefore cannot make versions for before 1.19,
however if there will be a lot of people asking for it I could try sometime in the future. But no gurantees.

View file

@ -0,0 +1,81 @@
local tardim = peripheral.find("digital_tardim_interface")
local screen = peripheral.find("monitor")
-- This requires Wojbie's bigfont
-- pastebin get 3LfWxRWh bigfont
local bigfont = require("bigfont")
screen.clear()
screen.setCursorBlink(false)
screen.setTextScale(0.5)
local owner = tardim.getOwnerName()
screen.setCursorPos(1, 1)
bigfont.writeOn(screen, 1, owner .. "'s TARDIM", 2, 2)
while true do
local fuel = tardim.getFuel()
local in_flight = tardim.isInFlight()
local loca_curr = tardim.getCurrentLocation()
local loca_dest = tardim.getTravelLocation()
screen.setCursorPos(1, 6)
bigfont.writeOn(screen, 1, "Fuel", 2, 6)
fuel = math.floor(fuel)
screen.write("")
local fuel_bar = "["
for i = 1, 50 do
if i * 2 <= fuel then
fuel_bar = fuel_bar .. "#"
else
fuel_bar = fuel_bar .. "."
end
end
fuel_bar = fuel_bar .. "]"
screen.setCursorPos(14, 6)
screen.write(fuel_bar)
screen.setCursorPos(14, 7)
screen.write(fuel_bar .. " " .. fuel .. "%")
screen.setCursorPos(14, 8)
screen.write(fuel_bar)
bigfont.writeOn(screen, 1, "Current position", 2, 10)
screen.setCursorPos(50, 10)
screen.write("X: " .. loca_curr.pos.x)
screen.setCursorPos(50, 11)
screen.write("Y: " .. loca_curr.pos.y)
screen.setCursorPos(50, 12)
screen.write("Z: " .. loca_curr.pos.z)
screen.setCursorPos(50, 13)
screen.write("Dim: " .. loca_curr.dimension)
bigfont.writeOn(screen, 1, "Destination", 2, 15)
screen.setCursorPos(50, 15)
screen.write("X: " .. loca_dest.pos.x)
screen.setCursorPos(50, 16)
screen.write("Y: " .. loca_dest.pos.y)
screen.setCursorPos(50, 17)
screen.write("Z: " .. loca_dest.pos.z)
screen.setCursorPos(50, 18)
screen.write("Dim: " .. loca_dest.dimension)
screen.setCursorPos(1, 20)
screen.clearLine()
screen.setCursorPos(1, 21)
screen.clearLine()
screen.setCursorPos(1, 22)
screen.clearLine()
if in_flight then
bigfont.blitOn(screen, 1, "In Flight", "000000000", "ddddddddd", 2, 20)
else
bigfont.blitOn(screen, 1, "Not In Flight", "0000000000000", "eeeeeeeeeeeee", 2, 20)
end
sleep(0.1)
end

View file

@ -27,6 +27,6 @@ public class DigitalInterfaceBlock extends Block implements EntityBlock {
@Nullable @Nullable
@Override @Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) { public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
return Registration.CC_TILEENTITY.get().create(pos, state); return Registration.DIGITAL_TARDIM_INTERFACE_TILEENTITY.get().create(pos, state);
} }
} }

View file

@ -8,11 +8,15 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.lua.ObjectLuaTable; import dan200.computercraft.api.lua.ObjectLuaTable;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.server.ServerLifecycleHooks;
// TODO: Fabric and Forge diffirence? (Bottom: Fabric) // TODO: Fabric and Forge diffirence? (Bottom: Fabric)
import com.swdteam.tardim.TardimData; import com.swdteam.tardim.TardimData;
@ -24,13 +28,9 @@ import com.swdteam.tardim.TardimData.Location;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.*;
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 DigitalInterfacePeripheral implements IPeripheral { public class DigitalInterfacePeripheral implements IPeripheral {
private final List<IComputerAccess> connectedComputers = new ArrayList<>(); // List of computers connected to the peripheral private final List<IComputerAccess> connectedComputers = new ArrayList<>(); // List of computers connected to the peripheral
@ -52,12 +52,9 @@ public class DigitalInterfacePeripheral implements IPeripheral {
@Override @Override
public boolean equals(@Nullable IPeripheral iPeripheral) { return this == iPeripheral; } public boolean equals(@Nullable IPeripheral iPeripheral) { return this == iPeripheral; }
// Called when a computer disconnects from the peripheral // Called when a computer connects/disconnects from the peripheral
@Override @Override
public void detach(@Nonnull IComputerAccess computer) { connectedComputers.remove(computer); } public void detach(@Nonnull IComputerAccess computer) { connectedComputers.remove(computer); }
// Called when a computer connects to the peripheral
// TODO: add a sound effect? Like a simple TARDIS beep?
@Override @Override
public void attach(@Nonnull IComputerAccess computer) { connectedComputers.add(computer); } public void attach(@Nonnull IComputerAccess computer) { connectedComputers.add(computer); }
@ -66,15 +63,21 @@ public class DigitalInterfacePeripheral implements IPeripheral {
} }
/* Get TARDIM's data, which we need for *every* function /**
* * Get TARDIM's data, which we need for *every* function
* <p>
* We can't do a simple * We can't do a simple
* TardimManager.getFromPos(getTileEntity().getPos()) * <code>TardimManager.getFromPos(getTileEntity().getPos())</code>
* <p>
* 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). * 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. * Which is obviously not what we want.
* <p>
* So instead we use this, and get the ability to give user a LuaException if they think that fiddling with time is funny
* This is mostly a copy of getIDForXZ function with some added checks
* *
* So instead we use this, and recieve ability to give user a LuaException if they think that fiddling with time devices is funny * @return TardimData of the TARDIM that the peripheral is in
* This is mostly a copy of getIDForXZ function */ * @throws LuaException if the peripheral is not in a TARDIM
* */
public TardimData getTardimData() throws LuaException { public TardimData getTardimData() throws LuaException {
int X = getTileEntity().getPos().getX(), Z = getTileEntity().getPos().getZ(); int X = getTileEntity().getPos().getX(), Z = getTileEntity().getPos().getZ();
@ -131,12 +134,20 @@ public class DigitalInterfacePeripheral implements IPeripheral {
// Peripheral methods =============================================================== // Peripheral methods ===============================================================
// Get amount of fuel we have (Out of 100) /**
* Return how much fuel is left in the TARDIM
*
* @return Fuel left (Out of 100)
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final double getFuel() throws LuaException { public final double getFuel() throws LuaException {
return getTardimData().getFuel(); return getTardimData().getFuel();
} }
/**
* Get how much fuel it would take to travel to the destination
* @return Amount of fuel needed (Out of 100)
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final double calculateFuelForJourney() throws LuaException { public final double calculateFuelForJourney() throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
@ -153,20 +164,34 @@ public class DigitalInterfacePeripheral implements IPeripheral {
fuel = 10.0; fuel = 10.0;
} }
return 100; //data.calculateFuelForJourney(((Level) curr.getLevel()), dest.getLevel().location(), curr.getPos(), dest.getPos()); Vec3 posA = new Vec3(curr.getPos().getX(), curr.getPos().getY(), curr.getPos().getZ());
Vec3 posB = new Vec3(dest.getPos().getX(), dest.getPos().getY(), dest.getPos().getZ());
fuel += posA.distanceTo(posB) / 100.0;
if (fuel > 100.0) fuel = 100.0;
return fuel;
} }
// Check whether the TARDIM is locked /**
* Check whether the TARDIM is locked
* @return true if locked, false if not
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final boolean isLocked() throws LuaException { public final boolean isLocked() throws LuaException {
return getTardimData().isLocked(); return getTardimData().isLocked();
} }
// Check whether the TARDIM is in flight /**
* Check whether the TARDIM is in flight
* @return true if in flight, false if not
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final boolean isInFlight() throws LuaException { return getTardimData().isInFlight(); } public final boolean isInFlight() throws LuaException { return getTardimData().isInFlight(); }
// Supposedly gets UNIX timestamp of when we entered flight /**
* Supposedly gets UNIX timestamp of when we entered flight
* @return UNIX timestamp if in flight, -1 if not
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final long getTimeEnteredFlight() throws LuaException { public final long getTimeEnteredFlight() throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
@ -176,20 +201,34 @@ public class DigitalInterfacePeripheral implements IPeripheral {
return data.getTimeEnteredFlight(); return data.getTimeEnteredFlight();
} }
// Get username of the TARDIM's owner /**
* Get username of the TARDIM's owner
* @return String of the owner's username
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final String getOwnerName() throws LuaException { public final String getOwnerName() throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
return data.getOwnerName(); return data.getOwnerName();
} }
// Lock/Unlock the TARDIM /**
* Lock/unlock the TARDIM
* @param locked true to lock, false to unlock
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final void setLocked(boolean locked) throws LuaException { public final void setLocked(boolean locked) throws LuaException {
getTardimData().setLocked(locked); getTardimData().setLocked(locked);
} }
// Returns table with current TARDIM location /**
* Get the current location of the TARDIM
* @return ObjectLuaTable of the current location with the following keys:
* <ul>
* <li>dimension - String of the dimension</li>
* <li>pos - table with the keys x, y, z that hold numbers</li>
* <li>facing - String of the facing</li>
* </ul>
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final ObjectLuaTable getCurrentLocation() throws LuaException { public final ObjectLuaTable getCurrentLocation() throws LuaException {
Location loc = getTardimData().getCurrentLocation(); Location loc = getTardimData().getCurrentLocation();
@ -204,7 +243,17 @@ public class DigitalInterfacePeripheral implements IPeripheral {
)); ));
} }
// Returns flight destination (or null if there isn't one) /**
* Get the current location of the TARDIM
* @return if there is no destination returns null.
* <p>
* Otherwise, ObjectLuaTable of the current location with the following keys:
* <ul>
* <li>dimension - String of the dimension</li>
* <li>pos - table with the keys x, y, z that hold numbers</li>
* <li>facing - String of the facing</li>
* </ul>
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final ObjectLuaTable getTravelLocation() throws LuaException { public final ObjectLuaTable getTravelLocation() throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
@ -224,7 +273,10 @@ public class DigitalInterfacePeripheral implements IPeripheral {
} }
} }
// Returns table with all companions of this TARDIM's owner /**
* Get list of the TARDIM owner's companions
* @return ObjectLuaTable containing the usernames of the companions
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final ObjectLuaTable getCompanions() throws LuaException { public final ObjectLuaTable getCompanions() throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
@ -235,13 +287,18 @@ public class DigitalInterfacePeripheral implements IPeripheral {
return companions; return companions;
} }
// Supposed to set dimension of the destination /**
// TODO: This looks like a hazard if someone inserts a dimension that doesn't exist * Set dimension for the TARDIM to travel to
* <p>
* This is a serious hazard right now due to the fact that I am unable to check if the dimension is valid.
* <p>
* TODO: If invalid dimension is given, the TARDIM is unable to land until the dimension is changed. Add proper checks.
* @param dimension String of the dimension e.g. "minecraft:overworld"
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final void setDimension(String dimension) throws LuaException { public final void setDimension(String dimension) throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
String key = dimension; String key = dimension;
dimension = DimensionMapReloadListener.toTitleCase(dimension); dimension = DimensionMapReloadListener.toTitleCase(dimension);
if (TardimManager.DIMENSION_MAP.containsKey(dimension)) { if (TardimManager.DIMENSION_MAP.containsKey(dimension)) {
@ -262,7 +319,12 @@ public class DigitalInterfacePeripheral implements IPeripheral {
} }
} }
// Set X, Y and Z of travel destination /**
* Set the destination's coordinates
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
*/
@LuaFunction(mainThread = true) @LuaFunction(mainThread = true)
public final void setTravelLocation(int x, int y, int z) throws LuaException { public final void setTravelLocation(int x, int y, int z) throws LuaException {
TardimData data = getTardimData(); TardimData data = getTardimData();
@ -273,18 +335,56 @@ public class DigitalInterfacePeripheral implements IPeripheral {
data.getTravelLocation().setPosition(x, y, z); data.getTravelLocation().setPosition(x, y, z);
} }
/*
@LuaFunction(mainThread = true)
public final void demat() throws LuaException {
TardimData data = getTardimData();
data.setInFlight(true);
}
@LuaFunction(mainThread = true)
public final void remat() throws LuaException {
TardimData data = getTardimData();
data.setInFlight(false);
}
/**
* Set destination to the TARDIM's owner's home (Must be online)
*/ */
@LuaFunction(mainThread = true)
public final void home() throws LuaException {
TardimData data = getTardimData();
UUID uuid = data.getOwner();
String username = data.getOwnerName();
if (uuid == null || username == null) {
throw new LuaException("TARDIM has no owner");
}
PlayerList playerList = ServerLifecycleHooks.getCurrentServer().getPlayerList();
ServerPlayer player = playerList.getPlayer(uuid);
if (player == null) {
throw new LuaException("TARDIM owner is not online");
}
ResourceKey<Level> dim = player.getRespawnDimension();
BlockPos pos = player.getRespawnPosition();
if (pos == null) {
throw new LuaException("TARDIM owner has no home");
}
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
}
/**
* Set destination for a player's location (Player must be online)
* @param username - String of the username of the player
*/
@LuaFunction(mainThread = true)
public final void locatePlayer(String username) throws LuaException {
PlayerList playerList = ServerLifecycleHooks.getCurrentServer().getPlayerList();
ServerPlayer player = playerList.getPlayerByName(username);
if (player == null) {
throw new LuaException("Player not found");
}
ResourceKey<Level> dim = player.getCommandSenderWorld().dimension();
BlockPos pos = player.blockPosition();
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
}
// I would love to add this, however it requires TARDIM source code.
// TODO: If I am ever part of the TARDIM team, I will add this.
// TODO: locateBiome, demat, remat, setFacing, toggleFacing
} }

View file

@ -14,7 +14,7 @@ import static dan200.computercraft.shared.Capabilities.CAPABILITY_PERIPHERAL;
public class DigitalInterfaceTileEntity extends BlockEntity { public class DigitalInterfaceTileEntity extends BlockEntity {
public DigitalInterfaceTileEntity(BlockPos pos, BlockState state) { public DigitalInterfaceTileEntity(BlockPos pos, BlockState state) {
super(Registration.CC_TILEENTITY.get(), pos, state); super(Registration.DIGITAL_TARDIM_INTERFACE_TILEENTITY.get(), pos, state);
} }
/** /**

View file

@ -21,7 +21,7 @@ public class Registration {
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, TardimInControl.MODID); public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, TardimInControl.MODID);
// Blocks // Blocks
public static final RegistryObject<Block> CC_BLOCK = register("digital_tardim_interface", DigitalInterfaceBlock::new); public static final RegistryObject<Block> DIGITAL_TARDIM_INTERFACE = register("digital_tardim_interface", DigitalInterfaceBlock::new);
private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) { private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) {
RegistryObject<T> registryObject = BLOCKS.register(name, block); RegistryObject<T> registryObject = BLOCKS.register(name, block);
@ -30,7 +30,7 @@ public class Registration {
} }
// Tile Entities // Tile Entities
public static final RegistryObject<BlockEntityType<DigitalInterfaceTileEntity>> CC_TILEENTITY = Registration.BLOCK_ENTITIES.register("digital_tardim_interface", () -> new BlockEntityType<>(DigitalInterfaceTileEntity::new, Sets.newHashSet(CC_BLOCK.get()), null)); public static final RegistryObject<BlockEntityType<DigitalInterfaceTileEntity>> DIGITAL_TARDIM_INTERFACE_TILEENTITY = Registration.BLOCK_ENTITIES.register("digital_tardim_interface", () -> new BlockEntityType<>(DigitalInterfaceTileEntity::new, Sets.newHashSet(DIGITAL_TARDIM_INTERFACE.get()), null));
// Register our stuff // Register our stuff
public static void register() { public static void register() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB