Skip to content

Commit

Permalink
Fix customIngredients sync (FabricMC#4225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salandora committed Dec 22, 2024
1 parent 96c409c commit 3396500
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
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);
}

((SupportedIngredientsPacketEncoder) ((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 @@ -26,4 +26,6 @@
*/
public interface SupportedIngredientsPacketEncoder {
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.SupportedIngredientsPacketEncoder;

@Mixin(ClientConnection.class)
public abstract class ClientConnectionMixin implements SupportedIngredientsPacketEncoder {
@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,20 @@

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 net.fabricmc.fabric.impl.recipe.ingredient.CustomIngredientSync;
import net.fabricmc.fabric.impl.recipe.ingredient.SupportedIngredientsPacketEncoder;
import net.minecraft.network.handler.EncoderHandler;
import net.minecraft.network.packet.Packet;
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;

@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 +38,10 @@ 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 client = channelHandlerContext.pipeline().get("packet_handler");
if (client != null) {
CustomIngredientSync.CURRENT_SUPPORTED_INGREDIENTS.set(((SupportedIngredientsPacketEncoder) client).fabric_getSupportedCustomIngredients());
}
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"required": true,
"package": "net.fabricmc.fabric.mixin.recipe",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ingredient.IngredientMixin",
"ingredient.EncoderHandlerMixin",
"ingredient.ShapelessRecipeMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"package": "net.fabricmc.fabric.mixin.recipe",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ingredient.ClientConnectionMixin",
"ingredient.EncoderHandlerMixin",
"ingredient.IngredientMixin",
"ingredient.ShapelessRecipeMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 3396500

Please sign in to comment.