2016-06-07 18:50:41 -04:00
|
|
|
package WayofTime.bloodmagic.network;
|
|
|
|
|
2016-09-13 16:09:45 -07:00
|
|
|
import WayofTime.bloodmagic.client.key.IKeybindable;
|
|
|
|
import WayofTime.bloodmagic.client.key.KeyBindings;
|
2016-06-07 18:50:41 -04:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class KeyProcessor implements IMessage, IMessageHandler<KeyProcessor, IMessage> {
|
2016-06-07 18:50:41 -04:00
|
|
|
public int keyId;
|
|
|
|
public boolean showInChat;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public KeyProcessor() {
|
2016-06-07 18:50:41 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public KeyProcessor(KeyBindings key, boolean showInChat) {
|
2016-06-07 18:50:41 -04:00
|
|
|
this.keyId = key.ordinal();
|
|
|
|
this.showInChat = showInChat;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void fromBytes(ByteBuf buf) {
|
2016-06-07 18:50:41 -04:00
|
|
|
this.keyId = buf.readInt();
|
|
|
|
this.showInChat = buf.readBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void toBytes(ByteBuf buf) {
|
2016-06-07 18:50:41 -04:00
|
|
|
buf.writeInt(this.keyId);
|
|
|
|
buf.writeBoolean(this.showInChat);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public IMessage onMessage(KeyProcessor msg, MessageContext ctx) {
|
2017-08-14 20:53:42 -07:00
|
|
|
EntityPlayer entityPlayer = ctx.getServerHandler().player;
|
2016-06-07 18:50:41 -04:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (entityPlayer != null) {
|
2016-06-07 18:50:41 -04:00
|
|
|
ItemStack heldStack = entityPlayer.getHeldItemMainhand();
|
2017-08-15 21:30:48 -07:00
|
|
|
if (heldStack.getItem() instanceof IKeybindable) {
|
|
|
|
if (msg.keyId < 0 || msg.keyId >= KeyBindings.values().length) {
|
2016-06-07 18:50:41 -04:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-13 16:09:45 -07:00
|
|
|
KeyBindings key = KeyBindings.values()[msg.keyId];
|
2016-06-07 18:50:41 -04:00
|
|
|
((IKeybindable) heldStack.getItem()).onKeyPressed(heldStack, entityPlayer, key, msg.showInChat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|