Skip to content

Commit

Permalink
Move all the data to an image.json file, to allow changing what is di…
Browse files Browse the repository at this point in the history
…splayed on screen, and where.
  • Loading branch information
AlexIIL committed Apr 16, 2015
1 parent 261e5e6 commit b7503ce
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/main/java/alexiil/mods/load/BetterLoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.google.common.eventbus.EventBus;

@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", useMetadata = true, dependencies = "required-after:alexiillib",
@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", dependencies = "required-after:alexiillib",
acceptableRemoteVersions = "*")
public class BetterLoadingScreen extends AlexIILMod {
@Instance(Lib.Mod.ID)
Expand Down
255 changes: 173 additions & 82 deletions src/main/java/alexiil/mods/load/MinecraftDisplayer.java

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions src/main/java/alexiil/mods/load/ProgressDisplayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.io.File;

import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.FMLFileResourcePack;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.ModMetadata;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -66,6 +70,8 @@ public void close() {}
private static int clientState = -1;
public static Configuration cfg;
public static boolean playSound;
public static File coreModLocation;
public static ModContainer modContainer;

public static boolean isClient() {
if (clientState != -1)
Expand All @@ -81,8 +87,41 @@ public static boolean isClient() {
return true;
}

public static void start() {
Configuration cfg = new Configuration(new File("./config/betterloadingscreen.cfg"));
public static void start(File coremodLocation) {
coreModLocation = coremodLocation;
if (coreModLocation == null)
coreModLocation = new File("./../bin/");
// Assume this is a dev environment, and that the build dir is in bin, and the test dir has the same parent as
// the bin dir...
ModMetadata md = new ModMetadata();
md.name = Lib.Mod.NAME;
md.modId = Lib.Mod.ID;
modContainer = new DummyModContainer(md) {
@Override
public Class<?> getCustomResourcePackClass() {
return FMLFileResourcePack.class;
}

@Override
public File getSource() {
return coreModLocation;
}

@Override
public String getModId() {
return Lib.Mod.ID;
}
};

File fileOld = new File("./config/betterloadingscreen.cfg");
File fileNew = new File("./config/BetterLoadingScreen/config.cfg");

Configuration cfg;
if (fileOld.exists())
cfg = new Configuration(fileOld);
else
cfg = new Configuration(fileNew);

boolean useMinecraft = isClient();
if (useMinecraft) {
String comment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ private byte[] transformMinecraft(byte[] before, boolean dev) {
else if (method.owner.startsWith("com/mumfrey")) {
System.out.println("Started with \"com/mumfrey\", was actually \"" + method.owner + "\"");
}
else
System.out.println("Started with \"" + method.owner + "\"");
}

// LiteLoader removing end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
@IFMLLoadingPlugin.TransformerExclusions({ "alexiil.mods.load.coremod" })
@IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE - 80)
public class LoadingScreenLoadPlugin implements IFMLLoadingPlugin {
// The only reason this coremod exists is this static method: its the first time our code is called
static {
ProgressDisplayer.start();
}

@Override
public String[] getASMTransformerClass() {
return new String[] { "alexiil.mods.load.coremod.BetterLoadingScreenTransformer" };
Expand All @@ -33,7 +28,9 @@ public String getSetupClass() {

@Override
public void injectData(Map<String, Object> data) {
Translation.addTranslations((File) data.get("coremodLocation"));
File coremodLocation = (File) data.get("coremodLocation");
Translation.addTranslations(coremodLocation);
ProgressDisplayer.start(coremodLocation);
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/alexiil/mods/load/json/Area.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package alexiil.mods.load.json;

public class Area {
public final int x;
public final int y;
public final int width;
public final int height;

public Area(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public String toString() {
return "ImageTexture [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
}
}
34 changes: 34 additions & 0 deletions src/main/java/alexiil/mods/load/json/EPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package alexiil.mods.load.json;

public enum EPosition {
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,
1), BOTTOM_RIGHT(1, 1);

private final int x;
private final int y;

private EPosition(int x, int y) {
this.x = x;
this.y = y;
}

private int transform(int switcher, int coord, int screenThing) {
switch (switcher) {
case -1:
return coord;
case 0:
return screenThing / 2 - coord;
case 1:
return screenThing - coord;
}
throw new Error("switcher (" + switcher + ") != -1, 0 or 1 (" + this.toString() + ")");
}

public int transformX(int x, int screenWidth) {
return transform(this.x, x, screenWidth);
}

public int transformY(int y, int screenHeight) {
return transform(this.y, y, screenHeight);
}
}
5 changes: 5 additions & 0 deletions src/main/java/alexiil/mods/load/json/EType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package alexiil.mods.load.json;

public enum EType {
STATIC, STATIC_TEXT, DYNAMIC_TEXT_STATUS, DYNAMIC_TEXT_PERCENTAGE, DYNAMIC_PERCENTAGE;
}
62 changes: 62 additions & 0 deletions src/main/java/alexiil/mods/load/json/ImageRender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package alexiil.mods.load.json;

public class ImageRender {
public final String resourceLocation;
public final EPosition positionType;
public final EType type;
public final Area texture;
public final Area position;
public final String colour;
public final String text;

public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position, String colour, String text) {
this.resourceLocation = resourceLocation;
this.positionType = positionType;
this.type = type;
this.texture = texture;
this.position = position;
this.colour = colour;
this.text = text;
}

public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position) {
this(resourceLocation, positionType, type, texture, position, null, null);
}

public int transformX(int screenWidth) {
return positionType.transformX(position.x, screenWidth - position.width);
}

public int transformY(int screenWidth) {
return positionType.transformY(position.y, screenWidth - position.height);
}

public int getColour() {
if (colour == null)
return 0xFFFFFF;
else {
try {
return Integer.parseInt(colour, 16);
}
catch (NumberFormatException nfe) {
return 0xFFFFFF;
}
}
}

private float getColourPart(int bitStart) {
return ((getColour() >> bitStart) & 0xFF) / 256F;
}

public float getRed() {
return getColourPart(16);
}

public float getGreen() {
return getColourPart(8);
}

public float getBlue() {
return getColourPart(0);
}
}
67 changes: 67 additions & 0 deletions src/main/java/alexiil/mods/load/json/JsonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package alexiil.mods.load.json;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonConfig<T> {
private final Class<T> clazz;
private final File file;
private final T defaultConfig;

public JsonConfig(File file, Class<T> clazz, T defaultConfig) {
this.file = file;
this.clazz = clazz;
this.defaultConfig = defaultConfig;
}

/** Overwrite any existing config: Treat it as a default config */
public void createNew() {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(defaultConfig));
writer.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
finally {
if (writer != null)
try {
writer.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}

public T load() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
return new Gson().fromJson(reader, clazz);
}
catch (FileNotFoundException e) {
createNew();
}
finally {
if (reader != null)
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return defaultConfig;
}
}

0 comments on commit b7503ce

Please sign in to comment.