Skip to content

Commit

Permalink
Add Network Primitives
Browse files Browse the repository at this point in the history
Change TypedObject
  • Loading branch information
FirstMegaGame4 committed Dec 2, 2023
1 parent ca7af1b commit 71e44c7
Show file tree
Hide file tree
Showing 20 changed files with 847 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mmodding.mmodding_lib.library.networking;
package com.mmodding.mmodding_lib.library.network.support;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
Expand All @@ -19,6 +19,16 @@ static <T extends NetworkSupport> void register(Identifier identifier, Class<T>
MANAGER.put(type, function);
}

@SuppressWarnings("unchecked")
static <T extends NetworkSupport> Class<T> getType(PacketByteBuf buf) {
PacketByteBuf copied = new PacketByteBuf(buf.copy());
Identifier identifier = copied.readIdentifier();
if (REGISTRY.containsKey(identifier)) {
return (Class<T>) REGISTRY.get(identifier);
}
throw new IllegalArgumentException("Identifier is not present in the NetworkSupport Registry");
}

@SuppressWarnings("unchecked")
static <T extends NetworkSupport> T readComplete(PacketByteBuf buf) {
PacketByteBuf check = new PacketByteBuf(buf.copy());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkBoolean extends NetworkPrimitive<Boolean> implements NetworkSupport {

public static NetworkBoolean of(Boolean value) {
return new NetworkBoolean(value);
}

private NetworkBoolean(Boolean value) {
super(value, PacketByteBuf::writeBoolean);
}

static {
NetworkSupport.register(new Identifier("java", "boolean"), NetworkBoolean.class, buf -> NetworkBoolean.of(buf.readBoolean()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkByte extends NetworkPrimitive<Byte> implements NetworkSupport {

public static NetworkByte of(byte value) {
return new NetworkByte(value);
}

private NetworkByte(byte value) {
super(value, PacketByteBuf::writeByte);
}

static {
NetworkSupport.register(new Identifier("java", "byte"), NetworkByte.class, buf -> NetworkByte.of(buf.readByte()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkByteArray extends NetworkPrimitive<byte[]> implements NetworkSupport {

public static NetworkByteArray of(byte[] value) {
return new NetworkByteArray(value);
}

private NetworkByteArray(byte[] value) {
super(value, PacketByteBuf::writeByteArray);
}

static {
NetworkSupport.register(new Identifier("java", "byte_array"), NetworkByteArray.class, buf -> NetworkByteArray.of(buf.readByteArray()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkDouble extends NetworkPrimitive<Double> implements NetworkSupport {

public static NetworkDouble of(double value) {
return new NetworkDouble(value);
}

private NetworkDouble(double value) {
super(value, PacketByteBuf::writeDouble);
}

static {
NetworkSupport.register(new Identifier("java", "double"), NetworkDouble.class, buf -> NetworkDouble.of(buf.readDouble()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkFloat extends NetworkPrimitive<Float> implements NetworkSupport {

public static NetworkFloat of(float value) {
return new NetworkFloat(value);
}

private NetworkFloat(float value) {
super(value, PacketByteBuf::writeFloat);
}

static {
NetworkSupport.register(new Identifier("java", "float"), NetworkFloat.class, buf -> NetworkFloat.of(buf.readFloat()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkIdentifier extends NetworkPrimitive<Identifier> implements NetworkSupport {

public static NetworkIdentifier of(Identifier value) {
return new NetworkIdentifier(value);
}

private NetworkIdentifier(Identifier value) {
super(value, PacketByteBuf::writeIdentifier);
}

static {
NetworkSupport.register(new Identifier("minecraft", "identifier"), NetworkIdentifier.class, buf -> NetworkIdentifier.of(buf.readIdentifier()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkInteger extends NetworkPrimitive<Integer> implements NetworkSupport {

public static NetworkInteger of(int value) {
return new NetworkInteger(value);
}

private NetworkInteger(int value) {
super(value, PacketByteBuf::writeVarInt);
}

static {
NetworkSupport.register(new Identifier("java", "integer"), NetworkInteger.class, buf -> NetworkInteger.of(buf.readVarInt()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkIntegerArray extends NetworkPrimitive<int[]> implements NetworkSupport {

public static NetworkIntegerArray of(int[] value) {
return new NetworkIntegerArray(value);
}

private NetworkIntegerArray(int[] value) {
super(value, PacketByteBuf::writeIntArray);
}

static {
NetworkSupport.register(new Identifier("java", "int_array"), NetworkIntegerArray.class, buf -> NetworkIntegerArray.of(buf.readIntArray()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mmodding.mmodding_lib.library.network.support.type;

import com.mmodding.mmodding_lib.library.network.support.NetworkSupport;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

public class NetworkItemStack extends NetworkPrimitive<ItemStack> implements NetworkSupport {

public static NetworkItemStack of(ItemStack value) {
return new NetworkItemStack(value);
}

private NetworkItemStack(ItemStack value) {
super(value, PacketByteBuf::writeItemStack);
}

static {
NetworkSupport.register(new Identifier("minecraft", "itemstack"), NetworkItemStack.class, buf -> NetworkItemStack.of(buf.readItemStack()));
}
}
Loading

0 comments on commit 71e44c7

Please sign in to comment.