Skip to content

Commit 7c5e2bd

Browse files
author
FalsePattern
authored
Merge pull request #27 from basdxz/util-extension
Util extension
2 parents 173e60a + 502577b commit 7c5e2bd

File tree

7 files changed

+180
-60
lines changed

7 files changed

+180
-60
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.falsepattern.lib.internal.render;
2+
3+
import com.falsepattern.lib.util.MathUtil;
4+
import lombok.AllArgsConstructor;
5+
import net.minecraft.util.IIcon;
6+
7+
@AllArgsConstructor
8+
public final class ClampedIcon implements IIcon {
9+
private final IIcon delegate;
10+
11+
@Override
12+
public int getIconWidth() {
13+
return delegate.getIconWidth();
14+
}
15+
16+
@Override
17+
public int getIconHeight() {
18+
return delegate.getIconHeight();
19+
}
20+
21+
@Override
22+
public float getMinU() {
23+
return delegate.getMinU();
24+
}
25+
26+
@Override
27+
public float getMaxU() {
28+
return delegate.getMaxU();
29+
}
30+
31+
@Override
32+
public float getMinV() {
33+
return delegate.getMinV();
34+
}
35+
36+
@Override
37+
public float getMaxV() {
38+
return delegate.getMaxV();
39+
}
40+
41+
@Override
42+
public float getInterpolatedU(double textureU) {
43+
return delegate.getInterpolatedU(clampTextureCoordinate(textureU));
44+
}
45+
46+
@Override
47+
public float getInterpolatedV(double textureV) {
48+
return delegate.getInterpolatedV(clampTextureCoordinate(textureV));
49+
}
50+
51+
@Override
52+
public String getIconName() {
53+
return delegate.getIconName();
54+
}
55+
56+
private double clampTextureCoordinate(double textureCoordinate) {
57+
return MathUtil.clamp(textureCoordinate, 0D, 16D);
58+
}
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.falsepattern.lib.internal.render;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import net.minecraft.util.IIcon;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public final class FullTextureIcon implements IIcon {
10+
private final String iconName;
11+
12+
private final int iconWidth;
13+
private final int iconHeight;
14+
15+
@Override
16+
public float getMinU() {
17+
return 0F;
18+
}
19+
20+
@Override
21+
public float getMinV() {
22+
return 0F;
23+
}
24+
25+
@Override
26+
public float getMaxU() {
27+
return 1F;
28+
}
29+
30+
@Override
31+
public float getMaxV() {
32+
return 1F;
33+
}
34+
35+
@Override
36+
public float getInterpolatedU(double textureU) {
37+
return (float) textureU / 16F;
38+
}
39+
40+
@Override
41+
public float getInterpolatedV(double textureV) {
42+
return (float) textureV / 16F;
43+
}
44+
}

src/main/java/com/falsepattern/lib/util/FileUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
import com.falsepattern.lib.StableAPI;
2424

25+
import lombok.experimental.UtilityClass;
2526
import net.minecraft.launchwrapper.Launch;
2627

2728
import java.io.File;
2829

30+
@UtilityClass
2931
@StableAPI(since = "0.8.2")
30-
public class FileUtil {
32+
public final class FileUtil {
3133
@StableAPI.Expose
3234
public static File getMinecraftHome() {
3335
return Launch.minecraftHome == null ? new File(".") : Launch.minecraftHome;

src/main/java/com/falsepattern/lib/util/LangUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.falsepattern.lib.StableAPI;
2424
import lombok.NoArgsConstructor;
2525
import lombok.NonNull;
26+
import lombok.experimental.UtilityClass;
2627
import lombok.val;
2728

2829
import cpw.mods.fml.common.registry.LanguageRegistry;
@@ -32,7 +33,7 @@
3233

3334
import static lombok.AccessLevel.PRIVATE;
3435

35-
@NoArgsConstructor(access = PRIVATE)
36+
@UtilityClass
3637
@StableAPI(since = "0.8.0")
3738
public final class LangUtil {
3839
@StableAPI.Expose

src/main/java/com/falsepattern/lib/util/MathUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
import com.falsepattern.lib.StableAPI;
2424
import com.falsepattern.lib.compat.Vec3i;
25+
import lombok.experimental.UtilityClass;
2526

2627
import java.util.Random;
2728
import java.util.UUID;
2829

30+
@UtilityClass
2931
@StableAPI(since = "0.10.0")
30-
public class MathUtil {
32+
public final class MathUtil {
3133
@StableAPI.Expose
3234
public static final float SQRT_2 = sqrt(2.0F);
3335
/**
@@ -621,4 +623,4 @@ public static int hash(int x) {
621623
x = x ^ x >>> 16;
622624
return x;
623625
}
624-
}
626+
}

src/main/java/com/falsepattern/lib/util/RenderUtil.java

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,86 +21,96 @@
2121
package com.falsepattern.lib.util;
2222

2323
import com.falsepattern.lib.StableAPI;
24-
import lombok.NoArgsConstructor;
24+
import com.falsepattern.lib.internal.render.ClampedIcon;
25+
import com.falsepattern.lib.internal.render.FullTextureIcon;
2526
import lombok.SneakyThrows;
27+
import lombok.experimental.UtilityClass;
2628
import lombok.val;
2729

2830
import net.minecraft.client.Minecraft;
31+
import net.minecraft.client.renderer.RenderBlocks;
2932
import net.minecraft.util.IIcon;
3033
import net.minecraft.util.Timer;
3134
import cpw.mods.fml.relauncher.ReflectionHelper;
3235
import cpw.mods.fml.relauncher.SideOnly;
36+
import org.lwjgl.opengl.GL11;
37+
38+
import java.util.Collections;
3339

3440
import static cpw.mods.fml.relauncher.Side.CLIENT;
35-
import static lombok.AccessLevel.PRIVATE;
3641
import static net.minecraft.client.Minecraft.getMinecraft;
3742

3843
@SideOnly(CLIENT)
39-
@NoArgsConstructor(access = PRIVATE)
44+
@UtilityClass
4045
@StableAPI(since = "0.8.0")
4146
public final class RenderUtil {
42-
private final static Timer MINECRAFT_TIMER = getMinecraftTimer();
47+
private static final Timer MINECRAFT_TIMER = getMinecraftTimer();
4348

44-
@StableAPI.Expose
45-
@SneakyThrows
46-
private static Timer getMinecraftTimer() {
47-
val timerField = ReflectionHelper.findField(Minecraft.class, "timer", "field_71428_T");
48-
timerField.setAccessible(true);
49-
return (Timer) timerField.get(getMinecraft());
49+
/**
50+
* Sets the OpenGL translation, relative to the player's position.
51+
* <p>
52+
* This is useful for rendering things that are not part of the world mesh, but should be rendered as if they were.
53+
* <p>
54+
* It's good practice to make this call inside a {@link GL11#glPushMatrix() push}/{@link GL11#glPopMatrix() pop} matrix block.
55+
*/
56+
@StableAPI.Expose(since = "0.12.0")
57+
public static void setGLTranslationRelativeToPlayer() {
58+
val player = getMinecraft().thePlayer;
59+
val partialTick = partialTick();
60+
61+
val offsetX = (float) (player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTick);
62+
val offsetY = (float) (player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTick);
63+
val offsetZ = (float) (player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTick);
64+
65+
GL11.glTranslatef(-offsetX, -offsetY, -offsetZ);
5066
}
5167

68+
/**
69+
* Provides a texture icon with the given name and dimensions.
70+
* <p>
71+
* This is useful for rendering textures that are not part of the Minecraft texture atlas.
72+
*
73+
* @param iconName The icon name
74+
* @param width The icon width in pixels
75+
* @param height The icon height in pixels
76+
* @return The full resolution texture icon.
77+
*/
5278
@StableAPI.Expose(since = "0.10.0")
5379
public static IIcon getFullTextureIcon(String iconName, int width, int height) {
54-
return new IIcon() {
55-
@Override
56-
public int getIconWidth() {
57-
return width;
58-
}
59-
60-
@Override
61-
public int getIconHeight() {
62-
return height;
63-
}
64-
65-
@Override
66-
public float getMinU() {
67-
return 0;
68-
}
69-
70-
@Override
71-
public float getMaxU() {
72-
return 1;
73-
}
74-
75-
@Override
76-
public float getInterpolatedU(double u) {
77-
return (float) (u / 16D);
78-
}
79-
80-
@Override
81-
public float getMinV() {
82-
return 0;
83-
}
84-
85-
@Override
86-
public float getMaxV() {
87-
return 1;
88-
}
89-
90-
@Override
91-
public float getInterpolatedV(double v) {
92-
return (float) (v / 16D);
93-
}
80+
return new FullTextureIcon(iconName, width, height);
81+
}
9482

95-
@Override
96-
public String getIconName() {
97-
return iconName;
98-
}
99-
};
83+
/**
84+
* Wraps the given icon as a clamped icon.
85+
* <p>
86+
* A clamped icon will clamp the coordinates given to {@link IIcon#getInterpolatedU(double)} and {@link IIcon#getInterpolatedV(double)} to the range of 0 to 16.
87+
* <p>
88+
* This is helpful when using {@link RenderBlocks} but having different bounds.
89+
*
90+
* @param icon The icon to clamp
91+
*/
92+
@StableAPI.Expose(since = "0.12.0")
93+
public static IIcon wrapAsClampedIcon(IIcon icon) {
94+
return new ClampedIcon(icon);
10095
}
10196

97+
/**
98+
* Provides the partial tick between the last and next client tick, in the range of 0 to 1.
99+
* <p>
100+
* Sometimes referred to as 'subTick', it is used mostly for interpolation in rendering.
101+
*
102+
* @return The current partial tick
103+
*/
102104
@StableAPI.Expose
103105
public static float partialTick() {
104106
return MINECRAFT_TIMER.renderPartialTicks;
105107
}
108+
109+
@StableAPI.Expose
110+
@SneakyThrows
111+
private static Timer getMinecraftTimer() {
112+
val timerField = ReflectionHelper.findField(Minecraft.class, "timer", "field_71428_T");
113+
timerField.setAccessible(true);
114+
return (Timer) timerField.get(getMinecraft());
115+
}
106116
}

src/main/java/com/falsepattern/lib/util/ResourceUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.falsepattern.lib.util;
2222

2323
import com.falsepattern.lib.StableAPI;
24+
import lombok.experimental.UtilityClass;
2425
import lombok.val;
2526

2627
import java.io.ByteArrayOutputStream;
@@ -34,8 +35,9 @@
3435
/**
3536
* A utility class for reading resources in many ways.
3637
*/
38+
@UtilityClass
3739
@StableAPI(since = "0.6.0")
38-
public class ResourceUtil {
40+
public final class ResourceUtil {
3941

4042
/**
4143
* Reads a resource as a string, using the {@link StandardCharsets#UTF_8} charset, from a specific jar file. See

0 commit comments

Comments
 (0)