InputDevicePacket

This commit is contained in:
somehussar committed 2025-02-03 01:32:49 +01:00
1 parent 42fe078efd
commit fb32ca6e29
7 files changed
+230 -106

No files matched your search

@@ -16,6 +16,7 @@ import kamkeel.npcs.network.packets.data.npc.*;
import kamkeel.npcs.network.packets.data.script.*;
import kamkeel.npcs.network.packets.player.BankPacket;
import kamkeel.npcs.network.packets.player.FollowerPacket;
import kamkeel.npcs.network.packets.player.InputDevicePacket;
import kamkeel.npcs.network.packets.player.TransportPacket;
import kamkeel.npcs.network.packets.request.*;
import kamkeel.npcs.network.packets.request.animation.*;
@@ -341,6 +342,8 @@ public class PacketHandler {
PLAYER_PACKET.registerPacket(new TransportPacket());
PLAYER_PACKET.registerPacket(new BankPacket());
PLAYER_PACKET.registerPacket(new InputDevicePacket());
}
private void registerChannels() {
@@ -14,8 +14,7 @@ public enum EnumPlayerPacket {
SaveBook, // TODO
Companion, // TODO: OpenInv, TalentExp
RoleGet, // TODO
KeyPressed, // TODO
MouseClicked, // TODO
InputDevice,
ScreenSize; // TODO
@@ -5,6 +5,7 @@ import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumPlayerPacket;
import net.minecraft.entity.player.EntityPlayer;
@@ -38,14 +39,14 @@ public class BankPacket extends AbstractPacket {
return true;
}
public static BankPacket Open(int bankID, int slotID) {
return new BankPacket(Action.OpenSlot, bankID, slotID);
public static void Open(int bankID, int slotID) {
PacketClient.sendClient(new BankPacket(Action.OpenSlot, bankID, slotID));
}
public static BankPacket Upgrade() {
return new BankPacket(Action.Upgrade, -1, -1);
public static void Upgrade() {
PacketClient.sendClient(new BankPacket(Action.Upgrade, -1, -1));
}
public static BankPacket Unlock() {
return new BankPacket(Action.Unlock, -1, -1);
public static void Unlock() {
PacketClient.sendClient(new BankPacket(Action.Unlock, -1, -1));
}
@Override
@@ -0,0 +1,149 @@
package kamkeel.npcs.network.packets.player;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumPlayerPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import noppes.npcs.EventHooks;
import org.lwjgl.input.Keyboard;
import java.io.IOException;
public class InputDevicePacket extends AbstractPacket {
public static final String packetName = "Player|InputDevice";
private Type type;
private int key, scroll;
private boolean isKeyPressed, ctrlPressed, shiftPressed, altPressed, metaPressed;
private String keysDownList;
public InputDevicePacket() {
}
private InputDevicePacket(Type type, int key, boolean isDown) {
this.type = type;
this.key = key;
this.isKeyPressed = isDown;
}
public static void sendMouse(int mouseButton, int scroll, boolean isDown) {
InputDevicePacket packet = new InputDevicePacket(Type.MOUSE, mouseButton, isDown);
packet.scroll = scroll;
packet.commonDataGrabber();
PacketClient.sendClient(packet);
}
public static void sendKeyboard(int key, boolean isDown) {
InputDevicePacket packet = new InputDevicePacket(Type.KEYBOARD, key, isDown);
packet.commonDataGrabber();
PacketClient.sendClient(packet);
}
private void commonDataGrabber() {
ctrlPressed = Keyboard.isKeyDown(157) || Keyboard.isKeyDown(29);
shiftPressed = Keyboard.isKeyDown(54) || Keyboard.isKeyDown(42);
altPressed = Keyboard.isKeyDown(184) || Keyboard.isKeyDown(56);
metaPressed = Keyboard.isKeyDown(220) || Keyboard.isKeyDown(219);
StringBuilder keysDownString = new StringBuilder();
for (int i = 0; i < Keyboard.getKeyCount(); i++) {//Creates a comma separated string of the integer IDs of held keys
if (Keyboard.isKeyDown(i)) {
keysDownString.append(Integer.valueOf(i)).append(",");
}
}
if (keysDownString.length() > 0) {//Removes last comma for later parsing
keysDownString.deleteCharAt(keysDownString.length() - 1);
}
keysDownList = keysDownString.toString();
}
@Override
public Enum getType() {
return EnumPlayerPacket.InputDevice;
}
@Override
public PacketChannel getChannel() {
return PacketHandler.PLAYER_PACKET;
}
@Override
@SideOnly(Side.CLIENT)
public void sendData(ByteBuf out) throws IOException {
out.writeInt(type.ordinal());
out.writeInt(key);
out.writeBoolean(isKeyPressed);
if (type == Type.MOUSE) {
out.writeInt(scroll);
}
out.writeBoolean(ctrlPressed);
out.writeBoolean(shiftPressed);
out.writeBoolean(altPressed);
out.writeBoolean(metaPressed);
ByteBufUtils.writeString(out, keysDownList);
}
@Override
public void receiveData(ByteBuf in, EntityPlayer player) throws IOException {
if (!(player instanceof EntityPlayerMP))
return;
EntityPlayerMP playerMP = (EntityPlayerMP) player;
Type incomingType = Type.values()[in.readInt()];
boolean isMouse = incomingType == Type.MOUSE;
int inputKey = in.readInt();
boolean isKeyDown = in.readBoolean();
int scroll = isMouse ? in.readInt() : 0;
boolean ctrl = in.readBoolean();
boolean shift = in.readBoolean();
boolean alt = in.readBoolean();
boolean meta = in.readBoolean();
int[] pressedKeys = getPressedArray(ByteBufUtils.readString(in));
if (isMouse)
EventHooks.onPlayerMouseClicked(playerMP, inputKey, scroll, isKeyDown, ctrl, shift, alt, meta, pressedKeys);
else
EventHooks.onPlayerKeyPressed(playerMP, inputKey, ctrl, shift, alt, meta, isKeyDown, pressedKeys);
}
private static int[] getPressedArray(String pressedKeys) {
if(pressedKeys == null)
return new int[0];
String[] split = pressedKeys.split(",");
int[] keysDown;
if(pressedKeys.length() > 0) {
keysDown = new int[split.length];
try {
for (int i = 0; i < split.length; i++) {
keysDown[i] = Integer.parseInt(split[i]);
}
}
catch (NumberFormatException ignored){
}
} else {
keysDown = new int[0];
}
return keysDown;
}
private enum Type {
MOUSE,
KEYBOARD
}
}
@@ -62,70 +62,70 @@ public class PacketHandlerPlayer{
return;
NoppesUtilServer.sendOpenGui(player, EnumGuiType.CompanionInv, npc);
}
else if(type == EnumPlayerPacket.KeyPressed) {
if(ScriptController.Instance.languages.isEmpty()) {
return;
}
int button = buffer.readInt();
boolean isCtrlPressed = buffer.readBoolean();
boolean isShiftPressed = buffer.readBoolean();
boolean isAltPressed = buffer.readBoolean();
boolean isMetaPressed = buffer.readBoolean();
boolean buttonDown = buffer.readBoolean();
String ints = ByteBufUtils.readString(buffer);
String[] split = ints.split(",");
int[] keysDown;
if(ints.length() > 0) {
keysDown = new int[split.length];
try {
for (int i = 0; i < split.length; i++) {
keysDown[i] = Integer.parseInt(split[i]);
}
}
catch (NumberFormatException ignored){
}
} else {
keysDown = new int[0];
}
EventHooks.onPlayerKeyPressed(player, button, isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, buttonDown, keysDown);
}
else if(type == EnumPlayerPacket.MouseClicked) {
if(ScriptController.Instance.languages.isEmpty()) {
return;
}
int button = buffer.readInt();
int mouseWheel = buffer.readInt();
boolean buttonDown = buffer.readBoolean();
boolean isCtrlPressed = buffer.readBoolean();
boolean isShiftPressed = buffer.readBoolean();
boolean isAltPressed = buffer.readBoolean();
boolean isMetaPressed = buffer.readBoolean();
String ints = ByteBufUtils.readString(buffer);
String[] split = ints.split(",");
int[] keysDown;
if(ints.length() > 0) {
keysDown = new int[split.length];
try {
for (int i = 0; i < split.length; i++) {
keysDown[i] = Integer.parseInt(split[i]);
}
}
catch (NumberFormatException ignored){
}
} else {
keysDown = new int[0];
}
EventHooks.onPlayerMouseClicked(player, button, mouseWheel, buttonDown, isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, keysDown);
}
// else if(type == EnumPlayerPacket.KeyPressed) {
// if(ScriptController.Instance.languages.isEmpty()) {
// return;
// }
//
// int button = buffer.readInt();
// boolean isCtrlPressed = buffer.readBoolean();
// boolean isShiftPressed = buffer.readBoolean();
// boolean isAltPressed = buffer.readBoolean();
// boolean isMetaPressed = buffer.readBoolean();
// boolean buttonDown = buffer.readBoolean();
//
// String ints = ByteBufUtils.readString(buffer);
// String[] split = ints.split(",");
// int[] keysDown;
//
// if(ints.length() > 0) {
// keysDown = new int[split.length];
// try {
// for (int i = 0; i < split.length; i++) {
// keysDown[i] = Integer.parseInt(split[i]);
// }
// }
// catch (NumberFormatException ignored){
// }
// } else {
// keysDown = new int[0];
// }
//
// EventHooks.onPlayerKeyPressed(player, button, isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, buttonDown, keysDown);
// }
// else if(type == EnumPlayerPacket.MouseClicked) {
// if(ScriptController.Instance.languages.isEmpty()) {
// return;
// }
//
// int button = buffer.readInt();
// int mouseWheel = buffer.readInt();
// boolean buttonDown = buffer.readBoolean();
//
// boolean isCtrlPressed = buffer.readBoolean();
// boolean isShiftPressed = buffer.readBoolean();
// boolean isAltPressed = buffer.readBoolean();
// boolean isMetaPressed = buffer.readBoolean();
//
// String ints = ByteBufUtils.readString(buffer);
// String[] split = ints.split(",");
// int[] keysDown;
//
// if(ints.length() > 0) {
// keysDown = new int[split.length];
// try {
// for (int i = 0; i < split.length; i++) {
// keysDown[i] = Integer.parseInt(split[i]);
// }
// }
// catch (NumberFormatException ignored){
// }
// } else {
// keysDown = new int[0];
// }
//
// EventHooks.onPlayerMouseClicked(player, button, mouseWheel, buttonDown, isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, keysDown);
// }
// else if(type == EnumPlayerPacket.FollowerHire){
// EntityNPCInterface npc = NoppesUtilServer.getEditingNpc(player);
// if(npc == null || npc.advanced.role != EnumRoleType.Follower)
@@ -6,6 +6,7 @@ import cpw.mods.fml.common.gameevent.InputEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import kamkeel.npcs.addon.client.DBCClient;
import kamkeel.npcs.network.packets.player.InputDevicePacket;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.inventory.ContainerPlayer;
@@ -80,22 +81,7 @@ public class ClientTickHandler{
if(Mouse.getEventButton() == -1 && Mouse.getEventDWheel() == 0)
return;
boolean isCtrlPressed = Keyboard.isKeyDown(157) || Keyboard.isKeyDown(29);
boolean isShiftPressed = Keyboard.isKeyDown(54) || Keyboard.isKeyDown(42);
boolean isAltPressed = Keyboard.isKeyDown(184) || Keyboard.isKeyDown(56);
boolean isMetaPressed = Keyboard.isKeyDown(220) || Keyboard.isKeyDown(219);
StringBuilder keysDownString = new StringBuilder();
for (int i = 0; i < Keyboard.getKeyCount(); i++) {//Creates a comma separated string of the integer IDs of held keys
if (Keyboard.isKeyDown(i)) {
keysDownString.append(Integer.valueOf(i)).append(",");
}
}
if (keysDownString.length() > 0) {//Removes last comma for later parsing
keysDownString.deleteCharAt(keysDownString.length() - 1);
}
NoppesUtilPlayer.sendData(EnumPlayerPacket.MouseClicked, Mouse.getEventButton(),Mouse.getEventDWheel(),Mouse.isButtonDown(Mouse.getEventButton()), isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, keysDownString.toString());
InputDevicePacket.sendMouse(Mouse.getEventButton(),Mouse.getEventDWheel(),Mouse.isButtonDown(Mouse.getEventButton()));
}
@SubscribeEvent
@@ -127,23 +113,9 @@ public class ClientTickHandler{
if(!Keyboard.isRepeatEvent()) {
int key = Keyboard.getEventKey();
boolean isCtrlPressed = Keyboard.isKeyDown(157) || Keyboard.isKeyDown(29);
boolean isShiftPressed = Keyboard.isKeyDown(54) || Keyboard.isKeyDown(42);
boolean isAltPressed = Keyboard.isKeyDown(184) || Keyboard.isKeyDown(56);
boolean isMetaPressed = Keyboard.isKeyDown(220) || Keyboard.isKeyDown(219);
boolean keyDown = Keyboard.isKeyDown(key);
StringBuilder keysDownString = new StringBuilder();
for (int i = 0; i < Keyboard.getKeyCount(); i++) {//Creates a comma separated string of the integer IDs of held keys
if (Keyboard.isKeyDown(i)) {
keysDownString.append(Integer.valueOf(i)).append(",");
}
}
if (keysDownString.length() > 0) {//Removes last comma for later parsing
keysDownString.deleteCharAt(keysDownString.length() - 1);
}
NoppesUtilPlayer.sendData(EnumPlayerPacket.KeyPressed, key, isCtrlPressed, isShiftPressed, isAltPressed, isMetaPressed, keyDown, keysDownString.toString());
InputDevicePacket.sendKeyboard(key, keyDown);
}
}
@@ -72,13 +72,13 @@ public class GuiNPCBankChest extends GuiContainerNPCInterface implements IGuiDat
int id = guibutton.id;
if(id < 6){
close();
PacketClient.sendClient(BankPacket.Open(container.bankid, id));
BankPacket.Open(container.bankid, id);
}
if(id == 8){
PacketClient.sendClient(BankPacket.Unlock());
BankPacket.Unlock();
}
if(id == 9){
PacketClient.sendClient(BankPacket.Upgrade());
BankPacket.Upgrade();
}