Java tutorial
/* * This class was created by: RANKSHANK as a part of the Arbitraria mod. * Full source can be found @ https://github.com/RANKSHANK/Arbitraria * * Arbitraria is provided in binary and source forms under the Arbitraria License. * Arbitraria License can be found in the repository @ https://github.com/RANKSHANK/Arbitraria */ package mod.rankshank.arbitraria.common.network.packet; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufOutputStream; import mod.rankshank.arbitraria.common.cap.occurrence.CapabilityOccurrence; import mod.rankshank.arbitraria.common.init.Arbitraria; import mod.rankshank.arbitraria.common.occurrence.CurrentOccurrence; import mod.rankshank.arbitraria.common.vars.ArbitraryCaps; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; import net.minecraftforge.fml.client.FMLClientHandler; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; import net.minecraftforge.fml.relauncher.Side; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.Collection; import java.util.UUID; public final class PacketOccurrence implements IMessage { private CurrentOccurrence occurrence; byte flavor; UUID id; Collection<CurrentOccurrence> occurrences; public PacketOccurrence() { } public PacketOccurrence(CurrentOccurrence toCreate) { flavor = 0; this.occurrence = toCreate; } public PacketOccurrence(UUID toKill) { flavor = 1; id = toKill; } public PacketOccurrence(Collection<CurrentOccurrence> toNotify) { flavor = 2; occurrences = toNotify; } @Override public void fromBytes(ByteBuf buf) { DataInputStream data = new DataInputStream(new ByteBufInputStream(buf)); try { flavor = data.readByte(); switch (flavor) { case 0: occurrence = CurrentOccurrence.readFromNBT(CompressedStreamTools.read(data)); break; case 1: id = UUID.fromString(data.readUTF()); break; case 2: NBTTagList list = CompressedStreamTools.read(data).getTagList("data", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < list.tagCount(); i++) occurrences.add(CurrentOccurrence.readFromNBT((NBTTagCompound) list.get(i))); break; default: break; } } catch (IOException e) { Arbitraria.error(new Throwable("Issue reading CurrentOccurrence packet"), e); } finally { try { data.close(); //yeah close up you fucking trollop } catch (IOException e) { //ALREADY ERRED YOU TWAT } } } @Override public void toBytes(ByteBuf buf) { DataOutputStream data = new DataOutputStream(new ByteBufOutputStream(buf)); try { data.writeByte(flavor); switch (flavor) { case 0: CompressedStreamTools.write(occurrence.writeToNBT(), data); break; case 1: data.writeUTF(id.toString()); break; case 2: NBTTagCompound compound = new NBTTagCompound(); NBTTagList list = new NBTTagList(); for (CurrentOccurrence write : occurrences) list.appendTag(write.writeToNBT()); compound.setTag("data", list); CompressedStreamTools.write(compound, data); break; default: break; } } catch (IOException e) { Arbitraria.error(new Throwable("Issue writing CurrentOccurrence packet"), e); } } public static final class PacketOccurrenceHandler implements IMessageHandler<PacketOccurrence, IMessage> { @Override @SuppressWarnings({ "MethodCallSideOnly", "ConstantConditions" }) public IMessage onMessage(PacketOccurrence message, MessageContext ctx) { if (ctx.side == Side.CLIENT) { FMLClientHandler.instance().getClient().addScheduledTask(() -> { CapabilityOccurrence cap = FMLClientHandler.instance().getClient().world .getCapability(ArbitraryCaps.CAPABILITY_OCCURRENCE, null); switch (message.flavor) { case 0: if (message.occurrence != null) cap.addOccurrenceQuietly(message.occurrence); break; case 1: if (message.id != null) cap.removeOccurrenceQuietly(message.id); break; case 2: if (message.occurrences != null) for (CurrentOccurrence occurrence : message.occurrences) cap.addOccurrenceQuietly(occurrence); break; } }); } return null; } } }