Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard #659

Open
wants to merge 17 commits into
base: dev/0.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2025-01-16T15:46:41.860623 LootTables
// 1.19.2 2024-09-23T21:28:44.4809605 LootTables
618b63c020ab64890c8a2d2506dd61cd30259a44 data/advancedperipherals/loot_tables/blocks/block_reader.json
0923665563d05307a7fa7d711a2d7a994a31eb6e data/advancedperipherals/loot_tables/blocks/chat_box.json
bf2a80256cfba0bd8c0283d493882e5816882f1f data/advancedperipherals/loot_tables/blocks/colony_integrator.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2025-01-16T15:59:34.693839 Languages: en_us
d0fe3ab5a88d6b925369860038c76f23d9910143 assets/advancedperipherals/lang/en_us.json
// 1.19.2 2025-01-18T00:08:46.0946621 Languages: en_us
d4cb5d2a7d78fc44e503663eb08d67a0fb981deb assets/advancedperipherals/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"item.advancedperipherals.end_automata_core": "End Automata Core",
"item.advancedperipherals.hotkey_module": "Hotkey Module",
"item.advancedperipherals.husbandry_automata_core": "Husbandry Automata Core",
"item.advancedperipherals.keyboard": "Wireless Keyboard",
"item.advancedperipherals.memory_card": "Memory Card",
"item.advancedperipherals.nightvision_module": "Night Vision Module",
"item.advancedperipherals.overlay_module": "Overlay Module",
Expand All @@ -48,6 +49,8 @@
"item.advancedperipherals.smart_glasses": "Smart Glasses",
"item.advancedperipherals.smart_glasses_interface": "Smart Glasses Interface",
"item.advancedperipherals.smart_glasses_netherite": "Netherite reinforced Smart Glasses",
"item.advancedperipherals.tooltip.binding.bound_to": "&7Bound to &b%s&7.",
"item.advancedperipherals.tooltip.binding.bound_to_glasses": "&7Bound to Glasses with id &b%s&7.",
"item.advancedperipherals.tooltip.block_reader": "&7Reads nbt data of blocks to interact with blocks which do not have computer support.",
"item.advancedperipherals.tooltip.chat_box": "&7Interacts with the ingame chat, can read and write messages.",
"item.advancedperipherals.tooltip.chunk_controller": "&7A crafting ingredient for the Chunky Turtle.",
Expand All @@ -62,7 +65,6 @@
"item.advancedperipherals.tooltip.inventory_manager": "&7This block is able to send or receive specific items from a player inventory.",
"item.advancedperipherals.tooltip.me_bridge": "&7The ME Bridge interacts with Applied Energistics to manage your items.",
"item.advancedperipherals.tooltip.memory_card": "&7Can save the rights of a player to use it in an inventory manager.",
"item.advancedperipherals.tooltip.memory_card.bound": "&7Bound to &b%s&7.",
"item.advancedperipherals.tooltip.nbt_storage": "&7Acts like a storage disk. Can store nbt based data.",
"item.advancedperipherals.tooltip.overpowered_end_automata_core": "&7Improved version of the end automata core, that provides some overpowered uses! Be careful, the upgrade is very fragile.",
"item.advancedperipherals.tooltip.overpowered_husbandry_automata_core": "&7Improved version of the husbandry automata core, that provides some overpowered uses! Be careful, the upgrade is very fragile.",
Expand All @@ -82,9 +84,13 @@
"pocket.advancedperipherals.environment_pocket": "Environment",
"pocket.advancedperipherals.geoscanner_pocket": "Geo",
"pocket.advancedperipherals.player_pocket": "Player Detector",
"text.advancedperipherals.added_player": "Added you to the memory card",
"text.advancedperipherals.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?",
"text.advancedperipherals.removed_player": "Cleared the memory card",
"text.advancedperipherals.bind_keyboard": "Bounded the keyboard to %s",
"text.advancedperipherals.bind_memorycard": "Bounded the memory card to you",
"text.advancedperipherals.cleared_keyboard": "Cleared the keyboard",
"text.advancedperipherals.cleared_memorycard": "Cleared the memory card",
"text.advancedperipherals.keyboard.close": "Press ESC to close the Keyboard Screen",
"text.advancedperipherals.keyboard_notbound": "The keyboard it 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
Expand Up @@ -5,6 +5,7 @@
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.renderer.DistanceDetectorRenderer;
import de.srendi.advancedperipherals.client.screens.InventoryManagerScreen;
import de.srendi.advancedperipherals.client.screens.KeyboardScreen;
import de.srendi.advancedperipherals.client.screens.SaddleTurtleScreen;
import de.srendi.advancedperipherals.client.screens.SmartGlassesScreen;
import de.srendi.advancedperipherals.client.smartglasses.OverlayModuleOverlay;
Expand Down Expand Up @@ -42,6 +43,7 @@ public static void registerModels(ModelEvent.RegisterAdditional event) {
@SubscribeEvent
public static void onClientSetup(FMLClientSetupEvent event) {
MenuScreens.register(APContainerTypes.INVENTORY_MANAGER_CONTAINER.get(), InventoryManagerScreen::new);
MenuScreens.register(APContainerTypes.KEYBOARD_CONTAINER.get(), KeyboardScreen::new);
MenuScreens.register(APContainerTypes.SMART_GLASSES_CONTAINER.get(), SmartGlassesScreen::new);

ComputerCraftAPIClient.registerTurtleUpgradeModeller(CCRegistration.CHUNKY_TURTLE.get(), TurtleUpgradeModeller.flatItem());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package de.srendi.advancedperipherals.client.screens;

import com.mojang.blaze3d.vertex.PoseStack;
import dan200.computercraft.client.gui.ClientInputHandler;
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
import dan200.computercraft.core.terminal.Terminal;
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.client.KeyMapping;
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;

/**
* A simple screen but without any rendering calls. Used to unlock the mouse so we can freely write stuff
* <p>
* We just create a terminal which is used to forward all the key presses and mouse clicks but we don't render it.
*/
public class KeyboardScreen extends BaseScreen<KeyboardContainer> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not extend from AbstractContainerScreen, but inherit Screen or GuiComponent, then jade should not render on it


protected final InputHandler input;
private final Terminal terminalData;

private WidgetTerminal terminal;

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

@Override
public void render(@NotNull PoseStack poseStack, int x, int y, float partialTicks) {
Minecraft minecraft = Minecraft.getInstance();
float scale = 2f;
int screenWidth = minecraft.getWindow().getGuiScaledWidth();
// Make the text a bit smaller on small screens
if (screenWidth <= 1080)
scale = 1f;

poseStack.scale(scale, scale, 1);
Component text = Component.translatable("text.advancedperipherals.keyboard.close");
float textX = (screenWidth / 2f - minecraft.font.width(text) * scale / 2f) / scale;
minecraft.font.drawShadow(poseStack, text, textX, 1, 0xFFFFFF);
}

@Override
protected void init() {
passEvents = true;
KeyMapping.releaseAll();

super.init();
minecraft.keyboardHandler.setSendRepeatsToGui(true);

terminal = addWidget(new WidgetTerminal(terminalData, new ClientInputHandler(menu), 0, 0));
terminal.visible = false;
terminal.active = false;
setFocused(terminal);
}


@Override
protected void renderBg(@NotNull PoseStack matrixStack, float partialTicks, int x, int y) {
}

@Override
public void renderBackground(@NotNull PoseStack pPoseStack) {
}


@Override
public final void removed() {
super.removed();
minecraft.keyboardHandler.setSendRepeatsToGui(false);
}

@Override
public boolean mouseScrolled(double pMouseX, double pMouseY, double pDelta) {
minecraft.player.getInventory().swapPaint(pDelta);
return super.mouseScrolled(pMouseX, pMouseY, pDelta);
}

@Override
public void onClose() {
// Don't allow closing using standard keys like E. Closing using ESCAPE is still possible due to the keyPressed method
}

@Override
public boolean isPauseScreen() {
return false;
}

@Override
public final boolean keyPressed(int key, int scancode, int modifiers) {
if (key == GLFW.GLFW_KEY_ESCAPE) {
super.onClose();
return true;
}
// Forward the tab key to the terminal, rather than moving between controls.
if (key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal) {
return getFocused().keyPressed(key, scancode, modifiers);
}

return super.keyPressed(key, scancode, modifiers);
}

// We prevent jei by increasing the image size, even if we don't render it
@Override
public int getSizeX() {
return 4096;
}

@Override
public int getSizeY() {
return 4096;
}

@Override
public ResourceLocation getTexture() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.srendi.advancedperipherals.common.container;

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.nbt.CompoundTag;
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;
import org.jetbrains.annotations.Nullable;

public class KeyboardContainer extends BaseContainer implements ComputerMenu {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And so we don't need KeyboardContainer anymore


private final ServerInputState<KeyboardContainer> input;
@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);
this.input = new ServerInputState<>( this );

CompoundTag data = keyboardItem.getOrCreateTag();

if (!data.getBoolean(KeyboardItem.BOUND_TYPE_TAG)) {
BlockPos computerPos = NBTUtil.blockPosFromNBT(keyboardItem.getOrCreateTag().getCompound(KeyboardItem.BIND_TAG));

for (ServerComputer computer : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) {
if (computer.getPosition() != null && computer.getPosition().equals(computerPos)) {
this.computer = computer;
}
}
} else if (data.contains(KeyboardItem.GLASSES_BIND_TAG)) {
computer = ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().get(data.getInt(KeyboardItem.GLASSES_BIND_TAG));
}

}

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

@Nullable
@Override
public ServerComputer getComputer() {
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 @@ -10,6 +10,7 @@
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
Expand All @@ -23,8 +24,16 @@ public abstract class BaseContainer extends AbstractContainerMenu {
protected BaseContainer(@Nullable MenuType<?> type, int id, Inventory inventory, BlockPos pos, Level world) {
super(type, id);
this.inventory = new InvWrapper(inventory);
if (world != null)
this.tileEntity = (PeripheralBlockEntity<?>) world.getBlockEntity(pos);
if (world != null) {
BlockEntity blockEntity = world.getBlockEntity(pos);
// for player containers, pos is the position of the player
// We don't actual need a block entity for player containers
// But if a player stands for example on a mekanism cable, setting the tileEntity by casting and without a check
// would prevent opening the screen
if (blockEntity instanceof PeripheralBlockEntity<?> peripheralBlockEntity) {
tileEntity = peripheralBlockEntity;
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private void addItems() {
addItem(APItems.CHUNK_CONTROLLER, "Chunk Controller");
addItem(APItems.COMPUTER_TOOL, "Computer Tool");
addItem(APItems.HOTKEY_MODULE, "Hotkey Module");
addItem(APItems.KEYBOARD, "Wireless Keyboard");
addItem(APItems.MEMORY_CARD, "Memory Card");
addItem(APItems.NIGHT_VISION_MODULE, "Night Vision Module");
addItem(APItems.OVERLAY_MODULE, "Overlay Module");
Expand Down Expand Up @@ -144,13 +145,19 @@ private void addTooltips() {

addTooltip(APItems.COMPUTER_TOOL.get(), "&7This tool was made to tune our blocks. But for now, it's just a blue useless wrench.");
addTooltip(APItems.MEMORY_CARD.get(), "&7Can save the rights of a player to use it in an inventory manager.");
addTooltip("memory_card.bound", "&7Bound to &b%s&7.");
addTooltip("binding.bound_to", "&7Bound to &b%s&7.");
addTooltip("binding.bound_to_glasses", "&7Bound to Glasses with id &b%s&7.");

}

private void addTexts() {
addText("removed_player", "Cleared the memory card");
addText("added_player", "Added you to the memory card");
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("keyboard.close", "Press ESC to close the Keyboard Screen");
addText("cleared_memorycard", "Cleared the memory card");
addText("bind_memorycard", "Bounded the memory card to you");
addText("keyboard_notbound", "The keyboard it not bound");
addText("bind_keyboard", "Bounded the keyboard to %s");
addText("cleared_keyboard", "Cleared the keyboard");
addText("smart_glasses.peripherals", "Peripherals");
addText("smart_glasses.modules", "Modules");
addText("saddle_turtle.dismount_hint", "Controlling %1$s. Press %2$s and %3$s to dismount.");
Expand Down
Loading
Loading