Skip to content

Commit

Permalink
Small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Nov 5, 2023
1 parent dc21828 commit 3f4b586
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
* - Check if relevant for protocol translation: TakeItemEntityPacket isEmpty case (1.20 -> 1.20.1 change)
* - Window interactions in <= 1.16.5 has changed and can be detected by the server
* - Entity hit boxes and eye heights has changed in almost all versions
* - Crafting Recipes are missing in ViaVersion (see https://github.com/ViaVersion/ViaFabricPlus/issues/60)
* - Most CTS protocol features aren't supported (see https://github.com/ViaVersion/ViaFabricPlus/issues/181)
* - Most CPE features aren't implemented correctly (see https://github.com/ViaVersion/ViaFabricPlus/issues/152)
* - Bedrock scaffolding should be added as soon as ViaBedrock supports block placement (see https://github.com/ViaVersion/ViaFabricPlus/issues/204)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.FontStorage;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.inventory.RecipeInputInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.Registries;
import net.minecraft.screen.ScreenHandler;
import net.raphimc.vialegacy.protocols.classic.protocolc0_28_30toc0_28_30cpe.data.ClassicProtocolExtension;
import net.raphimc.vialoader.util.VersionEnum;
import net.raphimc.vialoader.util.VersionRange;
Expand Down Expand Up @@ -180,30 +175,6 @@ public static int getLegacyArmorPoints(final ItemStack itemStack) {
return LEGACY_ARMOR_POINTS.get(itemStack.getItem());
}

/**
* Sets the result slot of a crafting screen handler to the correct item stack. In MC <= 1.11.2 the result slot
* is not updated when the input slots change, so we need to update it manually, Spigot and Paper re-syncs the slot,
* so we don't notice this bug on servers that use Spigot or Paper
*
* @param syncId The sync id of the screen handler
* @param screenHandler The screen handler
* @param inventory The inventory of the screen handler
*/
public static void setCraftingResultSlot(final int syncId, final ScreenHandler screenHandler, final RecipeInputInventory inventory) {
final var network = MinecraftClient.getInstance().getNetworkHandler();
if (network == null) return;

final var world = MinecraftClient.getInstance().world;

final var result = network.getRecipeManager().
getFirstMatch(RecipeType.CRAFTING, inventory, world). // Get the first matching recipe
map(recipe -> recipe.value().craft(inventory, world.getRegistryManager())). // Craft the recipe to get the result
orElse(ItemStack.EMPTY); // If there is no recipe, set the result to air

// Update the result slot
network.onScreenHandlerSlotUpdate(new ScreenHandlerSlotUpdateS2CPacket(syncId, screenHandler.getRevision(), 0, result));
}

public static int getCurrentChatLimit() {
return currentChatLimit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,29 @@
import net.minecraft.block.GlazedTerracottaBlock;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.inventory.RecipeInputInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.recipe.*;
import net.minecraft.screen.ScreenHandler;
import net.raphimc.vialoader.util.VersionEnum;

import java.util.List;

/**
* Handles all recipe related stuff for versions older than 1.12.
*/
public class RecipesPre1_12 {

/**
* Removes recipes that are not supported in 1.11 and older versions.
*
* @param recipes List of recipes
* @param version Version of the client
*/
public static void editRecipes(final List<Recipe<?>> recipes, final VersionEnum version) {
final var registryManager = MinecraftClient.getInstance().world.getRegistryManager();

Expand Down Expand Up @@ -64,4 +77,28 @@ public static void editRecipes(final List<Recipe<?>> recipes, final VersionEnum
}
}
}

/**
* Sets the result slot of a crafting screen handler to the correct item stack. In MC <= 1.11.2 the result slot
* is not updated when the input slots change, so we need to update it manually, Spigot and Paper re-syncs the slot,
* so we don't notice this bug on servers that use Spigot or Paper
*
* @param syncId The sync id of the screen handler
* @param screenHandler The screen handler
* @param inventory The inventory of the screen handler
*/
public static void setCraftingResultSlot(final int syncId, final ScreenHandler screenHandler, final RecipeInputInventory inventory) {
final var network = MinecraftClient.getInstance().getNetworkHandler();
if (network == null) return;

final var world = MinecraftClient.getInstance().world;

final var result = network.getRecipeManager().
getFirstMatch(RecipeType.CRAFTING, inventory, world). // Get the first matching recipe
map(recipe -> recipe.value().craft(inventory, world.getRegistryManager())). // Craft the recipe to get the result
orElse(ItemStack.EMPTY); // If there is no recipe, set the result to air

// Update the result slot
network.onScreenHandlerSlotUpdate(new ScreenHandlerSlotUpdateS2CPacket(syncId, screenHandler.getRevision(), 0, result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.screen.screenhandler;

import de.florianmichael.viafabricplus.definition.ClientsideFixes;
import de.florianmichael.viafabricplus.definition.RecipesPre1_12;
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.RecipeInputInventory;
Expand All @@ -44,7 +44,7 @@ public MixinCraftingScreenHandler(ScreenHandlerType<?> screenHandlerType, int i)
@Inject(method = "onContentChanged", at = @At("HEAD"))
public void updateResultSlot(Inventory inventory, CallbackInfo ci) {
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_11_1to1_11_2)) {
ClientsideFixes.setCraftingResultSlot(syncId, this, input);
RecipesPre1_12.setCraftingResultSlot(syncId, this, input);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.screen.screenhandler;

import de.florianmichael.viafabricplus.definition.ClientsideFixes;
import de.florianmichael.viafabricplus.definition.RecipesPre1_12;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.RecipeInputInventory;
import net.raphimc.vialoader.util.VersionEnum;
Expand Down Expand Up @@ -65,7 +65,7 @@ private EquipmentSlot injectTransferSlot(EquipmentSlot slot) {
@Inject(method = "onContentChanged", at = @At("HEAD"))
public void updateResultSlot(Inventory inventory, CallbackInfo ci) {
if (ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(VersionEnum.r1_11_1to1_11_2)) {
ClientsideFixes.setCraftingResultSlot(syncId, this, craftingInput);
RecipesPre1_12.setCraftingResultSlot(syncId, this, craftingInput);
}
}
}

0 comments on commit 3f4b586

Please sign in to comment.