forked from GregTechCEu/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProspectingTexture.java
More file actions
185 lines (165 loc) · 7.49 KB
/
Copy pathProspectingTexture.java
File metadata and controls
185 lines (165 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package gregtech.common.gui.widget.prospector;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.stack.MaterialStack;
import gregtech.client.utils.RenderUtil;
import gregtech.core.network.packets.PacketProspecting;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IResourceManager;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Map;
public class ProspectingTexture extends AbstractTexture {
public static final String SELECTED_ALL = "[all]";
private String selected = SELECTED_ALL;
private boolean darkMode;
private int imageWidth = -1;
private int imageHeight = -1;
public final Map<Byte, String>[][] map;
public static Map<Byte, String> emptyTag = new Byte2ObjectOpenHashMap<>();
private int playerXGui;
private int playerYGui;
private final ProspectorMode mode;
private final int radius;
public ProspectingTexture(ProspectorMode mode, int radius, boolean darkMode) {
this.darkMode = darkMode;
this.radius = radius;
this.mode = mode;
if (this.mode == ProspectorMode.FLUID) {
// noinspection unchecked
map = new Byte2ObjectOpenHashMap[(radius * 2 - 1)][(radius * 2 - 1)];
} else {
// noinspection unchecked
map = new Byte2ObjectOpenHashMap[(radius * 2 - 1) * 16][(radius * 2 - 1) * 16];
}
}
public void updateTexture(PacketProspecting packet) {
int playerChunkX = packet.playerChunkX;
int playerChunkZ = packet.playerChunkZ;
playerXGui = packet.posX - (playerChunkX - this.radius + 1) * 16 + (packet.posX > 0 ? 1 : 0);
playerYGui = packet.posZ - (playerChunkZ - this.radius + 1) * 16 + (packet.posX > 0 ? 1 : 0);
int ox;
if ((packet.chunkX > 0 && playerChunkX > 0) || (packet.chunkX < 0 && playerChunkX < 0)) {
ox = Math.abs(Math.abs(packet.chunkX) - Math.abs(playerChunkX));
} else {
ox = Math.abs(playerChunkX) + Math.abs(packet.chunkX);
}
if (playerChunkX > packet.chunkX) {
ox = -ox;
}
int oy;
if ((packet.chunkZ > 0 && playerChunkZ > 0) || (packet.chunkZ < 0 && playerChunkZ < 0)) {
oy = Math.abs(Math.abs(packet.chunkZ) - Math.abs(playerChunkZ));
} else {
oy = Math.abs(playerChunkZ) + Math.abs(packet.chunkZ);
}
if (playerChunkZ > packet.chunkZ) {
oy = -oy;
}
int currentColumn = (this.radius - 1) + ox;
int currentRow = (this.radius - 1) + oy;
if (currentRow < 0) {
return;
}
if (this.mode == ProspectorMode.FLUID) {
map[currentColumn][currentRow] = packet.map[0][0] == null ?
emptyTag : packet.map[0][0];
} else {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
map[x + currentColumn * 16][z + currentRow * 16] = packet.map[x][z] == null ?
emptyTag : packet.map[x][z];
}
}
}
loadTexture(null);
}
private BufferedImage getImage() {
int wh = (this.radius * 2 - 1) * 16;
BufferedImage image = new BufferedImage(wh, wh, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
for (int i = 0; i < wh; i++) {
for (int j = 0; j < wh; j++) {
Map<Byte, String> data = this.map[this.mode == ProspectorMode.ORE ? i : i / 16][this.mode ==
ProspectorMode.ORE ? j : j / 16];
// draw bg
image.setRGB(i, j, ((data == null) ^ darkMode) ? Color.darkGray.getRGB() : Color.WHITE.getRGB());
// draw ore
if (this.mode == ProspectorMode.ORE && data != null) {
for (String orePrefix : data.values()) {
if (!selected.equals(SELECTED_ALL) && !selected.equals(orePrefix)) continue;
MaterialStack mterialStack = OreDictUnifier.getMaterial(OreDictUnifier.get(orePrefix));
image.setRGB(i, j, mterialStack == null ? orePrefix.hashCode() :
mterialStack.material.getMaterialRGB() | 0XFF000000);
break;
}
}
// draw grid
if ((i) % 16 == 0 || (j) % 16 == 0) {
raster.setSample(i, j, 0, raster.getSample(i, j, 0) / 2);
raster.setSample(i, j, 1, raster.getSample(i, j, 1) / 2);
raster.setSample(i, j, 2, raster.getSample(i, j, 2) / 2);
}
}
}
return image;
}
@Override
public void loadTexture(@Nullable IResourceManager resourceManager) {
this.deleteGlTexture();
int tId = getGlTextureId();
if (tId < 0) return;
TextureUtil.uploadTextureImageAllocate(this.getGlTextureId(), getImage(), false, false);
imageWidth = (radius * 2 - 1) * 16;
imageHeight = (radius * 2 - 1) * 16;
}
public void loadTexture(@Nullable IResourceManager resourceManager, String selected) {
this.selected = selected;
loadTexture(resourceManager);
}
public void loadTexture(@Nullable IResourceManager resourceManager, boolean darkMode) {
this.darkMode = darkMode;
loadTexture(resourceManager);
}
public String getSelected() {
return selected;
}
public void draw(int x, int y) {
if (this.glTextureId < 0) return;
GlStateManager.bindTexture(this.getGlTextureId());
Gui.drawModalRectWithCustomSizedTexture(x, y, 0, 0, imageWidth, imageHeight, imageWidth, imageHeight);
if (this.mode == ProspectorMode.FLUID) { // draw fluids in grid
for (int cx = 0; cx < this.radius * 2 - 1; cx++) {
for (int cz = 0; cz < this.radius * 2 - 1; cz++) {
if (this.map[cx][cz] != null && !this.map[cx][cz].isEmpty()) {
Fluid fluid = FluidRegistry.getFluid(this.map[cx][cz].get((byte) 1));
if (selected.equals(SELECTED_ALL) || selected.equals(fluid.getName())) {
RenderUtil.drawFluidForGui(new FluidStack(fluid, 1), 1, x + cx * 16 + 1, y + cz * 16 + 1,
16, 16);
}
}
}
}
}
// draw red vertical line
if (playerXGui % 16 > 7 || playerXGui % 16 == 0) {
Gui.drawRect(x + playerXGui - 1, y, x + playerXGui, y + imageHeight, Color.RED.getRGB());
} else {
Gui.drawRect(x + playerXGui, y, x + playerXGui + 1, y + imageHeight, Color.RED.getRGB());
}
// draw red horizontal line
if (playerYGui % 16 > 7 || playerYGui % 16 == 0) {
Gui.drawRect(x, y + playerYGui - 1, x + imageWidth, y + playerYGui, Color.RED.getRGB());
} else {
Gui.drawRect(x, y + playerYGui, x + imageWidth, y + playerYGui + 1, Color.RED.getRGB());
}
}
}