Skip to content

Commit

Permalink
feat: inventory check inspects large fluid containers (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Caedis <[email protected]>
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
3 people authored Dec 27, 2024
1 parent adc4358 commit eba71ca
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
4 changes: 4 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ dependencies {
// runtime("curse.maven:cofh-core-69162:2388751")

testCompile 'org.junit.jupiter:junit-jupiter-api:5.2.0'

// Required to load in GTNH mods inside the dev environment for testing. Useful for checking item stack label
// generation.
runtimeOnlyNonPublishable("com.github.GTNewHorizons:GT5-Unofficial:5.09.48.133:dev")
}
53 changes: 43 additions & 10 deletions src/main/java/me/towdium/jecalculation/data/label/ILabel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.towdium.jecalculation.data.label;

import static me.towdium.jecalculation.utils.ItemStackHelper.isGregTechLargeFluidContainer;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
Expand All @@ -23,6 +25,7 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.util.GTUtility;
import me.towdium.jecalculation.JustEnoughCalculation;
import me.towdium.jecalculation.data.label.labels.LFluidStack;
import me.towdium.jecalculation.data.label.labels.LItemStack;
Expand Down Expand Up @@ -194,7 +197,7 @@ public void register(String identifier, Function<NBTTagCompound, ILabel> deseria
* @param nbt NBT to deserialize
* @return the recovered label
* A typical NBT structure of an {@link ILabel} is as follows:
*
*
* <pre>
* {@code
* {
Expand Down Expand Up @@ -258,18 +261,48 @@ public enum Priority {
handlers.put(Priority.FALLBACK, new ArrayList<>());
}

public static ILabel from(@Nullable Object o) {
if (o == null) return ILabel.EMPTY;
else if (o instanceof ItemStack) return new LItemStack((ItemStack) o);
else if (o instanceof FluidStack) return new LFluidStack((FluidStack) o);
else if (o instanceof EnchantmentData) {
/**
* Converts an {@code ItemStack} to its corresponding {@link ILabel} implementation.
*
* @param item The {@code ItemStack} to convert.
* @return A {@link LItemStack} if the passed argument is an item stack, a {@link LFluidStack} if the past
* item is a fluid stack or an item stack containing a fluid. If the item is neither, returns an
* instance of {@link LPlaceholder}. If the argument is {@code null}, returns an instance of
* {@link ILabel.LEmpty}.
*/
public static ILabel from(@Nullable Object item) {
if (item == null) {
return ILabel.EMPTY;
}

if (item instanceof ItemStack) {
// We require special handling of item stacks that contain fluids. For those, we check if they are any
// variant of the large fluid cell. If that is the case, we return a fluid stack instead of an item
// stack label. `getFluidForFilledItem` returns `null` if the cell is empty. For those cases, we still
// return an item stack label.
ItemStack itemStack = (ItemStack) item;
if (isGregTechLargeFluidContainer(itemStack)) {
FluidStack fluidStack = GTUtility.getFluidForFilledItem(itemStack, true);
if (fluidStack != null) {
return new LFluidStack(fluidStack);
}
}

return new LItemStack((ItemStack) item);
}

if (item instanceof FluidStack) {
return new LFluidStack((FluidStack) item);
}

if (item instanceof EnchantmentData) {
ItemStack itemStack = new ItemStack(Items.enchanted_book);
new ItemEnchantedBook().addEnchantment(itemStack, (EnchantmentData) o);
new ItemEnchantedBook().addEnchantment(itemStack, (EnchantmentData) item);
return new LItemStack(itemStack);
} else {
JustEnoughCalculation.logger.warn("Unrecognized ingredient type: " + o.getClass());
return LPlaceholder.Converter.from(o);
}

JustEnoughCalculation.logger.warn("Unrecognized ingredient type: {}", item.getClass());
return LPlaceholder.Converter.from(item);
}

public void register(ConverterFunction handler, Priority priority) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import me.towdium.jecalculation.utils.Utilities;

/**
* Author: towdium
* Date: 17-9-27.
* A label that represents some quantity of a fluid.
*/
@ParametersAreNonnullByDefault
public class LFluidStack extends ILabel.Impl {
Expand All @@ -30,10 +29,24 @@ public class LFluidStack extends ILabel.Impl {
public static final String KEY_FLUID = "fluid";
public static final String KEY_NBT = "nbt";

/**
* The fluid the label represents.
*/
Fluid fluid;

/**
* The fluids optional NBT tag.
*/
NBTTagCompound nbt;

/**
* An instance of {@link FluidStack} that represents the fluid inside inventories.
*/
FluidStack temp;

/**
* Returns the fluid (item) stack representation.
*/
@Override
public FluidStack getRepresentation() {
return temp;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,20 @@ void refreshCalculator() {
refreshResult();
}

/*
* Gets all items in the player's inventory. Additionally, checks a list of fluid containers to see if the player
* carries and fluids for crafting tasks with them.
*/
List<ILabel> getInventory() {
InventoryPlayer inv = Utilities.getPlayer().inventory;
ArrayList<ILabel> labels = new ArrayList<>();

Consumer<ItemStack[]> add = i -> Arrays.stream(i)
.filter(j -> !ItemStackHelper.isEmpty(j))
.forEach(j -> labels.add(ILabel.Converter.from(j)));
add.accept(inv.armorInventory);
add.accept(inv.mainInventory);

return labels;
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@ public class ItemStackHelper {
public static final Item EMPTY_ITEM = null;
public static final ItemStack EMPTY_ITEM_STACK = new ItemStack((Item) null);

private static final String GREGTECH_LARGE_FLUID_CELL_ID = "gt.metaitem.01";

public static boolean isEmpty(ItemStack stack) {
return stack == null || stack == EMPTY_ITEM_STACK || stack.getItem() == EMPTY_ITEM;
}

/**
* Checks if the given ItemStack is a GregTech large fluid container. This method checks for all variants of the
* item.
*
* @param itemStack The ItemStack to check.
* @return True if the ItemStack is a GregTech large fluid container, false otherwise.
*/
public static boolean isGregTechLargeFluidContainer(ItemStack itemStack) {
Item item = itemStack.getItem();
if (item == null) {
return false;
}

return item.getUnlocalizedName()
.equals(GREGTECH_LARGE_FLUID_CELL_ID);
}
}

0 comments on commit eba71ca

Please sign in to comment.