From 9e0010830208b5c5dad197830361d51f63499ae4 Mon Sep 17 00:00:00 2001 From: Hexeption Date: Mon, 24 Apr 2017 11:42:50 +0100 Subject: [PATCH] :gem: Added Waypoint Saving. :bug: Fixed Notification Alignment. :bug: Fixed Mods from saving. --- .../darkforge/command/commands/Waypoint.java | 1 + .../darkforge/managers/FileManager.java | 71 +++++++++++++++++-- .../darkforge/managers/WaypointManager.java | 6 +- .../uk/co/hexeption/darkforge/mod/Mod.java | 4 -- .../darkforge/ui/hud/themes/DarkForgeHud.java | 2 +- .../darkforge/waypoint/Waypoint.java | 5 ++ 6 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/main/java/uk/co/hexeption/darkforge/command/commands/Waypoint.java b/src/main/java/uk/co/hexeption/darkforge/command/commands/Waypoint.java index fb0afe8..dc4e6da 100644 --- a/src/main/java/uk/co/hexeption/darkforge/command/commands/Waypoint.java +++ b/src/main/java/uk/co/hexeption/darkforge/command/commands/Waypoint.java @@ -77,6 +77,7 @@ public void execute(String input, String[] args) throws Exception { if (args[0].equalsIgnoreCase("clear")) { DarkForge.INSTANCE.addNotification(Notification.Type.INFO, "Waypoint", "Waypoints Cleared", 5000); DarkForge.INSTANCE.waypointManager.getWaypoints().clear(); + DarkForge.INSTANCE.fileManager.saveWaypoints(); } } } diff --git a/src/main/java/uk/co/hexeption/darkforge/managers/FileManager.java b/src/main/java/uk/co/hexeption/darkforge/managers/FileManager.java index 00f4346..ac2a67e 100644 --- a/src/main/java/uk/co/hexeption/darkforge/managers/FileManager.java +++ b/src/main/java/uk/co/hexeption/darkforge/managers/FileManager.java @@ -29,7 +29,9 @@ import uk.co.hexeption.darkforge.value.DoubleValue; import uk.co.hexeption.darkforge.value.FloatValue; import uk.co.hexeption.darkforge.value.Value; +import uk.co.hexeption.darkforge.waypoint.Waypoint; +import javax.vecmath.Vector3d; import java.io.*; import java.util.Map; @@ -44,18 +46,20 @@ public class FileManager { public final File DARKFORGE_DIR = new File(String.format("%s%sdarkforge%s", Minecraft.getMinecraft().mcDataDir, File.separator, File.separator)); - private final File MODULE = new File(DARKFORGE_DIR, "mods.json"); + private final File MODS = new File(DARKFORGE_DIR, "mods.json"); private final File ALTS = new File(DARKFORGE_DIR, "alts.json"); private final File FRIENDS = new File(DARKFORGE_DIR, "friends.json"); + private final File WAYPOINTS = new File(DARKFORGE_DIR, "waypoints.json"); + public void Initialization() { if (!DARKFORGE_DIR.exists()) DARKFORGE_DIR.mkdir(); - if (!MODULE.exists()) + if (!MODS.exists()) saveModules(); else loadModules(); @@ -69,13 +73,19 @@ public void Initialization() { saveFriends(); else loadFriends(); + + if (!WAYPOINTS.exists()) + saveWaypoints(); + else + loadWaypoints(); + } public void loadModules() { try { - BufferedReader loadJson = new BufferedReader(new FileReader(MODULE)); + BufferedReader loadJson = new BufferedReader(new FileReader(MODS)); JsonObject moduleJason = (JsonObject) jsonParser.parse(loadJson); loadJson.close(); @@ -146,7 +156,7 @@ public void saveModules() { json.add(mod.getName(), jsonModules); } - PrintWriter saveJson = new PrintWriter(new FileWriter(MODULE)); + PrintWriter saveJson = new PrintWriter(new FileWriter(MODS)); saveJson.println(gsonPretty.toJson(json)); saveJson.close(); } catch (IOException e) { @@ -241,5 +251,58 @@ public void loadFriends() { } } + public void saveWaypoints() { + + try { + JsonObject jsonObject = new JsonObject(); + for (Waypoint waypoint : DarkForge.INSTANCE.waypointManager.getWaypoints()) { + JsonObject object = new JsonObject(); + if (waypoint.server == null) + object.addProperty("server", "localhost"); + else + object.addProperty("server", waypoint.server); + + object.addProperty("x", waypoint.position.getX()); + object.addProperty("y", waypoint.position.getY()); + object.addProperty("z", waypoint.position.getZ()); + object.addProperty("dimension", waypoint.dimension); + object.addProperty("color", waypoint.color); + jsonObject.add(waypoint.name, object); + } + + PrintWriter savedJson = new PrintWriter(new FileWriter(WAYPOINTS)); + savedJson.println(gsonPretty.toJson(jsonObject)); + savedJson.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void loadWaypoints() { + + try { + BufferedReader loadJson = new BufferedReader(new FileReader(WAYPOINTS)); + JsonObject wayPointJson = (JsonObject) jsonParser.parse(loadJson); + DarkForge.INSTANCE.waypointManager.getWaypoints().clear(); + + for (Map.Entry entry : wayPointJson.entrySet()) { + JsonObject waypoint = entry.getValue().getAsJsonObject(); + + String name = entry.getKey(); + String server = waypoint.get("server").getAsString(); + double x = waypoint.get("x").getAsDouble(); + double y = waypoint.get("y").getAsDouble(); + double z = waypoint.get("z").getAsDouble(); + int dimension = waypoint.get("dimension").getAsInt(); + int color = waypoint.get("color").getAsInt(); + DarkForge.INSTANCE.waypointManager.addWaypoint(new Waypoint(name, new Vector3d(x, y, z), server, dimension, color)); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/src/main/java/uk/co/hexeption/darkforge/managers/WaypointManager.java b/src/main/java/uk/co/hexeption/darkforge/managers/WaypointManager.java index 60a2437..203a25b 100644 --- a/src/main/java/uk/co/hexeption/darkforge/managers/WaypointManager.java +++ b/src/main/java/uk/co/hexeption/darkforge/managers/WaypointManager.java @@ -17,6 +17,7 @@ ******************************************************************************/ package uk.co.hexeption.darkforge.managers; +import uk.co.hexeption.darkforge.DarkForge; import uk.co.hexeption.darkforge.waypoint.Waypoint; import java.util.ArrayList; @@ -33,7 +34,7 @@ public boolean addWaypoint(Waypoint waypoint) { boolean added = waypoints.add(waypoint); if (added) { - //TODO: Save + DarkForge.INSTANCE.fileManager.saveWaypoints(); } return added; @@ -43,7 +44,8 @@ public boolean removeWaypoint(Waypoint waypoint) { boolean removed = waypoints.remove(waypoint); if (removed) { - //TODO:Save + DarkForge.INSTANCE.fileManager.saveWaypoints(); + } return removed; diff --git a/src/main/java/uk/co/hexeption/darkforge/mod/Mod.java b/src/main/java/uk/co/hexeption/darkforge/mod/Mod.java index d60ac0d..79f6fe8 100644 --- a/src/main/java/uk/co/hexeption/darkforge/mod/Mod.java +++ b/src/main/java/uk/co/hexeption/darkforge/mod/Mod.java @@ -138,10 +138,6 @@ public void setState(boolean state) { onDisable(); } - //TODO: File save - - DarkForge.INSTANCE.fileManager.saveModules(); - } public boolean isVisable() { diff --git a/src/main/java/uk/co/hexeption/darkforge/ui/hud/themes/DarkForgeHud.java b/src/main/java/uk/co/hexeption/darkforge/ui/hud/themes/DarkForgeHud.java index 09e300f..ed68e09 100644 --- a/src/main/java/uk/co/hexeption/darkforge/ui/hud/themes/DarkForgeHud.java +++ b/src/main/java/uk/co/hexeption/darkforge/ui/hud/themes/DarkForgeHud.java @@ -110,7 +110,7 @@ private void drawNotifications(ScaledResolution scaledResolution) { break; } - DarkForge.INSTANCE.fontManager.arraylist.drawStringWithShadow(notification.getMessage(), scaledResolution.getScaledWidth() - DarkForge.INSTANCE.fontManager.arraylist.getStringWidth(notification.getMessage()) - 10, scaledResolution.getScaledHeight() - 20 - ycount, Color.white.hashCode()); + DarkForge.INSTANCE.fontManager.arraylist.drawStringWithShadow(notification.getMessage(), scaledResolution.getScaledWidth() - DarkForge.INSTANCE.fontManager.arraylist.getStringWidth(notification.getMessage()) - 10, scaledResolution.getScaledHeight() - 18 - ycount, Color.white.hashCode()); } ycount += 15; } diff --git a/src/main/java/uk/co/hexeption/darkforge/waypoint/Waypoint.java b/src/main/java/uk/co/hexeption/darkforge/waypoint/Waypoint.java index dd431e0..ea6efa2 100644 --- a/src/main/java/uk/co/hexeption/darkforge/waypoint/Waypoint.java +++ b/src/main/java/uk/co/hexeption/darkforge/waypoint/Waypoint.java @@ -45,6 +45,11 @@ public Waypoint(String name, Vector3d position, ServerData server, int dimension setName(name).setPosition(position).setServer(server).setDimension(dimension).setColor(color); } + public Waypoint(String name, Vector3d position, String server, int dimension, int color) { + + setName(name).setPosition(position).setServer(server).setDimension(dimension).setColor(color); + } + public Waypoint(String name, Vector3d position, ServerData server) { this(name, position, server, mc.player.dimension, GLUtils.getRandomColor().hashCode());