Skip to content

Commit

Permalink
remove optional int for fluid colours and avoid the use of iterators …
Browse files Browse the repository at this point in the history
…for displaying colour bars from attachments
  • Loading branch information
thiakil committed Jan 26, 2025
1 parent 3d23e21 commit d1fad7e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected static void renderBar(GuiGraphics guiGraphics, int stackXPos, int yPos

protected static void renderBar(GuiGraphics guiGraphics, int stackXPos, int yPos, IExtendedFluidTank tank) {
FluidStack fluid = tank.getFluid();
renderBar(guiGraphics, stackXPos, yPos, fluid.getAmount(), tank.getCapacity(), FluidUtils.getRGBDurabilityForDisplay(fluid).orElse(0xFFFFFFFF));
renderBar(guiGraphics, stackXPos, yPos, fluid.getAmount(), tank.getCapacity(), FluidUtils.getRGBDurabilityForDisplay(fluid));
}

protected static void renderBar(GuiGraphics guiGraphics, int stackXPos, int yPos, long amount, long capacity, int color) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mekanism/common/item/ItemGaugeDropper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public int getBarWidth(@NotNull ItemStack stack) {

@Override
public int getBarColor(@NotNull ItemStack stack) {
OptionalInt color = FluidUtils.getRGBDurabilityForDisplay(stack);
if (color.isPresent()) {
return color.getAsInt();
FluidStack fluid = StorageUtils.getFirstFluidFromAttachment(stack);
if (!fluid.isEmpty()) {
return FluidUtils.getRGBDurabilityForDisplay(stack);
}
return ChemicalUtil.getRGBDurabilityForDisplay(stack);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/item/gear/ItemCanteen.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public int getBarWidth(@NotNull ItemStack stack) {

@Override
public int getBarColor(@NotNull ItemStack stack) {
return FluidUtils.getRGBDurabilityForDisplay(stack).orElse(0);
return FluidUtils.getRGBDurabilityForDisplay(stack);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/util/ChemicalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static ItemStack getFilledVariant(ItemStack toFill, IChemicalProvider pro
}

public static int getRGBDurabilityForDisplay(ItemStack stack) {
ChemicalStack chemicalStack = StorageUtils.getStoredChemicalFromAttachment(stack);
ChemicalStack chemicalStack = StorageUtils.getFirstChemicalFromAttachment(stack);
return chemicalStack.isEmpty() ? 0 : chemicalStack.getChemicalColorRepresentation();
}

Expand Down
31 changes: 16 additions & 15 deletions src/main/java/mekanism/common/util/FluidUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,25 @@ public static ItemStack getFilledVariant(ItemStack toFill, Fluid fluid) {
return toFill;
}

public static OptionalInt getRGBDurabilityForDisplay(ItemStack stack) {
return getRGBDurabilityForDisplay(StorageUtils.getStoredFluidFromAttachment(stack));
public static int getRGBDurabilityForDisplay(ItemStack stack) {
return getRGBDurabilityForDisplay(StorageUtils.getFirstFluidFromAttachment(stack));
}

public static OptionalInt getRGBDurabilityForDisplay(FluidStack stack) {
if (!stack.isEmpty()) {
//TODO: Technically doesn't support things where the color is part of the texture such as lava
// for chemicals it is supported via allowing people to override getColorRepresentation in their
// chemicals
if (stack.getFluid().isSame(Fluids.LAVA)) {//Special case lava
return OptionalInt.of(0xFFDB6B19);
} else if (FMLEnvironment.dist.isClient()) {
//Note: We can only return an accurate result on the client side. This method should never be called from the server
// but in case it is make sure we only run on the client side
return OptionalInt.of(IClientFluidTypeExtensions.of(stack.getFluid()).getTintColor(stack));
}
public static int getRGBDurabilityForDisplay(FluidStack stack) {
if (stack.isEmpty()) {
return 0xFFFFFFFF;
}
//TODO: Technically doesn't support things where the color is part of the texture such as lava
// for chemicals it is supported via allowing people to override getColorRepresentation in their
// chemicals
if (stack.getFluid().isSame(Fluids.LAVA)) {//Special case lava
return 0xFFDB6B19;
} else if (FMLEnvironment.dist.isClient()) {
//Note: We can only return an accurate result on the client side. This method should never be called from the server
// but in case it is make sure we only run on the client side
return IClientFluidTypeExtensions.of(stack.getFluid()).getTintColor(stack);
}
return OptionalInt.empty();
return 0xFFFFFFFF;
}

public static void emit(Collection<BlockCapabilityCache<IFluidHandler, @Nullable Direction>> targets, IExtendedFluidTank tank) {
Expand Down
127 changes: 96 additions & 31 deletions src/main/java/mekanism/common/util/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,48 +184,113 @@ public static FluidStack getContainedFluid(@NotNull IFluidHandlerItem fluidHandl
*/
@NotNull
public static FluidStack getStoredFluidFromAttachment(ItemStack stack) {
FluidStack fluid = FluidStack.EMPTY;
for (IExtendedFluidTank tank : ContainerType.FLUID.getAttachmentContainersIfPresent(stack)) {
if (tank.isEmpty()) {
continue;
}
if (fluid.isEmpty()) {
fluid = tank.getFluid().copy();
} else if (tank.isFluidEqual(fluid)) {
if (fluid.getAmount() < Integer.MAX_VALUE - tank.getFluidAmount()) {
fluid.grow(tank.getFluidAmount());
} else {
fluid.setAmount(Integer.MAX_VALUE);
List<IExtendedFluidTank> containers = ContainerType.FLUID.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return FluidStack.EMPTY;
case 1:
return containers.getFirst().getFluid().copy();
default:
FluidStack fluid = FluidStack.EMPTY;
for (IExtendedFluidTank tank : containers) {
if (tank.isEmpty()) {
continue;
}
if (fluid.isEmpty()) {
fluid = tank.getFluid().copy();
} else if (tank.isFluidEqual(fluid)) {
if (fluid.getAmount() < Integer.MAX_VALUE - tank.getFluidAmount()) {
fluid.grow(tank.getFluidAmount());
} else {
fluid.setAmount(Integer.MAX_VALUE);
}
}
//Note: If we have multiple tanks that have different types stored we only return the first type
}
}
//Note: If we have multiple tanks that have different types stored we only return the first type
return fluid;
}
}

/**
* Gets the FIRST fluid stored in an item's container by checking the attachment. This is for cases when we may not actually have a fluid handler provided as a
* capability from our item, but it may have stored data in its container from when it was a block. Do NOT modify the result
*
* @return the first found fluid FOR DISPLAY. Do NOT modify.
*/
public static FluidStack getFirstFluidFromAttachment(ItemStack stack) {
List<IExtendedFluidTank> containers = ContainerType.FLUID.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return FluidStack.EMPTY;
case 1:
return containers.getFirst().getFluid();
default:
for (IExtendedFluidTank tank : containers) {
if (tank.isEmpty()) {
continue;
}
return tank.getFluid();
}
return FluidStack.EMPTY;
}
return fluid;
}

/**
* Gets the pigment stored in an item's container by checking the attachment. This is for cases when we may not actually have a chemical handler provided as a
* Gets the chemical stored in an item's container by checking the attachment. This is for cases when we may not actually have a chemical handler provided as a
* capability from our item, but it may have stored data in its container from when it was a block
*/
@NotNull
public static ChemicalStack getStoredChemicalFromAttachment(ItemStack stack) {
ChemicalStack chemicalStack = ChemicalStack.EMPTY;
for (IChemicalTank tank : ContainerType.CHEMICAL.getAttachmentContainersIfPresent(stack)) {
if (tank.isEmpty()) {
continue;
}
if (chemicalStack.isEmpty()) {
chemicalStack = tank.getStack().copy();
} else if (tank.isTypeEqual(chemicalStack)) {
if (chemicalStack.getAmount() < Long.MAX_VALUE - tank.getStored()) {
chemicalStack.grow(tank.getStored());
} else {
chemicalStack.setAmount(Long.MAX_VALUE);
List<IChemicalTank> containers = ContainerType.CHEMICAL.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return ChemicalStack.EMPTY;
case 1:
return containers.getFirst().getStack().copy();
default:
ChemicalStack chemicalStack = ChemicalStack.EMPTY;
for (IChemicalTank tank : containers) {
if (tank.isEmpty()) {
continue;
}
if (chemicalStack.isEmpty()) {
chemicalStack = tank.getStack().copy();
} else if (tank.isTypeEqual(chemicalStack)) {
if (chemicalStack.getAmount() < Long.MAX_VALUE - tank.getStored()) {
chemicalStack.grow(tank.getStored());
} else {
chemicalStack.setAmount(Long.MAX_VALUE);
}
}
//Note: If we have multiple tanks that have different types stored we only return the first type
}
}
//Note: If we have multiple tanks that have different types stored we only return the first type
return chemicalStack;
}
}

/**
* Gets the FIRST chemical stored in an item's container by checking the attachment. This is for cases when we may not actually have a chemical handler provided as a
* capability from our item, but it may have stored data in its container from when it was a block. Do NOT modify the result
*
* @return the first found chemical FOR DISPLAY. Do NOT modify.
*/
@NotNull
public static ChemicalStack getFirstChemicalFromAttachment(ItemStack stack) {
List<IChemicalTank> containers = ContainerType.CHEMICAL.getAttachmentContainersIfPresent(stack);
switch (containers.size()) {
case 0:
return ChemicalStack.EMPTY;
case 1:
return containers.getFirst().getStack();
default:
for (IChemicalTank tank : containers) {
if (tank.isEmpty()) {
continue;
}
return tank.getStack();
}
return ChemicalStack.EMPTY;
}
return chemicalStack;
}

/**
Expand Down

0 comments on commit d1fad7e

Please sign in to comment.