Add key inputs
This commit is contained in:
parent
a86c1086b7
commit
d1abbeec7b
5 changed files with 114 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
||||||
package dev.exhq.ajarc;
|
package dev.exhq.ajarc;
|
||||||
|
|
||||||
|
import dev.exhq.ajarc.network.ComputerCommandSent;
|
||||||
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
||||||
import net.neoforged.bus.api.SubscribeEvent;
|
import net.neoforged.bus.api.SubscribeEvent;
|
||||||
import net.neoforged.fml.common.EventBusSubscriber;
|
import net.neoforged.fml.common.EventBusSubscriber;
|
||||||
|
@ -11,6 +12,7 @@ public class ModEvents {
|
||||||
public static void register(RegisterPayloadHandlersEvent event) {
|
public static void register(RegisterPayloadHandlersEvent event) {
|
||||||
var registrar = event.registrar("1");
|
var registrar = event.registrar("1");
|
||||||
registrar.playToClient(ComputerScreenUpdate.TYPE, ComputerScreenUpdate.STREAM_CODEC, ComputerScreenUpdate::handle);
|
registrar.playToClient(ComputerScreenUpdate.TYPE, ComputerScreenUpdate.STREAM_CODEC, ComputerScreenUpdate::handle);
|
||||||
|
registrar.playToServer(ComputerCommandSent.TYPE, ComputerCommandSent.STREAM_CODEC, ComputerCommandSent::handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,9 @@ import net.minecraft.world.level.block.entity.BlockEntity;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ComputerBlockEntity extends BlockEntity {
|
public class ComputerBlockEntity extends BlockEntity {
|
||||||
public ComputerBlockEntity(BlockPos pPos, BlockState pBlockState) {
|
public ComputerBlockEntity(BlockPos pPos, BlockState pBlockState) {
|
||||||
|
@ -32,6 +34,8 @@ public class ComputerBlockEntity extends BlockEntity {
|
||||||
.fieldOf("screen")
|
.fieldOf("screen")
|
||||||
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
|
.setPartial(() -> ComputerTerminal.ofSize(20, 30));
|
||||||
|
|
||||||
|
private List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadAdditional(@NotNull CompoundTag pTag, @NotNull HolderLookup.Provider pRegistries) {
|
protected void loadAdditional(@NotNull CompoundTag pTag, @NotNull HolderLookup.Provider pRegistries) {
|
||||||
super.loadAdditional(pTag, pRegistries);
|
super.loadAdditional(pTag, pRegistries);
|
||||||
|
@ -65,7 +69,26 @@ public class ComputerBlockEntity extends BlockEntity {
|
||||||
return screen;
|
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) {
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
package dev.exhq.ajarc.computer;
|
package dev.exhq.ajarc.computer;
|
||||||
|
|
||||||
|
import dev.exhq.ajarc.network.ComputerCommandSent;
|
||||||
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
import dev.exhq.ajarc.network.ComputerScreenUpdate;
|
||||||
import dev.exhq.ajarc.register.Register;
|
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.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.neoforged.neoforge.network.PacketDistributor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -67,5 +71,21 @@ public class ComputerMenu extends AbstractContainerMenu {
|
||||||
this.columns = computerScreenUpdate.columns();
|
this.columns = computerScreenUpdate.columns();
|
||||||
this.rows = computerScreenUpdate.rows();
|
this.rows = computerScreenUpdate.rows();
|
||||||
this.lines = computerScreenUpdate.lines();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,28 @@
|
||||||
package dev.exhq.ajarc.computer;
|
package dev.exhq.ajarc.computer;
|
||||||
|
|
||||||
|
import dev.exhq.ajarc.network.ComputerCommandSent;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.world.entity.player.Inventory;
|
import net.minecraft.world.entity.player.Inventory;
|
||||||
|
import net.neoforged.neoforge.network.PacketDistributor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.lwjgl.glfw.GLFW;
|
||||||
|
|
||||||
public class ComputerScreen extends AbstractContainerScreen<ComputerMenu> {
|
public class ComputerScreen extends AbstractContainerScreen<ComputerMenu> {
|
||||||
public ComputerScreen(ComputerMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
|
public ComputerScreen(ComputerMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
|
||||||
super(pMenu, pPlayerInventory, pTitle);
|
super(pMenu, pPlayerInventory, pTitle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String editingLine = "";
|
||||||
|
|
||||||
|
public void reinitDimensions() {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void init() {
|
protected void init() {
|
||||||
imageHeight = menu.getRows() * 11;
|
imageHeight = menu.getRows() * 11 - 1;
|
||||||
imageWidth = menu.getColumns() * 10;
|
imageWidth = menu.getColumns() * 10;
|
||||||
super.init();
|
super.init();
|
||||||
}
|
}
|
||||||
|
@ -25,6 +34,25 @@ public class ComputerScreen extends AbstractContainerScreen<ComputerMenu> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Override
|
||||||
protected void renderLabels(@NotNull GuiGraphics graphics, int pMouseX, int pMouseY) {
|
protected void renderLabels(@NotNull GuiGraphics graphics, int pMouseX, int pMouseY) {
|
||||||
int offsetY = 0;
|
int offsetY = 0;
|
||||||
|
@ -32,5 +60,6 @@ public class ComputerScreen extends AbstractContainerScreen<ComputerMenu> {
|
||||||
graphics.drawString(font, line, 0, offsetY, 0xFFFFFFFF);
|
graphics.drawString(font, line, 0, offsetY, 0xFFFFFFFF);
|
||||||
offsetY += 11;
|
offsetY += 11;
|
||||||
}
|
}
|
||||||
|
graphics.drawString(font, "$ " + editingLine, 0, offsetY, 0xFFFFFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<ComputerCommandSent> TYPE = new Type<>(Ajar.identifier("command_sent"));
|
||||||
|
public static final StreamCodec<ByteBuf, ComputerCommandSent> STREAM_CODEC = StreamCodec.composite(
|
||||||
|
ByteBufCodecs.VAR_INT,
|
||||||
|
ComputerCommandSent::containerId,
|
||||||
|
ByteBufCodecs.STRING_UTF8,
|
||||||
|
ComputerCommandSent::line,
|
||||||
|
ComputerCommandSent::new
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Type<? extends CustomPacketPayload> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue