[1.21.1-1.21.4] Custom Ingredients sync fix ()

* Fix customIngredients sync ()

* Add client side test for custom ingredients sync

* Fixed styling issues and missing license header

* Applied requested changes and styling fixes
This commit is contained in:
Salandora 2024-12-30 14:11:34 +01:00 committed by GitHub
parent d527f9fdd9
commit 248df81c7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 132 additions and 28 deletions
fabric-recipe-api-v1
build.gradle
src
main
testmod/resources
data/fabric-recipe-api-v1-testmod/recipe
fabric.mod.json
testmodClient/java/net/fabricmc/fabric/test/recipe/ingredient/client

View file

@ -5,5 +5,9 @@ loom {
}
moduleDependencies(project, [
"fabric-networking-api-v1",
'fabric-networking-api-v1',
])
testDependencies(project, [
':fabric-lifecycle-events-v1',
])

View file

@ -19,8 +19,6 @@ package net.fabricmc.fabric.impl.recipe.ingredient;
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;
@ -90,12 +88,7 @@ public class CustomIngredientSync implements ModInitializer {
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);
});
}

View file

@ -18,12 +18,14 @@ package net.fabricmc.fabric.impl.recipe.ingredient;
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();
}

View file

@ -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;
}
}

View file

@ -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",
@ -51,7 +40,11 @@ public class EncoderHandlerMixin implements SupportedIngredientsPacketEncoder {
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(

View file

@ -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": {

View file

@ -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"
}
}

View file

@ -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"
]
}
}

View file

@ -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");
}
});
}
}