keybind config almost working

This commit is contained in:
MachineMuse 2013-01-25 23:17:16 -07:00
parent 910b5d19ae
commit 7a9c91bff6
5 changed files with 83 additions and 2 deletions

View file

@ -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();
}
}

View file

@ -79,6 +79,10 @@ public class ClickableKeybinding extends ClickableButton {
return keybind;
}
public List<ClickableModule> getBoundModules() {
return boundModules;
}
public void bindModule(ClickableModule module) {
boundModules.add(module);
}

View file

@ -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;

View file

@ -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<PowerModule> 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();
}
}
}

View file

@ -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();
}
}