diff options
author | DJtheRedstoner <52044242+DJtheRedstoner@users.noreply.github.com> | 2020-08-31 16:55:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-31 16:55:56 -0400 |
commit | 2dd36367ec1ea8e18a0e6bc5cafbd65ac5383bde (patch) | |
tree | 7648e103ffa817262fed18cba00360f9b53a10ac /src/main | |
parent | f0e754871b6464a5c852f7b2b2d99b53b21f3896 (diff) | |
parent | c5b101efe2ed6aed5338d304acce2cd13b369725 (diff) | |
download | PerspectiveModv4-2dd36367ec1ea8e18a0e6bc5cafbd65ac5383bde.tar.gz PerspectiveModv4-2dd36367ec1ea8e18a0e6bc5cafbd65ac5383bde.tar.bz2 PerspectiveModv4-2dd36367ec1ea8e18a0e6bc5cafbd65ac5383bde.zip |
Merge pull request #1 from asbyth/master
detect canelex's perspective mod and die
Diffstat (limited to 'src/main')
4 files changed, 104 insertions, 30 deletions
diff --git a/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java index f819c66..5cdbd3b 100644 --- a/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java +++ b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java @@ -22,7 +22,6 @@ import org.lwjgl.opengl.Display; import java.io.File; - @Mod(modid = "djperspectivemod", name = "Perspective Mod v4", version = "4.0", acceptedMinecraftVersions = "[1.8.9]", clientSideOnly = true) public class PerspectiveMod { @@ -42,9 +41,7 @@ public class PerspectiveMod { public void preInit(FMLPreInitializationEvent event) { File configFile = new File(event.getModConfigurationDirectory(), "perspectivemodv4.json"); loadConfig(configFile); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - saveConfig(configFile); - })); + Runtime.getRuntime().addShutdownHook(new Thread(() -> saveConfig(configFile))); } @Mod.EventHandler @@ -56,21 +53,21 @@ public class PerspectiveMod { @SubscribeEvent public void onKeyEvent(InputEvent.KeyInputEvent event) { - if(perspectiveKey.getKeyCode() > 0) { + if (perspectiveKey.getKeyCode() > 0) { onPressed(Keyboard.getEventKey(), Keyboard.getEventKeyState()); } } @SubscribeEvent public void onMouseEvent(InputEvent.MouseInputEvent event) { - if(perspectiveKey.getKeyCode() < 0) { + if (perspectiveKey.getKeyCode() < 0) { onPressed(Mouse.getEventButton() - 100, Mouse.getEventButtonState()); } } public static void onPressed(int eventKey, boolean state) { if (eventKey == perspectiveKey.getKeyCode()) { - if(config.modEnabled) { + if (config.modEnabled) { if (state) { perspectiveToggled = !perspectiveToggled; cameraYaw = mc.thePlayer.rotationYaw; @@ -86,7 +83,7 @@ public class PerspectiveMod { perspectiveToggled = false; mc.gameSettings.thirdPersonView = previousPerspective; } - } else if(perspectiveToggled) { + } else if (perspectiveToggled) { perspectiveToggled = false; mc.gameSettings.thirdPersonView = previousPerspective; } @@ -94,10 +91,8 @@ public class PerspectiveMod { } public static boolean overrideMouse() { - if (mc.inGameHasFocus && Display.isActive()) - { - if (!perspectiveToggled) - { + if (mc.inGameHasFocus && Display.isActive()) { + if (!perspectiveToggled) { return true; } @@ -119,7 +114,7 @@ public class PerspectiveMod { } public static void loadConfig(File configFile) { - if(configFile.exists()) { + if (configFile.exists()) { try { String json = FileUtils.readFileToString(configFile); config = gson.fromJson(json, PerspectiveModConfig.class); @@ -140,6 +135,5 @@ public class PerspectiveMod { } catch (Exception e) { e.printStackTrace(); } - }; - + } } diff --git a/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java index c19c81c..3eae96a 100644 --- a/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java +++ b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java @@ -5,7 +5,7 @@ import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class PerspectiveModCommand extends CommandBase { @@ -19,39 +19,37 @@ public class PerspectiveModCommand extends CommandBase { @Override public List<String> getCommandAliases() { - List<String> aliases = new ArrayList<>(); - aliases.add("pmod"); - return aliases; + return Collections.singletonList("pmod"); } @Override public String getCommandUsage(ICommandSender sender) { - return "§6Perspective Mod Help\n" + - "§b/pmod <enable|disable> §7- Enables or disables the mod.\n" + - "§b/pmod mode <hold|toggle> §7- Changes the mode.\n" + - "§7Edit the keybind in the minecraft controls menu."; + return "§6Perspective Mod Help\n" + + "§b/pmod <enable|disable> §7- Enables or disables the mod.\n" + + "§b/pmod mode <hold|toggle> §7- Changes the mode.\n" + + "§7Edit the keybind in the minecraft controls menu."; } @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { - if(args.length > 0 && args.length < 3) { + if (args.length > 0 && args.length < 3) { String arg = args[0]; - if(arg.equalsIgnoreCase("enable")) { + if (arg.equalsIgnoreCase("enable")) { PerspectiveMod.config.modEnabled = true; sendMessage(sender, "§bMod §aEnabled§b."); - } else if(arg.equalsIgnoreCase("disable")) { + } else if (arg.equalsIgnoreCase("disable")) { PerspectiveMod.config.modEnabled = false; sendMessage(sender, "§bMod §cDisabled§b."); - } else if(arg.equalsIgnoreCase("mode")) { - if(args.length == 2) { + } else if (arg.equalsIgnoreCase("mode")) { + if (args.length == 2) { String mode = args[1]; - if(mode.equalsIgnoreCase("hold")) { + if (mode.equalsIgnoreCase("hold")) { PerspectiveMod.config.holdMode = true; sendMessage(sender, "§bMode set to hold."); - } else if(mode.equalsIgnoreCase("toggle")) { + } else if (mode.equalsIgnoreCase("toggle")) { PerspectiveMod.config.holdMode = false; sendMessage(sender, "§bMode set to toggle."); diff --git a/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java index c54efd7..1242671 100644 --- a/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java +++ b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java @@ -1,5 +1,6 @@ package me.djtheredstoner.perspectivemod.forge; +import net.minecraftforge.common.ForgeVersion; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.mixin.Mixins; @@ -7,6 +8,7 @@ import org.spongepowered.asm.mixin.Mixins; import java.util.Map; // ONLY needed in development environment, the mixin tweaker handles this in prod. +@IFMLLoadingPlugin.MCVersion(ForgeVersion.mcVersion) public class PerspectiveModLoadingPlugin implements IFMLLoadingPlugin { public PerspectiveModLoadingPlugin() { diff --git a/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveTweaker.java b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveTweaker.java new file mode 100644 index 0000000..f22ba05 --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveTweaker.java @@ -0,0 +1,80 @@ +package me.djtheredstoner.perspectivemod.forge; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import net.minecraft.launchwrapper.Launch; +import net.minecraftforge.common.ForgeVersion; +import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; + +import javax.swing.JOptionPane; +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +// this one will be loaded in prod, used to detect Canelex's perspective mod +@IFMLLoadingPlugin.MCVersion(ForgeVersion.mcVersion) +public class PerspectiveTweaker implements IFMLLoadingPlugin { + + public PerspectiveTweaker() { + File mods = new File(Launch.minecraftHome, "mods"); + + if (!mods.exists()) { + mods.mkdirs(); // mods folder may not exist in dev yet + } + + File[] coreModList = mods.listFiles((dir, name) -> name.endsWith(".jar")); + for (File file : coreModList) { + try { + try (ZipFile zipFile = new ZipFile(file)) { + if (zipFile.getEntry("net/canelex/perspectivemod/PerspectiveMod.class") != null) { + halt("<html><p>Perspective Mod v4 is not compatible with Perspective Mod v3 by Canelex. Please remove Canelex's in order to launch the game.</p></html>"); + continue; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void halt(final String message) { + JOptionPane.showMessageDialog(null, message, "Launch Aborted", JOptionPane.ERROR_MESSAGE); + try { + final Class<?> aClass = Class.forName("java.lang.Shutdown"); + final Method exit = aClass.getDeclaredMethod("exit", int.class); + exit.setAccessible(true); + exit.invoke(null, 0); + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Override + public String[] getASMTransformerClass() { + return new String[0]; + } + + @Override + public String getModContainerClass() { + return null; + } + + @Override + public String getSetupClass() { + return null; + } + + @Override + public void injectData(Map<String, Object> map) { + + } + + @Override + public String getAccessTransformerClass() { + return null; + } +} |