|
| 1 | +package org.embeddedt.modernfix.forge.mixin.bugfix.recipe_book_type_desync; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.sugar.Local; |
| 4 | +import net.minecraft.network.FriendlyByteBuf; |
| 5 | +import net.minecraft.stats.RecipeBookSettings; |
| 6 | +import net.minecraft.world.inventory.RecipeBookType; |
| 7 | +import org.embeddedt.modernfix.ModernFix; |
| 8 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 9 | +import org.embeddedt.modernfix.forge.packet.NetworkUtils; |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 13 | + |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.lang.reflect.Modifier; |
| 16 | + |
| 17 | +@Mixin(RecipeBookSettings.class) |
| 18 | +@ClientOnlyMixin |
| 19 | +public class RecipeBookSettingsMixin { |
| 20 | + private static int mfix$maxVanillaOrdinal; |
| 21 | + |
| 22 | + static { |
| 23 | + int ord = 0; |
| 24 | + for(Field f : RecipeBookType.class.getDeclaredFields()) { |
| 25 | + if(RecipeBookType.class.isAssignableFrom(f.getType()) && Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers())) { |
| 26 | + try { |
| 27 | + f.setAccessible(true); |
| 28 | + RecipeBookType type = (RecipeBookType)f.get(null); |
| 29 | + ord = Math.max(type.ordinal(), ord); |
| 30 | + } catch(Exception e) { |
| 31 | + e.printStackTrace(); |
| 32 | + ord = Integer.MAX_VALUE - 1; |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + mfix$maxVanillaOrdinal = ord; |
| 38 | + } |
| 39 | + @Redirect(method = "read(Lnet/minecraft/network/FriendlyByteBuf;)Lnet/minecraft/stats/RecipeBookSettings;", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/FriendlyByteBuf;readBoolean()Z")) |
| 40 | + private static boolean useDefaultBooleanIfVanilla(FriendlyByteBuf buf, @Local(ordinal = 0) RecipeBookType type) { |
| 41 | + if(type.ordinal() >= (mfix$maxVanillaOrdinal + 1) && NetworkUtils.isCurrentlyVanilla) { |
| 42 | + ModernFix.LOGGER.warn("Not reading recipe book data for type '{}' as we are using vanilla connection", type.name()); |
| 43 | + return false; // skip actually reading buffer |
| 44 | + } |
| 45 | + return buf.readBoolean(); |
| 46 | + } |
| 47 | +} |
0 commit comments