From 06597c4506f200aada8d19a61cd0ca865bb3f5c8 Mon Sep 17 00:00:00 2001 From: GiantLuigi4 <49770992+GiantLuigi4@users.noreply.github.com> Date: Sun, 5 Mar 2023 21:03:38 -0500 Subject: [PATCH 1/2] better keyboard handling, VR keyboard, and also you can turn on displays while crouching --- build.gradle | 2 + .../net/montoyo/wd/block/BlockScreen.java | 7 +- .../montoyo/wd/client/gui/GuiKeyboard.java | 103 ++++++++++-------- .../montoyo/wd/entity/TileEntityScreen.java | 6 +- 4 files changed, 68 insertions(+), 50 deletions(-) diff --git a/build.gradle b/build.gradle index 8d4ee11..fd49ede 100644 --- a/build.gradle +++ b/build.gradle @@ -79,6 +79,8 @@ dependencies { implementation fg.deobf("curse.maven:cloth_config_forge-348521:3972423") implementation fg.deobf("curse.maven:SU-370704:4410614") implementation fg.deobf("curse.maven:spark-361579:4381167") + + implementation fg.deobf("curse.maven:vivecraft-667903:4409427") } sourceSets { diff --git a/src/main/java/net/montoyo/wd/block/BlockScreen.java b/src/main/java/net/montoyo/wd/block/BlockScreen.java index d7b5723..b060b21 100644 --- a/src/main/java/net/montoyo/wd/block/BlockScreen.java +++ b/src/main/java/net/montoyo/wd/block/BlockScreen.java @@ -184,10 +184,11 @@ public class BlockScreen extends BaseEntityBlock { return InteractionResult.SUCCESS; } - } else if(sneaking) { - Util.toast(player, "turnOn"); - return InteractionResult.SUCCESS; } +// else if(sneaking) { +// Util.toast(player, "turnOn"); +// return InteractionResult.SUCCESS; +// } Vector2i size = Multiblock.measure(world, pos, side); if(size.x < 2 || size.y < 2) { diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java b/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java index efd75fe..5be8b5c 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiKeyboard.java @@ -10,6 +10,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.loading.FMLPaths; import net.montoyo.wd.WebDisplays; import net.montoyo.wd.client.gui.controls.Button; @@ -25,8 +26,11 @@ import net.montoyo.wd.utilities.TypeData; import net.montoyo.wd.utilities.Util; import org.lwjgl.glfw.GLFW; import org.cef.browser.CefBrowserOsr; +import org.vivecraft.gameplay.VRPlayer; +import org.vivecraft.gameplay.screenhandlers.KeyboardHandler; import java.io.*; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Locale; import java.util.Map; @@ -64,6 +68,30 @@ public class GuiKeyboard extends WDScreen { vars.put("showWarning", showWarning ? 1.0 : 0.0); } + private static final boolean vivecraftPresent; + + static { + boolean vivePres = false; + if (ModList.get().isLoaded("vivecraft")) vivePres = true; + // I believe the non-mixin version of vivecraft is not a proper mod, so + // detect the mod reflectively if the mod is not found + else { + try { + Class clazz = Class.forName("org.vivecraft.gameplay.screenhandlers.KeyboardHandler"); + //noinspection ConstantConditions + if (clazz == null) vivePres = false; + else { + Method m = clazz.getMethod("setOverlayShowing", boolean.class); + //noinspection ConstantConditions + vivePres = m != null; + } + } catch (Throwable ignored) { + vivePres = false; + } + } + vivecraftPresent = vivePres; + } + @Override public void init() { super.init(); @@ -109,60 +137,47 @@ public class GuiKeyboard extends WDScreen { defaultBackground = showWarning; syncTicks = 5; + + if (vivecraftPresent) + if (VRPlayer.get() != null) + KeyboardHandler.setOverlayShowing(true); } - + + @Override + public void onClose() { + if (vivecraftPresent) + if (VRPlayer.get() != null) + KeyboardHandler.setOverlayShowing(false); + super.onClose(); + } + @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - key(keyCode, scanCode, true, modifiers); + if(quitOnEscape && keyCode == GLFW.GLFW_KEY_ESCAPE) { + Minecraft.getInstance().setScreen(null); + } + evStack.add(new TypeData(TypeData.Action.PRESS, keyCode, modifiers)); + if (!evStack.isEmpty() && !syncRequested()) + requestSync(); return super.keyPressed(keyCode, scanCode, modifiers); } - + + @Override + public boolean charTyped(char codePoint, int modifiers) { + evStack.add(new TypeData(TypeData.Action.TYPE, codePoint, modifiers)); + if (!evStack.isEmpty() && !syncRequested()) + requestSync(); + return super.charTyped(codePoint, modifiers); + } + @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - key(keyCode, scanCode, false, modifiers); + evStack.add(new TypeData(TypeData.Action.RELEASE, keyCode, modifiers)); + if (!evStack.isEmpty() && !syncRequested()) + requestSync(); return super.keyPressed(keyCode, scanCode, modifiers); } - public void key(int keyCode, int scanCode, boolean pressed, int mod) { - if (pressed) { - if(quitOnEscape && keyCode == GLFW.GLFW_KEY_ESCAPE) { - Minecraft.getInstance().setScreen(null); - } - - int chr = CefBrowserOsr.remapKeycode(keyCode, (char) keyCode, mod); - evStack.add(new TypeData(TypeData.Action.PRESS, chr, mod)); - evStack.add(new TypeData(TypeData.Action.RELEASE, chr, mod)); - - if (keyCode != 0) - evStack.add(new TypeData(TypeData.Action.TYPE, chr, mod)); - - if (!evStack.isEmpty() && !syncRequested()) - requestSync(); - } - } - - public int getChar(int keyCode, int scanCode) { - String keystr = GLFW.glfwGetKeyName(keyCode, scanCode); - if(keystr == null){ - keystr = "\0"; - } - if(keyCode == GLFW.GLFW_KEY_ENTER){ - return 13; - } - if (keyCode == GLFW.GLFW_KEY_SPACE) { - return 32; - } - if(keystr.length() == 0){ - return -1; - } - if(hasShiftDown()) { - keystr = keystr.toUpperCase(Locale.ROOT); - return CefBrowserOsr.remapKeycode(keyCode, keystr.charAt(keystr.length() - 1), 0); - } else { - return CefBrowserOsr.remapKeycode(keyCode, keystr.charAt(keystr.length() - 1), 0); - } - } - @Override protected void sync() { if(!evStack.isEmpty()) { diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java index 1638b84..6266d4f 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java @@ -786,9 +786,9 @@ public class TileEntityScreen extends BlockEntity{ for(TypeData ev : data) { switch (ev.getAction()) { - case PRESS -> scr.browser.injectKeyPressedByKeyCode(ev.getKeyCode(), (char) ev.getKeyCode(), 0); - case RELEASE -> scr.browser.injectKeyReleasedByKeyCode(ev.getKeyCode(), (char) ev.getKeyCode(), 0); - case TYPE -> scr.browser.injectKeyTyped(ev.getKeyCode(), 0); + case PRESS -> scr.browser.injectKeyPressedByKeyCode(ev.getKeyCode(), (char) ev.getKeyCode(), ev.getModifier()); + case RELEASE -> scr.browser.injectKeyReleasedByKeyCode(ev.getKeyCode(), (char) ev.getKeyCode(), ev.getModifier()); + case TYPE -> scr.browser.injectKeyTyped(ev.getKeyCode(), ev.getModifier()); default -> throw new RuntimeException("Invalid type action '" + ev.getAction() + '\''); } } From c9589d9e3c2bcc0cf08a72c9c7a1fd62748f59f4 Mon Sep 17 00:00:00 2001 From: GiantLuigi4 <49770992+GiantLuigi4@users.noreply.github.com> Date: Sun, 5 Mar 2023 21:25:48 -0500 Subject: [PATCH 2/2] better linker handling, remove excess imports in BlockKeyboardRight --- .../montoyo/wd/block/BlockKeyboardRight.java | 8 ------- .../net/montoyo/wd/block/BlockScreen.java | 21 ++++++++++++++++++- .../java/net/montoyo/wd/item/ItemLinker.java | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java index b95dcbe..2fa4400 100644 --- a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java +++ b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java @@ -5,34 +5,26 @@ package net.montoyo.wd.block; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.core.Vec3i; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.DirectionProperty; -import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Material; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraftforge.network.PacketDistributor; -import net.montoyo.wd.core.DefaultPeripheral; import net.montoyo.wd.core.IPeripheral; import net.montoyo.wd.entity.TileEntityKeyboard; -import net.montoyo.wd.init.BlockInit; import net.montoyo.wd.item.ItemLinker; import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.client.CMessageCloseGui; diff --git a/src/main/java/net/montoyo/wd/block/BlockScreen.java b/src/main/java/net/montoyo/wd/block/BlockScreen.java index b060b21..38ff14c 100644 --- a/src/main/java/net/montoyo/wd/block/BlockScreen.java +++ b/src/main/java/net/montoyo/wd/block/BlockScreen.java @@ -126,7 +126,26 @@ public class BlockScreen extends BaseEntityBlock { return ret; } - + + @Override + public void onRemove(BlockState p_60515_, Level p_60516_, BlockPos p_60517_, BlockState p_60518_, boolean p_60519_) { + // TODO: make this also get called on client? + for (BlockSide value : BlockSide.values()) { + Vector3i vec = new Vector3i(p_60517_.getX(), p_60517_.getY(), p_60517_.getZ()); + Multiblock.findOrigin(p_60516_, vec, value, null); + BlockPos bp = new BlockPos(vec.x, vec.y, vec.z); + if (!bp.equals(p_60517_)) { + p_60516_.removeBlockEntity(bp); + p_60516_.setBlock( + bp, p_60516_.getBlockState(bp).setValue(hasTE, false), + 11 + ); + } + } + + super.onRemove(p_60515_, p_60516_, p_60517_, p_60518_, p_60519_); + } + @Override public InteractionResult use(BlockState state, Level world, BlockPos position, Player player, InteractionHand hand, BlockHitResult hit) { ItemStack heldItem = player.getItemInHand(hand); diff --git a/src/main/java/net/montoyo/wd/item/ItemLinker.java b/src/main/java/net/montoyo/wd/item/ItemLinker.java index 3e7579a..68f3fce 100644 --- a/src/main/java/net/montoyo/wd/item/ItemLinker.java +++ b/src/main/java/net/montoyo/wd/item/ItemLinker.java @@ -87,7 +87,7 @@ public class ItemLinker extends Item implements WDItem { } Vector3i pos = new Vector3i(context.getClickedPos()); - BlockSide side = BlockSide.values()[context.getHorizontalDirection().ordinal()]; + BlockSide side = BlockSide.values()[context.getClickedFace().ordinal()]; Multiblock.findOrigin(context.getLevel(), pos, side, null); BlockEntity te = context.getLevel().getBlockEntity(pos.toBlock());