diff options
Diffstat (limited to 'src/main/java/me')
6 files changed, 348 insertions, 0 deletions
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<String> getCommandAliases() { + List<String> aliases = new ArrayList<>(); + aliases.add("pmod"); + return aliases; + } + + @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."; + } + + @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<String, Object> 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; + } + +} |