Skip to content

Commit

Permalink
KeyPressed / KeyDown Packet Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghostipedia committed May 31, 2024
1 parent 307e148 commit 84db8af
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.gregtechceu.gtceu.api.item.tool.aoe.AoESymmetrical;
import com.gregtechceu.gtceu.client.renderer.item.ToolItemRenderer;
import com.gregtechceu.gtceu.common.item.tool.behavior.ToolModeSwitchBehavior;
import com.gregtechceu.gtceu.utils.input.IKeyPressedListener;
import com.gregtechceu.gtceu.utils.input.KeyBind;
import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.gui.factory.HeldItemUIFactory;
Expand All @@ -17,11 +18,10 @@
import com.lowdragmc.lowdraglib.gui.widget.LabelWidget;
import lombok.Getter;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.Minecraft;
import net.minecraft.client.ToggleKeyMapping;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
Expand All @@ -33,14 +33,13 @@
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.naming.ldap.LdapContext;

import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.*;
import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.LAST_CRAFTING_USE_KEY;
import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.getBehaviorsTag;
import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.getMaxAoEDefinition;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class GTOmniToolItem extends GTToolItem implements IGTTool {
public class GTOmniToolItem extends GTToolItem implements IGTTool, IKeyPressedListener {
@Getter
private final GTToolType toolType;
@Getter
Expand All @@ -49,7 +48,7 @@ public class GTOmniToolItem extends GTToolItem implements IGTTool {
private final int electricTier;
@Getter
private final IGTToolDefinition omniToolStats;

private static boolean onInit = false;
protected GTOmniToolItem(GTToolType toolType, MaterialToolTier tier, Material material, IGTToolDefinition toolStats, Properties properties) {
super(toolType, tier, material ,toolStats, properties);
this.material = material;
Expand All @@ -73,24 +72,26 @@ protected GTOmniToolItem(GTToolType toolType, MaterialToolTier tier, Material ma
public static GTOmniToolItem create(GTToolType toolType, MaterialToolTier tier, Material material, IGTToolDefinition toolStats, Item.Properties properties){
return new GTOmniToolItem(toolType, tier, material, toolStats, properties);
}

@Override
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotId, boolean isSelected) {
super.inventoryTick(stack, level, entity, slotId, isSelected);
CompoundTag data = stack.getOrCreateTag();
byte toggleTimer = data.contains("toggleTimer") ? data.getByte("toggleTimer") : 0;

if (entity instanceof Player player && KeyBind.QUARK_TOOL_MODE_SWITCH.isKeyDown(player) && toggleTimer == 0){
toggleTimer = 20;
var tagCompound = getBehaviorsTag(stack);
public void onKeyPressed(ServerPlayer player, KeyBind keyPressed) {
if (keyPressed.isPressed()){
var stackHand = player.getMainHandItem();
CompoundTag data = stackHand.getOrCreateTag();
var tagCompound = getBehaviorsTag(stackHand);
tagCompound.putByte("OmniToolMode", (byte) ((tagCompound.getByte("OmniToolMode") + 1) % ToolModeSwitchBehavior.OmniModeType.values().length));
player.displayClientMessage(Component.translatable("metaitem.machine_configuration.mode", ToolModeSwitchBehavior.OmniModeType.values()[tagCompound.getByte("OmniToolMode")].getName()), true);
setLastCraftingSoundTime(stack);
setLastCraftingSoundTime(stackHand);
}
if (!level.isClientSide && toggleTimer > 0) {
--toggleTimer;
data.putByte("toggleTimer", toggleTimer);
}
@Override
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slotId, boolean isSelected) {
super.inventoryTick(stack, level, entity, slotId, isSelected);
if (!onInit){
//Registers the listener then stops after the first tick. This is kinda hacky but it works.
KeyBind.QUARK_TOOL_MODE_SWITCH.registerListener((ServerPlayer) entity,this);
onInit = true;
}

}

@Override
Expand Down Expand Up @@ -147,4 +148,6 @@ public ModularUI createUI(Player entityPlayer, HeldItemUIFactory.HeldItemHolder
.widget(new LabelWidget(93, 65, () ->
Integer.toString(1 + AoESymmetrical.getLayer(getBehaviorsTag(holder.getHeld()), defaultDefinition))));
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gregtechceu.gtceu.common.network;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.common.network.packets.CPacketKeysDown;
import com.gregtechceu.gtceu.common.network.packets.CPacketKeysPressed;
import com.gregtechceu.gtceu.common.network.packets.SPacketSyncBedrockOreVeins;
import com.gregtechceu.gtceu.common.network.packets.SPacketSyncFluidVeins;
Expand All @@ -15,6 +16,7 @@ public class GTNetwork {

public static void init() {
NETWORK.registerC2S(CPacketKeysPressed.class);
NETWORK.registerC2S(CPacketKeysDown.class);
NETWORK.registerC2S(SPacketSyncOreVeins.class);
NETWORK.registerC2S(SPacketSyncFluidVeins.class);
NETWORK.registerC2S(SPacketSyncBedrockOreVeins.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.gregtechceu.gtceu.common.network.packets;

import com.gregtechceu.gtceu.utils.input.KeyBind;
import com.lowdragmc.lowdraglib.networking.IHandlerContext;
import com.lowdragmc.lowdraglib.networking.IPacket;
import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
import lombok.NoArgsConstructor;
import net.minecraft.network.FriendlyByteBuf;

@NoArgsConstructor
public class CPacketKeysDown implements IPacket {
private Int2BooleanMap updateKeys;

public CPacketKeysDown(Int2BooleanMap updateKeys) {
this.updateKeys = updateKeys;
}

@Override
public void encode(FriendlyByteBuf buf) {
buf.writeVarInt(updateKeys.size());
for (var entry : updateKeys.int2BooleanEntrySet()) {
buf.writeVarInt(entry.getIntKey());
buf.writeBoolean(entry.getBooleanValue());
}
}

@Override
public void decode(FriendlyByteBuf buf) {
this.updateKeys = new Int2BooleanOpenHashMap();
int size = buf.readVarInt();
for (int i = 0; i < size; i++) {
updateKeys.put(buf.readVarInt(), buf.readBoolean());
}
}

@Override
public void execute(IHandlerContext handler) {
KeyBind[] keybinds = KeyBind.VALUES;
for (var entry : updateKeys.int2BooleanEntrySet()) {
keybinds[entry.getIntKey()].updateKeyDown(entry.getBooleanValue(), handler.getPlayer());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,45 @@
package com.gregtechceu.gtceu.common.network.packets;

import com.gregtechceu.gtceu.utils.input.KeyBind;

import com.lowdragmc.lowdraglib.networking.IHandlerContext;
import com.lowdragmc.lowdraglib.networking.IPacket;

import net.minecraft.network.FriendlyByteBuf;

import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import lombok.NoArgsConstructor;

import java.util.List;
import net.minecraft.network.FriendlyByteBuf;

@NoArgsConstructor
public class CPacketKeysPressed implements IPacket {

private Object updateKeys;
private IntList pressedKeys;


public CPacketKeysPressed(List<KeyBind> updateKeys) {
this.updateKeys = updateKeys;
public CPacketKeysPressed(IntList pressedKeys) {
this.pressedKeys = pressedKeys;
}

@Override
public void encode(FriendlyByteBuf buf) {
List<KeyBind> updateKeys = (List<KeyBind>) this.updateKeys;
buf.writeVarInt(updateKeys.size());
for (KeyBind keyBind : updateKeys) {
buf.writeVarInt(keyBind.ordinal());
buf.writeBoolean(keyBind.isPressed());
buf.writeBoolean(keyBind.isKeyDown());
buf.writeVarInt(pressedKeys.size());
for (int key : pressedKeys) {
buf.writeVarInt(key);
}
}

@Override
public void decode(FriendlyByteBuf buf) {
this.updateKeys = new Pair[KeyBind.VALUES.length];
Pair<Boolean, Boolean>[] updateKeys = (Pair<Boolean, Boolean>[]) this.updateKeys;
pressedKeys = new IntArrayList();
int size = buf.readVarInt();
for (int i = 0; i < size; i++) {
updateKeys[buf.readVarInt()] = Pair.of(buf.readBoolean(), buf.readBoolean());
pressedKeys.add(buf.readVarInt());
}
}

@Override
public void execute(IHandlerContext handler) {
if (handler.getPlayer() != null) {
KeyBind[] keybinds = KeyBind.VALUES;
Pair<Boolean, Boolean>[] updateKeys = (Pair<Boolean, Boolean>[]) this.updateKeys;
for (int i = 0; i < updateKeys.length; i++) {
Pair<Boolean, Boolean> pair = updateKeys[i];
if (pair != null) {
keybinds[i].update(pair.getFirst(), pair.getSecond(), handler.getPlayer());
}
}
KeyBind[] keyBinds = KeyBind.VALUES;
for (int index : pressedKeys) {
keyBinds[index].onKeyPressed(handler.getPlayer());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gregtechceu.gtceu.utils.input;

import net.minecraft.server.level.ServerPlayer;

public interface IKeyPressedListener {
void onKeyPressed(ServerPlayer player, KeyBind keyPressed);
}
Loading

0 comments on commit 84db8af

Please sign in to comment.