- 3
- 0
Отправляю этот пакет с клиента из эвента InputEvent.KeyInputEvent:
public class Packet implements IMessage {
private int entityId;
private String packetName;
public Packet() {}
public Packet(int entityId, String packetName) {
this.entityId = entityId;
this.packetName = packetName;
}
@Override
public void fromBytes(ByteBuf buf) {
entityId = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(entityId);
}
public static class Handler
implements IMessageHandler<PacketAbilityEnable, IMessage> {
@Override
public IMessage onMessage(PacketAbilityEnable message, MessageContext ctx) {
World world = ctx.getServerHandler().playerEntity.worldObj;
Entity entity = world.getEntityByID(message.entityId);
if (entity != null) {
entity.getEntityData().setBoolean(message.packetName, true);
}
return null;
}
}
}
Сам код эвента и отправки пакета, нажатие на кнопку определяется, но с пакетом видимо что то не то:
@SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent event) {
EntityPlayer player = mc.thePlayer;
Hero hero = HeroTracker.get(player);
if (hero != null) {
for (Ability ability : AbilityRegister.ability) {
if (hero.getKey(player, ability.KEY) != null) {
if (ability.getActive()
|| hero.getKey(player, ability.KEY).getIsKeyPressed()) {
Main.NETWORK.sendToServer(new Packet(player.getEntityId(), ability.KEY + "_onTick"));
} else {
}
if (ability.type == Ability.Type.PRESSED) {
if (isKeyBindDown(hero.getKey(player, ability.KEY).getKeyCode())) {
if (ability.getActive()) {
} else {
Main.NETWORK.sendToServer(new Packet( player.getEntityId(), ability.KEY + "_onEnable"));
}
Main.NETWORK.sendToServer(new Packet(player.getEntityId(), ability.KEY + "_onClick"));
ability.setToggle();
}
}
}
}
}
}
private boolean keyStates[] = new boolean[256];
public boolean isKeyBindDown(int key) {
if (Keyboard.isKeyDown(key) != keyStates[key]) {
return keyStates[key] = !keyStates[key];
} else {
return false;
}
}
Player.Tick тут и должен чекаться nbt, но не работает :(:
@SubscribeEvent
public void onEvent(TickEvent.PlayerTickEvent event) {
EntityPlayer player = event.player;
for (Ability ability : AbilityRegister.ability) {
if (player.getEntityData().hasKey(ability.KEY + "_onTick")) {
if (player.getEntityData().getBoolean(ability.KEY + "_onTick")) {
ability.onTick(hero, player);
}
}
if (player.getEntityData().hasKey(ability.KEY + "_onClick")) {
if (player.getEntityData().getBoolean(ability.KEY + "_onClick")) {
ability.onClick(hero, player);
player.getEntityData().setBoolean(ability.KEY, false);
}
}
}
}
Сама регистрация в прокси:
public class ServerProxy {
public void preInit() {
int packetId = 0;
Main.NETWORK.registerMessage(Packet.Handler.class,
Packet.class, packetId++, Side.SERVER);
}
public void init() {
AbilityRegister.register();
AttributeRegister.register();
MinecraftForge.EVENT_BUS.register(new CommonEventHandler());
FMLCommonHandler.instance().bus().register(new CommonEventHandler());
}
}