From 7a9c91bff6df42d9044712a59b81c5304f7b59be Mon Sep 17 00:00:00 2001 From: MachineMuse Date: Fri, 25 Jan 2013 23:17:16 -0700 Subject: [PATCH] keybind config almost working --- .../machinemuse/general/gui/KeyConfigGui.java | 7 ++ .../gui/clickable/ClickableKeybinding.java | 4 ++ .../general/gui/frame/KeybindConfigFrame.java | 5 ++ .../powersuits/client/KeybindManager.java | 64 +++++++++++++++++++ .../powersuits/common/PowersuitsMod.java | 5 +- 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/minecraft/net/machinemuse/general/gui/KeyConfigGui.java b/src/minecraft/net/machinemuse/general/gui/KeyConfigGui.java index 66e5b26..dbe2818 100644 --- a/src/minecraft/net/machinemuse/general/gui/KeyConfigGui.java +++ b/src/minecraft/net/machinemuse/general/gui/KeyConfigGui.java @@ -2,6 +2,7 @@ package net.machinemuse.general.gui; import net.machinemuse.general.geometry.MusePoint2D; import net.machinemuse.general.gui.frame.KeybindConfigFrame; +import net.machinemuse.powersuits.client.KeybindManager; import net.minecraft.entity.player.EntityPlayer; public class KeyConfigGui extends MuseGui { @@ -38,4 +39,10 @@ public class KeyConfigGui extends MuseGui { super.handleKeyboardInput(); frame.handleKeyboard(); } + + @Override + public void onGuiClosed() { + super.onGuiClosed(); + KeybindManager.writeOutKeybinds(); + } } diff --git a/src/minecraft/net/machinemuse/general/gui/clickable/ClickableKeybinding.java b/src/minecraft/net/machinemuse/general/gui/clickable/ClickableKeybinding.java index 21eac72..9fa47d9 100644 --- a/src/minecraft/net/machinemuse/general/gui/clickable/ClickableKeybinding.java +++ b/src/minecraft/net/machinemuse/general/gui/clickable/ClickableKeybinding.java @@ -79,6 +79,10 @@ public class ClickableKeybinding extends ClickableButton { return keybind; } + public List getBoundModules() { + return boundModules; + } + public void bindModule(ClickableModule module) { boundModules.add(module); } diff --git a/src/minecraft/net/machinemuse/general/gui/frame/KeybindConfigFrame.java b/src/minecraft/net/machinemuse/general/gui/frame/KeybindConfigFrame.java index ce40634..3928850 100644 --- a/src/minecraft/net/machinemuse/general/gui/frame/KeybindConfigFrame.java +++ b/src/minecraft/net/machinemuse/general/gui/frame/KeybindConfigFrame.java @@ -37,6 +37,11 @@ public class KeybindConfigFrame implements IGuiFrame { public KeybindConfigFrame(MuseGui gui, MusePoint2D ul, MusePoint2D br, EntityPlayer player) { modules = new HashSet(); + for (ClickableKeybinding kb : KeybindManager.getKeybindings()) { + for (ClickableModule module : kb.getBoundModules()) { + modules.add(module); + } + } this.gui = gui; this.ul = ul; this.br = br; diff --git a/src/minecraft/net/machinemuse/powersuits/client/KeybindManager.java b/src/minecraft/net/machinemuse/powersuits/client/KeybindManager.java index 3d6bc36..752c879 100644 --- a/src/minecraft/net/machinemuse/powersuits/client/KeybindManager.java +++ b/src/minecraft/net/machinemuse/powersuits/client/KeybindManager.java @@ -1,10 +1,21 @@ package net.machinemuse.powersuits.client; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import net.machinemuse.general.geometry.MusePoint2D; import net.machinemuse.general.gui.clickable.ClickableKeybinding; +import net.machinemuse.general.gui.clickable.ClickableModule; +import net.machinemuse.powersuits.common.MuseLogger; +import net.machinemuse.powersuits.item.ItemUtils; +import net.machinemuse.powersuits.powermodule.ModuleManager; +import net.machinemuse.powersuits.powermodule.PowerModule; +import net.minecraft.client.Minecraft; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -42,4 +53,57 @@ public class KeybindManager { return Keyboard.getKeyName(keybind.keyCode); } } + + public static void writeOutKeybinds() { + try { + File file = new File("config/powersuits-keybinds.cfg"); + if (!file.exists()) { + file.createNewFile(); + } + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + List modulesToWrite = ItemUtils.getPlayerInstalledModules(Minecraft.getMinecraft().thePlayer); + for (ClickableKeybinding keybinding : getInstance().keybindings) { + writer.write(keybinding.getKeyBinding().keyCode + ":" + keybinding.getPosition().x() + ":" + keybinding.getPosition().y() + "\n"); + for (ClickableModule module : keybinding.getBoundModules()) { + writer.write(module.getModule().getName() + "~" + module.getPosition().x() + "~" + module.getPosition().y() + "\n"); + } + } + writer.close(); + } catch (Exception e) { + MuseLogger.logError("Problem writing out keyconfig :("); + e.printStackTrace(); + } + } + + public static void readInKeybinds() { + try { + File file = new File("config/powersuits-keybinds.cfg"); + if (!file.exists()) { + MuseLogger.logError("No powersuits keybind file found."); + return; + } + BufferedReader reader = new BufferedReader(new FileReader(file)); + ClickableKeybinding workingKeybinding = null; + while (reader.ready()) { + String line = reader.readLine(); + MuseLogger.logDebug("Read in line: " + line); + if (line.contains(":")) { + String[] exploded = line.split(":"); + int id = Integer.parseInt(exploded[0]); + MusePoint2D position = new MusePoint2D(Double.parseDouble(exploded[1]), Double.parseDouble(exploded[2])); + workingKeybinding = new ClickableKeybinding(new KeyBinding(Keyboard.getKeyName(id), id), position); + } else if (line.contains("~") && workingKeybinding != null) { + String[] exploded = line.split("~"); + MusePoint2D position = new MusePoint2D(Double.parseDouble(exploded[1]), Double.parseDouble(exploded[2])); + PowerModule module = ModuleManager.getModule(exploded[0]); + ClickableModule cmodule = new ClickableModule(module, position); + workingKeybinding.bindModule(cmodule); + + } + } + } catch (Exception e) { + MuseLogger.logError("Problem reading in keyconfig :("); + e.printStackTrace(); + } + } } diff --git a/src/minecraft/net/machinemuse/powersuits/common/PowersuitsMod.java b/src/minecraft/net/machinemuse/powersuits/common/PowersuitsMod.java index ec69301..af9b064 100644 --- a/src/minecraft/net/machinemuse/powersuits/common/PowersuitsMod.java +++ b/src/minecraft/net/machinemuse/powersuits/common/PowersuitsMod.java @@ -1,6 +1,7 @@ package net.machinemuse.powersuits.common; import net.machinemuse.powersuits.block.BlockTinkerTable; +import net.machinemuse.powersuits.client.KeybindManager; import net.machinemuse.powersuits.event.EventHandler; import net.machinemuse.powersuits.event.MovementManager; import net.machinemuse.powersuits.item.ItemComponent; @@ -119,9 +120,9 @@ public class PowersuitsMod { tinkerTable = new BlockTinkerTable(); components = new ItemComponent(); components.populate(); - Config.loadPowerModules(); + KeybindManager.readInKeybinds(); proxy.registerHandlers(); proxy.registerRenderers(); @@ -140,7 +141,7 @@ public class PowersuitsMod { proxy.postInit(); RecipeManager.addRecipes(); ModCompatability.registerModSpecificModules(); - + Config.getConfig().save(); } } \ No newline at end of file