Skip to content

Commit

Permalink
limit altar inventory size to recipe with highest amount of inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rlnt committed Feb 2, 2023
1 parent 8226278 commit f324d01
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.almostreliable.summoningrituals.Constants;
import com.almostreliable.summoningrituals.platform.PlatformBlockEntity;
import com.almostreliable.summoningrituals.recipe.AltarRecipe;
import com.almostreliable.summoningrituals.recipe.AltarRecipeSerializer;
import com.almostreliable.summoningrituals.util.GameUtils;
import manifold.ext.props.rt.api.override;
import manifold.ext.props.rt.api.val;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void deserialize(CompoundTag tag) {
public void setStackInSlot(int slot, ItemStack stack) {
validateSlot(slot);

if (slot == SIZE) {
if (slot == AltarRecipeSerializer.MAX_INPUTS) {
catalyst = stack;
return;
}
Expand All @@ -111,7 +112,7 @@ public ItemStack handleInsertion(ItemStack stack) {
if (stack.isEmpty) return ItemStack.EMPTY;

var remaining = stack.copy();
for (var i = 0; i < SIZE; i++) {
for (var i = 0; i < AltarRecipeSerializer.MAX_INPUTS; i++) {
var original = remaining.copy();
remaining = insertItem(i, remaining);

Expand Down Expand Up @@ -257,14 +258,14 @@ private int getMaxStackSize(int slot, ItemStack stack) {

@Override
public int getSlots() {
return SIZE + 1;
return AltarRecipeSerializer.MAX_INPUTS + 1;
}

@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
validateSlot(slot);
if (slot == items.size()) return catalyst;
if (slot == AltarRecipeSerializer.MAX_INPUTS) return catalyst;
return items.get(slot);
}

Expand All @@ -278,7 +279,7 @@ public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
@Override
public int getSlotLimit(int slot) {
validateSlot(slot);
if (slot == SIZE) return 1;
if (slot == AltarRecipeSerializer.MAX_INPUTS) return 1;
return 64;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.almostreliable.summoningrituals.mixin;

import com.almostreliable.summoningrituals.recipe.AltarRecipe;
import com.almostreliable.summoningrituals.recipe.AltarRecipeSerializer;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
Expand All @@ -13,14 +14,15 @@

import java.util.Map;

@SuppressWarnings("NewMethodNamingConvention")
@Mixin(RecipeManager.class)
public abstract class RecipeManagerMixin {
@SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod")
@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("HEAD"))
private void summoning$apply(
Map<ResourceLocation, JsonElement> recipes, ResourceManager resourceManager, ProfilerFiller profilerFiller,
CallbackInfo ci
) {
AltarRecipe.CATALYST_CACHE.clear();
AltarRecipeSerializer.MAX_INPUTS = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

public class AltarRecipeSerializer implements RecipeSerializer<AltarRecipe> {

@SuppressWarnings({"StaticNonFinalField", "NonConstantFieldWithUpperCaseName"})
public static int MAX_INPUTS;

@Override
public AltarRecipe fromJson(ResourceLocation id, JsonObject json) {
var catalyst = Ingredient.fromJson(json.getAsJsonObject(Constants.CATALYST));
Expand All @@ -36,6 +39,7 @@ public AltarRecipe fromJson(ResourceLocation id, JsonObject json) {
}
}
}
expandMaxInputs(inputs.size());

RecipeSacrifices sacrifices = new RecipeSacrifices();
if (json.has(Constants.SACRIFICES)) {
Expand Down Expand Up @@ -81,6 +85,7 @@ public AltarRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buffer) {
for (var i = 0; i < inputCount; i++) {
inputs.add(IngredientStack.fromNetwork(buffer));
}
expandMaxInputs(inputs.size());

RecipeSacrifices sacrifices = new RecipeSacrifices();
if (buffer.readBoolean()) {
Expand Down Expand Up @@ -147,4 +152,8 @@ private void addInput(NonNullList<IngredientStack> inputs, IngredientStack input
}
inputs.add(input);
}

private static void expandMaxInputs(int amount) {
if (MAX_INPUTS < amount) MAX_INPUTS = amount;
}
}

0 comments on commit f324d01

Please sign in to comment.