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.client.util.math.MatrixStack;
11 import net.minecraft.nbt.NbtIo;
12 import net.minecraft.util.Identifier;
13 import net.minecraft.util.Util;
14 import net.minecraft.util.WorldSavePath;
15 import net.minecraft.world.level.storage.LevelStorage;
16 import net.minecraft.world.level.storage.LevelSummary;
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.function.Consumer;
29 public class WorldSaveRow extends ControlListEntry implements Comparable<WorldSaveRow> {
30 private static final Logger LOGGER = LogManager.getLogger();
31 private static final DateFormat DATE_FORMAT = new SimpleDateFormat();
32 private static final Identifier ICON_MISSING = new Identifier("textures/misc/unknown_server.png");
33 private static final int ICON_SIZE = 20;
34 private final MinecraftClient client;
35 private final LevelSummary worldSummary;
36 private final LevelStorage saveLoader;
37 private final Consumer<ControlListEntry> setSelectedEntry;
38 private final Identifier iconLocation;
39 private final NativeImageBackedTexture icon;
41 private File iconFile;
42 private long lastClickTime;
44 WorldSaveRow(LevelSummary worldSummary, LevelStorage saveLoader, Consumer<ControlListEntry> setSelectedEntry) {
45 this.worldSummary = worldSummary;
46 this.saveLoader = saveLoader;
47 this.setSelectedEntry = setSelectedEntry;
48 this.client = MinecraftClient.getInstance();
49 this.iconLocation = new Identifier("worlds/" + Hashing.sha1().hashUnencodedChars(worldSummary.getName()) + "/icon");
50 this.iconFile = worldSummary.getFile();
51 if (!this.iconFile.isFile()) {
55 this.icon = this.loadIcon();
59 public boolean isMouseOver(double mouseX, double mouseY) {
60 return mouseX > this.getX() &&
61 mouseX < this.getX() + ControlList.CONTROLS_WIDTH &&
62 mouseY > this.getY() &&
63 mouseY < this.getY() + this.getControlHeight();
67 public boolean mouseClicked(double mouseX, double mouseY, int button) {
68 if (!isMouseOver(mouseX, mouseY)) return false;
70 this.setSelectedEntry.accept(this);
71 if (Util.getMeasuringTimeMs() - this.lastClickTime < 250L) {
74 this.lastClickTime = Util.getMeasuringTimeMs();
81 String fileName = this.worldSummary.getName();
82 LevelStorage.Session worldInfo = null;
84 worldInfo = saveLoader.createSession(fileName);
85 } catch (IOException e) {
89 long seed = NbtIo.readCompressed(new FileInputStream(worldInfo.getDirectory(WorldSavePath.LEVEL_DAT).toFile()))
91 .getCompound("WorldGenSettings").getLong("seed");
93 ClientInterop.saveLoaded(fileName, seed);
94 } catch (IOException ignored) {
98 private NativeImageBackedTexture loadIcon() {
99 if (this.iconFile == null || !this.iconFile.isFile()) {
100 this.client.getTextureManager().destroyTexture(this.iconLocation);
104 try (InputStream stream = new FileInputStream(this.iconFile)) {
105 NativeImageBackedTexture texture = new NativeImageBackedTexture(NativeImage.read(stream));
106 this.client.getTextureManager().registerTexture(this.iconLocation, texture);
108 } catch (Throwable exception) {
109 LOGGER.error("Invalid icon for world {}", this.worldSummary.getName(), exception);
110 this.iconFile = null;
116 public void render(MatrixStack matrixStack, int mouseX, int mouseY) {
117 String displayName = this.worldSummary.getDisplayName();
118 String details = this.worldSummary.getName() + " (" + DATE_FORMAT.format(new Date(this.worldSummary.getLastPlayed())) + ")";
122 this.client.textRenderer.draw(matrixStack, displayName, (float) (x + ICON_SIZE + 3), (float) (y + 1), 16777215);
123 this.client.textRenderer.draw(matrixStack, details, (float) (x + ICON_SIZE + 3), (float) (y + 1 + this.client.textRenderer.fontHeight + 1), 8421504);
124 this.client.getTextureManager().bindTexture(this.icon != null ? this.iconLocation : ICON_MISSING);
125 RenderHelper.enableBlend();
126 DrawableHelper.drawTexture(matrixStack, x, y, 0.0F, 0.0F, ICON_SIZE, ICON_SIZE, 32, 32);
127 RenderHelper.disableBlend();
131 public void filter(String lowerValue) {
132 super.setVisible(lowerValue.isEmpty() ||
133 this.worldSummary.getDisplayName().toLowerCase().contains(lowerValue) ||
134 this.worldSummary.getName().toLowerCase().contains(lowerValue));
138 public void close() {
139 if (this.icon != null) {
145 public int compareTo(WorldSaveRow other) {
146 return this.worldSummary.compareTo(other.worldSummary);