Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package flaxbeard.cyberware.common.network;
- import flaxbeard.cyberware.api.CyberwareAPI;
- import io.netty.buffer.ByteBuf;
- import java.util.concurrent.Callable;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.Entity;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraftforge.common.DimensionManager;
- import net.minecraftforge.fml.common.network.ByteBufUtils;
- import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
- import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
- import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
- public class CyberwareSyncPacket implements IMessage
- {
- public CyberwareSyncPacket() {}
- private NBTTagCompound data;
- private int entityId;
- private int dimensionId;
- public CyberwareSyncPacket(NBTTagCompound data, int dimensionId, int entityId)
- {
- this.data = data;
- this.entityId = entityId;
- this.dimensionId = dimensionId;
- }
- @Override
- public void toBytes(ByteBuf buf)
- {
- buf.writeInt(dimensionId);
- buf.writeInt(entityId);
- ByteBufUtils.writeTag(buf, data);
- }
- @Override
- public void fromBytes(ByteBuf buf)
- {
- dimensionId = buf.readInt();
- entityId = buf.readInt();
- data = ByteBufUtils.readTag(buf);
- }
- public static class CyberwareSyncPacketHandler implements IMessageHandler<CyberwareSyncPacket, IMessage>
- {
- @Override
- public IMessage onMessage(CyberwareSyncPacket message, MessageContext ctx)
- {
- Minecraft.getMinecraft().addScheduledTask(new DoSync(message.dimensionId, message.entityId, message.data));
- return null;
- }
- }
- private static class DoSync implements Callable<Void>
- {
- private int dimensionId;
- private int entityId;
- private NBTTagCompound data;
- public DoSync(int dimensionId, int entityId, NBTTagCompound data)
- {
- this.dimensionId = dimensionId;
- this.entityId = entityId;
- this.data = data;
- }
- @Override
- public Void call() throws Exception
- {
- Entity targetEntity = DimensionManager.getWorld(dimensionId).getEntityByID(entityId);
- if (targetEntity != null && CyberwareAPI.hasCapability(targetEntity))
- {
- CyberwareAPI.getCapability(targetEntity).deserializeNBT(data);
- }
- else
- {
- System.out.println("C// " + entityId);
- }
- return null;
- }
- }
- }
- // EVENT HANDLER CODE
- @SubscribeEvent
- public void syncCyberwareData(EntityJoinWorldEvent event)
- {
- Entity e = event.getEntity();
- if (CyberwareAPI.hasCapability(e))
- {
- NBTTagCompound nbt = CyberwareAPI.getCapability(e).serializeNBT();
- CyberwarePacketHandler.INSTANCE.sendToAll(new CyberwareSyncPacket(nbt, e.worldObj.provider.getDimension(), e.getEntityId()));
- System.out.println("S// " + e.getEntityId());
- }
- }
Add Comment
Please, Sign In to add comment