1 package com.irtimaled.bbor.client.gui;
3 import com.google.common.hash.Hashing;
4 import com.irtimaled.bbor.client.interop.ClientInterop;
5 import com.irtimaled.bbor.client.renderers.RenderHelper;
6 import net.minecraft.client.MinecraftClient;
7 import net.minecraft.client.gui.DrawableHelper;
8 import net.minecraft.client.texture.NativeImage;
9 import net.minecraft.client.texture.NativeImageBackedTexture;
10 import net.minecraft.util.Identifier;
11 import net.minecraft.util.Util;
12 import net.minecraft.world.level.LevelProperties;
13 import net.minecraft.world.level.storage.LevelStorage;
14 import net.minecraft.world.level.storage.LevelSummary;
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.function.Consumer;
26 public class WorldSaveRow extends ControlListEntry implements Comparable<WorldSaveRow> {
27 private static final Logger LOGGER = LogManager.getLogger();
28 private static final DateFormat DATE_FORMAT = new SimpleDateFormat();
29 private static final Identifier ICON_MISSING = new Identifier("textures/misc/unknown_server.png");
30 private static final int ICON_SIZE = 20;
31 private final MinecraftClient client;
32 private final LevelSummary worldSummary;
33 private final LevelStorage saveLoader;
34 private final Consumer<ControlListEntry> setSelectedEntry;
35 private final Identifier iconLocation;
36 private final NativeImageBackedTexture icon;
38 private File iconFile;
39 private long lastClickTime;
41 WorldSaveRow(LevelSummary worldSummary, LevelStorage saveLoader, Consumer<ControlListEntry> setSelectedEntry) {
42 this.worldSummary = worldSummary;
43 this.saveLoader = saveLoader;
44 this.setSelectedEntry = setSelectedEntry;
45 this.client = MinecraftClient.getInstance();
46 this.iconLocation = new Identifier("worlds/" + Hashing.sha1().hashUnencodedChars(worldSummary.getName()) + "/icon");
47 this.iconFile = saveLoader.resolveFile(worldSummary.getName(), "icon.png");
48 if (!this.iconFile.isFile()) {
52 this.icon = this.loadIcon();
56 public boolean isMouseOver(double mouseX, double mouseY) {
57 return mouseX > this.getX() &&
58 mouseX < this.getX() + ControlList.CONTROLS_WIDTH &&
59 mouseY > this.getY() &&
60 mouseY < this.getY() + this.getControlHeight();
64 public boolean mouseClicked(double mouseX, double mouseY, int button) {
65 if (!isMouseOver(mouseX, mouseY)) return false;
67 this.setSelectedEntry.accept(this);
68 if (Util.getMeasuringTimeMs() - this.lastClickTime < 250L) {
71 this.lastClickTime = Util.getMeasuringTimeMs();
78 String fileName = this.worldSummary.getName();
79 LevelProperties worldInfo = saveLoader.getLevelProperties(fileName);
80 long seed = worldInfo.getSeed();
81 ClientInterop.saveLoaded(fileName, seed);
84 private NativeImageBackedTexture loadIcon() {
85 if (this.iconFile == null || !this.iconFile.isFile()) {
86 this.client.getTextureManager().destroyTexture(this.iconLocation);
90 try (InputStream stream = new FileInputStream(this.iconFile)) {
91 NativeImageBackedTexture texture = new NativeImageBackedTexture(NativeImage.read(stream));
92 this.client.getTextureManager().registerTexture(this.iconLocation, texture);
94 } catch (Throwable exception) {
95 LOGGER.error("Invalid icon for world {}", this.worldSummary.getName(), exception);
102 public void render(int mouseX, int mouseY) {
103 String displayName = this.worldSummary.getDisplayName();
104 String details = this.worldSummary.getName() + " (" + DATE_FORMAT.format(new Date(this.worldSummary.getLastPlayed())) + ")";
108 this.client.textRenderer.draw(displayName, (float) (x + ICON_SIZE + 3), (float) (y + 1), 16777215);
109 this.client.textRenderer.draw(details, (float) (x + ICON_SIZE + 3), (float) (y + 1 + this.client.textRenderer.fontHeight + 1), 8421504);
110 this.client.getTextureManager().bindTexture(this.icon != null ? this.iconLocation : ICON_MISSING);
111 RenderHelper.enableBlend();
112 DrawableHelper.blit(x, y, 0.0F, 0.0F, ICON_SIZE, ICON_SIZE, 32, 32);
113 RenderHelper.disableBlend();
117 public void filter(String lowerValue) {
118 super.setVisible(lowerValue.isEmpty() ||
119 this.worldSummary.getDisplayName().toLowerCase().contains(lowerValue) ||
120 this.worldSummary.getName().toLowerCase().contains(lowerValue));
124 public void close() {
125 if (this.icon != null) {
131 public int compareTo(WorldSaveRow other) {
132 return this.worldSummary.compareTo(other.worldSummary);