Skip to content

Commit

Permalink
Fix Crash with PA, Condense Chanced Outputs in TOP
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Feb 26, 2025
1 parent 440b48d commit 69dff18
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -138,7 +137,14 @@ private IProbeInfo createHorizontalLayout(IProbeInfo mainPanel) {

private Pair<List<Pair<String, ElementItemStack>>, List<Pair<LabsFluidNameElement, LabsFluidStackElement>>> createItemFluidElementLists(AccessibleAbstractRecipeLogic recipe) {
// Items
var outputs = getUniqueItems(recipe.labs$getOutputs().subList(0, recipe.labs$getNonChancedItemAmt()));
var outputs = getUnique(recipe.labs$getOutputs().subList(0, recipe.labs$getNonChancedItemAmt()),
ItemStack::isEmpty, ItemMeta::new, ItemStack::getCount);

var chancedOutputs = getUnique(recipe.labs$getChancedItemOutputs(),
(chanced) -> chanced.getKey().isEmpty() || chanced.getValue() == 0,
(chanced) -> Pair.of(new ItemMeta(chanced.getKey()), chanced.getValue()),
(chanced) -> chanced.getKey().getCount());

IItemStyle style = new ItemStyle().width(16).height(16);
List<Pair<String, ElementItemStack>> items = new ArrayList<>();

Expand All @@ -147,52 +153,46 @@ private Pair<List<Pair<String, ElementItemStack>>, List<Pair<LabsFluidNameElemen
items.add(Pair.of(stack.getDisplayName(), new ElementItemStack(stack, style)));
}

for (var chanced : recipe.labs$getChancedItemOutputs()) {
String display = chanced.getKey().getDisplayName() + " (" + formatChance(chanced.getValue()) + ")";
items.add(Pair.of(display, new LabsChancedItemStackElement(chanced.getKey(), chanced.getValue(), style)));
for (var chanced : chancedOutputs.entrySet()) {
ItemStack stack = chanced.getKey().getKey().toStack(chanced.getValue());
String display = stack.getDisplayName() + " (" + formatChance(chanced.getKey().getValue()) + ")";
items.add(Pair.of(display, new LabsChancedItemStackElement(stack, chanced.getKey().getValue(), style)));
}

// Fluids
var fluidOutputs = getUniqueFluids(
recipe.labs$getFluidOutputs().subList(0, recipe.labs$getNonChancedFluidAmt()));
var fluidOutputs = getUnique(recipe.labs$getFluidOutputs().subList(0, recipe.labs$getNonChancedFluidAmt()),
(stack) -> stack.amount == 0, FluidStack::getFluid, (stack) -> stack.amount);

var chancedFluidOutputs = getUnique(recipe.labs$getChancedFluidOutputs(),
(chanced) -> chanced.getKey().amount == 0 || chanced.getValue() == 0,
(chanced) -> Pair.of(chanced.getKey().getFluid(), chanced.getValue()),
(chanced) -> chanced.getKey().amount);

List<Pair<LabsFluidNameElement, LabsFluidStackElement>> fluids = new ArrayList<>();

for (var output : fluidOutputs.entrySet()) {
FluidStack stack = new FluidStack(output.getKey(), output.getValue());
fluids.add(Pair.of(new LabsFluidNameElement(stack, false), new LabsFluidStackElement(stack)));
}

for (var chanced : recipe.labs$getChancedFluidOutputs()) {
fluids.add(Pair.of(new LabsChancedFluidNameElement(chanced.getKey(), chanced.getValue(), false),
new LabsChancedFluidStackElement(chanced.getKey(), chanced.getValue())));
for (var chanced : chancedFluidOutputs.entrySet()) {
FluidStack stack = new FluidStack(chanced.getKey().getKey(), chanced.getValue());
fluids.add(Pair.of(new LabsChancedFluidNameElement(stack, chanced.getKey().getValue(), false),
new LabsChancedFluidStackElement(stack, chanced.getKey().getValue())));
}
return Pair.of(items, fluids);
}

private Map<ItemMeta, Integer> getUniqueItems(List<ItemStack> stacks) {
Map<ItemMeta, Integer> map = new Object2ObjectLinkedOpenHashMap<>();
private <T, K> Map<K, Integer> getUnique(List<T> stacks, Function<T, Boolean> emptyCheck, Function<T, K> getKey,
Function<T, Integer> getCount) {
Map<K, Integer> map = new Object2ObjectLinkedOpenHashMap<>();

for (var stack : stacks) {
if (stack.isEmpty()) continue;
for (T stack : stacks) {
if (emptyCheck.apply(stack)) continue;

map.compute(new ItemMeta(stack), (meta, count) -> {
map.compute(getKey.apply(stack), (key, count) -> {
if (count == null) count = 0;
return count + stack.getCount();
});
}

return map;
}

private Map<Fluid, Integer> getUniqueFluids(List<FluidStack> stacks) {
Map<Fluid, Integer> map = new Object2ObjectLinkedOpenHashMap<>();

for (var stack : stacks) {
if (stack.amount == 0) continue;

map.compute(stack.getFluid(), (meta, amount) -> {
if (amount == null) amount = 0;
return amount + stack.amount;
return count + getCount.apply(stack);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -12,7 +13,7 @@

import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -23,6 +24,7 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import com.llamalad7.mixinextras.sugar.Local;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.gregtech.mixinhelper.AccessibleAbstractRecipeLogic;

import gregtech.api.capability.impl.AbstractRecipeLogic;
Expand Down Expand Up @@ -64,12 +66,12 @@ public abstract class AbstractRecipeLogicMixin extends MTETrait implements Acces

@Shadow
protected int recipeEUt;

@Shadow
@Final
private RecipeMap<?> recipeMap;
@Shadow
protected int progressTime;

@Shadow
public abstract @Nullable RecipeMap<?> getRecipeMap();

/**
* List of non-chanced item outputs.The actual non-chanced item outputs are taken from the item outputs saved list,
* taking the first n elements.
Expand Down Expand Up @@ -106,12 +108,22 @@ private AbstractRecipeLogicMixin(@NotNull MetaTileEntity metaTileEntity) {
@Unique
@Override
public List<ItemStack> labs$getOutputs() {
if (itemOutputs == null) {
NomiLabs.LOGGER.error("Item Outputs List for Recipe Logic {} of Recipe Map {} is null!",
getClass().getName(), getRecipeMap().getUnlocalizedName());
return new ArrayList<>();
}
return itemOutputs;
}

@Unique
@Override
public List<FluidStack> labs$getFluidOutputs() {
if (fluidOutputs == null) {
NomiLabs.LOGGER.error("Fluid Outputs List for Recipe Logic {} of Recipe Map {} is null!",
getClass().getName(), getRecipeMap().getUnlocalizedName());
return new ArrayList<>();
}
return fluidOutputs;
}

Expand All @@ -130,6 +142,11 @@ private AbstractRecipeLogicMixin(@NotNull MetaTileEntity metaTileEntity) {
@Unique
@Override
public List<Pair<ItemStack, Integer>> labs$getChancedItemOutputs() {
if (labs$chancedItemOutputs == null) {
NomiLabs.LOGGER.error("Chanced Item Outputs List for Recipe Logic {} of Recipe Map {} is null!",
getClass().getName(), getRecipeMap().getUnlocalizedName());
return new ArrayList<>();
}
return labs$chancedItemOutputs;
}

Expand All @@ -142,6 +159,11 @@ private AbstractRecipeLogicMixin(@NotNull MetaTileEntity metaTileEntity) {
@Unique
@Override
public List<Pair<FluidStack, Integer>> labs$getChancedFluidOutputs() {
if (labs$chancedFluidOutputs == null) {
NomiLabs.LOGGER.error("Chanced Fluid Outputs List for Recipe Logic {} of Recipe Map {} is null!",
getClass().getName(), getRecipeMap().getUnlocalizedName());
return new ArrayList<>();
}
return labs$chancedFluidOutputs;
}

Expand All @@ -160,10 +182,11 @@ private void setupLabsValues(Recipe recipe, CallbackInfo ci, @Local(ordinal = 0)
labs$nonChancedItemAmt = recipe.getOutputs().size();
labs$nonChancedFluidAmt = recipe.getFluidOutputs().size();

labs$chancedItemOutputs = labs$fillChancedOutputsMap(recipe.getChancedOutputs(), recipeMap.getChanceFunction(),
labs$chancedItemOutputs = labs$fillChancedOutputsMap(recipe.getChancedOutputs(),
getRecipeMap().getChanceFunction(),
recipeTier, machineTier);
labs$chancedFluidOutputs = labs$fillChancedOutputsMap(recipe.getChancedFluidOutputs(),
recipeMap.getChanceFunction(),
getRecipeMap().getChanceFunction(),
recipeTier, machineTier);
}

Expand Down Expand Up @@ -211,22 +234,23 @@ private void saveLabsValues(CallbackInfoReturnable<NBTTagCompound> cir) {
nbt.setInteger(LABS_NON_CHANCED_ITEM_AMT_KEY, labs$nonChancedItemAmt);
nbt.setInteger(LABS_NON_CHANCED_FLUID_AMT_KEY, labs$nonChancedFluidAmt);

var items = new NBTTagList();
for (var entry : labs$chancedItemOutputs) {
var tag = new NBTTagCompound();
entry.getKey().writeToNBT(tag);
tag.setInteger(LABS_CHANCE_KEY, entry.getValue());
items.appendTag(tag);
}
nbt.setTag(LABS_CHANCED_ITEM_OUTPUTS_KEY, items);

var fluids = new NBTTagList();
for (var entry : labs$chancedFluidOutputs) {
var tag = new NBTTagCompound();
entry.getKey().writeToNBT(tag);
tag.setInteger(LABS_CHANCE_KEY, entry.getValue());
fluids.appendTag(tag);
labs$addChancedToTag(nbt, LABS_CHANCED_ITEM_OUTPUTS_KEY, labs$chancedItemOutputs,
(entry) -> entry.getKey().writeToNBT(new NBTTagCompound()), Pair::getValue);
labs$addChancedToTag(nbt, LABS_CHANCED_FLUID_OUTPUTS_KEY, labs$chancedFluidOutputs,
(entry) -> entry.getKey().writeToNBT(new NBTTagCompound()), Pair::getValue);
}

@Unique
private <T> void labs$addChancedToTag(NBTTagCompound nbt, String key, List<T> list,
Function<T, NBTTagCompound> createTag, Function<T, Integer> getChance) {
if (list == null) return;

var entries = new NBTTagList();
for (var entry : list) {
NBTTagCompound tag = createTag.apply(entry);
tag.setInteger(LABS_CHANCE_KEY, getChance.apply(entry));
entries.appendTag(tag);
}
nbt.setTag(LABS_CHANCED_FLUID_OUTPUTS_KEY, fluids);
nbt.setTag(key, entries);
}
}

0 comments on commit 69dff18

Please sign in to comment.