aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/makamys/lodmod/util')
-rw-r--r--src/main/java/makamys/lodmod/util/SpriteUtil.java55
-rw-r--r--src/main/java/makamys/lodmod/util/Util.java26
2 files changed, 81 insertions, 0 deletions
diff --git a/src/main/java/makamys/lodmod/util/SpriteUtil.java b/src/main/java/makamys/lodmod/util/SpriteUtil.java
new file mode 100644
index 0000000..34bc544
--- /dev/null
+++ b/src/main/java/makamys/lodmod/util/SpriteUtil.java
@@ -0,0 +1,55 @@
+package makamys.lodmod.util;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.renderer.texture.TextureAtlasSprite;
+import net.minecraft.client.renderer.texture.TextureMap;
+import net.minecraft.world.ChunkCoordIntPair;
+
+public class SpriteUtil {
+
+ private static int[] spriteIndexMap;
+ public static List<TextureAtlasSprite> sprites;
+
+ private static Map<Long, Integer> uv2spriteIndex = new HashMap<>();
+
+ private static int findSpriteIndexForUV(float u, float v) {
+ Map<String, TextureAtlasSprite> uploadedSprites = ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites;
+
+ int spriteIndex = 0;
+ for(TextureAtlasSprite tas : uploadedSprites.values()) {
+ if(tas.getMinU() <= u && u <= tas.getMaxU() && tas.getMinV() <= v && v <= tas.getMaxV()) {
+ break;
+ }
+ spriteIndex++;
+ }
+ return spriteIndex;
+ }
+
+ public static int getSpriteIndexForUV(float u, float v){
+ long key = ChunkCoordIntPair.chunkXZ2Int((int)(u * Integer.MAX_VALUE), (int)(v * Integer.MAX_VALUE));
+ int index = uv2spriteIndex.getOrDefault(key, -1);
+ if(index == -1) {
+ index = findSpriteIndexForUV(u, v);
+ uv2spriteIndex.put(key, index);
+ }
+ return index;
+ }
+
+ public static TextureAtlasSprite getSprite(int i){
+ if(i >= 0 && i < sprites.size()) {
+ return sprites.get(i);
+ } else {
+ return null;
+ }
+ }
+
+ public static void init() {
+ Map<String, TextureAtlasSprite> uploadedSprites = ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites;
+ sprites = uploadedSprites.values().stream().collect(Collectors.toList());
+ }
+}
diff --git a/src/main/java/makamys/lodmod/util/Util.java b/src/main/java/makamys/lodmod/util/Util.java
index 92cc5cd..6964e9e 100644
--- a/src/main/java/makamys/lodmod/util/Util.java
+++ b/src/main/java/makamys/lodmod/util/Util.java
@@ -4,7 +4,10 @@ import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
import java.nio.file.FileSystems;
+import java.nio.file.Files;
import java.nio.file.Path;
public class Util {
@@ -27,4 +30,27 @@ public class Util {
return null;
}
}
+
+ public static String readFile(String path){
+ try {
+ return new String(Files.readAllBytes(Util.getResourcePath(path)));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ public static byte[] byteBufferToArray(ByteBuffer buffer) {
+ byte[] dst = new byte[buffer.remaining()];
+ buffer.get(dst);
+ buffer.flip();
+ return dst;
+ }
+
+ public static int[] intBufferToArray(IntBuffer buffer) {
+ int[] dst = new int[buffer.remaining()];
+ buffer.get(dst);
+ buffer.flip();
+ return dst;
+ }
}