package info.u_team.u_team_core.api.sync;

import java.util.function.*;

import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketBuffer;

public abstract class BufferReferenceHolder {
	
	private PacketBuffer lastKnownValue;
	
	public abstract PacketBuffer get();
	
	public abstract void set(PacketBuffer buffer);
	
	public boolean isDirty() {
		final PacketBuffer buffer = get();
		final boolean dirty = !buffer.equals(lastKnownValue);
		lastKnownValue = buffer;
		return dirty;
	}
	
	public static final BufferReferenceHolder createHolder(Supplier<PacketBuffer> get, Consumer<PacketBuffer> set) {
		return new BufferReferenceHolder() {
			
			@Override
			public PacketBuffer get() {
				return get.get();
			}
			
			@Override
			public void set(PacketBuffer buffer) {
				set.accept(buffer);
			}
		};
	}
	
	public static final BufferReferenceHolder createIntHolder(IntSupplier get, IntConsumer set) {
		return new BufferReferenceHolder() {
			
			@Override
			public PacketBuffer get() {
				return new PacketBuffer(Unpooled.copyInt(get.getAsInt()));
			}
			
			@Override
			public void set(PacketBuffer buffer) {
				set.accept(buffer.readInt());
			}
		};
	}
	
	public static final BufferReferenceHolder createShortHolder(Supplier<Short> get, Consumer<Short> set) {
		return new BufferReferenceHolder() {
			
			@Override
			public PacketBuffer get() {
				return new PacketBuffer(Unpooled.copyShort(get.get()));
			}
			
			@Override
			public void set(PacketBuffer buffer) {
				set.accept(buffer.readShort());
			}
		};
	}
	
	public static final BufferReferenceHolder createLongHolder(LongSupplier get, LongConsumer set) {
		return new BufferReferenceHolder() {
			
			@Override
			public PacketBuffer get() {
				return new PacketBuffer(Unpooled.copyLong(get.getAsLong()));
			}
			
			@Override
			public void set(PacketBuffer buffer) {
				set.accept(buffer.readLong());
			}
		};
	}
	
}

@@ -1165,32 +1165,405 @@
 %0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A%09%0A
+%09public static final BufferReferenceHolder createByteHolder(Supplier%3CByte%3E get, Consumer%3CByte%3E set) %7B%0A%09%09return new BufferReferenceHolder() %7B%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public PacketBuffer get() %7B%0A%09%09%09%09return new PacketBuffer(Unpooled.buffer(1).writeByte(get.get()));%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public void set(PacketBuffer buffer) %7B%0A%09%09%09%09set.accept(buffer.readByte());%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A%09%0A
 %09public static f

Add byte holder