Search in sources :

Example 1 with AnvilScreen

use of net.minecraft.client.gui.screen.ingame.AnvilScreen in project nbt-crafting by Siphalor.

the class NbtCraftingClient method onInitializeClient.

@Override
public void onInitializeClient() {
    ClientLoginNetworking.registerGlobalReceiver(NbtCrafting.PRESENCE_CHANNEL, (client, handler, buf, listenerAdder) -> {
        return CompletableFuture.completedFuture(new PacketByteBuf(Unpooled.buffer()));
    });
    ClientSidePacketRegistry.INSTANCE.register(NbtCrafting.UPDATE_ANVIL_TEXT_S2C_PACKET_ID, (packetContext, packetByteBuf) -> {
        if (MinecraftClient.getInstance().currentScreen instanceof AnvilScreen) {
            ((AnvilScreenAccessor) MinecraftClient.getInstance().currentScreen).getNameField().setText(packetByteBuf.readString());
        } else
            packetByteBuf.readString();
    });
    ClientPlayNetworking.registerGlobalReceiver(NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, (client, handler, buf, responseSender) -> {
        RecipeManager recipeManager = handler.getRecipeManager();
        Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipeMap = ((RecipeManagerAccessor) recipeManager).getRecipes();
        recipeMap = new HashMap<>(recipeMap);
        int recipeCount = buf.readVarInt();
        NbtCrafting.advancedIngredientSerializationEnabled.set(true);
        for (int i = 0; i < recipeCount; i++) {
            RecipeSerializer<?> serializer = Registry.RECIPE_SERIALIZER.get(buf.readIdentifier());
            Identifier id = buf.readIdentifier();
            Recipe<?> recipe = serializer.read(id, buf);
            Map<Identifier, Recipe<?>> recipeType = recipeMap.computeIfAbsent(recipe.getType(), rt -> new HashMap<>());
            recipeType.put(id, recipe);
        }
        NbtCrafting.advancedIngredientSerializationEnabled.set(false);
        ((RecipeManagerAccessor) recipeManager).setRecipes(ImmutableMap.copyOf(recipeMap));
    });
}
Also used : Recipe(net.minecraft.recipe.Recipe) AnvilScreen(net.minecraft.client.gui.screen.ingame.AnvilScreen) RecipeManagerAccessor(de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor) Identifier(net.minecraft.util.Identifier) RecipeType(net.minecraft.recipe.RecipeType) PacketByteBuf(net.minecraft.util.PacketByteBuf) RecipeManager(net.minecraft.recipe.RecipeManager) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 RecipeManagerAccessor (de.siphalor.nbtcrafting.mixin.RecipeManagerAccessor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AnvilScreen (net.minecraft.client.gui.screen.ingame.AnvilScreen)1 Recipe (net.minecraft.recipe.Recipe)1 RecipeManager (net.minecraft.recipe.RecipeManager)1 RecipeType (net.minecraft.recipe.RecipeType)1 Identifier (net.minecraft.util.Identifier)1 PacketByteBuf (net.minecraft.util.PacketByteBuf)1