Skip to content

Commit

Permalink
Key press/Char typed sync to the computer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SirEndii committed Sep 24, 2024
1 parent c507f0a commit ab6a8cd
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-09-23T21:38:21.973108 Languages: en_us
03b7b20272b605dc01218ad17a0f8259e91ad45d assets/advancedperipherals/lang/en_us.json
// 1.19.2 2024-09-24T12:35:48.4625121 Languages: en_us
324712d2a78b7f5276fc1bd1c69925939ea0b1b1 assets/advancedperipherals/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"text.advancedperipherals.bind_memorycard": "&7Added you to the memory card",
"text.advancedperipherals.cleared_keyboard": "&7Bind removed",
"text.advancedperipherals.cleared_memorycard": "&7Cleared the memory card",
"text.advancedperipherals.keyboard_notbound": "&7Keyboard not bound",
"text.advancedperipherals.saddle_turtle_dismount_hint": "Controlling %1$s. Press %2$s and %3$s to dismount.",
"text.advancedperipherals.smart_glasses.modules": "Modules",
"text.advancedperipherals.smart_glasses.peripherals": "Peripherals",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package de.srendi.advancedperipherals.client.screens;

import com.mojang.blaze3d.vertex.PoseStack;
import dan200.computercraft.client.gui.ClientInputHandler;
import dan200.computercraft.shared.computer.core.InputHandler;
import de.srendi.advancedperipherals.client.screens.base.BaseScreen;
import de.srendi.advancedperipherals.common.container.KeyboardContainer;
import net.minecraft.SharedConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;

import java.util.BitSet;

/**
* A simple screen but without any rendering calls. Used to unlock the mouse so we can freely write stuff
* <p>
* Char/key logic stolen from CC's WidgetTerminal
*/
public class KeyboardScreen extends BaseScreen<KeyboardContainer> {

protected final InputHandler input;
private final BitSet keysDown = new BitSet( 256 );

private float terminateTimer = -1;
private float rebootTimer = -1;
private float shutdownTimer = -1;

public KeyboardScreen(KeyboardContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn);
input = new ClientInputHandler( menu );
}

