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;
}
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();
}
}
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;
}
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));
});
}
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);
}
Aggregations