mirror of
https://github.com/FabricMC/fabric.git
synced 2025-04-08 21:14:41 -04:00
[1.21.1-1.21.4] Custom Ingredients sync fix (#4322)
* Fix customIngredients sync (#4225) * Add client side test for custom ingredients sync * Fixed styling issues and missing license header * Applied requested changes and styling fixes (cherry picked from commit248df81c7e
) (cherry picked from commit1572dc3798
)
This commit is contained in:
parent
b1c29d8eb4
commit
1fe5646007
9 changed files with 132 additions and 28 deletions
fabric-recipe-api-v1
build.gradle
src
main
java/net/fabricmc/fabric
impl/recipe/ingredient
mixin/recipe/ingredient
resources
testmod/resources
testmodClient/java/net/fabricmc/fabric/test/recipe/ingredient/client
|
@ -5,5 +5,9 @@ loom {
|
|||
}
|
||||
|
||||
moduleDependencies(project, [
|
||||
"fabric-networking-api-v1",
|
||||
'fabric-networking-api-v1',
|
||||
])
|
||||
|
||||
testDependencies(project, [
|
||||
':fabric-lifecycle-events-v1',
|
||||
])
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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(
|
||||
|
|
|
@ -3,8 +3,9 @@
|
|||
"package": "net.fabricmc.fabric.mixin.recipe",
|
||||
"compatibilityLevel": "JAVA_21",
|
||||
"mixins": [
|
||||
"ingredient.IngredientMixin",
|
||||
"ingredient.ClientConnectionMixin",
|
||||
"ingredient.EncoderHandlerMixin",
|
||||
"ingredient.IngredientMixin",
|
||||
"ingredient.ShapelessRecipeMixin"
|
||||
],
|
||||
"injectors": {
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue