]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/WorldSaveRow.java
5f26c74bdcfb2f56227a1673d4fadc39c92d5542
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / WorldSaveRow.java
1 package com.irtimaled.bbor.client.gui;
2
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.Gui;
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.ISaveFormat;
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;
17
18 import java.io.File;
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
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 ISaveFormat saveLoader;
33     private final ResourceLocation iconLocation;
34     private final DynamicTexture icon;
35
36     private File iconFile;
37     private long lastClickTime;
38
39     WorldSaveRow(WorldSummary worldSummary, ISaveFormat 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()) {
46             this.iconFile = null;
47         }
48
49         this.icon = this.loadIcon();
50     }
51
52     @Override
53     public boolean mouseClicked(double mouseX, double mouseY, int button) {
54         this.list.setSelectedIndex(this.index);
55         if (Util.milliTime() - this.lastClickTime < 250L) {
56             loadWorld();
57             return true;
58         } else {
59             this.lastClickTime = Util.milliTime();
60             return false;
61         }
62     }
63
64     @Override
65     public boolean mouseReleased(double mouseX, double mouseY, int button) {
66         return false;
67     }
68
69     void loadWorld() {
70         String fileName = this.worldSummary.getFileName();
71         WorldInfo worldInfo = saveLoader.getWorldInfo(fileName);
72         long seed = worldInfo.getSeed();
73         ClientInterop.saveLoaded(fileName, seed);
74     }
75
76     private DynamicTexture loadIcon() {
77         if (this.iconFile == null || !this.iconFile.isFile()) {
78             this.client.getTextureManager().deleteTexture(this.iconLocation);
79             return null;
80         }
81
82         try (InputStream stream = new FileInputStream(this.iconFile)) {
83             DynamicTexture texture = new DynamicTexture(NativeImage.read(stream));
84             this.client.getTextureManager().loadTexture(this.iconLocation, texture);
85             return texture;
86         } catch (Throwable exception) {
87             LOGGER.error("Invalid icon for world {}", this.worldSummary.getFileName(), exception);
88             this.iconFile = null;
89             return null;
90         }
91     }
92
93     @Override
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())) + ")";
97
98         int x = this.getX();
99         int y = this.getY();
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         Gui.drawModalRectWithCustomSizedTexture(x, y, 0.0F, 0.0F, ICON_SIZE, ICON_SIZE, 32.0F, 32.0F);
105         GL11.glDisable(GL11.GL_BLEND);
106     }
107
108     @Override
109     public int getControlWidth() {
110         return 310;
111     }
112
113     @Override
114     public void filter(String lowerValue) {
115         setVisible(lowerValue == "" ||
116                 this.worldSummary.getDisplayName().toLowerCase().contains(lowerValue) ||
117                 this.worldSummary.getFileName().toLowerCase().contains(lowerValue));
118     }
119
120     @Override
121     public void close() {
122         if (this.icon != null) {
123             this.icon.close();
124         }
125     }
126
127     @Override
128     public int compareTo(WorldSaveRow other) {
129         return this.worldSummary.compareTo(other.worldSummary);
130     }
131 }