-
-
Save izzyaxel/67abf1acba6d96dba94a to your computer and use it in GitHub Desktop.
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
Preinit | |
snw.registerMessage(MessageRegenMana.RegenManaHandler.class, MessageRegenMana.class, 8, Side.SERVER); | |
snw.registerMessage(MessageSyncMana.ManaSyncHandler.class, MessageSyncMana.class, 9, Side.CLIENT); | |
Server mana packet | |
public class MessageRegenMana implements IMessage | |
{ | |
int id; | |
public MessageRegenMana(){} | |
public MessageRegenMana(int id) | |
{ | |
this.id = id; | |
} | |
@Override | |
public void fromBytes(ByteBuf buf) | |
{ | |
this.id = buf.readInt(); | |
} | |
@Override | |
public void toBytes(ByteBuf buf) | |
{ | |
buf.writeInt(this.id); | |
} | |
public static class RegenManaHandler implements IMessageHandler<MessageRegenMana, IMessage> | |
{ | |
@Override | |
public IMessage onMessage(MessageRegenMana message, MessageContext ctx) | |
{ | |
EntityPlayer player = (EntityPlayer)ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.id); | |
AAExtendedPlayer props = (AAExtendedPlayer)player.getExtendedProperties(AAExtendedPlayer.EEPName); | |
props.addMana(new Mana(ManaType.W, 1)); | |
props.addMana(new Mana(ManaType.U, 1)); | |
props.addMana(new Mana(ManaType.B, 1)); | |
props.addMana(new Mana(ManaType.G, 1)); | |
props.addMana(new Mana(ManaType.R, 1)); | |
ArcaneArtificing.snw.sendToAll(new MessageSyncMana(player.getEntityId(), props.getWhiteMana(), props.getBlueMana(), props.getBlackMana(), props.getGreenMana(), props.getRedMana())); | |
return null; | |
} | |
} | |
} | |
Client sync packet | |
public class MessageSyncMana implements IMessage | |
{ | |
int id, w, u, b, g, r; | |
public MessageSyncMana(){} | |
public MessageSyncMana(int id, int w, int u, int b, int g, int r) | |
{ | |
this.id = id; | |
this.w = w; | |
this.u = u; | |
this.b = b; | |
this.g = g; | |
this.r = r; | |
} | |
@Override | |
public void fromBytes(ByteBuf buf) | |
{ | |
this.id = buf.readInt(); | |
this.w = buf.readInt(); | |
this.u = buf.readInt(); | |
this.b = buf.readInt(); | |
this.g = buf.readInt(); | |
this.r = buf.readInt(); | |
} | |
@Override | |
public void toBytes(ByteBuf buf) | |
{ | |
buf.writeInt(this.id); | |
buf.writeInt(this.w); | |
buf.writeInt(this.u); | |
buf.writeInt(this.b); | |
buf.writeInt(this.g); | |
buf.writeInt(this.r); | |
} | |
public static class ManaSyncHandler implements IMessageHandler<MessageSyncMana, IMessage> | |
{ | |
@Override | |
public IMessage onMessage(MessageSyncMana message, MessageContext ctx) | |
{ | |
Entity entity = ArcaneArtificing.proxy.getClientWorld().getEntityByID(message.id); | |
if(entity instanceof EntityPlayer) | |
{ | |
EntityPlayer player = (EntityPlayer)entity; | |
AAExtendedPlayer props = (AAExtendedPlayer)player.getExtendedProperties(AAExtendedPlayer.EEPName); | |
props.setWhiteMana(message.w); | |
props.setBlueMana(message.u); | |
props.setBlackMana(message.b); | |
props.setGreenMana(message.g); | |
props.setRedMana(message.r); | |
} | |
return null; | |
} | |
} | |
} | |
Persistence events | |
public class ManaPersistence | |
{ | |
@SubscribeEvent | |
public void persistMana(PlayerEvent.Clone event) | |
{ | |
if(!event.wasDeath) | |
{ | |
EntityPlayer playerOrig = event.original; | |
EntityPlayer playerNew = event.entityPlayer; | |
AAExtendedPlayer propsOrig = (AAExtendedPlayer)playerOrig.getExtendedProperties(AAExtendedPlayer.EEPName); | |
playerNew.registerExtendedProperties(AAExtendedPlayer.EEPName, propsOrig); | |
} | |
} | |
@SubscribeEvent | |
public void syncPersistenceToClient(EntityJoinWorldEvent event) | |
{ | |
if(!event.world.isRemote && event.entity instanceof EntityPlayer) | |
{ | |
EntityPlayer player = (EntityPlayer)event.entity; | |
AAExtendedPlayer props = (AAExtendedPlayer)player.getExtendedProperties(AAExtendedPlayer.EEPName); | |
ArcaneArtificing.snw.sendToAll(new MessageSyncMana(player.getEntityId(), props.getWhiteMana(), props.getBlueMana(), props.getBlackMana(), props.getGreenMana(), props.getRedMana())); | |
} | |
} | |
} | |
Keybind | |
@SideOnly(Side.CLIENT) | |
public class GatherManaKeybind extends KeyBinding | |
{ | |
public GatherManaKeybind() | |
{ | |
super("key.gatherMana", Keyboard.KEY_G, "key.categories." + AAReference.MODID); | |
} | |
@SubscribeEvent() | |
public void onKeyInput(TickEvent.ClientTickEvent event) | |
{ | |
if(event.phase == TickEvent.Phase.START && this.getIsKeyPressed()) | |
{ | |
ArcaneArtificing.snw.sendToServer(new MessageRegenMana(ArcaneArtificing.proxy.getClientPlayer().getEntityId())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment