Say it with me, not even close

This commit is contained in:
Andrew-71 2023-05-08 23:22:33 +03:00
parent eecac8ac7c
commit 024d00049e
29 changed files with 1451 additions and 433 deletions

View file

@ -2,6 +2,21 @@ package su.a71.tardim_ic.tardim_ic.digital_interface;
import com.mojang.datafixers.util.Pair;
import com.swdteam.common.command.tardim.CommandTardimBase;
import com.swdteam.common.command.tardim.CommandTravel;
import com.swdteam.common.data.DimensionMapReloadListener;
import com.swdteam.common.init.TRDSounds;
import com.swdteam.common.init.TardimRegistry;
import com.swdteam.common.item.ItemTardim;
import com.swdteam.main.Tardim;
import com.swdteam.tardim.TardimData;
import com.swdteam.tardim.TardimData.Location;
import com.swdteam.tardim.TardimManager;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.ObjectLuaTable;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
@ -10,31 +25,14 @@ import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.phys.Vec3;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.lua.ObjectLuaTable;
import dan200.computercraft.api.lua.LuaException;
import com.swdteam.tardim.TardimData;
import com.swdteam.tardim.TardimManager;
import com.swdteam.tardim.TardimData.Location;
import com.swdteam.common.init.TardimRegistry;
import com.swdteam.common.command.tardim.CommandTravel;
import com.swdteam.common.data.DimensionMapReloadListener;
import com.swdteam.common.init.TRDSounds;
import com.swdteam.common.item.ItemTardim;
import com.swdteam.main.Tardim;
import su.a71.tardim_ic.tardim_ic.Registration;
import su.a71.tardim_ic.tardim_ic.utils.FakePlayer;
@ -46,13 +44,12 @@ import java.util.*;
public class DigitalInterfacePeripheral implements IPeripheral {
private final List<IComputerAccess> connectedComputers = new ArrayList<>(); // List of computers connected to the peripheral
private final DigitalInterfaceTileEntity tileEntity; // Peripheral's BlockEntity, used for accessing coordinates
private final IDigitalInterfaceEntity tileEntity; // Peripheral's BlockEntity, used for accessing coordinates
/**
* @param tileEntity the tile entity of this peripheral
* @hidden
*/
public DigitalInterfacePeripheral(DigitalInterfaceTileEntity tileEntity) {
public DigitalInterfacePeripheral(IDigitalInterfaceEntity tileEntity) {
this.tileEntity = tileEntity;
}
@ -92,7 +89,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
* @hidden
* @return
*/
public DigitalInterfaceTileEntity getTileEntity() {
public IDigitalInterfaceEntity getTileEntity() {
return tileEntity;
}
@ -113,7 +110,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
* @throws LuaException if the peripheral is not in a TARDIM
* @hidden
*/
public TardimData getTardimData() throws LuaException {
public TardimData getTardimDataInitial() {
int X = getTileEntity().getPos().getX(), Z = getTileEntity().getPos().getZ();
int index = 0;
@ -156,15 +153,24 @@ public class DigitalInterfacePeripheral implements IPeripheral {
}
// We really don't want to access a ghost TARDIM, do we?
// If we fail checks here are not inside a TARDIM
if (!found) {
throw new LuaException("Peripheral is not inside a TARDIM");
return null;
}
TardimData T = TardimManager.getTardim(index);
if (T.getCurrentLocation() == null || T.getOwnerName() == null) {
throw new LuaException("Peripheral is not inside a TARDIM");
return null;
}
return T;
return T;
}
public TardimData getTardimData() throws LuaException {
TardimData data = this.getTileEntity().getTardim();
if (data == null || data.getCurrentLocation() == null || data.getOwnerName() == null) {
throw new LuaException("Peripheral is not inside a TARDIM");
}
return data;
}
// Peripheral methods ===============================================================
@ -173,7 +179,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
* Return how much fuel is left in the TARDIM
*
* @return Fuel left (Out of 100)
*/
*/
@LuaFunction(mainThread = true)
public final double getFuel() throws LuaException {
return getTardimData().getFuel();
@ -182,7 +188,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
/**
* Get how much fuel it would take to travel to the destination
* @return Amount of fuel needed (Out of 100)
*/
*/
@LuaFunction(mainThread = true)
public final double calculateFuelForJourney() throws LuaException {
TardimData data = getTardimData();
@ -266,15 +272,15 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getCurrentLocation() throws LuaException {
Location loc = getTardimData().getCurrentLocation();
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()
"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()
));
}
@ -291,7 +297,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getTravelLocation() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(data.getCurrentLocation());
}
@ -299,9 +305,9 @@ public class DigitalInterfacePeripheral implements IPeripheral {
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()
"x", loc.getPos().getX(),
"y", loc.getPos().getY(),
"z", loc.getPos().getZ()
)),
"facing", loc.getFacing().toString()
));
@ -313,14 +319,15 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final ObjectLuaTable getCompanions() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
Map<Integer, String> companions = new HashMap<>();
for (int i = 0; i < data.getCompanions().size(); i++) {
companions.put(i + 1, data.getCompanions().get(i).getUsername());
}
return new ObjectLuaTable(companions);
for (int i = 0; i < data.getCompanions().size(); i++) {
companions.put(i + 1, data.getCompanions().get(i).getUsername());
}
return new ObjectLuaTable(companions);
}
/**
* Set dimension for the TARDIM to travel to
* <p>
@ -331,7 +338,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final void setDimension(String dimension) throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
String key = dimension;
dimension = DimensionMapReloadListener.toTitleCase(dimension);
@ -378,7 +385,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
if (this.tileEntity.getLevel().isClientSide()) {
return;
}
TardimData data = getTardimData();
TardimData data = getTardimData();
UUID uuid = data.getOwner();
String username = data.getOwnerName();
@ -415,15 +422,15 @@ public class DigitalInterfacePeripheral implements IPeripheral {
PlayerList playerList = this.tileEntity.getLevel().getServer().getPlayerList();
ServerPlayer player = playerList.getPlayerByName(username);
if (player == null) {
throw new LuaException("Player not found");
}
if (player == null) {
throw new LuaException("Player not found");
}
ResourceKey<Level> dim = player.getCommandSenderWorld().dimension();
BlockPos pos = player.blockPosition();
ResourceKey<Level> dim = player.getCommandSenderWorld().dimension();
BlockPos pos = player.blockPosition();
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
setDimension(dim.location().toString());
setTravelLocation(pos.getX(), pos.getY(), pos.getZ());
}
/**
@ -437,13 +444,13 @@ public class DigitalInterfacePeripheral implements IPeripheral {
return null;
}
PlayerList playerList = this.tileEntity.getLevel().getServer().getPlayerList();
PlayerList playerList = this.tileEntity.getLevel().getServer().getPlayerList();
Map<Integer, String> players = new HashMap<>();
for (int i = 0; i < playerList.getPlayers().size(); i++) {
players.put(i + 1, playerList.getPlayers().get(i).getGameProfile().getName());
}
return new ObjectLuaTable(players);
return new ObjectLuaTable(players);
}
/**
@ -452,8 +459,8 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final String getDoorRotation() throws LuaException {
TardimData data = getTardimData();
Direction rotation = data.getTravelLocation().getFacing();
TardimData data = getTardimData();
Direction rotation = data.getTravelLocation().getFacing();
switch (rotation) {
case NORTH -> {
return "north";
@ -479,7 +486,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final void setDoorRotation(String rotation) throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
switch (rotation) {
case "north" -> data.getTravelLocation().setFacing(Direction.NORTH);
case "east" -> data.getTravelLocation().setFacing(Direction.EAST);
@ -496,7 +503,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final void toggleDoorRotation() throws LuaException {
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
@ -523,18 +530,18 @@ public class DigitalInterfacePeripheral implements IPeripheral {
*/
@LuaFunction(mainThread = true)
public final void coordAdd(String axis, int amount) throws LuaException {
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
TardimData data = getTardimData();
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}
Location location = data.getTravelLocation();
switch (axis) {
case "x" -> location.addPosition(amount, 0, 0);
Location location = data.getTravelLocation();
switch (axis) {
case "x" -> location.addPosition(amount, 0, 0);
case "y" -> location.addPosition(0, amount, 0);
case "z" -> location.addPosition(0, 0, amount);
default -> throw new LuaException("Invalid axis");
}
}
}
/**
@ -546,7 +553,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
return;
}
TardimData data = getTardimData();
TardimData data = getTardimData();
if (data.isInFlight()) {
throw new LuaException("TARDIM is already in flight");
@ -555,7 +562,7 @@ public class DigitalInterfacePeripheral implements IPeripheral {
ServerLevel level = this.tileEntity.getLevel().getServer().getLevel(loc.getLevel());
ItemTardim.destroyTardim(level, loc.getPos(), Direction.NORTH);
data.setInFlight(true);
if (data.getTravelLocation() == null) {
if (data.getTravelLocation() == null) {
data.setTravelLocation(new Location(data.getCurrentLocation()));
}

View file

@ -1,7 +1,10 @@
package su.a71.tardim_ic.tardim_ic.digital_interface;
import com.swdteam.tardim.TardimData;
import com.swdteam.tardim.TardimManager;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
@ -15,9 +18,11 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import static dan200.computercraft.shared.Capabilities.CAPABILITY_PERIPHERAL;
public class DigitalInterfaceTileEntity extends BlockEntity implements IDigitalInterfaceEntity {
public TardimData data; // Our TARDIM
public DigitalInterfaceTileEntity(BlockPos pos, BlockState state) {
super(Registration.DIGITAL_TARDIM_INTERFACE_TILEENTITY.get(), pos, state);
this.data = getTardimDataInitial();
}
/**
@ -29,6 +34,11 @@ public class DigitalInterfaceTileEntity extends BlockEntity implements IDigitalI
return this.worldPosition;
}
@Override
public TardimData getTardim() {
return this.data;
}
/**
* When a computer modem tries to wrap our block, the modem will call getCapability to receive our peripheral.
* Then we just simply return a {@link LazyOptional} with our Peripheral
@ -44,4 +54,65 @@ public class DigitalInterfaceTileEntity extends BlockEntity implements IDigitalI
}
return super.getCapability(cap, direction);
}
public TardimData getTardimDataInitial() {
int X = this.getPos().getX(), Z = this.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 access a ghost TARDIM, do we?
// If we fail checks here are not inside a TARDIM
if (!found) {
return null;
}
TardimData T = TardimManager.getTardim(index);
if (T.getCurrentLocation() == null || T.getOwnerName() == null) {
return null;
}
return T;
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
this.data = getTardimDataInitial();
}
}

View file

@ -0,0 +1,89 @@
package su.a71.tardim_ic.tardim_ic.digital_interface;
import com.swdteam.tardim.TardimData;
import com.swdteam.tardim.TardimManager;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
public class FakeDigitalInterfaceTileEntity implements IDigitalInterfaceEntity {
public BlockPos blockPos;
public Level level;
public TardimData data; // Our TARDIM
FakeDigitalInterfaceTileEntity(BlockPos in_block, Level in_level) {
this.blockPos = in_block;
this.level = in_level;
this.data = getTardimDataInitial();
}
@Override
public BlockPos getPos() {
return this.blockPos;
}
@Override
public Level getLevel() {
return this.level;
}
@Override
public TardimData getTardim() {
return this.data;
}
public TardimData getTardimDataInitial() {
int X = this.getPos().getX(), Z = this.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 access a ghost TARDIM, do we?
// If we fail checks here are not inside a TARDIM
if (!found) {
return null;
}
TardimData T = TardimManager.getTardim(index);
if (T.getCurrentLocation() == null || T.getOwnerName() == null) {
return null;
}
return T;
}
}

View file

@ -0,0 +1,12 @@
package su.a71.tardim_ic.tardim_ic.digital_interface;
import com.swdteam.tardim.TardimData;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
public interface IDigitalInterfaceEntity {
public BlockPos getPos();
public Level getLevel();
public TardimData getTardim();
}