]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/WorldSaveRow.java
Setup for 1.14.4-Fabric
[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 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;
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 import java.util.function.Consumer;
25
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;
37
38     private File iconFile;
39     private long lastClickTime;
40
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()) {
49             this.iconFile = null;
50         }
51
52         this.icon = this.loadIcon();
53     }
54
55     @Override
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();
61     }
62
63     @Override
64     public boolean mouseClicked(double mouseX, double mouseY, int button) {
65         if (!isMouseOver(mouseX, mouseY)) return false;
66
67         this.setSelectedEntry.accept(this);
68         if (Util.getMeasuringTimeMs() - this.lastClickTime < 250L) {
69             done();
70         } else {
71             this.lastClickTime = Util.getMeasuringTimeMs();
72         }
73         return true;
74     }
75
76     @Override
77     public void done() {
78         String fileName = this.worldSummary.getName();
79         LevelProperties worldInfo = saveLoader.getLevelProperties(fileName);
80         long seed = worldInfo.getSeed();
81         ClientInterop.saveLoaded(fileName, seed);
82     }
83
84     private NativeImageBackedTexture loadIcon() {
85         if (this.iconFile == null || !this.iconFile.isFile()) {
86             this.client.getTextureManager().destroyTexture(this.iconLocation);
87             return null;
88         }
89
90         try (InputStream stream = new FileInputStream(this.iconFile)) {
91             NativeImageBackedTexture texture = new NativeImageBackedTexture(NativeImage.read(stream));
92             this.client.getTextureManager().registerTexture(this.iconLocation, texture);
93             return texture;
94         } catch (Throwable exception) {
95             LOGGER.error("Invalid icon for world {}", this.worldSummary.getName(), exception);
96             this.iconFile = null;
97             return null;
98         }
99     }
100
101     @Override
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())) + ")";
105
106         int x = this.getX();
107         int y = this.getY();
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();
114     }
115
116     @Override
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));
121     }
122
123     @Override
124     public void close() {
125         if (this.icon != null) {
126             this.icon.close();
127         }
128     }
129
130     @Override
131     public int compareTo(WorldSaveRow other) {
132         return this.worldSummary.compareTo(other.worldSummary);
133     }
134 }