diff --git a/mod_info.txt b/mod_info.txt new file mode 100644 index 0000000..95e6c07 --- /dev/null +++ b/mod_info.txt @@ -0,0 +1 @@ +

Create a trailing effect to make things appear smoother. Originally by 2Pi

\ No newline at end of file diff --git a/sk1er_mod.properties b/sk1er_mod.properties new file mode 100644 index 0000000..56e8972 --- /dev/null +++ b/sk1er_mod.properties @@ -0,0 +1,4 @@ +mod_id=motionblurmod +display_name=Motion Blur +not_complete=false +hide=false \ No newline at end of file diff --git a/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java b/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java index 5de3eef..764b948 100644 --- a/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java +++ b/src/main/java/club/sk1er/modcore/FMLLoadingPlugin.java @@ -1,84 +1,77 @@ package club.sk1er.modcore; +import java.util.Map; import net.minecraft.launchwrapper.Launch; import net.minecraftforge.common.ForgeVersion; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; -import java.util.Map; - @IFMLLoadingPlugin.MCVersion(ForgeVersion.mcVersion) public class FMLLoadingPlugin implements IFMLLoadingPlugin { - /** - * Return a list of classes that implements the IClassTransformer interface - * - * @return a list of classes that implements the IClassTransformer interface - */ - @Override - public String[] getASMTransformerClass() { - int initialize = ModCoreInstaller.initialize(Launch.minecraftHome, "1.8.9"); - - if (ModCoreInstaller.isErrored() || initialize != 0 && initialize != -1) { - // Technically wouldn't happen in simulated installed but is important for actual impl - System.out.println("Failed to load Sk1er Modcore - " + initialize + " - " + ModCoreInstaller.getError()); - } - - // If true the classes are loaded - if (ModCoreInstaller.isIsRunningModCore()) { - return new String[]{"club.sk1er.mods.core.forge.ClassTransformer"}; - } + /** + * Return a list of classes that implements the IClassTransformer interface + * + * @return a list of classes that implements the IClassTransformer interface + */ + @Override + public String[] getASMTransformerClass() { + int initialize = ModCoreInstaller.initialize(Launch.minecraftHome, "1.8.9"); - return new String[]{}; + if (ModCoreInstaller.isErrored() || initialize != 0 && initialize != -1) { + // Technically wouldn't happen in simulated installed but is important for actual impl + System.out.println( + "Failed to load Sk1er Modcore - " + initialize + " - " + ModCoreInstaller.getError()); } - /** - * Return a class name that implements "ModContainer" for injection into the mod list - * The "getName" function should return a name that other mods can, if need be, - * depend on. - * Trivially, this modcontainer will be loaded before all regular mod containers, - * which means it will be forced to be "immutable" - not susceptible to normal - * sorting behaviour. - * All other mod behaviours are available however- this container can receive and handle - * normal loading events - */ - @Override - public String getModContainerClass() { - return null; + // If true the classes are loaded + if (ModCoreInstaller.isIsRunningModCore()) { + return new String[] {"club.sk1er.mods.core.forge.ClassTransformer"}; } - /** - * Return the class name of an implementor of "IFMLCallHook", that will be run, in the - * main thread, to perform any additional setup this coremod may require. It will be - * run prior to Minecraft starting, so it CANNOT operate on minecraft - * itself. The game will deliberately crash if this code is detected to trigger a - * minecraft class loading (TODO: implement crash ;) ) - */ - @Override - public String getSetupClass() { - return null; - } + return new String[] {}; + } - /** - * Inject coremod data into this coremod - * This data includes: - * "mcLocation" : the location of the minecraft directory, - * "coremodList" : the list of coremods - * "coremodLocation" : the file this coremod loaded from, - * - * @param data - */ - @Override - public void injectData(Map data) { + /** + * Return a class name that implements "ModContainer" for injection into the mod list The + * "getName" function should return a name that other mods can, if need be, depend on. Trivially, + * this modcontainer will be loaded before all regular mod containers, which means it will be + * forced to be "immutable" - not susceptible to normal sorting behaviour. All other mod + * behaviours are available however- this container can receive and handle normal loading events + */ + @Override + public String getModContainerClass() { + return null; + } - } + /** + * Return the class name of an implementor of "IFMLCallHook", that will be run, in the main + * thread, to perform any additional setup this coremod may require. It will be run + * prior to Minecraft starting, so it CANNOT operate on minecraft itself. The + * game will deliberately crash if this code is detected to trigger a minecraft class loading + * (TODO: implement crash ;) ) + */ + @Override + public String getSetupClass() { + return null; + } - /** - * Return an optional access transformer class for this coremod. It will be injected post-deobf - * so ensure your ATs conform to the new srgnames scheme. - * - * @return the name of an access transformer class or null if none is provided - */ - @Override - public String getAccessTransformerClass() { - return null; - } + /** + * Inject coremod data into this coremod This data includes: "mcLocation" : the location of the + * minecraft directory, "coremodList" : the list of coremods "coremodLocation" : the file this + * coremod loaded from, + * + * @param data + */ + @Override + public void injectData(Map data) {} + + /** + * Return an optional access transformer class for this coremod. It will be injected post-deobf so + * ensure your ATs conform to the new srgnames scheme. + * + * @return the name of an access transformer class or null if none is provided + */ + @Override + public String getAccessTransformerClass() { + return null; + } } diff --git a/src/main/java/club/sk1er/modcore/ModCoreInstaller.java b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java index e17a20e..2cf6f77 100644 --- a/src/main/java/club/sk1er/modcore/ModCoreInstaller.java +++ b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java @@ -4,13 +4,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import net.minecraft.launchwrapper.Launch; -import net.minecraft.launchwrapper.LaunchClassLoader; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; - -import javax.swing.JFrame; -import javax.swing.JProgressBar; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; @@ -29,428 +22,458 @@ import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map.Entry; import java.util.Set; +import javax.swing.JFrame; +import javax.swing.JProgressBar; +import net.minecraft.launchwrapper.Launch; +import net.minecraft.launchwrapper.LaunchClassLoader; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; public class ModCoreInstaller { - private static final String VERSION_URL = "https://api.sk1er.club/modcore_versions"; - private static final String className = "club.sk1er.mods.core.ModCore"; - private static boolean errored = false; - private static String error; - private static File dataDir = null; - private static boolean isRunningModCore = false; - - public static boolean isIsRunningModCore() { - return isRunningModCore; - } - - private static boolean isInitalized() { - try { - LinkedHashSet objects = new LinkedHashSet<>(); - objects.add(className); - Launch.classLoader.clearNegativeEntries(objects); - Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); - invalidClasses.setAccessible(true); - Object obj = invalidClasses.get(ModCoreInstaller.class.getClassLoader()); - ((Set) obj).remove(className); - return Class.forName("club.sk1er.mods.core.ModCore") != null; - } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) { - ignored.printStackTrace(); - } - return false; + private static final String VERSION_URL = "https://api.sk1er.club/modcore_versions"; + private static final String className = "club.sk1er.mods.core.ModCore"; + private static boolean errored = false; + private static String error; + private static File dataDir = null; + private static boolean isRunningModCore = false; + + public static boolean isIsRunningModCore() { + return isRunningModCore; + } + + private static boolean isInitalized() { + try { + LinkedHashSet objects = new LinkedHashSet<>(); + objects.add(className); + Launch.classLoader.clearNegativeEntries(objects); + Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); + invalidClasses.setAccessible(true); + Object obj = invalidClasses.get(ModCoreInstaller.class.getClassLoader()); + ((Set) obj).remove(className); + return Class.forName("club.sk1er.mods.core.ModCore") != null; + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) { + ignored.printStackTrace(); } + return false; + } - public static boolean isErrored() { - return errored; - } + public static boolean isErrored() { + return errored; + } - public static String getError() { - return error; - } + public static String getError() { + return error; + } - private static void bail(String error) { - errored = true; - ModCoreInstaller.error = error; - } + private static void bail(String error) { + errored = true; + ModCoreInstaller.error = error; + } - private static JsonHolder readFile(File in) { - try { - return new JsonHolder(FileUtils.readFileToString(in)); - } catch (IOException ignored) { + private static JsonHolder readFile(File in) { + try { + return new JsonHolder(FileUtils.readFileToString(in)); + } catch (IOException ignored) { - } - return new JsonHolder(); } + return new JsonHolder(); + } - public static void initializeModCore(File gameDir) { - if (!isIsRunningModCore()) { - return; - } - try { - Class modCore = Class.forName(className); - Method instanceMethod = modCore.getMethod("getInstance"); - Method initialize = modCore.getMethod("initialize", File.class); - Object modCoreObject = instanceMethod.invoke(null); - initialize.invoke(modCoreObject, gameDir); - System.out.println("Loaded ModCore Successfully"); - return; - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } - System.out.println("Did NOT ModCore Successfully"); + public static void initializeModCore(File gameDir) { + if (!isIsRunningModCore()) { + return; + } + try { + Class modCore = Class.forName(className); + Method instanceMethod = modCore.getMethod("getInstance"); + Method initialize = modCore.getMethod("initialize", File.class); + Object modCoreObject = instanceMethod.invoke(null); + initialize.invoke(modCoreObject, gameDir); + System.out.println("Loaded ModCore Successfully"); + return; + } catch (ClassNotFoundException + | NoSuchMethodException + | IllegalAccessException + | InvocationTargetException e) { + e.printStackTrace(); + } + System.out.println("Did NOT ModCore Successfully"); + } + + public static int initialize(File gameDir, String minecraftVersion) { + if (isInitalized()) return -1; + dataDir = new File(gameDir, "modcore"); + if (!dataDir.exists()) { + if (!dataDir.mkdirs()) { + bail("Unable to create necessary files"); + return 1; + } + } + JsonHolder jsonHolder = fetchJSON(VERSION_URL); + String latestRemote = jsonHolder.optString(minecraftVersion); + boolean failed = + jsonHolder.getKeys().size() == 0 + || (jsonHolder.has("success") && !jsonHolder.optBoolean("success")); + + File metadataFile = new File(dataDir, "metadata.json"); + JsonHolder localMetadata = readFile(metadataFile); + if (failed) latestRemote = localMetadata.optString(minecraftVersion); + File modcoreFile = + new File(dataDir, "Sk1er Modcore-" + latestRemote + " (" + minecraftVersion + ").jar"); + + if (!modcoreFile.exists() + || !localMetadata.optString(minecraftVersion).equalsIgnoreCase(latestRemote) && !failed) { + // File does not exist, or is out of date, download it + File old = + new File( + dataDir, + "Sk1er Modcore-" + + localMetadata.optString(minecraftVersion) + + " (" + + minecraftVersion + + ").jar"); + if (old.exists()) old.delete(); + + if (!download( + "https://static.sk1er.club/repo/mods/modcore/" + + latestRemote + + "/" + + minecraftVersion + + "/ModCore-" + + latestRemote + + " (" + + minecraftVersion + + ").jar", + latestRemote, + modcoreFile, + minecraftVersion, + localMetadata)) { + bail("Unable to download"); + return 2; + } } - public static int initialize(File gameDir, String minecraftVersion) { - if (isInitalized()) return -1; - dataDir = new File(gameDir, "modcore"); - if (!dataDir.exists()) { - if (!dataDir.mkdirs()) { - bail("Unable to create necessary files"); - return 1; - } - } - JsonHolder jsonHolder = fetchJSON(VERSION_URL); - String latestRemote = jsonHolder.optString(minecraftVersion); - boolean failed = jsonHolder.getKeys().size() == 0 || (jsonHolder.has("success") && !jsonHolder.optBoolean("success")); - - File metadataFile = new File(dataDir, "metadata.json"); - JsonHolder localMetadata = readFile(metadataFile); - if (failed) latestRemote = localMetadata.optString(minecraftVersion); - File modcoreFile = new File(dataDir, "Sk1er Modcore-" + latestRemote + " (" + minecraftVersion + ").jar"); - - if (!modcoreFile.exists() || !localMetadata.optString(minecraftVersion).equalsIgnoreCase(latestRemote) && !failed) { - //File does not exist, or is out of date, download it - File old = new File(dataDir, "Sk1er Modcore-" + localMetadata.optString(minecraftVersion) + " (" + minecraftVersion + ").jar"); - if (old.exists()) old.delete(); - - if (!download("https://static.sk1er.club/repo/mods/modcore/" + latestRemote + "/" + minecraftVersion + "/ModCore-" + latestRemote + " (" + minecraftVersion + ").jar", latestRemote, modcoreFile, minecraftVersion, localMetadata)) { - bail("Unable to download"); - return 2; - } - - } - - addToClasspath(modcoreFile); + addToClasspath(modcoreFile); - if (!isInitalized()) { - bail("Something went wrong and it did not add the jar to the class path. Local file exists? " + modcoreFile.exists()); - return 3; - } - isRunningModCore = true; - return 0; + if (!isInitalized()) { + bail( + "Something went wrong and it did not add the jar to the class path. Local file exists? " + + modcoreFile.exists()); + return 3; } + isRunningModCore = true; + return 0; + } + + public static void addToClasspath(File file) { + try { + URL url = file.toURI().toURL(); + + ClassLoader classLoader = ModCoreInstaller.class.getClassLoader(); + Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, url); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception", e); + } + } + + private static boolean download( + String url, String version, File file, String mcver, JsonHolder versionData) { + url = url.replace(" ", "%20"); + System.out.println("Downloading ModCore " + " version " + version + " from: " + url); + JFrame frame = new JFrame("ModCore Initializer"); + JProgressBar bar = new JProgressBar(); + TextArea comp = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE); + frame.getContentPane().add(comp); + frame.getContentPane().add(bar); + GridLayout manager = new GridLayout(); + frame.setLayout(manager); + manager.setColumns(1); + manager.setRows(2); + comp.setText( + "Downloading Sk1er ModCore Library Version " + version + " for Minecraft " + mcver); + comp.setSize(399, 80); + comp.setEditable(false); + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); + + Dimension preferredSize = new Dimension(400, 225); + bar.setSize(preferredSize); + frame.setSize(preferredSize); + frame.setResizable(false); + bar.setBorderPainted(true); + bar.setMinimum(0); + bar.setStringPainted(true); + frame.setVisible(true); + frame.setLocation( + dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2); + Font font = bar.getFont(); + bar.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 4)); + comp.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); + + try { + + URL u = new URL(url); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er Modcore Initializer)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + InputStream is = connection.getInputStream(); + int contentLength = connection.getContentLength(); + FileOutputStream outputStream = new FileOutputStream(file); + byte[] buffer = new byte[1024]; + System.out.println("MAX: " + contentLength); + bar.setMaximum(contentLength); + int read; + bar.setValue(0); + while ((read = is.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + bar.setValue(bar.getValue() + 1024); + } + outputStream.close(); + FileUtils.write( + new File(dataDir, "metadata.json"), versionData.put(mcver, version).toString()); + } catch (Exception e) { + e.printStackTrace(); + frame.dispose(); + return false; + } + frame.dispose(); + return true; + } + + public static final JsonHolder fetchJSON(String url) { + return new JsonHolder(fetchString(url)); + } + + public static final String fetchString(String url) { + url = url.replace(" ", "%20"); + System.out.println("Fetching " + url); + try { + URL u = new URL(url); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er ModCore)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + InputStream is = connection.getInputStream(); + return IOUtils.toString(is, Charset.defaultCharset()); + } catch (Exception e) { + e.printStackTrace(); + } + return "Failed to fetch"; + } + // Added because we need to use before ModCore is loaded + static class JsonHolder { + private JsonObject object; - public static void addToClasspath(File file) { - try { - URL url = file.toURI().toURL(); - - ClassLoader classLoader = ModCoreInstaller.class.getClassLoader(); - Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); - method.setAccessible(true); - method.invoke(classLoader, url); - } catch (Exception e) { - throw new RuntimeException("Unexpected exception", e); - } + public JsonHolder(JsonObject object) { + this.object = object; } - private static boolean download(String url, String version, File file, String mcver, JsonHolder versionData) { - url = url.replace(" ", "%20"); - System.out.println("Downloading ModCore " + " version " + version + " from: " + url); - JFrame frame = new JFrame("ModCore Initializer"); - JProgressBar bar = new JProgressBar(); - TextArea comp = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE); - frame.getContentPane().add(comp); - frame.getContentPane().add(bar); - GridLayout manager = new GridLayout(); - frame.setLayout(manager); - manager.setColumns(1); - manager.setRows(2); - comp.setText("Downloading Sk1er ModCore Library Version " + version + " for Minecraft " + mcver); - comp.setSize(399, 80); - comp.setEditable(false); - Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); - - - Dimension preferredSize = new Dimension(400, 225); - bar.setSize(preferredSize); - frame.setSize(preferredSize); - frame.setResizable(false); - bar.setBorderPainted(true); - bar.setMinimum(0); - bar.setStringPainted(true); - frame.setVisible(true); - frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2); - Font font = bar.getFont(); - bar.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 4)); - comp.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); - + public JsonHolder(String raw) { + if (raw == null) object = new JsonObject(); + else try { - - URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection) u.openConnection(); - connection.setRequestMethod("GET"); - connection.setUseCaches(true); - connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er Modcore Initializer)"); - connection.setReadTimeout(15000); - connection.setConnectTimeout(15000); - connection.setDoOutput(true); - InputStream is = connection.getInputStream(); - int contentLength = connection.getContentLength(); - FileOutputStream outputStream = new FileOutputStream(file); - byte[] buffer = new byte[1024]; - System.out.println("MAX: " + contentLength); - bar.setMaximum(contentLength); - int read; - bar.setValue(0); - while ((read = is.read(buffer)) > 0) { - outputStream.write(buffer, 0, read); - bar.setValue(bar.getValue() + 1024); - } - outputStream.close(); - FileUtils.write(new File(dataDir, "metadata.json"), versionData.put(mcver, version).toString()); + this.object = new JsonParser().parse(raw).getAsJsonObject(); } catch (Exception e) { - e.printStackTrace(); - frame.dispose(); - return false; + this.object = new JsonObject(); + e.printStackTrace(); } - frame.dispose(); - return true; } - public static final JsonHolder fetchJSON(String url) { - return new JsonHolder(fetchString(url)); + public JsonHolder() { + this(new JsonObject()); } - public static final String fetchString(String url) { - url = url.replace(" ", "%20"); - System.out.println("Fetching " + url); - try { - URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection) u.openConnection(); - connection.setRequestMethod("GET"); - connection.setUseCaches(true); - connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er ModCore)"); - connection.setReadTimeout(15000); - connection.setConnectTimeout(15000); - connection.setDoOutput(true); - InputStream is = connection.getInputStream(); - return IOUtils.toString(is, Charset.defaultCharset()); - } catch (Exception e) { - e.printStackTrace(); - } - return "Failed to fetch"; + @Override + public String toString() { + if (object != null) return object.toString(); + return "{}"; } + public JsonHolder put(String key, boolean value) { + object.addProperty(key, value); + return this; + } - //Added because we need to use before ModCore is loaded - static class JsonHolder { - private JsonObject object; - - public JsonHolder(JsonObject object) { - this.object = object; - } - - public JsonHolder(String raw) { - if (raw == null) - object = new JsonObject(); - else - try { - this.object = new JsonParser().parse(raw).getAsJsonObject(); - } catch (Exception e) { - this.object = new JsonObject(); - e.printStackTrace(); - } - } - - public JsonHolder() { - this(new JsonObject()); - } - - @Override - public String toString() { - if (object != null) - return object.toString(); - return "{}"; - } - - public JsonHolder put(String key, boolean value) { - object.addProperty(key, value); - return this; - } - - public void mergeNotOverride(JsonHolder merge) { - merge(merge, false); - } - - public void mergeOverride(JsonHolder merge) { - merge(merge, true); - } - - public void merge(JsonHolder merge, boolean override) { - JsonObject object = merge.getObject(); - for (String s : merge.getKeys()) { - if (override || !this.has(s)) - put(s, object.get(s)); - } - } - - private void put(String s, JsonElement element) { - this.object.add(s, element); - } - - public JsonHolder put(String key, String value) { - object.addProperty(key, value); - return this; - } - - public JsonHolder put(String key, int value) { - object.addProperty(key, value); - return this; - } - - public JsonHolder put(String key, double value) { - object.addProperty(key, value); - return this; - } + public void mergeNotOverride(JsonHolder merge) { + merge(merge, false); + } - public JsonHolder put(String key, long value) { - object.addProperty(key, value); - return this; - } + public void mergeOverride(JsonHolder merge) { + merge(merge, true); + } - private JsonHolder defaultOptJSONObject(String key, JsonObject fallBack) { - try { - return new JsonHolder(object.get(key).getAsJsonObject()); - } catch (Exception e) { - return new JsonHolder(fallBack); - } - } + public void merge(JsonHolder merge, boolean override) { + JsonObject object = merge.getObject(); + for (String s : merge.getKeys()) { + if (override || !this.has(s)) put(s, object.get(s)); + } + } - public JsonArray defaultOptJSONArray(String key, JsonArray fallback) { - try { - return object.get(key).getAsJsonArray(); - } catch (Exception e) { - return fallback; - } - } + private void put(String s, JsonElement element) { + this.object.add(s, element); + } - public JsonArray optJSONArray(String key) { - return defaultOptJSONArray(key, new JsonArray()); - } + public JsonHolder put(String key, String value) { + object.addProperty(key, value); + return this; + } + public JsonHolder put(String key, int value) { + object.addProperty(key, value); + return this; + } - public boolean has(String key) { - return object.has(key); - } + public JsonHolder put(String key, double value) { + object.addProperty(key, value); + return this; + } - public long optLong(String key, long fallback) { - try { - return object.get(key).getAsLong(); - } catch (Exception e) { - return fallback; - } - } + public JsonHolder put(String key, long value) { + object.addProperty(key, value); + return this; + } - public long optLong(String key) { - return optLong(key, 0); - } + private JsonHolder defaultOptJSONObject(String key, JsonObject fallBack) { + try { + return new JsonHolder(object.get(key).getAsJsonObject()); + } catch (Exception e) { + return new JsonHolder(fallBack); + } + } - public boolean optBoolean(String key, boolean fallback) { - try { - return object.get(key).getAsBoolean(); - } catch (Exception e) { - return fallback; - } - } + public JsonArray defaultOptJSONArray(String key, JsonArray fallback) { + try { + return object.get(key).getAsJsonArray(); + } catch (Exception e) { + return fallback; + } + } - public boolean optBoolean(String key) { - return optBoolean(key, false); - } + public JsonArray optJSONArray(String key) { + return defaultOptJSONArray(key, new JsonArray()); + } - public JsonObject optActualJSONObject(String key) { - try { - return object.get(key).getAsJsonObject(); - } catch (Exception e) { - return new JsonObject(); - } - } + public boolean has(String key) { + return object.has(key); + } - public JsonHolder optJSONObject(String key) { - return defaultOptJSONObject(key, new JsonObject()); - } + public long optLong(String key, long fallback) { + try { + return object.get(key).getAsLong(); + } catch (Exception e) { + return fallback; + } + } + public long optLong(String key) { + return optLong(key, 0); + } - public int optInt(String key, int fallBack) { - try { - return object.get(key).getAsInt(); - } catch (Exception e) { - return fallBack; - } - } + public boolean optBoolean(String key, boolean fallback) { + try { + return object.get(key).getAsBoolean(); + } catch (Exception e) { + return fallback; + } + } - public int optInt(String key) { - return optInt(key, 0); - } + public boolean optBoolean(String key) { + return optBoolean(key, false); + } + public JsonObject optActualJSONObject(String key) { + try { + return object.get(key).getAsJsonObject(); + } catch (Exception e) { + return new JsonObject(); + } + } - public String defaultOptString(String key, String fallBack) { - try { - return object.get(key).getAsString(); - } catch (Exception e) { - return fallBack; - } - } + public JsonHolder optJSONObject(String key) { + return defaultOptJSONObject(key, new JsonObject()); + } - public String optString(String key) { - return defaultOptString(key, ""); - } + public int optInt(String key, int fallBack) { + try { + return object.get(key).getAsInt(); + } catch (Exception e) { + return fallBack; + } + } + public int optInt(String key) { + return optInt(key, 0); + } - public double optDouble(String key, double fallBack) { - try { - return object.get(key).getAsDouble(); - } catch (Exception e) { - return fallBack; - } - } + public String defaultOptString(String key, String fallBack) { + try { + return object.get(key).getAsString(); + } catch (Exception e) { + return fallBack; + } + } - public List getKeys() { - List tmp = new ArrayList<>(); - object.entrySet().forEach(e -> tmp.add(e.getKey())); - return tmp; - } + public String optString(String key) { + return defaultOptString(key, ""); + } - public double optDouble(String key) { - return optDouble(key, 0.0); - } + public double optDouble(String key, double fallBack) { + try { + return object.get(key).getAsDouble(); + } catch (Exception e) { + return fallBack; + } + } + public List getKeys() { + List list = new ArrayList<>(); + for (Entry stringJsonElementEntry : object.entrySet()) { + String key = stringJsonElementEntry.getKey(); + list.add(key); + } - public JsonObject getObject() { - return object; - } + return list; + } - public boolean isNull(String key) { - return object.has(key) && object.get(key).isJsonNull(); - } + public double optDouble(String key) { + return optDouble(key, 0.0); + } - public JsonHolder put(String values, JsonHolder values1) { - return put(values, values1.getObject()); - } + public JsonObject getObject() { + return object; + } - public JsonHolder put(String values, JsonObject object) { - this.object.add(values, object); - return this; - } + public boolean isNull(String key) { + return object.has(key) && object.get(key).isJsonNull(); + } - public void put(String blacklisted, JsonArray jsonElements) { - this.object.add(blacklisted, jsonElements); - } + public JsonHolder put(String values, JsonHolder values1) { + return put(values, values1.getObject()); + } - public void remove(String header) { - object.remove(header); - } + public JsonHolder put(String values, JsonObject object) { + this.object.add(values, object); + return this; } + public void put(String blacklisted, JsonArray jsonElements) { + this.object.add(blacklisted, jsonElements); + } + public void remove(String header) { + object.remove(header); + } + } } diff --git a/src/main/java/club/sk1er/motionblur/MotionBlur.java b/src/main/java/club/sk1er/motionblur/MotionBlur.java index 6b3e52b..a610b96 100644 --- a/src/main/java/club/sk1er/motionblur/MotionBlur.java +++ b/src/main/java/club/sk1er/motionblur/MotionBlur.java @@ -5,6 +5,9 @@ import club.sk1er.motionblur.command.MotionBlurCommand; import club.sk1er.motionblur.config.BlurConfig; import club.sk1er.motionblur.resource.MotionBlurResourceManager; +import java.io.File; +import java.lang.reflect.Field; +import java.util.Map; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.FallbackResourceManager; import net.minecraft.client.resources.SimpleReloadableResourceManager; @@ -22,94 +25,102 @@ import net.minecraftforge.fml.common.gameevent.TickEvent; import org.lwjgl.input.Keyboard; -import java.io.File; -import java.lang.reflect.Field; -import java.util.Map; - -@Mod(name = "Motion Blur", modid = "motionblurmod", version = "2.0", acceptedMinecraftVersions = "*") +@Mod(name = "Motion Blur", modid = "motionblurmod", version = "2.0") public class MotionBlur { - private final Minecraft mc = Minecraft.getMinecraft(); - private final Map domainResourceManagers = ((SimpleReloadableResourceManager) mc.getResourceManager()).domainResourceManagers; - private Field cachedFastRender; - private int ticks; + private final Minecraft mc = Minecraft.getMinecraft(); + private final Map domainResourceManagers = + ((SimpleReloadableResourceManager) mc.getResourceManager()).domainResourceManagers; + private Field cachedFastRender; + private int ticks; - @Mod.Instance("motionblurmod") - public static MotionBlur instance; + @Mod.Instance("motionblurmod") + public static MotionBlur instance; - private File configFile; + private File configFile; - public MotionBlur() { - try { - cachedFastRender = GameSettings.class.getDeclaredField("ofFastRender"); - } catch (Exception ignored) { - } + public MotionBlur() { + try { + cachedFastRender = GameSettings.class.getDeclaredField("ofFastRender"); + } catch (Exception ignored) { } - - @Mod.EventHandler - public void init(FMLInitializationEvent event) { - configFile = new File("./config/motionblur.cfg"); - ModCoreInstaller.initializeModCore(Minecraft.getMinecraft().mcDataDir); - MinecraftForge.EVENT_BUS.register(this); - ClientCommandHandler.instance.registerCommand(new MotionBlurCommand()); - - loadConfig(); + } + + @Mod.EventHandler + public void init(FMLInitializationEvent event) { + configFile = new File("./config/motionblur.cfg"); + ModCoreInstaller.initializeModCore(Minecraft.getMinecraft().mcDataDir); + MinecraftForge.EVENT_BUS.register(this); + ClientCommandHandler.instance.registerCommand(new MotionBlurCommand()); + + loadConfig(); + } + + @SubscribeEvent + public void tick(TickEvent.ClientTickEvent event) { + if (domainResourceManagers != null) { + if (!domainResourceManagers.containsKey("motionblur")) { + domainResourceManagers.put( + "motionblur", new MotionBlurResourceManager(mc.metadataSerializer_)); + } } - @SubscribeEvent - public void tick(TickEvent.ClientTickEvent event) { - if (domainResourceManagers != null) { - if (!domainResourceManagers.containsKey("motionblur")) { - domainResourceManagers.put("motionblur", new MotionBlurResourceManager(mc.metadataSerializer_)); - } - } - - ++ticks; - if (ticks % 5000 == 0) { - if (isFastRenderEnabled() && BlurConfig.motionBlur) { - if (mc.thePlayer != null && mc.theWorld != null) { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Motion Blur is not compatible with OptiFine's Fast Render."); - } - } + ++ticks; + if (ticks % 5000 == 0) { + if (isFastRenderEnabled() && BlurConfig.motionBlur) { + if (mc.thePlayer != null && mc.theWorld != null) { + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", + " Motion Blur is not compatible with OptiFine's Fast Render."); } + } } - - @SubscribeEvent - public void onKey(InputEvent.KeyInputEvent event) { - if (mc.thePlayer != null && BlurConfig.motionBlur && Keyboard.isKeyDown(mc.gameSettings.keyBindTogglePerspective.getKeyCode())) { - mc.entityRenderer.loadShader(new ResourceLocation("motionblur", "motionblur")); - mc.entityRenderer.getShaderGroup().createBindFramebuffers(mc.displayWidth, mc.displayHeight); - } + } + + @SubscribeEvent + public void onKey(InputEvent.KeyInputEvent event) { + if (mc.thePlayer != null + && BlurConfig.motionBlur + && Keyboard.isKeyDown(mc.gameSettings.keyBindTogglePerspective.getKeyCode())) { + mc.entityRenderer.loadShader(new ResourceLocation("motionblur", "motionblur")); + mc.entityRenderer.getShaderGroup().createBindFramebuffers(mc.displayWidth, mc.displayHeight); } + } - public boolean isFastRenderEnabled() { - try { - return cachedFastRender.getBoolean(mc.gameSettings); - } catch (Exception ignored) { - return false; - } + public boolean isFastRenderEnabled() { + try { + return cachedFastRender.getBoolean(mc.gameSettings); + } catch (Exception ignored) { + return false; } - - public void saveConfig() { - Configuration configuration = new Configuration(configFile); - updateConfig(configuration, false); - configuration.save(); - } - - private void loadConfig() { - Configuration configuration = new Configuration(configFile); - configuration.load(); - updateConfig(configuration, true); - configuration.save(); + } + + public void saveConfig() { + Configuration configuration = new Configuration(configFile); + updateConfig(configuration, false); + configuration.save(); + } + + private void loadConfig() { + Configuration configuration = new Configuration(configFile); + configuration.load(); + updateConfig(configuration, true); + configuration.save(); + } + + private void updateConfig(Configuration config, boolean load) { + Property property = config.get("general", "enabled", false); + if (load) { + BlurConfig.motionBlur = property.getBoolean(); + } else { + property.set(BlurConfig.motionBlur); } - private void updateConfig(Configuration config, boolean load) { - Property property = config.get("general", "enabled", false); - if (load) BlurConfig.motionBlur = property.getBoolean(); - else property.set(BlurConfig.motionBlur); - - property = config.get("general", "amount", 0); - if (load) BlurConfig.blurAmount = property.getDouble(); - else property.set(BlurConfig.blurAmount); + property = config.get("general", "amount", 0); + if (load) { + BlurConfig.blurAmount = property.getDouble(); + } else { + property.set(BlurConfig.blurAmount); } + } } diff --git a/src/main/java/club/sk1er/motionblur/command/MotionBlurCommand.java b/src/main/java/club/sk1er/motionblur/command/MotionBlurCommand.java index 661695e..b66bbe0 100644 --- a/src/main/java/club/sk1er/motionblur/command/MotionBlurCommand.java +++ b/src/main/java/club/sk1er/motionblur/command/MotionBlurCommand.java @@ -11,62 +11,65 @@ public class MotionBlurCommand extends CommandBase { - private final Minecraft mc = Minecraft.getMinecraft(); + private final Minecraft mc = Minecraft.getMinecraft(); - /** - * Gets the name of the command - */ - @Override - public String getCommandName() { - return "motionblur"; - } + /** Gets the name of the command */ + @Override + public String getCommandName() { + return "motionblur"; + } - /** - * Gets the usage string for the command. - */ - @Override - public String getCommandUsage(ICommandSender sender) { - return "/" + getCommandName(); - } + /** Gets the usage string for the command. */ + @Override + public String getCommandUsage(ICommandSender sender) { + return "/" + getCommandName(); + } - /** - * Callback when the command is invoked - */ - @Override - public void processCommand(ICommandSender sender, String[] args) { - if (args.length == 0) { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Usage: /motionblur <0 - 7>"); + /** Callback when the command is invoked */ + @Override + public void processCommand(ICommandSender sender, String[] args) { + if (args.length == 0) { + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", " Usage: /motionblur <0 - 7>"); + } else { + int amount = Integer.parseInt(args[0]); + if (amount >= 0 && amount <= 7) { + if (MotionBlur.instance.isFastRenderEnabled()) { + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", + " Motion Blur is not compatible with OptiFine's Fast Render."); } else { - int amount = Integer.parseInt(args[0]); - if (amount >= 0 && amount <= 7) { - if (MotionBlur.instance.isFastRenderEnabled()) { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Motion Blur is not compatible with OptiFine's Fast Render."); - } else { - if (mc.entityRenderer.getShaderGroup() != null) { - mc.entityRenderer.getShaderGroup().deleteShaderGroup(); - } + if (mc.entityRenderer.getShaderGroup() != null) { + mc.entityRenderer.getShaderGroup().deleteShaderGroup(); + } - if (amount != 0) { - BlurConfig.motionBlur = true; - BlurConfig.blurAmount = amount; - mc.entityRenderer.loadShader(new ResourceLocation("motionblur", "motionblur")); - mc.entityRenderer.getShaderGroup().createBindFramebuffers(mc.displayWidth, mc.displayHeight); - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Motion Blur enabled with amount " + amount + "."); - } else { - BlurConfig.motionBlur = false; - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Motion Blur disabled."); - } + if (amount != 0) { + BlurConfig.motionBlur = true; + BlurConfig.blurAmount = amount; + mc.entityRenderer.loadShader(new ResourceLocation("motionblur", "motionblur")); + mc.entityRenderer + .getShaderGroup() + .createBindFramebuffers(mc.displayWidth, mc.displayHeight); + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", + " Motion Blur enabled with amount " + amount + "."); + } else { + BlurConfig.motionBlur = false; + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", " Motion Blur disabled."); + } - MotionBlur.instance.saveConfig(); - } - } else { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "[MotionBlur]", " Invalid blur amount, 0 - 7."); - } + MotionBlur.instance.saveConfig(); } + } else { + MinecraftUtils.sendMessage( + EnumChatFormatting.RED + "[MotionBlur]", " Invalid blur amount, 0 - 7."); + } } + } - @Override - public int getRequiredPermissionLevel() { - return -1; - } + @Override + public int getRequiredPermissionLevel() { + return -1; + } } diff --git a/src/main/java/club/sk1er/motionblur/config/BlurConfig.java b/src/main/java/club/sk1er/motionblur/config/BlurConfig.java index c39ad2a..64045fe 100644 --- a/src/main/java/club/sk1er/motionblur/config/BlurConfig.java +++ b/src/main/java/club/sk1er/motionblur/config/BlurConfig.java @@ -1,6 +1,6 @@ package club.sk1er.motionblur.config; public class BlurConfig { - public static boolean motionBlur; - public static double blurAmount; + public static boolean motionBlur; + public static double blurAmount; } diff --git a/src/main/java/club/sk1er/motionblur/resource/MotionBlurResource.java b/src/main/java/club/sk1er/motionblur/resource/MotionBlurResource.java index 0d1e7ff..5a0ce13 100644 --- a/src/main/java/club/sk1er/motionblur/resource/MotionBlurResource.java +++ b/src/main/java/club/sk1er/motionblur/resource/MotionBlurResource.java @@ -1,42 +1,43 @@ package club.sk1er.motionblur.resource; import club.sk1er.motionblur.config.BlurConfig; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.Locale; import net.minecraft.client.resources.IResource; import net.minecraft.client.resources.data.IMetadataSection; import net.minecraft.util.ResourceLocation; import org.apache.commons.io.IOUtils; -import java.io.InputStream; -import java.nio.charset.Charset; -import java.util.Locale; - public class MotionBlurResource implements IResource { - private static final String JSON = "{\"targets\":[\"swap\",\"previous\"],\"passes\":[{\"name\":\"phosphor\",\"intarget\":\"minecraft:main\",\"outtarget\":\"swap\",\"auxtargets\":[{\"name\":\"PrevSampler\",\"id\":\"previous\"}],\"uniforms\":[{\"name\":\"Phosphor\",\"values\":[%.2f, %.2f, %.2f]}]},{\"name\":\"blit\",\"intarget\":\"swap\",\"outtarget\":\"previous\"},{\"name\":\"blit\",\"intarget\":\"swap\",\"outtarget\":\"minecraft:main\"}]}"; - - @Override - public ResourceLocation getResourceLocation() { - return null; - } - - @Override - public InputStream getInputStream() { - double amount = 0.7 + BlurConfig.blurAmount / 100.0 * 3.0 - 0.01; - return IOUtils.toInputStream(String.format(Locale.ENGLISH, JSON, amount, amount, amount), Charset.defaultCharset()); - } - - @Override - public boolean hasMetadata() { - return false; - } - - @Override - public T getMetadata(String p_110526_1_) { - return null; - } - - @Override - public String getResourcePackName() { - return null; - } + private static final String JSON = + "{\"targets\":[\"swap\",\"previous\"],\"passes\":[{\"name\":\"phosphor\",\"intarget\":\"minecraft:main\",\"outtarget\":\"swap\",\"auxtargets\":[{\"name\":\"PrevSampler\",\"id\":\"previous\"}],\"uniforms\":[{\"name\":\"Phosphor\",\"values\":[%.2f, %.2f, %.2f]}]},{\"name\":\"blit\",\"intarget\":\"swap\",\"outtarget\":\"previous\"},{\"name\":\"blit\",\"intarget\":\"swap\",\"outtarget\":\"minecraft:main\"}]}"; + + @Override + public ResourceLocation getResourceLocation() { + return null; + } + + @Override + public InputStream getInputStream() { + double amount = 0.7 + BlurConfig.blurAmount / 100.0 * 3.0 - 0.01; + return IOUtils.toInputStream( + String.format(Locale.ENGLISH, JSON, amount, amount, amount), Charset.defaultCharset()); + } + + @Override + public boolean hasMetadata() { + return false; + } + + @Override + public T getMetadata(String p_110526_1_) { + return null; + } + + @Override + public String getResourcePackName() { + return null; + } } diff --git a/src/main/java/club/sk1er/motionblur/resource/MotionBlurResourceManager.java b/src/main/java/club/sk1er/motionblur/resource/MotionBlurResourceManager.java index 553ee08..5cc0c97 100644 --- a/src/main/java/club/sk1er/motionblur/resource/MotionBlurResourceManager.java +++ b/src/main/java/club/sk1er/motionblur/resource/MotionBlurResourceManager.java @@ -1,31 +1,30 @@ package club.sk1er.motionblur.resource; +import java.util.List; +import java.util.Set; import net.minecraft.client.resources.FallbackResourceManager; import net.minecraft.client.resources.IResource; import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.data.IMetadataSerializer; import net.minecraft.util.ResourceLocation; -import java.util.List; -import java.util.Set; - public class MotionBlurResourceManager extends FallbackResourceManager implements IResourceManager { - public MotionBlurResourceManager(IMetadataSerializer frmMetadataSerializerIn) { - super(frmMetadataSerializerIn); - } + public MotionBlurResourceManager(IMetadataSerializer frmMetadataSerializerIn) { + super(frmMetadataSerializerIn); + } - @Override - public Set getResourceDomains() { - return null; - } + @Override + public Set getResourceDomains() { + return null; + } - @Override - public IResource getResource(ResourceLocation location) { - return new MotionBlurResource(); - } + @Override + public IResource getResource(ResourceLocation location) { + return new MotionBlurResource(); + } - @Override - public List getAllResources(ResourceLocation location) { - return null; - } + @Override + public List getAllResources(ResourceLocation location) { + return null; + } }