-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
307e148
commit 84db8af
Showing
6 changed files
with
171 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/gregtechceu/gtceu/common/network/packets/CPacketKeysDown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
43 changes: 15 additions & 28 deletions
43
src/main/java/com/gregtechceu/gtceu/common/network/packets/CPacketKeysPressed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gregtechceu/gtceu/utils/input/IKeyPressedListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.