Skip to content

Commit b7503ce

Browse files
committed
Move all the data to an image.json file, to allow changing what is displayed on screen, and where.
1 parent 261e5e6 commit b7503ce

10 files changed

Lines changed: 406 additions & 93 deletions

src/main/java/alexiil/mods/load/BetterLoadingScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import com.google.common.eventbus.EventBus;
2424

25-
@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", useMetadata = true, dependencies = "required-after:alexiillib",
25+
@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", dependencies = "required-after:alexiillib",
2626
acceptableRemoteVersions = "*")
2727
public class BetterLoadingScreen extends AlexIILMod {
2828
@Instance(Lib.Mod.ID)

src/main/java/alexiil/mods/load/MinecraftDisplayer.java

Lines changed: 173 additions & 82 deletions
Large diffs are not rendered by default.

src/main/java/alexiil/mods/load/ProgressDisplayer.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import java.io.File;
55

66
import net.minecraftforge.common.config.Configuration;
7+
import net.minecraftforge.fml.client.FMLFileResourcePack;
8+
import net.minecraftforge.fml.common.DummyModContainer;
9+
import net.minecraftforge.fml.common.ModContainer;
10+
import net.minecraftforge.fml.common.ModMetadata;
711

812
import org.apache.logging.log4j.LogManager;
913
import org.apache.logging.log4j.Logger;
@@ -66,6 +70,8 @@ public void close() {}
6670
private static int clientState = -1;
6771
public static Configuration cfg;
6872
public static boolean playSound;
73+
public static File coreModLocation;
74+
public static ModContainer modContainer;
6975

7076
public static boolean isClient() {
7177
if (clientState != -1)
@@ -81,8 +87,41 @@ public static boolean isClient() {
8187
return true;
8288
}
8389

84-
public static void start() {
85-
Configuration cfg = new Configuration(new File("./config/betterloadingscreen.cfg"));
90+
public static void start(File coremodLocation) {
91+
coreModLocation = coremodLocation;
92+
if (coreModLocation == null)
93+
coreModLocation = new File("./../bin/");
94+
// Assume this is a dev environment, and that the build dir is in bin, and the test dir has the same parent as
95+
// the bin dir...
96+
ModMetadata md = new ModMetadata();
97+
md.name = Lib.Mod.NAME;
98+
md.modId = Lib.Mod.ID;
99+
modContainer = new DummyModContainer(md) {
100+
@Override
101+
public Class<?> getCustomResourcePackClass() {
102+
return FMLFileResourcePack.class;
103+
}
104+
105+
@Override
106+
public File getSource() {
107+
return coreModLocation;
108+
}
109+
110+
@Override
111+
public String getModId() {
112+
return Lib.Mod.ID;
113+
}
114+
};
115+
116+
File fileOld = new File("./config/betterloadingscreen.cfg");
117+
File fileNew = new File("./config/BetterLoadingScreen/config.cfg");
118+
119+
Configuration cfg;
120+
if (fileOld.exists())
121+
cfg = new Configuration(fileOld);
122+
else
123+
cfg = new Configuration(fileNew);
124+
86125
boolean useMinecraft = isClient();
87126
if (useMinecraft) {
88127
String comment =

src/main/java/alexiil/mods/load/coremod/BetterLoadingScreenTransformer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ private byte[] transformMinecraft(byte[] before, boolean dev) {
7979
else if (method.owner.startsWith("com/mumfrey")) {
8080
System.out.println("Started with \"com/mumfrey\", was actually \"" + method.owner + "\"");
8181
}
82-
else
83-
System.out.println("Started with \"" + method.owner + "\"");
8482
}
8583

8684
// LiteLoader removing end

src/main/java/alexiil/mods/load/coremod/LoadingScreenLoadPlugin.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
@IFMLLoadingPlugin.TransformerExclusions({ "alexiil.mods.load.coremod" })
1212
@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE - 80)
1313
public class LoadingScreenLoadPlugin implements IFMLLoadingPlugin {
14-
// The only reason this coremod exists is this static method: its the first time our code is called
15-
static {
16-
ProgressDisplayer.start();
17-
}
18-
1914
@Override
2015
public String[] getASMTransformerClass() {
2116
return new String[] { "alexiil.mods.load.coremod.BetterLoadingScreenTransformer" };
@@ -33,7 +28,9 @@ public String getSetupClass() {
3328

3429
@Override
3530
public void injectData(Map<String, Object> data) {
36-
Translation.addTranslations((File) data.get("coremodLocation"));
31+
File coremodLocation = (File) data.get("coremodLocation");
32+
Translation.addTranslations(coremodLocation);
33+
ProgressDisplayer.start(coremodLocation);
3734
}
3835

3936
@Override
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package alexiil.mods.load.json;
2+
3+
public class Area {
4+
public final int x;
5+
public final int y;
6+
public final int width;
7+
public final int height;
8+
9+
public Area(int x, int y, int width, int height) {
10+
this.x = x;
11+
this.y = y;
12+
this.width = width;
13+
this.height = height;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "ImageTexture [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package alexiil.mods.load.json;
2+
3+
public enum EPosition {
4+
TOP_LEFT(-1, -1), TOP_CENTER(0, -1), TOP_RIGHT(1, -1), CENTER_LEFT(-1, 0), CENTER(0, 0), CENTER_RIGHT(1, 0), BOTTOM_LEFT(-1, 1), BOTTOM_CENTER(0,
5+
1), BOTTOM_RIGHT(1, 1);
6+
7+
private final int x;
8+
private final int y;
9+
10+
private EPosition(int x, int y) {
11+
this.x = x;
12+
this.y = y;
13+
}
14+
15+
private int transform(int switcher, int coord, int screenThing) {
16+
switch (switcher) {
17+
case -1:
18+
return coord;
19+
case 0:
20+
return screenThing / 2 - coord;
21+
case 1:
22+
return screenThing - coord;
23+
}
24+
throw new Error("switcher (" + switcher + ") != -1, 0 or 1 (" + this.toString() + ")");
25+
}
26+
27+
public int transformX(int x, int screenWidth) {
28+
return transform(this.x, x, screenWidth);
29+
}
30+
31+
public int transformY(int y, int screenHeight) {
32+
return transform(this.y, y, screenHeight);
33+
}
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package alexiil.mods.load.json;
2+
3+
public enum EType {
4+
STATIC, STATIC_TEXT, DYNAMIC_TEXT_STATUS, DYNAMIC_TEXT_PERCENTAGE, DYNAMIC_PERCENTAGE;
5+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package alexiil.mods.load.json;
2+
3+
public class ImageRender {
4+
public final String resourceLocation;
5+
public final EPosition positionType;
6+
public final EType type;
7+
public final Area texture;
8+
public final Area position;
9+
public final String colour;
10+
public final String text;
11+
12+
public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position, String colour, String text) {
13+
this.resourceLocation = resourceLocation;
14+
this.positionType = positionType;
15+
this.type = type;
16+
this.texture = texture;
17+
this.position = position;
18+
this.colour = colour;
19+
this.text = text;
20+
}
21+
22+
public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position) {
23+
this(resourceLocation, positionType, type, texture, position, null, null);
24+
}
25+
26+
public int transformX(int screenWidth) {
27+
return positionType.transformX(position.x, screenWidth - position.width);
28+
}
29+
30+
public int transformY(int screenWidth) {
31+
return positionType.transformY(position.y, screenWidth - position.height);
32+
}
33+
34+
public int getColour() {
35+
if (colour == null)
36+
return 0xFFFFFF;
37+
else {
38+
try {
39+
return Integer.parseInt(colour, 16);
40+
}
41+
catch (NumberFormatException nfe) {
42+
return 0xFFFFFF;
43+
}
44+
}
45+
}
46+
47+
private float getColourPart(int bitStart) {
48+
return ((getColour() >> bitStart) & 0xFF) / 256F;
49+
}
50+
51+
public float getRed() {
52+
return getColourPart(16);
53+
}
54+
55+
public float getGreen() {
56+
return getColourPart(8);
57+
}
58+
59+
public float getBlue() {
60+
return getColourPart(0);
61+
}
62+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package alexiil.mods.load.json;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.File;
6+
import java.io.FileNotFoundException;
7+
import java.io.FileReader;
8+
import java.io.FileWriter;
9+
import java.io.IOException;
10+
11+
import com.google.gson.Gson;
12+
import com.google.gson.GsonBuilder;
13+
14+
public class JsonConfig<T> {
15+
private final Class<T> clazz;
16+
private final File file;
17+
private final T defaultConfig;
18+
19+
public JsonConfig(File file, Class<T> clazz, T defaultConfig) {
20+
this.file = file;
21+
this.clazz = clazz;
22+
this.defaultConfig = defaultConfig;
23+
}
24+
25+
/** Overwrite any existing config: Treat it as a default config */
26+
public void createNew() {
27+
BufferedWriter writer = null;
28+
try {
29+
writer = new BufferedWriter(new FileWriter(file));
30+
writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(defaultConfig));
31+
writer.close();
32+
}
33+
catch (IOException e1) {
34+
e1.printStackTrace();
35+
}
36+
finally {
37+
if (writer != null)
38+
try {
39+
writer.close();
40+
}
41+
catch (IOException e1) {
42+
e1.printStackTrace();
43+
}
44+
}
45+
}
46+
47+
public T load() {
48+
BufferedReader reader = null;
49+
try {
50+
reader = new BufferedReader(new FileReader(file));
51+
return new Gson().fromJson(reader, clazz);
52+
}
53+
catch (FileNotFoundException e) {
54+
createNew();
55+
}
56+
finally {
57+
if (reader != null)
58+
try {
59+
reader.close();
60+
}
61+
catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
return defaultConfig;
66+
}
67+
}

0 commit comments

Comments
 (0)