forked from Pandarix/Beautify
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visual tooltips for the list of plantable plants
- Loading branch information
Showing
6 changed files
with
343 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/io/github/suel_ki/beautify/client/tooltip/ClientPlantableItemStackTooltip.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.github.suel_ki.beautify.client.tooltip; | ||
|
||
import io.github.suel_ki.beautify.common.tooltip.PlantableItemStackTooltip; | ||
import net.minecraft.ChatFormatting; | ||
import net.minecraft.client.gui.Font; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.inventory.tooltip.TooltipComponent; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public class ClientPlantableItemStackTooltip implements ClientTooltipComponent { | ||
private static final ResourceLocation TEXTURE_LOCATION = new ResourceLocation("container/bundle/slot"); | ||
private static final Component TEXT = Component.translatable("tooltip.beautify.plantable").withStyle(ChatFormatting.GREEN); | ||
|
||
private static final int SLOT_SIZE = 18; | ||
private final int columns; | ||
private final List<ItemStack> plants; | ||
|
||
public ClientPlantableItemStackTooltip(PlantableItemStackTooltip tooltip) { | ||
this.columns = tooltip.getColumns(); | ||
this.plants = tooltip.plants(); | ||
} | ||
|
||
@Nullable | ||
public static ClientTooltipComponent get(TooltipComponent component) { | ||
if (component instanceof PlantableItemStackTooltip tooltip) { | ||
return new ClientPlantableItemStackTooltip(tooltip); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void renderImage(Font font, int tooltipX, int tooltipY, GuiGraphics graphics) { | ||
int slotSize = SLOT_SIZE; | ||
int x = tooltipX; | ||
int y = tooltipY + font.lineHeight + 3; | ||
|
||
for (var plant : this.plants) { | ||
|
||
graphics.blitSprite(TEXTURE_LOCATION, x - 1, y - 1, 0, 18, 20); | ||
graphics.renderItem(plant, x, y); | ||
graphics.renderItemDecorations(font, plant, x, y); | ||
|
||
x += slotSize; | ||
|
||
if (x >= tooltipX + slotSize * this.columns) { | ||
x = tooltipX; | ||
y += slotSize; | ||
} | ||
} | ||
|
||
graphics.drawString(font, TEXT, tooltipX, tooltipY, 0); | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
int columns = this.columns; | ||
int rows = (this.plants.size() + columns - 1) / columns; | ||
return rows * SLOT_SIZE + 15; | ||
} | ||
|
||
@Override | ||
public int getWidth(Font font) { | ||
int width = this.columns * SLOT_SIZE; | ||
int textWidth = font.width(TEXT); | ||
return Math.max(width, textWidth); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/io/github/suel_ki/beautify/common/tooltip/PlantableItemStackTooltip.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.suel_ki.beautify.common.tooltip; | ||
|
||
import net.minecraft.world.inventory.tooltip.TooltipComponent; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
import java.util.List; | ||
|
||
public record PlantableItemStackTooltip(List<ItemStack> plants) implements TooltipComponent { | ||
|
||
public int getColumns() { | ||
int maxColumns = 9; | ||
int columns = plants.size() / 3; | ||
if (plants.size() % 3 != 0) { | ||
columns++; | ||
} | ||
return Math.min(columns, maxColumns); | ||
} | ||
} |
Oops, something went wrong.