Skip to content

Commit

Permalink
fix: emi transfer handler not working on servers
Browse files Browse the repository at this point in the history
Closes #1223
  • Loading branch information
klikli-dev committed Oct 6, 2024
1 parent 6c18c4a commit 395ff34
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.klikli_dev.occultism.integration.emi.impl;

import com.google.common.base.Preconditions;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipe;

public class EmiHelper {
public static NonNullList<Ingredient> ensure3by3CraftingMatrix(Recipe<?> recipe) {
var ingredients = recipe.getIngredients();
var expandedIngredients = NonNullList.withSize(9, Ingredient.EMPTY);

Preconditions.checkArgument(ingredients.size() <= 9);

// shaped recipes can be smaller than 3x3, expand to 3x3 to match the crafting
// matrix
if (recipe instanceof ShapedRecipe shapedRecipe) {
var width = shapedRecipe.getWidth();
var height = shapedRecipe.getHeight();
Preconditions.checkArgument(width <= 3 && height <= 3);

for (var h = 0; h < height; h++) {
for (var w = 0; w < width; w++) {
var source = w + h * width;
var target = w + h * 3;
var i = ingredients.get(source);
expandedIngredients.set(target, i);
}
}
}
// Anything else should be a flat list
else {
for (var i = 0; i < ingredients.size(); i++) {
expandedIngredients.set(i, ingredients.get(i));
}
}

return expandedIngredients;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static NonNullList<ItemStack> findGoodTemplateItems(Recipe<?> recipe, St
var ingredientPriorities = getIngredientPriorities(menu, ENTRY_COMPARATOR);

var templateItems = NonNullList.withSize(9, ItemStack.EMPTY);
var ingredients = ensure3by3CraftingMatrix(recipe);
var ingredients = EmiHelper.ensure3by3CraftingMatrix(recipe);
for (int i = 0; i < ingredients.size(); i++) {
var ingredient = ingredients.get(i);
if (!ingredient.isEmpty()) {
Expand Down Expand Up @@ -163,37 +163,7 @@ public static Map<ItemStackKey, Integer> getIngredientPriorities(StorageControll
return result;
}

public static NonNullList<Ingredient> ensure3by3CraftingMatrix(Recipe<?> recipe) {
var ingredients = recipe.getIngredients();
var expandedIngredients = NonNullList.withSize(9, Ingredient.EMPTY);

Preconditions.checkArgument(ingredients.size() <= 9);

// shaped recipes can be smaller than 3x3, expand to 3x3 to match the crafting
// matrix
if (recipe instanceof ShapedRecipe shapedRecipe) {
var width = shapedRecipe.getWidth();
var height = shapedRecipe.getHeight();
Preconditions.checkArgument(width <= 3 && height <= 3);

for (var h = 0; h < height; h++) {
for (var w = 0; w < width; w++) {
var source = w + h * width;
var target = w + h * 3;
var i = ingredients.get(source);
expandedIngredients.set(target, i);
}
}
}
// Anything else should be a flat list
else {
for (var i = 0; i < ingredients.size(); i++) {
expandedIngredients.set(i, ingredients.get(i));
}
}

return expandedIngredients;
}

private static void renderMissingAndCraftableSlotOverlays(Map<Integer, SlotWidget> inputSlots,
GuiGraphics guiGraphics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.klikli_dev.occultism.Occultism;
import com.klikli_dev.occultism.api.common.blockentity.IStorageController;
import com.klikli_dev.occultism.api.common.container.IStorageControllerContainer;
import com.klikli_dev.occultism.integration.emi.impl.EmiHelper;
import com.klikli_dev.occultism.integration.emi.impl.StorageControllerEMIRecipeHandler;
import com.klikli_dev.occultism.network.IMessage;
import com.klikli_dev.occultism.network.Networking;
Expand Down Expand Up @@ -137,7 +138,7 @@ private NonNullList<Ingredient> getDesiredIngredients(Player player) {
if (this.recipeId != null) {
var recipe = player.level().getRecipeManager().byKey(this.recipeId).orElse(null);
if (recipe != null) {
return StorageControllerEMIRecipeHandler.ensure3by3CraftingMatrix(recipe.value());
return EmiHelper.ensure3by3CraftingMatrix(recipe.value());
}
}

Expand Down

0 comments on commit 395ff34

Please sign in to comment.