From d1abbeec7b6ac7f8dcdc865c12063ae49281f347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Tue, 16 Jul 2024 20:05:42 +0200 Subject: [PATCH] Add key inputs --- src/main/java/dev/exhq/ajarc/ModEvents.java | 2 + .../ajarc/computer/ComputerBlockEntity.java | 25 +++++++++++- .../dev/exhq/ajarc/computer/ComputerMenu.java | 20 ++++++++++ .../exhq/ajarc/computer/ComputerScreen.java | 31 ++++++++++++++- .../ajarc/network/ComputerCommandSent.java | 38 +++++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/exhq/ajarc/network/ComputerCommandSent.java diff --git a/src/main/java/dev/exhq/ajarc/ModEvents.java b/src/main/java/dev/exhq/ajarc/ModEvents.java index 1d32ddc..4de1ee1 100644 --- a/src/main/java/dev/exhq/ajarc/ModEvents.java +++ b/src/main/java/dev/exhq/ajarc/ModEvents.java @@ -1,5 +1,6 @@ package dev.exhq.ajarc; +import dev.exhq.ajarc.network.ComputerCommandSent; import dev.exhq.ajarc.network.ComputerScreenUpdate; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; @@ -11,6 +12,7 @@ public class ModEvents { public static void register(RegisterPayloadHandlersEvent event) { var registrar = event.registrar("1"); registrar.playToClient(ComputerScreenUpdate.TYPE, ComputerScreenUpdate.STREAM_CODEC, ComputerScreenUpdate::handle); + registrar.playToServer(ComputerCommandSent.TYPE, ComputerCommandSent.STREAM_CODEC, ComputerCommandSent::handle); } } diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java index 4df4d71..a16d5c3 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerBlockEntity.java @@ -15,7 +15,9 @@ import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class ComputerBlockEntity extends BlockEntity { public ComputerBlockEntity(BlockPos pPos, BlockState pBlockState) { @@ -32,6 +34,8 @@ public class ComputerBlockEntity extends BlockEntity { .fieldOf("screen") .setPartial(() -> ComputerTerminal.ofSize(20, 30)); + private List lines = new ArrayList<>(); + @Override protected void loadAdditional(@NotNull CompoundTag pTag, @NotNull HolderLookup.Provider pRegistries) { super.loadAdditional(pTag, pRegistries); @@ -65,7 +69,26 @@ public class ComputerBlockEntity extends BlockEntity { return screen; } + public void executeCommand(String line) { + lines.add("$ " + line); + if (line.equals("small")) { + screen = ComputerTerminal.ofSize(10, 20); + lines.add("Made small!"); + } else if (line.equals("big")) { + screen = ComputerTerminal.ofSize(20, 30); + lines.add("Made big!"); + } else { + lines.add("Made unknown!"); + } + while (lines.size() > screen.dimensions().rows() - 1 && !lines.isEmpty()) { + lines.removeFirst(); + } + } + public ComputerScreenUpdate getSyncPacket(int windowId) { - return new ComputerScreenUpdate(windowId, Arrays.asList("Hiii", "Hello"), screen.dimensions().rows(), screen.dimensions().columns()); + return new ComputerScreenUpdate(windowId, + lines, + screen.dimensions().rows(), + screen.dimensions().columns()); } } diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerMenu.java b/src/main/java/dev/exhq/ajarc/computer/ComputerMenu.java index 59d29b5..6baa36f 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerMenu.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerMenu.java @@ -1,11 +1,15 @@ package dev.exhq.ajarc.computer; +import dev.exhq.ajarc.network.ComputerCommandSent; import dev.exhq.ajarc.network.ComputerScreenUpdate; import dev.exhq.ajarc.register.Register; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.network.PacketDistributor; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -67,5 +71,21 @@ public class ComputerMenu extends AbstractContainerMenu { this.columns = computerScreenUpdate.columns(); this.rows = computerScreenUpdate.rows(); this.lines = computerScreenUpdate.lines(); + if (Minecraft.getInstance().screen instanceof ComputerScreen computerScreen) { + computerScreen.reinitDimensions(); + } + } + + public void onClientInput(ComputerCommandSent computerCommandSent, ServerPlayer player) { + computer.executeCommand(computerCommandSent.line()); + syncToClient(player); + } + + public void sendLineToServer(String toSend) { + PacketDistributor.sendToServer(new ComputerCommandSent(containerId, toSend)); + lines.add("$ " + toSend); + while (lines.size() > rows - 1 && !lines.isEmpty()) { + lines.removeFirst(); + } } } diff --git a/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java b/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java index 230ef6e..1c057fa 100644 --- a/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java +++ b/src/main/java/dev/exhq/ajarc/computer/ComputerScreen.java @@ -1,19 +1,28 @@ package dev.exhq.ajarc.computer; +import dev.exhq.ajarc.network.ComputerCommandSent; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; +import net.neoforged.neoforge.network.PacketDistributor; import org.jetbrains.annotations.NotNull; +import org.lwjgl.glfw.GLFW; public class ComputerScreen extends AbstractContainerScreen { public ComputerScreen(ComputerMenu pMenu, Inventory pPlayerInventory, Component pTitle) { super(pMenu, pPlayerInventory, pTitle); } + String editingLine = ""; + + public void reinitDimensions() { + init(); + } + @Override protected void init() { - imageHeight = menu.getRows() * 11; + imageHeight = menu.getRows() * 11 - 1; imageWidth = menu.getColumns() * 10; super.init(); } @@ -25,6 +34,25 @@ public class ComputerScreen extends AbstractContainerScreen { } + @Override + public boolean charTyped(char pCodePoint, int pModifiers) { + editingLine += pCodePoint; + return true; + } + + @Override + public boolean keyPressed(int pKeyCode, int pScanCode, int pModifiers) { + if (super.keyPressed(pKeyCode, pScanCode, pModifiers)) + return true; + if (pKeyCode == GLFW.GLFW_KEY_ENTER) { + var toSend = editingLine; + editingLine = ""; + menu.sendLineToServer(toSend); + return true; + } + return false; + } + @Override protected void renderLabels(@NotNull GuiGraphics graphics, int pMouseX, int pMouseY) { int offsetY = 0; @@ -32,5 +60,6 @@ public class ComputerScreen extends AbstractContainerScreen { graphics.drawString(font, line, 0, offsetY, 0xFFFFFFFF); offsetY += 11; } + graphics.drawString(font, "$ " + editingLine, 0, offsetY, 0xFFFFFFFF); } } diff --git a/src/main/java/dev/exhq/ajarc/network/ComputerCommandSent.java b/src/main/java/dev/exhq/ajarc/network/ComputerCommandSent.java new file mode 100644 index 0000000..e706ccc --- /dev/null +++ b/src/main/java/dev/exhq/ajarc/network/ComputerCommandSent.java @@ -0,0 +1,38 @@ +package dev.exhq.ajarc.network; + +import dev.exhq.ajarc.Ajar; +import dev.exhq.ajarc.computer.ComputerMenu; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import org.jetbrains.annotations.NotNull; + +public record ComputerCommandSent( + int containerId, + String line +) implements CustomPacketPayload { + public static final Type TYPE = new Type<>(Ajar.identifier("command_sent")); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.VAR_INT, + ComputerCommandSent::containerId, + ByteBufCodecs.STRING_UTF8, + ComputerCommandSent::line, + ComputerCommandSent::new + ); + + + @Override + public @NotNull Type type() { + return TYPE; + } + + public void handle(IPayloadContext iPayloadContext) { + var containerMenu = iPayloadContext.player().containerMenu; + if (containerMenu instanceof ComputerMenu computerMenu && computerMenu.containerId == this.containerId) { + computerMenu.onClientInput(this, (ServerPlayer) iPayloadContext.player()); + } + } +}