Skip to content

Commit

Permalink
Fix #105.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Jun 8, 2023
1 parent ddca832 commit 0a59de4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@
- Updated to Minecraft 1.19.4.
- Added painting preview in painting items with a known variant.
- Added Ukrainian translations ([#100](https://github.com/Queerbric/Inspecio/pull/100)).

### 1.8.1

- Fixed ignoring `HideFlags` in armor NBT ([#98](https://github.com/Queerbric/Inspecio/issues/98)).
- Fixed messed up colors in shulker box previews ([#102](https://github.com/Queerbric/Inspecio/issues/102)).
- Fixed unbounded hunger/saturation tooltips with auto-wrapping ([#105](https://github.com/Queerbric/Inspecio/issues/105)).
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.FoodComponent;
import net.minecraft.util.math.MathHelper;
import org.quiltmc.qsl.tooltip.api.ConvertibleTooltipData;

public record FoodTooltipComponent(FoodComponent component) implements ConvertibleTooltipData, TooltipComponent {
public record FoodTooltipComponent(int hunger, float saturation) implements ConvertibleTooltipData, TooltipComponent {
public FoodTooltipComponent(FoodComponent component) {
this(component.getHunger(), component.getHunger() * component.getSaturationModifier());
}

private static final int COLUMNS = 16;

@Override
public TooltipComponent toComponent() {
return this;
Expand All @@ -38,16 +45,23 @@ public TooltipComponent toComponent() {
@Override
public int getHeight() {
var foodConfig = Inspecio.getConfig().getFoodConfig();
int height = Math.max(
11 * this.getLines(this.getHungerChunks()),
11 * this.getLines(MathHelper.ceil(this.saturation))
);

int height = 11;
if (foodConfig.hasHunger() && foodConfig.getSaturationMode() == SaturationTooltipMode.SEPARATED)
height += 11;
height += 11 * this.getLines(this.getSaturationChunks());

return height;
}

@Override
public int getWidth(TextRenderer textRenderer) {
return Math.max(this.component.getHunger() / 2 * 9, (int) (this.component.getHunger() * this.component.getSaturationModifier() * 9));
return Math.min(
Math.max(this.hunger / 2 * 9, (int) this.saturation * 9),
COLUMNS * 9
);
}

@Override
Expand All @@ -56,56 +70,127 @@ public void drawItems(TextRenderer textRenderer, int x, int y, MatrixStack matri

RenderSystem.setShaderTexture(0, InGameHud.GUI_ICONS_TEXTURE);
int saturationY = y;
if (foodConfig.getSaturationMode() == SaturationTooltipMode.SEPARATED && foodConfig.hasHunger()) saturationY += 11;
if (foodConfig.getSaturationMode() == SaturationTooltipMode.SEPARATED && foodConfig.hasHunger()) {
saturationY += 11 * this.getLines(this.getHungerChunks());
}

var pos = new ChunkPos(x, y);

// Draw hunger outline.
if (foodConfig.hasHunger()) {
for (int i = 0; i < (this.component.getHunger() + 1) / 2; i++) {
DrawableHelper.drawTexture(matrices, x + i * 9, y, 16, 27, 9, 9, 256, 256);
for (int i = 0; i < (this.hunger + 1) / 2; i++) {
pos.wrap(i);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 16, 27, 9, 9, 256, 256);
pos.moveForward();
}
}

// Draw saturation outline.
float saturation = this.component.getHunger() * this.component.getSaturationModifier();
if (foodConfig.getSaturationMode().isEnabled()) {
RenderSystem.setShaderColor(159 / 255.f, 134 / 255.f, 9 / 255.f, 1.f);
for (int i = 0; i < saturation; i++) {

pos.reset(x, saturationY);

for (int i = 0; i < this.saturation; i++) {
pos.wrap(i);

int width = 9;
if (saturation - i < 1f) {
if (this.saturation - i < 1f) {
width = Math.round(width * (saturation - i));
}
DrawableHelper.drawTexture(matrices, x + i * 9, saturationY, 25, 27, width, 9, 256, 256);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 25, 27, width, 9, 256, 256);

pos.moveForward();
}
}

// Draw hunger bars.
RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f);
if (foodConfig.hasHunger()) {
for (int i = 0; i < this.component.getHunger() / 2; i++) {
DrawableHelper.drawTexture(matrices, x + i * 9, y, 52, 27, 9, 9, 256, 256);
pos.reset(x, y);

for (int i = 0; i < this.hunger / 2; i++) {
pos.wrap(i);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 52, 27, 9, 9, 256, 256);
pos.moveForward();
}
if (this.component.getHunger() % 2 == 1) {
DrawableHelper.drawTexture(matrices, x + this.component.getHunger() / 2 * 9, y, 61, 27, 9, 9, 256, 256);

if (this.hunger % 2 == 1) {
pos.wrap(this.hunger / 2);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 61, 27, 9, 9, 256, 256);
}
}

// Draw saturation bar if separate (or alone).
if (foodConfig.getSaturationMode() == SaturationTooltipMode.SEPARATED || !foodConfig.hasHunger()) {
RenderSystem.setShaderColor(229 / 255.f, 204 / 255.f, 209 / 255.f, 1.f);

pos.reset(x, saturationY);

int intSaturation = Math.max(1, this.getSaturation());
if (saturation * 2 - intSaturation > 0.2)
if (this.saturation * 2 - intSaturation > 0.2)
intSaturation++;

for (int i = 0; i < intSaturation / 2; i++) {
DrawableHelper.drawTexture(matrices, x + i * 9, saturationY, 52, 27, 9, 9, 256, 256);
pos.wrap(i);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 52, 27, 9, 9, 256, 256);
pos.moveForward();
}

if (intSaturation % 2 == 1) {
DrawableHelper.drawTexture(matrices, x + this.getSaturation() / 2 * 9, saturationY, 61, 27, 9, 9, 256, 256);
pos.wrap(intSaturation / 2);
DrawableHelper.drawTexture(matrices, pos.x, pos.y, 61, 27, 9, 9, 256, 256);
}

RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f);
}
}

private int getHungerChunks() {
return this.hunger / 2 + this.hunger % 2;
}

private int getSaturation() {
return (int) (this.component.getHunger() * this.component.getSaturationModifier() * 2.f);
return (int) (this.saturation * 2.f);
}

private int getSaturationChunks() {
int intSaturation = this.getSaturation();
if (this.saturation * 2 - intSaturation > 0.2) {
return (int) (this.saturation + 1);
} else {
return (int) this.saturation;
}
}

private int getLines(int chunks) {
return chunks / COLUMNS + (chunks % COLUMNS > 0 ? 1 : 0);
}

private static class ChunkPos {
private final int originalX;
private int x;
private int y;

public ChunkPos(int x, int y) {
this.originalX = this.x = x;
this.y = y;
}

public void moveForward() {
this.x += 9;
}

public void wrap(int progress) {
if (progress != 0 && progress % COLUMNS == 0) {
this.x = this.originalX;
this.y += 11;
}
}

public void reset(int x, int y) {
this.x = x;
this.y = y;
}
}
}

0 comments on commit 0a59de4

Please sign in to comment.