1 package com.irtimaled.bbor.client.gui;
3 import com.google.common.hash.Hashing;
4 import com.irtimaled.bbor.client.interop.ClientInterop;
5 import net.minecraft.client.Minecraft;
6 import net.minecraft.client.gui.AbstractGui;
7 import net.minecraft.client.renderer.texture.DynamicTexture;
8 import net.minecraft.client.renderer.texture.NativeImage;
9 import net.minecraft.util.ResourceLocation;
10 import net.minecraft.util.Util;
11 import net.minecraft.world.storage.SaveFormat;
12 import net.minecraft.world.storage.WorldInfo;
13 import net.minecraft.world.storage.WorldSummary;
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16 import org.lwjgl.opengl.GL11;
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;
25 public class WorldSaveRow extends ControlListEntry implements Comparable<WorldSaveRow> {
26 private static final Logger LOGGER = LogManager.getLogger();
27 private static final DateFormat DATE_FORMAT = new SimpleDateFormat();
28 private static final ResourceLocation ICON_MISSING = new ResourceLocation("textures/misc/unknown_server.png");
29 private static final int ICON_SIZE = 20;
30 private final Minecraft client;
31 private final WorldSummary worldSummary;
32 private final SaveFormat saveLoader;
33 private final ResourceLocation iconLocation;
34 private final DynamicTexture icon;
36 private File iconFile;
37 private long lastClickTime;
39 WorldSaveRow(WorldSummary worldSummary, SaveFormat saveLoader) {
40 this.worldSummary = worldSummary;
41 this.saveLoader = saveLoader;
42 this.client = Minecraft.getInstance();
43 this.iconLocation = new ResourceLocation("worlds/" + Hashing.sha1().hashUnencodedChars(worldSummary.getFileName()) + "/icon");
44 this.iconFile = saveLoader.getFile(worldSummary.getFileName(), "icon.png");
45 if (!this.iconFile.isFile()) {
49 this.icon = this.loadIcon();
53 public boolean mouseClicked(double mouseX, double mouseY, int button) {
54 this.list.setSelectedIndex(this.index);
55 if (Util.milliTime() - this.lastClickTime < 250L) {
59 this.lastClickTime = Util.milliTime();
65 public boolean mouseReleased(double mouseX, double mouseY, int button) {
70 String fileName = this.worldSummary.getFileName();
71 WorldInfo worldInfo = saveLoader.getWorldInfo(fileName);
72 long seed = worldInfo.getSeed();
73 ClientInterop.saveLoaded(fileName, seed);
76 private DynamicTexture loadIcon() {
77 if (this.iconFile == null || !this.iconFile.isFile()) {
78 this.client.getTextureManager().deleteTexture(this.iconLocation);
82 try (InputStream stream = new FileInputStream(this.iconFile)) {
83 DynamicTexture texture = new DynamicTexture(NativeImage.read(stream));
84 this.client.getTextureManager().loadTexture(this.iconLocation, texture);
86 } catch (Throwable exception) {
87 LOGGER.error("Invalid icon for world {}", this.worldSummary.getFileName(), exception);
94 public void render(int mouseX, int mouseY) {
95 String displayName = this.worldSummary.getDisplayName();
96 String details = this.worldSummary.getFileName() + " (" + DATE_FORMAT.format(new Date(this.worldSummary.getLastTimePlayed())) + ")";
100 this.client.fontRenderer.drawString(displayName, (float) (x + ICON_SIZE + 3), (float) (y + 1), 16777215);
101 this.client.fontRenderer.drawString(details, (float) (x + ICON_SIZE + 3), (float) (y + 1 + this.client.fontRenderer.FONT_HEIGHT + 1), 8421504);
102 this.client.getTextureManager().bindTexture(this.icon != null ? this.iconLocation : ICON_MISSING);
103 GL11.glEnable(GL11.GL_BLEND);
104 AbstractGui.blit(x, y, 0.0F, 0.0F, ICON_SIZE, ICON_SIZE, 32, 32);
105 GL11.glDisable(GL11.GL_BLEND);
109 public int getControlWidth() {
114 public void filter(String lowerValue) {
115 setVisible(lowerValue == "" ||
116 this.worldSummary.getDisplayName().toLowerCase().contains(lowerValue) ||
117 this.worldSummary.getFileName().toLowerCase().contains(lowerValue));
121 public void close() {
122 if (this.icon != null) {
128 public int compareTo(WorldSaveRow other) {
129 return this.worldSummary.compareTo(other.worldSummary);