Add ClientNetworkManager

This commit is contained in:
modmuss50 2022-03-02 14:39:23 +00:00
parent 08ed4abcd4
commit 0a8e34d9c6
5 changed files with 28 additions and 51 deletions

View file

@ -38,8 +38,6 @@ import java.util.function.Function;
* Created by Gigabit101 on 16/08/2016. * Created by Gigabit101 on 16/08/2016.
*/ */
public class RebornRegistry { public class RebornRegistry {
//public static LootManager.InnerPool lp = new LootManager.InnerPool();
//Yeah, this is horrible //Yeah, this is horrible
private static final HashMap<Object, Identifier> objIdentMap = new HashMap<>(); private static final HashMap<Object, Identifier> objIdentMap = new HashMap<>();
@ -125,16 +123,4 @@ public class RebornRegistry {
public static void registerIdent(Object object, Identifier identifier){ public static void registerIdent(Object object, Identifier identifier){
objIdentMap.put(object, identifier); objIdentMap.put(object, identifier);
} }
//eg: RebornRegistry.addLoot(Items.NETHER_STAR, 0.95, LootTableList.CHESTS_VILLAGE_BLACKSMITH);
//eg: RebornRegistry.addLoot(Items.DIAMOND, 1.95, LootTableList.ENTITIES_COW);
public static void addLoot(Item item, double chance, Identifier list) {
// lp.addItem(LootManager.createLootEntry(item, chance, list));
}
public static void addLoot(Item item, int minSize, int maxSize, double chance, Identifier list) {
// lp.addItem(LootManager.createLootEntry(item, minSize, maxSize, chance, list));
}
} }

View file

@ -58,7 +58,7 @@ public class ClientBoundPacketHandlers {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientBoundPacketHandlers.class); private static final Logger LOGGER = LoggerFactory.getLogger(ClientBoundPacketHandlers.class);
public static void init() { public static void init() {
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "custom_description"), (client, handler, packetBuffer, responseSender) -> { ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "custom_description"), (client, handler, packetBuffer, responseSender) -> {
BlockPos pos = packetBuffer.readBlockPos(); BlockPos pos = packetBuffer.readBlockPos();
NbtCompound tagCompound = packetBuffer.readNbt(); NbtCompound tagCompound = packetBuffer.readNbt();
client.execute(() -> { client.execute(() -> {
@ -72,7 +72,7 @@ public class ClientBoundPacketHandlers {
}); });
}); });
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "fluid_config_sync"), (client, handler, packetBuffer, responseSender) -> { ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "fluid_config_sync"), (client, handler, packetBuffer, responseSender) -> {
BlockPos pos = packetBuffer.readBlockPos(); BlockPos pos = packetBuffer.readBlockPos();
NbtCompound compoundTag = packetBuffer.readNbt(); NbtCompound compoundTag = packetBuffer.readNbt();
@ -93,7 +93,7 @@ public class ClientBoundPacketHandlers {
}); });
}); });
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "slot_sync"), (client, handler, packetBuffer, responseSender) -> { ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "slot_sync"), (client, handler, packetBuffer, responseSender) -> {
BlockPos pos = packetBuffer.readBlockPos(); BlockPos pos = packetBuffer.readBlockPos();
NbtCompound compoundTag = packetBuffer.readNbt(); NbtCompound compoundTag = packetBuffer.readNbt();
@ -111,7 +111,7 @@ public class ClientBoundPacketHandlers {
}); });
}); });
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "send_object"), (client, handler, packetBuffer, responseSender) -> { ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "send_object"), (client, handler, packetBuffer, responseSender) -> {
int size = packetBuffer.readInt(); int size = packetBuffer.readInt();
ExtendedPacketBuffer epb = new ExtendedPacketBuffer(packetBuffer); ExtendedPacketBuffer epb = new ExtendedPacketBuffer(packetBuffer);
Int2ObjectMap<Object> updatedValues = new Int2ObjectOpenHashMap<>(); Int2ObjectMap<Object> updatedValues = new Int2ObjectOpenHashMap<>();
@ -141,9 +141,9 @@ public class ClientBoundPacketHandlers {
}); });
}); });
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "sync_chunks"), ChunkLoaderManager.CODEC, ClientChunkManager::setLoadedChunks); ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "sync_chunks"), ChunkLoaderManager.CODEC, ClientChunkManager::setLoadedChunks);
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "no_spam_chat"), (client, handler, buf, responseSender) -> { ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "no_spam_chat"), (client, handler, buf, responseSender) -> {
final int messageId = buf.readInt(); final int messageId = buf.readInt();
final Text text = buf.readText(); final Text text = buf.readText();
@ -155,7 +155,7 @@ public class ClientBoundPacketHandlers {
}); });
}); });
NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "stacks_to_render"), Codec.list(ItemStack.CODEC), ItemStackRenderManager.RENDER_QUEUE::addAll); ClientNetworkManager.registerClientBoundHandler(new Identifier("reborncore", "stacks_to_render"), Codec.list(ItemStack.CODEC), ItemStackRenderManager.RENDER_QUEUE::addAll);
} }
} }

