valueEntry : map.get(entryValues.getKey()).entrySet()) {
+ valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
+ allSkipped = false;
+ }
+ if (!allSkipped) {
+ reallyAllSkipped = false;
+ valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
+ }
+ }
+ if (reallyAllSkipped) {
+ // Null = skip the chart
+ return null;
+ }
+ return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
+ }
+ }
+
+ /**
+ * An extremely simple JSON builder.
+ *
+ * While this class is neither feature-rich nor the most performant one, it's sufficient enough
+ * for its use-case.
+ */
+ public static class JsonObjectBuilder {
+
+ private StringBuilder builder = new StringBuilder();
+
+ private boolean hasAtLeastOneField = false;
+
+ public JsonObjectBuilder() {
+ builder.append("{");
+ }
+
+ /**
+ * Appends a null field to the JSON.
+ *
+ * @param key The key of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendNull(String key) {
+ appendFieldUnescaped(key, "null");
+ return this;
+ }
+
+ /**
+ * Appends a string field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String value) {
+ if (value == null) {
+ throw new IllegalArgumentException("JSON value must not be null");
+ }
+ appendFieldUnescaped(key, "\"" + escape(value) + "\"");
+ return this;
+ }
+
+ /**
+ * Appends an integer field to the JSON.
+ *
+ * @param key The key of the field.
+ * @param value The value of the field.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int value) {
+ appendFieldUnescaped(key, String.valueOf(value));
+ return this;
+ }
+
+ /**
+ * Appends an object to the JSON.
+ *
+ * @param key The key of the field.
+ * @param object The object.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject object) {
+ if (object == null) {
+ throw new IllegalArgumentException("JSON object must not be null");
+ }
+ appendFieldUnescaped(key, object.toString());
+ return this;
+ }
+
+ /**
+ * Appends a string array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The string array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, String[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values)
+ .map(value -> "\"" + escape(value) + "\"")
+ .collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an integer array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, int[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends an object array to the JSON.
+ *
+ * @param key The key of the field.
+ * @param values The integer array.
+ * @return A reference to this object.
+ */
+ public JsonObjectBuilder appendField(String key, JsonObject[] values) {
+ if (values == null) {
+ throw new IllegalArgumentException("JSON values must not be null");
+ }
+ String escapedValues =
+ Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
+ appendFieldUnescaped(key, "[" + escapedValues + "]");
+ return this;
+ }
+
+ /**
+ * Appends a field to the object.
+ *
+ * @param key The key of the field.
+ * @param escapedValue The escaped value of the field.
+ */
+ private void appendFieldUnescaped(String key, String escapedValue) {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ if (key == null) {
+ throw new IllegalArgumentException("JSON key must not be null");
+ }
+ if (hasAtLeastOneField) {
+ builder.append(",");
+ }
+ builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
+ hasAtLeastOneField = true;
+ }
+
+ /**
+ * Builds the JSON string and invalidates this builder.
+ *
+ * @return The built JSON string.
+ */
+ public JsonObject build() {
+ if (builder == null) {
+ throw new IllegalStateException("JSON has already been built");
+ }
+ JsonObject object = new JsonObject(builder.append("}").toString());
+ builder = null;
+ return object;
+ }
+
+ /**
+ * Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
+ *
+ *
This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
+ * Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
+ *
+ * @param value The value to escape.
+ * @return The escaped value.
+ */
+ private static String escape(String value) {
+ final StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < value.length(); i++) {
+ char c = value.charAt(i);
+ if (c == '"') {
+ builder.append("\\\"");
+ } else if (c == '\\') {
+ builder.append("\\\\");
+ } else if (c <= '\u000F') {
+ builder.append("\\u000").append(Integer.toHexString(c));
+ } else if (c <= '\u001F') {
+ builder.append("\\u00").append(Integer.toHexString(c));
+ } else {
+ builder.append(c);
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * A super simple representation of a JSON object.
+ *
+ *
This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
+ * allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
+ * JsonObject)}.
+ */
+ public static class JsonObject {
+
+ private final String value;
+
+ private JsonObject(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public String toString() {
+ return value;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/olliejw/oremarket/OreMarket.java b/src/main/java/com/olliejw/oremarket/OreMarket.java
new file mode 100644
index 0000000..ec3b0f9
--- /dev/null
+++ b/src/main/java/com/olliejw/oremarket/OreMarket.java
@@ -0,0 +1,93 @@
+package com.olliejw.oremarket;
+
+import com.olliejw.oremarket.Chat.ValueUpdates;
+import com.olliejw.oremarket.Commands.Reload;
+import com.olliejw.oremarket.Listeners.InventoryEvents;
+import com.olliejw.oremarket.Utils.AddItem;
+import net.milkbowl.vault.economy.Economy;
+import org.bukkit.configuration.InvalidConfigurationException;
+import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.plugin.RegisteredServiceProvider;
+import org.bukkit.plugin.java.JavaPlugin;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.logging.Logger;
+
+public final class OreMarket extends JavaPlugin {
+ ValueUpdates valueUpdates = new ValueUpdates();
+ private static OreMarket instance;
+ private static final Logger log = Logger.getLogger("Minecraft");
+ private static Economy econ = null;
+
+ private File guiFile;
+ private FileConfiguration guiConfig;
+
+ public void onEnable() {
+ instance = this;
+ saveDefaultConfig();
+ createGuiConfig();
+
+ final Metrics metrics = new Metrics(this, 10961);
+ metrics.addCustomChart(new Metrics.SimplePie("", () -> ""));
+
+ this.getServer().getPluginManager().registerEvents(new InventoryEvents(), this);
+ Objects.requireNonNull(this.getCommand("openmarket")).setExecutor(new AddItem());
+ Objects.requireNonNull(this.getCommand("om-reload")).setExecutor(new Reload());
+
+ if (!setupEconomy() ) {
+ log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
+ getServer().getPluginManager().disablePlugin(this);
+ }
+
+ valueUpdates.announceValue();
+ }
+
+ public FileConfiguration getGuiConfig() {
+ return this.guiConfig;
+ }
+
+ private void createGuiConfig() {
+ guiFile = new File(getDataFolder(), "gui.yml");
+ if (!guiFile.exists()) {
+ guiFile.getParentFile().mkdirs();
+ saveResource("gui.yml", false);
+ }
+
+ guiConfig = new YamlConfiguration();
+ try {
+ guiConfig.load(guiFile);
+ } catch (IOException | InvalidConfigurationException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void saveGuiConfig() {
+ try {
+ guiConfig.save(guiFile);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static OreMarket main(){
+ return instance;
+ }
+ private boolean setupEconomy() {
+ if (getServer().getPluginManager().getPlugin("Vault") == null) {
+ return false;
+ }
+ RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class);
+ if (rsp == null) {
+ return false;
+ }
+ econ = rsp.getProvider();
+ return true;
+ }
+ public static Economy getEconomy() {
+ return econ;
+ }
+}
+