@Override
Expand All @@ -29,6 +46,116 @@ protected void renderBg(@NotNull PoseStack matrixStack, float partialTicks, int
public void renderBackground(@NotNull PoseStack pPoseStack) {
}

@Override
public boolean charTyped( char ch, int modifiers )
{
if( ch >= 32 && ch <= 126 || ch >= 160 && ch <= 255 ) // printable chars in byte range
{
// Queue the "char" event
input.queueEvent( "char", new Object[] { Character.toString( ch ) } );
}

return true;
}

@Override
public boolean keyPressed( int key, int scancode, int modifiers )
{
if( key == GLFW.GLFW_KEY_ESCAPE ) {
onClose();
return true;
}
if( (modifiers & GLFW.GLFW_MOD_CONTROL) != 0 )
{
switch( key )
{
case GLFW.GLFW_KEY_T:
if( terminateTimer < 0 ) terminateTimer = 0;
return true;
case GLFW.GLFW_KEY_S:
if( shutdownTimer < 0 ) shutdownTimer = 0;
return true;
case GLFW.GLFW_KEY_R:
if( rebootTimer < 0 ) rebootTimer = 0;
return true;

case GLFW.GLFW_KEY_V:
// Ctrl+V for paste
String clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
if( clipboard != null )
{
// Clip to the first occurrence of \r or \n
int newLineIndex1 = clipboard.indexOf( "\r" );
int newLineIndex2 = clipboard.indexOf( "\n" );
if( newLineIndex1 >= 0 && newLineIndex2 >= 0 )
{
clipboard = clipboard.substring( 0, Math.min( newLineIndex1, newLineIndex2 ) );
}
else if( newLineIndex1 >= 0 )
{
clipboard = clipboard.substring( 0, newLineIndex1 );
}
else if( newLineIndex2 >= 0 )
{
clipboard = clipboard.substring( 0, newLineIndex2 );
}

// Filter the string
clipboard = SharedConstants.filterText( clipboard );
if( !clipboard.isEmpty() )
{
// Clip to 512 characters and queue the event
if( clipboard.length() > 512 ) clipboard = clipboard.substring( 0, 512 );
input.queueEvent( "paste", new Object[] { clipboard } );
}

return true;
}
}
}

if( key >= 0 && terminateTimer < 0 && rebootTimer < 0 && shutdownTimer < 0 )
{
// Queue the "key" event and add to the down set
boolean repeat = keysDown.get( key );
keysDown.set( key );
input.keyDown( key, repeat );
}

return true;
}

@Override
public boolean keyReleased( int key, int scancode, int modifiers )
{
// Queue the "key_up" event and remove from the down set
if( key >= 0 && keysDown.get( key ) )
{
keysDown.set( key, false );
input.keyUp( key );
}

switch( key )
{
case GLFW.GLFW_KEY_T:
terminateTimer = -1;
break;
case GLFW.GLFW_KEY_R:
rebootTimer = -1;
break;
case GLFW.GLFW_KEY_S:
shutdownTimer = -1;
break;
case GLFW.GLFW_KEY_LEFT_CONTROL:
case GLFW.GLFW_KEY_RIGHT_CONTROL:
terminateTimer = rebootTimer = shutdownTimer = -1;
break;
}

return true;
}


@Override
public int getSizeX() {
return 256;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
package de.srendi.advancedperipherals.common.container;

import de.srendi.advancedperipherals.AdvancedPeripherals;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.computer.menu.ComputerMenu;
import dan200.computercraft.shared.computer.menu.ServerInputHandler;
import dan200.computercraft.shared.computer.menu.ServerInputState;
import dan200.computercraft.shared.computer.terminal.TerminalState;
import de.srendi.advancedperipherals.common.container.base.BaseContainer;
import de.srendi.advancedperipherals.common.items.KeyboardItem;
import de.srendi.advancedperipherals.common.setup.APContainerTypes;
import de.srendi.advancedperipherals.common.util.NBTUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;

public class KeyboardContainer extends BaseContainer {
import javax.annotation.Nullable;

public KeyboardContainer(int id, Inventory inventory, BlockPos pos, Level level) {
public class KeyboardContainer extends BaseContainer implements ComputerMenu {

private final ServerInputState<KeyboardContainer> input;
private final BlockPos computerPos;
@Nullable
private ServerComputer computer = null;

public KeyboardContainer(int id, Inventory inventory, BlockPos pos, Level level, ItemStack keyboardItem) {
super(APContainerTypes.KEYBOARD_CONTAINER.get(), id, inventory, pos, level);
AdvancedPeripherals.debug("test");
this.input = new ServerInputState<>( this );
this.computerPos = NBTUtil.blockPosFromNBT(keyboardItem.getOrCreateTag().getCompound(KeyboardItem.BIND_TAG));
}

@Override
public boolean stillValid(@NotNull Player playerIn) {
return true;
}

@Override
public ServerComputer getComputer() {
if (computer != null)
return computer;

for (ServerComputer computer : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) {
if (computer.getPosition().equals(computerPos)) {
this.computer = computer;
}
}

return computer;
}

@Override
public ServerInputHandler getInput() {
return input;
}

@Override
public void updateTerminal(TerminalState state) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void addTexts() {
addText("cleared_memorycard", "&7Cleared the memory card");
addText("bind_keyboard", "&7Bound to &b%s&7");
addText("cleared_keyboard", "&7Bind removed");
addText("keyboard_notbound", "&7Keyboard not bound");
addText("automata_core_feed_by_player", "You're trying to feed an entity to a soul, but your own body refuses to do this. Maybe something more mechanical can do this?");
addText("smart_glasses.peripherals", "Peripherals");
addText("smart_glasses.modules", "Modules");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class KeyboardItem extends BaseItem implements IInventoryItem {

private static final String BIND_TAG = "bind";
public static final String BIND_TAG = "bind";

public KeyboardItem() {
super(new Properties().stacksTo(1));
Expand Down Expand Up @@ -63,9 +63,17 @@ public InteractionResult useOn(UseOnContext context) {

@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
if (playerIn.level.isClientSide())
return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn));
// Used to prevent the menu from opening when we just want to bind/unbind the keyboard
if (!playerIn.isShiftKeyDown())
if (!playerIn.isShiftKeyDown()) {
if (!playerIn.getItemInHand(handIn).getOrCreateTag().contains(BIND_TAG)) {
playerIn.displayClientMessage(EnumColor.buildTextComponent(Component.translatable("text.advancedperipherals.keyboard_notbound")), false);
return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn));
}
// Run the super use which handles the IInventoryItem stuff to actually open the container
return super.use(worldIn, playerIn, handIn);
}

return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn));
}
Expand All @@ -74,8 +82,8 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
public void appendHoverText(ItemStack stack, @Nullable Level levelIn, List<Component> tooltip, TooltipFlag flagIn) {
super.appendHoverText(stack, levelIn, tooltip, flagIn);
CompoundTag data = stack.getOrCreateTag();
if (data.contains("bind")) {
tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.boundto", NBTUtil.blockPosFromNBT(data.getCompound("bind")).toShortString())));
if (data.contains(BIND_TAG)) {
tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.boundto", NBTUtil.blockPosFromNBT(data.getCompound(BIND_TAG)).toShortString())));
}
}

Expand Down Expand Up @@ -103,7 +111,7 @@ public Component getDisplayName() {

@Override
public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory playerInv, @NotNull Player player) {
return new KeyboardContainer(pContainerId, playerInv, player.blockPosition(), player.getLevel());
return new KeyboardContainer(pContainerId, playerInv, player.blockPosition(), player.getLevel(), itemStack);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.srendi.advancedperipherals.common.container.SmartGlassesContainer;
import net.minecraft.core.BlockPos;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.registries.RegistryObject;
Expand All @@ -21,8 +22,9 @@ public class APContainerTypes {

public static final RegistryObject<MenuType<KeyboardContainer>> KEYBOARD_CONTAINER = APRegistration.CONTAINER_TYPES.register("keyboard_container", () -> IForgeMenuType.create((windowId, inv, data) -> {
BlockPos pos = data.readBlockPos();
ItemStack keyboardItem = data.readItem();
Level level = inv.player.getCommandSenderWorld();
return new KeyboardContainer(windowId, inv, pos, level);
return new KeyboardContainer(windowId, inv, pos, level, keyboardItem);
}));

public static final RegistryObject<MenuType<SmartGlassesContainer>> SMART_GLASSES_CONTAINER = APRegistration.CONTAINER_TYPES.register("smart_glasses_container", () -> ContainerData.toType(ComputerContainerData::new,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "advancedperipherals:item/keyboard"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ab6a8cd

Please sign in to comment.