View file

@ -22,19 +22,27 @@
* SOFTWARE. * SOFTWARE.
*/ */
package reborncore; package reborncore.client;
import com.mojang.serialization.Codec;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.util.Identifier;
import reborncore.common.network.ExtendedPacketBuffer;
public enum Distribution { import java.util.function.Consumer;
UNIVERSAL,
CLIENT,
SERVER;
public boolean isInvalid() { @Environment(EnvType.CLIENT)
if (this == UNIVERSAL) { public class ClientNetworkManager {
return false; public static void registerClientBoundHandler(Identifier identifier, ClientPlayNetworking.PlayChannelHandler handler) {
} ClientPlayNetworking.registerGlobalReceiver(identifier, handler);
return RebornCore.getSide() == EnvType.CLIENT && this == CLIENT; }
public static <T> void registerClientBoundHandler(Identifier identifier, Codec<T> codec, Consumer<T> consumer) {
registerClientBoundHandler(identifier, (client, handler, buf, responseSender) -> {
T value = new ExtendedPacketBuffer(buf).readCodec(codec);
client.execute(() -> consumer.accept(value));
});
} }
} }

View file

@ -25,8 +25,6 @@
package reborncore.common.network; package reborncore.common.network;
import com.mojang.serialization.Codec; import com.mojang.serialization.Codec;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.PacketSender;
@ -67,21 +65,6 @@ public class NetworkManager {
return createClientBoundPacket(identifier, extendedPacketBuffer -> extendedPacketBuffer.writeCodec(codec, value)); return createClientBoundPacket(identifier, extendedPacketBuffer -> extendedPacketBuffer.writeCodec(codec, value));
} }
// TODO move to own class
@Environment(EnvType.CLIENT)
public static void registerClientBoundHandler(Identifier identifier, ClientPlayNetworking.PlayChannelHandler handler) {
ClientPlayNetworking.registerGlobalReceiver(identifier, handler);
}
// TODO move to own class
@Environment(EnvType.CLIENT)
public static <T> void registerClientBoundHandler(Identifier identifier, Codec<T> codec, Consumer<T> consumer) {
registerClientBoundHandler(identifier, (client, handler, buf, responseSender) -> {
T value = new ExtendedPacketBuffer(buf).readCodec(codec);
client.execute(() -> consumer.accept(value));
});
}
public static void sendToServer(IdentifiedPacket packet) { public static void sendToServer(IdentifiedPacket packet) {
ClientPlayNetworking.send(packet.channel(), packet.packetByteBuf()); ClientPlayNetworking.send(packet.channel(), packet.packetByteBuf());
} }

View file

@ -25,7 +25,7 @@
package techreborn.client; package techreborn.client;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import reborncore.common.network.NetworkManager; import reborncore.client.ClientNetworkManager;
import techreborn.client.gui.GuiManual; import techreborn.client.gui.GuiManual;
import techreborn.events.OreDepthSyncHandler; import techreborn.events.OreDepthSyncHandler;
import techreborn.world.OreDepth; import techreborn.world.OreDepth;
@ -35,9 +35,9 @@ import static techreborn.packets.ClientboundPackets.ORE_DEPTH;
public class ClientboundPacketHandlers { public class ClientboundPacketHandlers {
public static void init() { public static void init() {
NetworkManager.registerClientBoundHandler(ORE_DEPTH, OreDepth.LIST_CODEC, OreDepthSyncHandler::updateDepths); ClientNetworkManager.registerClientBoundHandler(ORE_DEPTH, OreDepth.LIST_CODEC, OreDepthSyncHandler::updateDepths);
NetworkManager.registerClientBoundHandler(OPEN_MANUAL, (client, handler, buf, responseSender) -> ClientNetworkManager.registerClientBoundHandler(OPEN_MANUAL, (client, handler, buf, responseSender) ->
client.execute(() -> client.execute(() ->
MinecraftClient.getInstance().setScreen(new GuiManual()) MinecraftClient.getInstance().setScreen(new GuiManual())
) )