Search in sources :

Example 1 with PacketByteBuf

use of net.minecraft.util.PacketByteBuf in project nbt-crafting by Siphalor.

the class NbtCrafting method createAdvancedRecipeSyncPacket.

public static PacketByteBuf createAdvancedRecipeSyncPacket(RecipeManager recipeManager) {
    PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
    advancedIngredientSerializationEnabled.set(true);
    List<Recipe<?>> recipes = recipeManager.values().stream().filter(recipe -> {
        for (Ingredient ingredient : recipe.getPreviewInputs()) {
            if (((IIngredient) (Object) ingredient).nbtCrafting$isAdvanced()) {
                return true;
            }
        }
        return false;
    }).collect(Collectors.toList());
    buf.writeVarInt(recipes.size());
    for (Recipe<?> recipe : recipes) {
        @SuppressWarnings("rawtypes") RecipeSerializer serializer = recipe.getSerializer();
        buf.writeIdentifier(Registry.RECIPE_SERIALIZER.getId(serializer));
        buf.writeIdentifier(recipe.getId());
        // noinspection unchecked
        serializer.write(buf, recipe);
    }
    advancedIngredientSerializationEnabled.set(false);
    return buf;
}
Also used : LoadingCache(com.google.common.cache.LoadingCache) Unpooled(io.netty.buffer.Unpooled) ServerPlayConnectionEvents(net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents) CauldronRecipeSerializer(de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipeSerializer) AnvilRecipe(de.siphalor.nbtcrafting.recipe.AnvilRecipe) RecipeTypeHelper(de.siphalor.nbtcrafting.api.RecipeTypeHelper) WrappedRecipeSerializer(de.siphalor.nbtcrafting.recipe.WrappedRecipeSerializer) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) it.unimi.dsi.fastutil.ints(it.unimi.dsi.fastutil.ints) net.minecraft.recipe(net.minecraft.recipe) ModInitializer(net.fabricmc.api.ModInitializer) Collectors(java.util.stream.Collectors) Pair(com.mojang.datafixers.util.Pair) Registry(net.minecraft.util.registry.Registry) IIngredient(de.siphalor.nbtcrafting.ingredient.IIngredient) ServerLoginConnectionEvents(net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents) StatChangedCriterion(de.siphalor.nbtcrafting.advancement.StatChangedCriterion) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) MixinCriterions(de.siphalor.nbtcrafting.mixin.advancement.MixinCriterions) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) Logger(org.apache.logging.log4j.Logger) CauldronRecipe(de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipe) IServerPlayerEntity(de.siphalor.nbtcrafting.util.duck.IServerPlayerEntity) Identifier(net.minecraft.util.Identifier) CacheBuilder(com.google.common.cache.CacheBuilder) PacketByteBuf(net.minecraft.util.PacketByteBuf) LogManager(org.apache.logging.log4j.LogManager) ServerLoginNetworking(net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking) AnvilRecipe(de.siphalor.nbtcrafting.recipe.AnvilRecipe) BrewingRecipe(de.siphalor.nbtcrafting.recipe.BrewingRecipe) CauldronRecipe(de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipe) IIngredient(de.siphalor.nbtcrafting.ingredient.IIngredient) PacketByteBuf(net.minecraft.util.PacketByteBuf) CauldronRecipeSerializer(de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipeSerializer) WrappedRecipeSerializer(de.siphalor.nbtcrafting.recipe.WrappedRecipeSerializer)

Example 2 with PacketByteBuf

use of net.minecraft.util.PacketByteBuf in project nbt-crafting by Siphalor.

the class MixinAnvilContainer method updateResult.

@Inject(method = "updateResult", at = @At("HEAD"), cancellable = true)
public void updateResult(CallbackInfo callbackInfo) {
    recipe = player.world.getRecipeManager().getFirstMatch(NbtCrafting.ANVIL_RECIPE_TYPE, inventory, player.world).orElse(null);
    if (recipe != null) {
        ItemStack resultStack = recipe.craft(inventory);
        repairItemUsage = 1;
        if (userChangedName) {
            if (!StringUtils.isBlank(newItemName) && !newItemName.equals(resultStack.getName().getString())) {
                resultStack.setCustomName(new LiteralText(newItemName));
            }
            userChangedName = false;
        } else {
            newItemName = resultStack.getName().getString();
            if (player instanceof ServerPlayerEntity) {
                if (NbtCrafting.hasClientMod((ServerPlayerEntity) player)) {
                    PacketByteBuf packetByteBuf = new PacketByteBuf(Unpooled.buffer());
                    packetByteBuf.writeString(newItemName);
                    ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, NbtCrafting.UPDATE_ANVIL_TEXT_S2C_PACKET_ID, packetByteBuf);
                }
            }
        }
        result.setInvStack(0, resultStack);
        levelCost.set(recipe.getLevels());
        sendContentUpdates();
        callbackInfo.cancel();
    }
}
Also used : PacketByteBuf(net.minecraft.util.PacketByteBuf) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) LiteralText(net.minecraft.text.LiteralText) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with PacketByteBuf

use of net.minecraft.util.PacketByteBuf in project nbt-crafting by Siphalor.

the class NbtCraftingClient method sendModPresent.

public static void sendModPresent() {
    PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
    ClientSidePacketRegistry.INSTANCE.sendToServer(NbtCrafting.PRESENCE_CHANNEL, buffer);
    sentModPresent = true;
}
Also used : PacketByteBuf(net.minecraft.util.PacketByteBuf)

Example 4 with PacketByteBuf

use of net.minecraft.util.PacketByteBuf 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)

Example 5 with PacketByteBuf

use of net.minecraft.util.PacketByteBuf in project fabric by FabricMC.

the class RegistrySyncManager method createPacket.

public static Packet<?> createPacket() {
    PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
    buf.writeCompoundTag(toTag(true));
    return ServerSidePacketRegistry.INSTANCE.toPacket(ID, buf);
}
Also used : PacketByteBuf(net.minecraft.util.PacketByteBuf)

Aggregations

PacketByteBuf (net.minecraft.util.PacketByteBuf)25 CustomPayloadS2CPacket (net.minecraft.client.network.packet.CustomPayloadS2CPacket)7 IOException (java.io.IOException)4 CompoundTag (net.minecraft.nbt.CompoundTag)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)3 Identifier (net.minecraft.util.Identifier)3 IServerPlayerEntity (de.siphalor.nbtcrafting.util.duck.IServerPlayerEntity)2 Unpooled (io.netty.buffer.Unpooled)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 EntityType (net.minecraft.entity.EntityType)2 Registry (net.minecraft.util.registry.Registry)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Pair (com.mojang.datafixers.util.Pair)1 MyClientChunkManager (com.qouteall.immersive_portals.chunk_loading.MyClientChunkManager)1 IEClientPlayNetworkHandler (com.qouteall.immersive_portals.ducks.IEClientPlayNetworkHandler)1