Skip to content

[1.21.1-1.21.4] Custom Ingredients sync fix #4322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fabric-recipe-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ loom {
}

moduleDependencies(project, [
"fabric-networking-api-v1",
'fabric-networking-api-v1',
])

testDependencies(project, [
':fabric-lifecycle-events-v1',
])
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.Set;
import java.util.function.Consumer;

import io.netty.channel.ChannelHandler;

import net.minecraft.network.handler.EncoderHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.server.network.ServerPlayerConfigurationTask;
Expand Down Expand Up @@ -90,12 +88,7 @@ public void onInitialize() {

ServerConfigurationNetworking.registerGlobalReceiver(CustomIngredientPayloadC2S.ID, (payload, context) -> {
Set<Identifier> supportedCustomIngredients = decodeResponsePayload(payload);
ChannelHandler packetEncoder = ((ServerCommonNetworkHandlerAccessor) context.networkHandler()).getConnection().channel.pipeline().get("encoder");

if (packetEncoder != null) { // Null in singleplayer
((SupportedIngredientsPacketEncoder) packetEncoder).fabric_setSupportedCustomIngredients(supportedCustomIngredients);
}

((SupportedIngredientsClientConnection) ((ServerCommonNetworkHandlerAccessor) context.networkHandler()).getConnection()).fabric_setSupportedCustomIngredients(supportedCustomIngredients);
context.networkHandler().completeTask(IngredientSyncTask.KEY);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import java.util.Set;

import net.minecraft.network.handler.EncoderHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.util.Identifier;

/**
* Implemented on {@link EncoderHandler} to store which custom ingredients the client supports.
* Implemented on {@link ClientConnection} to store which custom ingredients the client supports.
*/
public interface SupportedIngredientsPacketEncoder {
public interface SupportedIngredientsClientConnection {
void fabric_setSupportedCustomIngredients(Set<Identifier> supportedCustomIngredients);

Set<Identifier> fabric_getSupportedCustomIngredients();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.recipe.ingredient;

import java.util.Set;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.network.ClientConnection;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.impl.recipe.ingredient.SupportedIngredientsClientConnection;

@Mixin(ClientConnection.class)
public abstract class ClientConnectionMixin implements SupportedIngredientsClientConnection {
@Unique
private Set<Identifier> fabric_supportedCustomIngredients = Set.of();

@Override
public void fabric_setSupportedCustomIngredients(Set<Identifier> supportedCustomIngredients) {
fabric_supportedCustomIngredients = supportedCustomIngredients;
}

@Override
public Set<Identifier> fabric_getSupportedCustomIngredients() {
return fabric_supportedCustomIngredients;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,22 @@

package net.fabricmc.fabric.mixin.recipe.ingredient;

import java.util.Set;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.network.handler.EncoderHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.impl.recipe.ingredient.CustomIngredientSync;
import net.fabricmc.fabric.impl.recipe.ingredient.SupportedIngredientsPacketEncoder;
import net.fabricmc.fabric.impl.recipe.ingredient.SupportedIngredientsClientConnection;

@Mixin(EncoderHandler.class)
public class EncoderHandlerMixin implements SupportedIngredientsPacketEncoder {
@Unique
private Set<Identifier> fabric_supportedCustomIngredients = Set.of();

@Override
public void fabric_setSupportedCustomIngredients(Set<Identifier> supportedCustomIngredients) {
fabric_supportedCustomIngredients = supportedCustomIngredients;
}

public class EncoderHandlerMixin {
@Inject(
at = @At(
value = "INVOKE",
Expand All @@ -51,7 +40,11 @@ public void fabric_setSupportedCustomIngredients(Set<Identifier> supportedCustom
method = "encode(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;Lio/netty/buffer/ByteBuf;)V"
)
private void capturePacketEncoder(ChannelHandlerContext channelHandlerContext, Packet<?> packet, ByteBuf byteBuf, CallbackInfo ci) {
CustomIngredientSync.CURRENT_SUPPORTED_INGREDIENTS.set(fabric_supportedCustomIngredients);
ChannelHandler channelHandler = channelHandlerContext.pipeline().get("packet_handler");

if (channelHandler instanceof SupportedIngredientsClientConnection) {
CustomIngredientSync.CURRENT_SUPPORTED_INGREDIENTS.set(((SupportedIngredientsClientConnection) channelHandler).fabric_getSupportedCustomIngredients());
}
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"package": "net.fabricmc.fabric.mixin.recipe",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ingredient.IngredientMixin",
"ingredient.ClientConnectionMixin",
"ingredient.EncoderHandlerMixin",
"ingredient.IngredientMixin",
"ingredient.ShapelessRecipeMixin"
],
"injectors": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"fabric:type": "fabric:components",
"base": {
"item": "minecraft:diamond_pickaxe"
},
"components": {
"minecraft:damage": 0
},
"strict": false
}
],
"result": {
"id": "minecraft:diamond_block"
}
}
3 changes: 3 additions & 0 deletions fabric-recipe-api-v1/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"net.fabricmc.fabric.test.recipe.ingredient.IngredientMatchTests",
"net.fabricmc.fabric.test.recipe.ingredient.SerializationTests",
"net.fabricmc.fabric.test.recipe.ingredient.ShapelessRecipeMatchTests"
],
"client": [
"net.fabricmc.fabric.test.recipe.ingredient.client.ClientCustomIngredientSyncTests"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.test.recipe.ingredient.client;

import net.minecraft.recipe.ShapelessRecipe;
import net.minecraft.test.GameTestException;
import net.minecraft.util.Identifier;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.impl.recipe.ingredient.CustomIngredientImpl;
import net.fabricmc.fabric.impl.recipe.ingredient.builtin.ComponentsIngredient;

public class ClientCustomIngredientSyncTests implements ClientModInitializer {
/**
* The recipe requires a custom ingredient.
*/
@Override
public void onInitializeClient() {
ClientTickEvents.END_WORLD_TICK.register(world -> {
Identifier recipeId = Identifier.of("fabric-recipe-api-v1-testmod", "test_customingredients_sync");
ShapelessRecipe recipe = (ShapelessRecipe) world.getRecipeManager().get(recipeId).get().value();

if (!(recipe.getIngredients().getFirst() instanceof CustomIngredientImpl customIngredient)) {
throw new GameTestException("Expected the first ingredient to be a CustomIngredientImpl");
}

if (!(customIngredient.getCustomIngredient() instanceof ComponentsIngredient)) {
throw new GameTestException("Expected the custom ingredient to be a ComponentsIngredient");
}
});
}
}