Skip to content

Commit

Permalink
Merge pull request #576 from zyxkad/dev/1.20.1
Browse files Browse the repository at this point in the history
Chunky Turtle Logic Rework
  • Loading branch information
SirEndii authored Apr 22, 2024
2 parents d1b2fd3 + 7e2d55f commit 42757d7
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ChunkyPeripheral extends BasePeripheral<TurtlePeripheralOwner> {

public static final String PERIPHERAL_TYPE = "chunky";
private static final String UUID_TAG = "uuid";
private @Nullable ChunkPos loadedCentralChunk;
private @Nullable ChunkPos loadedCentralChunk = null;

public ChunkyPeripheral(ITurtleAccess turtle, TurtleSide side) {
super(PERIPHERAL_TYPE, new TurtlePeripheralOwner(turtle, side));
Expand All @@ -45,36 +45,51 @@ public boolean isEnabled() {
}

public void updateChunkState() {
// TODO: should find someway to update after turtle moved or while moving, but not every tick
ServerLevel level = (ServerLevel) getLevel();
ChunkManager manager = ChunkManager.get(level);
ChunkPos currentChunk = getChunkPos();
if (loadedCentralChunk == null || !loadedCentralChunk.equals(currentChunk)) {
setLoadedChunk(currentChunk, manager, level);
} else {
manager.touch(getUUID());
}
setLoadedChunk(currentChunk, manager, level);
manager.touch(getUUID());
}

protected void setLoadedChunk(@Nullable ChunkPos newChunk, ChunkManager manager, ServerLevel level) {
if (loadedCentralChunk != null) {
manager.removeForceChunk(level, getUUID(), loadedCentralChunk);
//Should not be used
//level.setChunkForced(loadedChunk.x, loadedChunk.z, false);
if (loadedCentralChunk.equals(newChunk)) {
return;
}
manager.removeForceChunk(level, getUUID());
// Should not be used
// level.setChunkForced(loadedChunk.x, loadedChunk.z, false);
loadedCentralChunk = null;
}
if (newChunk != null) {
loadedCentralChunk = newChunk;
manager.addForceChunk(level, getUUID(), loadedCentralChunk);
//Should not be used
//level.setChunkForced(newChunk.x, newChunk.z, true);
// Should not be used
// level.setChunkForced(newChunk.x, newChunk.z, true);
}
}

@Override
public void detach(@NotNull IComputerAccess computer) {
super.detach(computer);
public void attach(IComputerAccess computer) {
super.attach(computer);
ServerLevel level = (ServerLevel) owner.getLevel();
ChunkManager manager = ChunkManager.get(Objects.requireNonNull(level));
setLoadedChunk(null, manager, level);
ChunkPos currentChunk = getChunkPos();
setLoadedChunk(currentChunk, manager, level);
}

@Override
public void detach(@NotNull IComputerAccess computer) {
super.detach(computer);
// Should not remove the loaded chunk when detaching,
// because CC:T will detach all peripherals before server stopped.
// So the chunk record will never be saved if we removed the chunk record when detaching.
// The records will be automatically removed by the ChunkManager if they have not been touched a while ago.

// ServerLevel level = (ServerLevel) owner.getLevel();
// ChunkManager manager = ChunkManager.get(Objects.requireNonNull(level));
// setLoadedChunk(null, manager, level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import org.jetbrains.annotations.NotNull;

public class TurtleChunkyUpgrade extends PeripheralTurtleUpgrade<ChunkyPeripheral> {
private int updateTick = 0;

public TurtleChunkyUpgrade(ResourceLocation id, ItemStack stack) {
super(id, stack);
}

@Override
public ModelResourceLocation getLeftModel() {
return null; //Null, the turtle uses the chunk controller item model. See BaseTurtle.java
return null; // Null, the turtle uses the chunk controller item model. See BaseTurtle.java
}

@Override
Expand All @@ -34,10 +35,16 @@ protected ChunkyPeripheral buildPeripheral(@NotNull ITurtleAccess turtle, @NotNu

@Override
public void update(@NotNull ITurtleAccess turtle, @NotNull TurtleSide side) {
//Add a chunk to the Chunk Manager every 10 ticks, if it's not already forced.
//The turtle can move, so we need to do that.
// Add a chunk to the Chunk Manager every 10 ticks, if it's not already forced.
// The turtle can move, so we need to do that.
super.update(turtle, side);
if (APConfig.PERIPHERALS_CONFIG.enableChunkyTurtle.get()) {
// TODO: turtle will stop work when crossing chunks if update every 10 ticks
// updateTick++;
// if (updateTick < 10) {
// return;
// }
// updateTick = 0;
IPeripheral peripheral = turtle.getPeripheral(side);
if (peripheral instanceof ChunkyPeripheral chunkyPeripheral) {
chunkyPeripheral.updateChunkState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package de.srendi.advancedperipherals.common.commands;

import com.mojang.brigadier.exceptions.CommandSyntaxException;

import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.core.computer.Environment;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.command.text.ChatHelpers;
import dan200.computercraft.shared.command.text.TableBuilder;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.core.ServerContext;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.ChunkyPeripheral;
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;

import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand All @@ -17,10 +28,25 @@

@Mod.EventBusSubscriber(modid = AdvancedPeripherals.MOD_ID)
public class APCommands {
public static final String ROOT_LITERAL = "advancedperipherals";
public static final String FORCELOAD_LITERAL = "forceload";
static final String FORCELOAD_HELP =
"/" + ROOT_LITERAL + " " + FORCELOAD_LITERAL + " help" + " - show this help message\n" +
"/" + ROOT_LITERAL + " " + FORCELOAD_LITERAL + " dump" + " - show all chunky turtles\n";

@SubscribeEvent
public static void register(RegisterCommandsEvent event) {
event.getDispatcher().register(Commands.literal("advancedperipherals").then(Commands.literal("getHashItem").executes(context -> getHashItem(context.getSource()))));
event.getDispatcher().register(Commands.literal(ROOT_LITERAL)
.then(Commands.literal("getHashItem").executes(context -> getHashItem(context.getSource())))
.then(Commands.literal(FORCELOAD_LITERAL)
.executes(context -> forceloadHelp(context.getSource()))
.then(Commands.literal("help")
.executes(context -> forceloadHelp(context.getSource())))
.then(Commands.literal("dump")
.requires(ModRegistry.Permissions.PERMISSION_DUMP)
.executes(context -> forceloadDump(context.getSource())))
)
);
}

private static int getHashItem(CommandSourceStack source) throws CommandSyntaxException {
Expand All @@ -42,4 +68,50 @@ private static int getHashItem(CommandSourceStack source) throws CommandSyntaxEx
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("Copy"))))), true);
return 1;
}

private static int forceloadHelp(CommandSourceStack source) throws CommandSyntaxException {
source.sendSuccess(() -> Component.literal(FORCELOAD_HELP), true);
return 1;
}

private static int forceloadDump(CommandSourceStack source) throws CommandSyntaxException {
TableBuilder table = new TableBuilder("ChunkyTurtles", "Computer", "Position");

ServerComputer[] computers = ServerContext.get(source.getServer()).registry().getComputers().stream().filter((computer) -> {
Environment env = computer.getComputer().getEnvironment();
for (ComputerSide side : ComputerSide.values()) {
if (env.getPeripheral(side) instanceof ChunkyPeripheral) {
return true;
}
}
return false;
}).sorted((a, b) -> a.getID() - b.getID()).toArray(size -> new ServerComputer[size]);

for (ServerComputer computer : computers) {
table.row(
makeComputerDumpCommand(computer),
makeComputerPosCommand(computer)
);
}

table.display(source);
return computers.length;
}


private static Component makeComputerDumpCommand(ServerComputer computer) {
return ChatHelpers.link(
Component.literal("#" + computer.getID()),
"/computercraft dump " + computer.getInstanceID(),
Component.translatable("commands.computercraft.dump.action")
);
}

private static Component makeComputerPosCommand(ServerComputer computer) {
return ChatHelpers.link(
ChatHelpers.position(computer.getPosition()),
"/computercraft tp " + computer.getInstanceID(),
Component.translatable("commands.computercraft.tp.action")
);
}
}
Loading

0 comments on commit 42757d7

Please sign in to comment.