From 09a500d0ae3693ed5fc03feec505c6b2664ada84 Mon Sep 17 00:00:00 2001 From: DJtheRedstoner <52044242+DJtheRedstoner@users.noreply.github.com> Date: Mon, 31 Aug 2020 09:30:59 -0400 Subject: Initial Commit --- .../perspectivemod/PerspectiveMod.java | 145 +++++++++++++++++++++ .../perspectivemod/PerspectiveModCommand.java | 88 +++++++++++++ .../config/PerspectiveModConfig.java | 8 ++ .../forge/PerspectiveModLoadingPlugin.java | 42 ++++++ .../perspectivemod/mixins/MixinEntityRenderer.java | 41 ++++++ .../perspectivemod/mixins/MixinRenderManager.java | 24 ++++ src/main/resources/LICENSE | 21 +++ src/main/resources/mcmod.info | 16 +++ src/main/resources/mixins.perspectivemod.json | 12 ++ 9 files changed, 397 insertions(+) create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/config/PerspectiveModConfig.java create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinEntityRenderer.java create mode 100644 src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinRenderManager.java create mode 100644 src/main/resources/LICENSE create mode 100644 src/main/resources/mcmod.info create mode 100644 src/main/resources/mixins.perspectivemod.json (limited to 'src') diff --git a/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java new file mode 100644 index 0000000..f819c66 --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveMod.java @@ -0,0 +1,145 @@ +package me.djtheredstoner.perspectivemod; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import me.djtheredstoner.perspectivemod.config.PerspectiveModConfig; +import net.minecraft.client.Minecraft; +import net.minecraft.client.settings.KeyBinding; +import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.client.registry.ClientRegistry; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.InputEvent; +import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; +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 { + + private static final Minecraft mc = Minecraft.getMinecraft(); + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + private static final KeyBinding perspectiveKey = new KeyBinding("Perspective", Keyboard.KEY_LMENU, "Perspective Mod"); + private static final Logger logger = LogManager.getLogger("Perspective Mod v4"); + + public static PerspectiveModConfig config; + + public static boolean perspectiveToggled = false; + public static float cameraYaw = 0F; + public static float cameraPitch = 0F; + private static int previousPerspective = 0; + + @Mod.EventHandler + public void preInit(FMLPreInitializationEvent event) { + File configFile = new File(event.getModConfigurationDirectory(), "perspectivemodv4.json"); + loadConfig(configFile); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + saveConfig(configFile); + })); + } + + @Mod.EventHandler + public void init(FMLInitializationEvent event) { + ClientRegistry.registerKeyBinding(perspectiveKey); + ClientCommandHandler.instance.registerCommand(new PerspectiveModCommand()); + MinecraftForge.EVENT_BUS.register(this); + } + + @SubscribeEvent + public void onKeyEvent(InputEvent.KeyInputEvent event) { + if(perspectiveKey.getKeyCode() > 0) { + onPressed(Keyboard.getEventKey(), Keyboard.getEventKeyState()); + } + } + + @SubscribeEvent + public void onMouseEvent(InputEvent.MouseInputEvent event) { + 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 (state) { + perspectiveToggled = !perspectiveToggled; + cameraYaw = mc.thePlayer.rotationYaw; + cameraPitch = mc.thePlayer.rotationPitch; + + if (perspectiveToggled) { + previousPerspective = mc.gameSettings.thirdPersonView; + mc.gameSettings.thirdPersonView = 1; + } else { + mc.gameSettings.thirdPersonView = previousPerspective; + } + } else if (config.holdMode) { + perspectiveToggled = false; + mc.gameSettings.thirdPersonView = previousPerspective; + } + } else if(perspectiveToggled) { + perspectiveToggled = false; + mc.gameSettings.thirdPersonView = previousPerspective; + } + } + } + + public static boolean overrideMouse() { + if (mc.inGameHasFocus && Display.isActive()) + { + if (!perspectiveToggled) + { + return true; + } + + // CODE + mc.mouseHelper.mouseXYChange(); + float f1 = mc.gameSettings.mouseSensitivity * 0.6F + 0.2F; + float f2 = f1 * f1 * f1 * 8.0F; + float f3 = (float) mc.mouseHelper.deltaX * f2; + float f4 = (float) mc.mouseHelper.deltaY * f2; + + cameraYaw += f3 * 0.15F; + cameraPitch += f4 * 0.15F; + + if (cameraPitch > 90) cameraPitch = 90; + if (cameraPitch < -90) cameraPitch = -90; + } + + return false; + } + + public static void loadConfig(File configFile) { + if(configFile.exists()) { + try { + String json = FileUtils.readFileToString(configFile); + config = gson.fromJson(json, PerspectiveModConfig.class); + } catch (Exception e) { + logger.error("Error Loading Config, try deleting the file."); + e.printStackTrace(); + } + } else { + config = new PerspectiveModConfig(); + saveConfig(configFile); + } + } + + public static void saveConfig(File configFile) { + try { + String json = gson.toJson(config); + FileUtils.write(configFile, json); + } 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 new file mode 100644 index 0000000..c19c81c --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/PerspectiveModCommand.java @@ -0,0 +1,88 @@ +package me.djtheredstoner.perspectivemod; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.util.ChatComponentText; + +import java.util.ArrayList; +import java.util.List; + +public class PerspectiveModCommand extends CommandBase { + + private final String PREFIX = "§c[§6Perspective Mod§c] §r"; + + @Override + public String getCommandName() { + return "perspectivemod"; + } + + @Override + public List getCommandAliases() { + List aliases = new ArrayList<>(); + aliases.add("pmod"); + return aliases; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "§6Perspective Mod Help\n" + + "§b/pmod §7- Enables or disables the mod.\n" + + "§b/pmod mode §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) { + String arg = args[0]; + if(arg.equalsIgnoreCase("enable")) { + PerspectiveMod.config.modEnabled = true; + + sendMessage(sender, "§bMod §aEnabled§b."); + } else if(arg.equalsIgnoreCase("disable")) { + PerspectiveMod.config.modEnabled = false; + + sendMessage(sender, "§bMod §cDisabled§b."); + } else if(arg.equalsIgnoreCase("mode")) { + if(args.length == 2) { + String mode = args[1]; + if(mode.equalsIgnoreCase("hold")) { + PerspectiveMod.config.holdMode = true; + + sendMessage(sender, "§bMode set to hold."); + } else if(mode.equalsIgnoreCase("toggle")) { + PerspectiveMod.config.holdMode = false; + + sendMessage(sender, "§bMode set to toggle."); + } else { + sendMessage(sender, "§cInvalid mode. Valid mode are hold and toggle."); + } + } else { + sendMessage(sender, "§cYou must specify a mode. Valid Modes are hold and toggle."); + } + } else { + sendHelp(sender); + } + } else { + sendHelp(sender); + } + } + + @Override + public int getRequiredPermissionLevel() { + return -1; + } + + private void sendHelp(ICommandSender sender) { + sendMessage(sender, getCommandUsage(sender), false); + } + + private void sendMessage(ICommandSender sender, String message, boolean addPrefix) { + sender.addChatMessage(new ChatComponentText((addPrefix ? PREFIX : "") + message)); + } + + private void sendMessage(ICommandSender sender, String message) { + sendMessage(sender, message, true); + } +} diff --git a/src/main/java/me/djtheredstoner/perspectivemod/config/PerspectiveModConfig.java b/src/main/java/me/djtheredstoner/perspectivemod/config/PerspectiveModConfig.java new file mode 100644 index 0000000..d4b1922 --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/config/PerspectiveModConfig.java @@ -0,0 +1,8 @@ +package me.djtheredstoner.perspectivemod.config; + +public class PerspectiveModConfig { + + public boolean modEnabled = true; + public boolean holdMode = true; + +} diff --git a/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java new file mode 100644 index 0000000..7eecf46 --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/forge/PerspectiveModLoadingPlugin.java @@ -0,0 +1,42 @@ +package me.djtheredstoner.perspectivemod.forge; + +import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; +import org.spongepowered.asm.launch.MixinBootstrap; +import org.spongepowered.asm.mixin.Mixins; + +import java.util.Map; + +// ONLY needed in development environment, the mixin tweaker handles this in prod. +public class PerspectiveModLoadingPlugin implements IFMLLoadingPlugin { + + public PerspectiveModLoadingPlugin() { + MixinBootstrap.init(); + + Mixins.addConfiguration("mixins.perspectivemod.json"); + } + + @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 data) { + + } + + @Override + public String getAccessTransformerClass() { + return null; + } +} diff --git a/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinEntityRenderer.java b/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinEntityRenderer.java new file mode 100644 index 0000000..3290aff --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinEntityRenderer.java @@ -0,0 +1,41 @@ +package me.djtheredstoner.perspectivemod.mixins; + +import me.djtheredstoner.perspectivemod.PerspectiveMod; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.entity.Entity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import static org.objectweb.asm.Opcodes.*; + +@Mixin(EntityRenderer.class) +public class MixinEntityRenderer { + + @Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;inGameHasFocus:Z", opcode = GETFIELD)) + public boolean updateCameraAndRender(Minecraft minecraft) { + return PerspectiveMod.overrideMouse(); + } + + @Redirect(method = "orientCamera", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/Entity;rotationYaw:F", opcode = GETFIELD)) + public float getRotationYaw(Entity entity) { + return PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraYaw : entity.rotationYaw; + } + + @Redirect(method = "orientCamera", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/Entity;prevRotationYaw:F", opcode = GETFIELD)) + public float getPrevRotationYaw(Entity entity) { + return PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraYaw : entity.prevRotationYaw; + } + + @Redirect(method = "orientCamera", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/Entity;rotationPitch:F", opcode = GETFIELD)) + public float getRotationPitch(Entity entity) { + return PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraPitch : entity.rotationPitch; + } + + @Redirect(method = "orientCamera", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/Entity;prevRotationPitch:F")) + public float getPrevRotationPitch(Entity entity) { + return PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraPitch : entity.prevRotationPitch; + } + +} diff --git a/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinRenderManager.java b/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinRenderManager.java new file mode 100644 index 0000000..469d7fa --- /dev/null +++ b/src/main/java/me/djtheredstoner/perspectivemod/mixins/MixinRenderManager.java @@ -0,0 +1,24 @@ +package me.djtheredstoner.perspectivemod.mixins; + +import me.djtheredstoner.perspectivemod.PerspectiveMod; +import net.minecraft.client.renderer.entity.RenderManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import static org.objectweb.asm.Opcodes.*; + +@Mixin(RenderManager.class) +public class MixinRenderManager { + + @Redirect(method = "cacheActiveRenderInfo", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/entity/RenderManager;playerViewX:F", opcode = PUTFIELD)) + public void getPlayerViewX(RenderManager renderManager, float value) { + renderManager.playerViewX = PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraPitch : value; + } + + @Redirect(method = "cacheActiveRenderInfo", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/entity/RenderManager;playerViewY:F", opcode = PUTFIELD)) + public void getPlayerViewY(RenderManager renderManager, float value) { + renderManager.playerViewY = PerspectiveMod.perspectiveToggled ? PerspectiveMod.cameraYaw : value; + } + +} diff --git a/src/main/resources/LICENSE b/src/main/resources/LICENSE new file mode 100644 index 0000000..45ae6c4 --- /dev/null +++ b/src/main/resources/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 DJtheRedstoner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100644 index 0000000..5827825 --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,16 @@ +[ +{ + "modid": "djperspectivemod", + "name": "Perspective Mod v4", + "description": "", + "version": "${version}", + "mcversion": "${mcversion}", + "url": "", + "updateUrl": "", + "authorList": ["DJtheRedstoner"], + "credits": "", + "logoFile": "", + "screenshots": [], + "dependencies": [] +} +] diff --git a/src/main/resources/mixins.perspectivemod.json b/src/main/resources/mixins.perspectivemod.json new file mode 100644 index 0000000..f307ca6 --- /dev/null +++ b/src/main/resources/mixins.perspectivemod.json @@ -0,0 +1,12 @@ +{ + "required": true, + "minVersion": "0.7.10", + "package": "me.djtheredstoner.perspectivemod.mixins", + "refmap": "mixins.perspectivemod.refmap.json", + "target": "@env(DEFAULT)", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "MixinEntityRenderer", + "MixinRenderManager" + ] +} \ No newline at end of file -- cgit