Skip to content

Commit

Permalink
more detail on votepoint reading failure / add softdepend to MultiWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
kory33 committed Apr 23, 2017
1 parent c8f411c commit 77f6f70
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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";
}
27 changes: 16 additions & 11 deletions src/main/java/com/github/kory33/signvote/model/VotePoint.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 77f6f70

Please sign in to comment.