Skip to content

Commit

Permalink
Fix crash in alloyer when the side tank capacity is 0 (#4937)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Sep 4, 2022
1 parent 57f8af4 commit 8f35397
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static void renderFluidTank(PoseStack matrices, AbstractContainerScreen<?
* @param depth Tank depth
*/
public static void renderFluidTank(PoseStack matrices, AbstractContainerScreen<?> screen, FluidStack stack, int amount, int capacity, int x, int y, int width, int height, int depth) {
if(!stack.isEmpty()) {
if(!stack.isEmpty() && capacity > 0) {
int maxY = y + height;
int fluidHeight = Math.min(height * amount / capacity, height);
renderTiledFluid(matrices, screen, stack, x, maxY - fluidHeight, width, fluidHeight, depth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import slimeknights.mantle.fluid.tooltip.FluidTooltipHandler;
Expand All @@ -24,9 +23,9 @@
*/
public class GuiSmelteryTank {
// fluid tooltips
public static final String TOOLTIP_CAPACITY = TConstruct.makeTranslationKey("gui", "melting.capacity");
public static final String TOOLTIP_AVAILABLE = TConstruct.makeTranslationKey("gui", "melting.available");
public static final String TOOLTIP_USED = TConstruct.makeTranslationKey("gui", "melting.used");
public static final Component TOOLTIP_CAPACITY = TConstruct.makeTranslation("gui", "melting.capacity");
public static final Component TOOLTIP_AVAILABLE = TConstruct.makeTranslation("gui", "melting.available");
public static final Component TOOLTIP_USED = TConstruct.makeTranslation("gui", "melting.used");

private final AbstractContainerScreen<?> parent;
private final SmelteryTank<?> tank;
Expand Down Expand Up @@ -163,17 +162,17 @@ public void drawTooltip(PoseStack matrices, int mouseX, int mouseY) {
BiConsumer<Integer, List<Component>> formatter = Screen.hasShiftDown() ? FluidTooltipHandler.BUCKET_FORMATTER : this.formatter;

tooltip = new ArrayList<>();
tooltip.add(new TranslatableComponent(TOOLTIP_CAPACITY));
tooltip.add(TOOLTIP_CAPACITY);

formatter.accept(tank.getCapacity(), tooltip);
int remaining = tank.getRemainingSpace();
if (remaining > 0) {
tooltip.add(new TranslatableComponent(TOOLTIP_AVAILABLE));
tooltip.add(TOOLTIP_AVAILABLE);
formatter.accept(remaining, tooltip);
}
int used = tank.getContained();
if (used > 0) {
tooltip.add(new TranslatableComponent(TOOLTIP_USED));
tooltip.add(TOOLTIP_USED);
formatter.accept(used, tooltip);
}
FluidTooltipHandler.appendShift(tooltip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.mojang.blaze3d.vertex.PoseStack;
import lombok.Getter;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import slimeknights.mantle.Mantle;
import slimeknights.mantle.fluid.tooltip.FluidTooltipHandler;
import slimeknights.tconstruct.library.client.GuiUtil;

Expand All @@ -21,6 +23,9 @@
* Module handling the melter tank UI display
*/
public class GuiTankModule {
/** Tooltip for when the capacity is 0, it breaks some stuff */
private static final Component NO_CAPACITY = new TranslatableComponent(Mantle.makeDescriptionId("gui", "fluid.millibucket"), 0).withStyle(ChatFormatting.GRAY);

private static final int TANK_INDEX = 0;
private final AbstractContainerScreen<?> screen;
private final IFluidHandler tank;
Expand Down Expand Up @@ -53,7 +58,11 @@ private boolean isHovered(int checkX, int checkY) {
* @return Fluid height
*/
private int getFluidHeight() {
return height * tank.getFluidInTank(TANK_INDEX).getAmount() / tank.getTankCapacity(TANK_INDEX);
int capacity = tank.getTankCapacity(TANK_INDEX);
if (capacity == 0) {
return height;
}
return height * tank.getFluidInTank(TANK_INDEX).getAmount() / capacity;
}

/**
Expand Down Expand Up @@ -103,7 +112,7 @@ public void renderTooltip(PoseStack matrices, int mouseX, int mouseY) {

// if hovering over the fluid, display with name
final List<Component> tooltip;
if (checkY > (y + height) - getFluidHeight()) {
if (capacity > 0 && checkY > (y + height) - getFluidHeight()) {
tooltip = FluidTooltipHandler.getFluidTooltip(fluid);
} else {
// function to call for amounts
Expand All @@ -113,16 +122,18 @@ public void renderTooltip(PoseStack matrices, int mouseX, int mouseY) {

// add tooltips
tooltip = new ArrayList<>();
tooltip.add(new TranslatableComponent(GuiSmelteryTank.TOOLTIP_CAPACITY));
formatter.accept(capacity, tooltip);
if (capacity != amount) {
tooltip.add(new TranslatableComponent(GuiSmelteryTank.TOOLTIP_AVAILABLE));
formatter.accept(capacity - amount, tooltip);
tooltip.add(GuiSmelteryTank.TOOLTIP_CAPACITY);
if (capacity == 0) {
tooltip.add(NO_CAPACITY);
} else {
formatter.accept(capacity, tooltip);
if (capacity != amount) {
tooltip.add(GuiSmelteryTank.TOOLTIP_AVAILABLE);
formatter.accept(capacity - amount, tooltip);
}
// add shift message
FluidTooltipHandler.appendShift(tooltip);
}

// add shift message
//tooltip.add("");
FluidTooltipHandler.appendShift(tooltip);
}

// TODO: renderComponentTooltip->renderTooltip
Expand Down

0 comments on commit 8f35397

Please sign in to comment.