aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/util
diff options
context:
space:
mode:
authormakamys <makamys@outlook.com>2022-06-09 12:57:52 +0200
committermakamys <makamys@outlook.com>2022-06-09 12:57:52 +0200
commit5bea63de0d78487b91dd701a8f6931b9314f2be5 (patch)
tree9a23fc91c986d8763b31c48f670b812e9ce24c8a /src/main/java/makamys/neodymium/util
parent5820fe56fb4022ddda044a8fe502b59e18147836 (diff)
downloadNeodymium-5bea63de0d78487b91dd701a8f6931b9314f2be5.tar.gz
Neodymium-5bea63de0d78487b91dd701a8f6931b9314f2be5.tar.bz2
Neodymium-5bea63de0d78487b91dd701a8f6931b9314f2be5.zip
Rebrand pt. 1: Rename mod to Neodymium
Diffstat (limited to 'src/main/java/makamys/neodymium/util')
-rw-r--r--src/main/java/makamys/neodymium/util/BufferWriter.java46
-rw-r--r--src/main/java/makamys/neodymium/util/GuiHelper.java61
-rw-r--r--src/main/java/makamys/neodymium/util/MCUtil.java20
-rw-r--r--src/main/java/makamys/neodymium/util/SpriteUtil.java55
-rw-r--r--src/main/java/makamys/neodymium/util/Util.java88
5 files changed, 270 insertions, 0 deletions
diff --git a/src/main/java/makamys/neodymium/util/BufferWriter.java b/src/main/java/makamys/neodymium/util/BufferWriter.java
new file mode 100644
index 0000000..901f5dc
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/BufferWriter.java
@@ -0,0 +1,46 @@
+package makamys.neodymium.util;
+
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.nio.ShortBuffer;
+
+public class BufferWriter {
+
+ private ByteBuffer buf;
+
+ private FloatBuffer floatBuffer;
+ private ShortBuffer shortBuffer;
+ private IntBuffer intBuffer;
+
+ public BufferWriter(ByteBuffer buf) {
+ this.buf = buf;
+ this.floatBuffer = buf.asFloatBuffer();
+ this.shortBuffer = buf.asShortBuffer();
+ this.intBuffer = buf.asIntBuffer();
+ }
+
+ private void incrementPosition(int add) {
+ buf.position(buf.position() + add);
+ floatBuffer.position(buf.position() / 4);
+ shortBuffer.position(buf.position() / 2);
+ intBuffer.position(buf.position() / 4);
+ }
+
+ public void writeFloat(float x) {
+ try {
+ floatBuffer.put(x);
+
+ incrementPosition(4);
+ } catch(Exception e){
+ e.printStackTrace();
+ }
+ }
+
+ public void writeInt(int x) {
+ intBuffer.put(x);
+
+ incrementPosition(4);
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/makamys/neodymium/util/GuiHelper.java b/src/main/java/makamys/neodymium/util/GuiHelper.java
new file mode 100644
index 0000000..1b56d63
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/GuiHelper.java
@@ -0,0 +1,61 @@
+package makamys.neodymium.util;
+
+import org.lwjgl.opengl.GL11;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.renderer.Tessellator;
+
+public class GuiHelper {
+
+ public static void begin() {
+ GL11.glDisable(GL11.GL_TEXTURE_2D);
+
+ Minecraft mc = Minecraft.getMinecraft();
+
+ //GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
+ GL11.glMatrixMode(GL11.GL_PROJECTION);
+ //GL11.glEnable(GL11.GL_COLOR_MATERIAL);
+ GL11.glLoadIdentity();
+ GL11.glOrtho(0.0D, (double)mc.displayWidth, (double)mc.displayHeight, 0.0D, 1000.0D, 3000.0D);
+ GL11.glMatrixMode(GL11.GL_MODELVIEW);
+ GL11.glLoadIdentity();
+ GL11.glTranslatef(0.0F, 0.0F, -2000.0F);
+ //GL11.glLineWidth(1.0F);
+ //GL11.glDisable(GL11.GL_TEXTURE_2D);
+ }
+
+ public static void drawRectangle(int x, int y, int w, int h, int color) {
+ Tessellator tessellator = Tessellator.instance;
+ tessellator.startDrawingQuads();
+ tessellator.setColorOpaque_I(color);
+ tessellator.addVertex(x, y, 0);
+ tessellator.addVertex(x, y+h, 0);
+ tessellator.addVertex(x+w, y+h, 0);
+ tessellator.addVertex(x+w, y, 0);
+
+ tessellator.draw();
+ }
+
+ public static void drawRectangle(int x, int y, int w, int h, int color, int opacity) {
+ GL11.glEnable(GL11.GL_BLEND);
+
+ Tessellator tessellator = Tessellator.instance;
+ tessellator.startDrawingQuads();
+ tessellator.setColorRGBA_I(color, opacity);
+ tessellator.addVertex(x, y, 0);
+ tessellator.addVertex(x, y+h, 0);
+ tessellator.addVertex(x+w, y+h, 0);
+ tessellator.addVertex(x+w, y, 0);
+
+ tessellator.draw();
+
+ GL11.glDisable(GL11.GL_BLEND);
+ }
+
+ public static void end() {
+ //GL11.glDisable(GL11.GL_BLEND);
+
+ //GL11.glEnable(GL11.GL_TEXTURE_2D);
+ }
+
+}
diff --git a/src/main/java/makamys/neodymium/util/MCUtil.java b/src/main/java/makamys/neodymium/util/MCUtil.java
new file mode 100644
index 0000000..7a2694d
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/MCUtil.java
@@ -0,0 +1,20 @@
+package makamys.neodymium.util;
+
+import net.minecraft.world.biome.BiomeGenBase;
+
+public class MCUtil {
+
+ public static float getBiomeTemperatureVanilla(BiomeGenBase biome, int p_150564_1_, int p_150564_2_, int p_150564_3_){
+ if (p_150564_2_ > 64)
+ {
+ float f = (float)BiomeGenBase.temperatureNoise
+ .func_151601_a((double)p_150564_1_ * 1.0D / 8.0D, (double)p_150564_3_ * 1.0D / 8.0D) * 4.0F;
+ return biome.temperature - (f + (float)p_150564_2_ - 64.0F) * 0.05F / 30.0F;
+ }
+ else
+ {
+ return biome.temperature;
+ }
+ }
+
+}
diff --git a/src/main/java/makamys/neodymium/util/SpriteUtil.java b/src/main/java/makamys/neodymium/util/SpriteUtil.java
new file mode 100644
index 0000000..4219802
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/SpriteUtil.java
@@ -0,0 +1,55 @@
+package makamys.neodymium.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/neodymium/util/Util.java b/src/main/java/makamys/neodymium/util/Util.java
new file mode 100644
index 0000000..2507a78
--- /dev/null
+++ b/src/main/java/makamys/neodymium/util/Util.java
@@ -0,0 +1,88 @@
+package makamys.neodymium.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import net.minecraft.launchwrapper.Launch;
+
+public class Util {
+
+ private static boolean allowResourceOverrides = Boolean.parseBoolean(System.getProperty("lodmod.allowResourceOverrides", "false"));
+
+ public static Path getResourcePath(String relPath) {
+ if(allowResourceOverrides) {
+ File overrideFile = new File(new File(Launch.minecraftHome, "lodmod/resources"), relPath);
+ if(overrideFile.exists()) {
+ return overrideFile.toPath();
+ }
+ }
+
+ try {
+ URL resourceURL = Util.class.getClassLoader().getResource(relPath);
+
+ switch(resourceURL.getProtocol()) {
+ case "jar":
+ String urlString = resourceURL.getPath();
+ int lastExclamation = urlString.lastIndexOf('!');
+ String newURLString = urlString.substring(0, lastExclamation);
+ return FileSystems.newFileSystem(new File(URI.create(newURLString)).toPath(), null).getPath(relPath);
+ case "file":
+ return new File(URI.create(resourceURL.toString())).toPath();
+ default:
+ return null;
+ }
+ } catch(IOException e) {
+ 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.limit()];
+ int pos = buffer.position();
+ buffer.position(0);
+ buffer.get(dst);
+ buffer.position(pos);
+ return dst;
+ }
+
+ public static int[] intBufferToArray(IntBuffer buffer) {
+ int[] dst = new int[buffer.limit()];
+ int pos = buffer.position();
+ buffer.position(0);
+ buffer.get(dst);
+ buffer.position(pos);
+ return dst;
+ }
+
+ public static float[] floatBufferToArray(FloatBuffer buffer) {
+ float[] dst = new float[buffer.limit()];
+ int pos = buffer.position();
+ buffer.position(0);
+ buffer.get(dst);
+ buffer.position(pos);
+ return dst;
+ }
+
+ public static double distSq(double x1, double y1, double z1, double x2, double y2, double z2) {
+ return Math.pow(x1 - x2, 2) +
+ Math.pow(y1 - y2, 2) +
+ Math.pow(z1 - z2, 2);
+ }
+}