diff --git a/src/main/java/com/github/kory33/signvote/constants/Formats.java b/src/main/java/com/github/kory33/signvote/constants/Formats.java index ad14a68..473ca84 100644 --- a/src/main/java/com/github/kory33/signvote/constants/Formats.java +++ b/src/main/java/com/github/kory33/signvote/constants/Formats.java @@ -1,6 +1,8 @@ package com.github.kory33.signvote.constants; +import java.nio.charset.Charset; + public class Formats { - public static final String FILE_ENCODING = "utf-8"; + public static final Charset FILE_ENCODING = Charset.forName("utf-8"); public static final String JSON_EXT = ".json"; } diff --git a/src/main/java/com/github/kory33/signvote/model/VotePoint.java b/src/main/java/com/github/kory33/signvote/model/VotePoint.java index 8c8ce28..1de7f5f 100644 --- a/src/main/java/com/github/kory33/signvote/model/VotePoint.java +++ b/src/main/java/com/github/kory33/signvote/model/VotePoint.java @@ -1,5 +1,6 @@ package com.github.kory33.signvote.model; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -9,6 +10,7 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Sign; +import com.github.kory33.signvote.constants.Formats; import com.github.kory33.signvote.constants.VotePointDataFileKeys; import com.github.kory33.signvote.session.VoteSession; import com.google.gson.JsonObject; @@ -24,36 +26,39 @@ public VotePoint(String name, Sign voteSign, VoteSession parentSession) { this.name = name; this.voteSign = voteSign; } - + public VotePoint(File votePointFIle) throws IllegalArgumentException, IOException { - JsonObject jsonObject = (new JsonParser()).parse(Files.newBufferedReader(votePointFIle.toPath())).getAsJsonObject(); - + BufferedReader reader = Files.newBufferedReader(votePointFIle.toPath(), Formats.FILE_ENCODING); + JsonObject jsonObject = (new JsonParser()).parse(reader).getAsJsonObject(); + reader.close(); + this.name = jsonObject.get(VotePointDataFileKeys.NAME).getAsString(); - - World world = Bukkit.getWorld(jsonObject.get(VotePointDataFileKeys.VOTE_SIGN_WORLD).getAsString()); + + String worldName = jsonObject.get(VotePointDataFileKeys.VOTE_SIGN_WORLD).getAsString(); + World world = Bukkit.getWorld(worldName); int signX = jsonObject.get(VotePointDataFileKeys.VOTE_SIGN_LOC_X).getAsInt(); int signY = jsonObject.get(VotePointDataFileKeys.VOTE_SIGN_LOC_Y).getAsInt(); int signZ = jsonObject.get(VotePointDataFileKeys.VOTE_SIGN_LOC_Z).getAsInt(); - + if (world == null) { - throw new IllegalArgumentException("Invalid file given! (world name missing)"); + throw new IllegalArgumentException("Invalid file given! (Invalid world name " + worldName + ")"); } - + BlockState state = world.getBlockAt(signX, signY, signZ).getState(); if (!(state instanceof Sign)) { throw new IllegalStateException("Sign does not exist at the location in the saved data!"); } - + this.voteSign = (Sign) state; } - + /** * Get the json-serialized data of this object. * @return */ public JsonObject toJson() { JsonObject jsonObject = new JsonObject(); - + jsonObject.addProperty(VotePointDataFileKeys.NAME, this.name); jsonObject.addProperty(VotePointDataFileKeys.VOTE_SIGN_WORLD, this.voteSign.getWorld().getName()); jsonObject.addProperty(VotePointDataFileKeys.VOTE_SIGN_LOC_X, this.voteSign.getX()); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 35f9dbc..cb87294 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,7 @@ name: @project.name@ version: @project.version@ main: com.github.kory33.signvote.core.SignVote +softdepend: [MultiWorld] description: A Bukkit plugin that allows player to vote through signs. author: kory33