From 446d5f6149b2022989089ef9ce40704cdf57abc6 Mon Sep 17 00:00:00 2001 From: Xanadu13 Date: Thu, 10 Aug 2023 21:46:25 +0800 Subject: [PATCH] update to v1.1.0 --- .../xanadu/slimefunrecipe/Main.java | 95 +- .../slimefunrecipe/SlimeRecipeType.java | 109 +- .../slimefunrecipe/commands/MainCommand.java | 85 + .../slimefunrecipe/commands/TabCompleter.java | 62 +- .../xanadu/slimefunrecipe/config/Config.java | 74 +- .../xanadu/slimefunrecipe/config/Lang.java | 258 +-- .../pers/xanadu/slimefunrecipe/gui/GUI.java | 83 + .../xanadu/slimefunrecipe/gui/GUI1x4.java | 58 + .../xanadu/slimefunrecipe/gui/GUI3x3.java | 58 + .../xanadu/slimefunrecipe/gui/GUI6x6.java | 58 + .../xanadu/slimefunrecipe/gui/GUISize.java | 19 + .../slimefunrecipe/manager/GuiManager.java | 230 +++ .../slimefunrecipe/manager/ItemManager.java | 132 ++ .../slimefunrecipe/metrics/Metrics.java | 1696 ++++++++--------- .../xanadu/slimefunrecipe/utils/GuiUtils.java | 52 +- .../slimefunrecipe/utils/VersionUpdater.java | 82 +- src/main/java/xanadu/slimefunrecipe/GUI.java | 73 - .../slimefunrecipe/commands/MainCommand.java | 75 - .../slimefunrecipe/manager/GuiManager.java | 90 - .../slimefunrecipe/manager/ItemManager.java | 89 - src/main/resources/config.yml | 2 +- src/main/resources/lang/Chinese.yml | 5 +- src/main/resources/lang/English.yml | 5 +- src/main/resources/plugin.yml | 19 +- 24 files changed, 1972 insertions(+), 1537 deletions(-) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/Main.java (53%) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/SlimeRecipeType.java (72%) create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/commands/MainCommand.java rename src/main/java/{ => pers}/xanadu/slimefunrecipe/commands/TabCompleter.java (62%) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/config/Config.java (84%) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/config/Lang.java (96%) create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/gui/GUI.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/gui/GUI1x4.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/gui/GUI3x3.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/gui/GUI6x6.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/gui/GUISize.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/manager/GuiManager.java create mode 100644 src/main/java/pers/xanadu/slimefunrecipe/manager/ItemManager.java rename src/main/java/{ => pers}/xanadu/slimefunrecipe/metrics/Metrics.java (97%) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/utils/GuiUtils.java (93%) rename src/main/java/{ => pers}/xanadu/slimefunrecipe/utils/VersionUpdater.java (64%) delete mode 100644 src/main/java/xanadu/slimefunrecipe/GUI.java delete mode 100644 src/main/java/xanadu/slimefunrecipe/commands/MainCommand.java delete mode 100644 src/main/java/xanadu/slimefunrecipe/manager/GuiManager.java delete mode 100644 src/main/java/xanadu/slimefunrecipe/manager/ItemManager.java diff --git a/src/main/java/xanadu/slimefunrecipe/Main.java b/src/main/java/pers/xanadu/slimefunrecipe/Main.java similarity index 53% rename from src/main/java/xanadu/slimefunrecipe/Main.java rename to src/main/java/pers/xanadu/slimefunrecipe/Main.java index e528425..f7f7a09 100644 --- a/src/main/java/xanadu/slimefunrecipe/Main.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/Main.java @@ -1,4 +1,4 @@ -package xanadu.slimefunrecipe; +package pers.xanadu.slimefunrecipe; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; @@ -8,21 +8,19 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; -import xanadu.slimefunrecipe.commands.MainCommand; -import xanadu.slimefunrecipe.commands.TabCompleter; -import xanadu.slimefunrecipe.config.Config; -import xanadu.slimefunrecipe.config.Lang; -import xanadu.slimefunrecipe.metrics.Metrics; +import pers.xanadu.slimefunrecipe.commands.MainCommand; +import pers.xanadu.slimefunrecipe.commands.TabCompleter; +import pers.xanadu.slimefunrecipe.config.Config; +import pers.xanadu.slimefunrecipe.config.Lang; +import pers.xanadu.slimefunrecipe.metrics.Metrics; import java.io.File; import java.util.*; -import static xanadu.slimefunrecipe.config.Config.enable; -import static xanadu.slimefunrecipe.config.Lang.error; -import static xanadu.slimefunrecipe.config.Lang.info; -import static xanadu.slimefunrecipe.manager.ItemManager.*; -import static xanadu.slimefunrecipe.utils.VersionUpdater.checkUpdate; +import static pers.xanadu.slimefunrecipe.config.Config.enable; +import static pers.xanadu.slimefunrecipe.config.Lang.*; +import static pers.xanadu.slimefunrecipe.manager.ItemManager.*; +import static pers.xanadu.slimefunrecipe.utils.VersionUpdater.checkUpdate; public final class Main extends JavaPlugin { private static Main instance; @@ -41,6 +39,12 @@ public void onEnable() { new Metrics(this,18362); reloadAll(); checkUpdate(); + if(!"1.1.0".equals(Lang.version)){ + warn(Lang.plugin_wrong_file_version.replace("{file_name}",Config.lang + ".yml")); + } + if(!"1.0.0".equals(Config.version)){ + warn(Lang.plugin_wrong_file_version.replace("{file_name}","config.yml")); + } } public static void reloadAll(){ getInstance().loadFiles(); @@ -52,42 +56,39 @@ private void registerCommands(){ } private void loadRecipes(){ initRecipeType(); - new BukkitRunnable(){ - @Override - public void run(){ - int cnt=0; - for (String key : data.getKeys(false)) { - ConfigurationSection section = (ConfigurationSection) data.get(key); - if(section == null){ - error(Lang.plugin_data_parsing_error+key); - return; - } - RecipeType type = getByName(section.getString("RecipeType")); - if(type == null) { - error(Lang.item_unknown_recipe_type.replace("%item%",key)); - continue; - } - SlimefunItem si = SlimefunItem.getById(key); - if(si == null){ - error(Lang.item_not_slimefun.replaceAll("%item%",key)); - continue; - } - ConfigurationSection section1 = section.getConfigurationSection("data"); - if(section1 == null){ - error(Lang.plugin_data_parsing_error+key); - return; - } - ItemStack[] recipe = new ItemStack[9]; - for(int i=0;i<9;i++){ - recipe[i] = readAsItem(section1, String.valueOf(i+1)); - } - si.setRecipeType(type); - si.setRecipe(recipe); - cnt++; - } - info(Lang.plugin_recipes_loaded.replace("%d",String.valueOf(cnt))); + + int cnt=0; + for (String key : data.getKeys(false)) { + ConfigurationSection section = (ConfigurationSection) data.get(key); + if(section == null){ + error(Lang.plugin_data_parsing_error+key); + return; + } + RecipeType type = getByName(section.getString("RecipeType")); + if(type == null) { + error(Lang.item_unknown_recipe_type.replace("%item%",key)); + continue; } - }.runTaskAsynchronously(this); + SlimefunItem si = SlimefunItem.getById(key); + if(si == null){ + error(Lang.item_not_slimefun.replaceAll("%item%",key)); + continue; + } + ConfigurationSection section1 = section.getConfigurationSection("data"); + if(section1 == null){ + error(Lang.plugin_data_parsing_error+key); + return; + } + int size = section.getInt("size",9); + ItemStack[] recipe = new ItemStack[size]; + for(int i=0;i mp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - public static SlimeRecipeType getByName(String str){ - if(str == null) return _unknown; - str=str.toLowerCase(); - if("null".equals(str)) return NULL; - return mp.getOrDefault(str,_unknown); - } - static{ - SlimeRecipeType[] values = values(); - for (SlimeRecipeType recipeType : values) { - String name = recipeType.name(); - mp.put(name, recipeType); - } - } -} +package pers.xanadu.slimefunrecipe; + +import java.util.Map; +import java.util.TreeMap; + +public enum SlimeRecipeType { + ancient_altar, + armor_forge, + barter_drop, + compressor, + enhanced_crafting_table, + food_composter, + food_fabricator, + freezer, + geo_miner, + gold_pan, + grind_stone, + heated_pressure_chamber, + interact, + juicer, + magic_workbench, + mob_drop, + multiblock, + nuclear_reactor, + NULL, + ore_crusher, + ore_washer, + pressure_chamber, + refinery, + smeltery, + //ExoticGarden starts + kitchen, + breaking_grass, + harvest_tree, + harvest_bush, + //InfinityExpansion starts + infinity_forge, + mob_data_infuser, + storage_forge, + //TranscEndence starts + //useless: quirp_oscillator, + quirp_annihilator, + stabilizer, + nanobot_crafter, + zot_overloader, + //FlowerPower starts + magic_basin, + _unknown; + public static final Map mp = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + public static SlimeRecipeType getByName(String str){ + if(str == null) return _unknown; + str=str.toLowerCase(); + if("null".equals(str)) return NULL; + return mp.getOrDefault(str,_unknown); + } + static{ + SlimeRecipeType[] values = values(); + for (SlimeRecipeType recipeType : values) { + String name = recipeType.name(); + mp.put(name, recipeType); + } + } +} diff --git a/src/main/java/pers/xanadu/slimefunrecipe/commands/MainCommand.java b/src/main/java/pers/xanadu/slimefunrecipe/commands/MainCommand.java new file mode 100644 index 0000000..f6020e9 --- /dev/null +++ b/src/main/java/pers/xanadu/slimefunrecipe/commands/MainCommand.java @@ -0,0 +1,85 @@ +package pers.xanadu.slimefunrecipe.commands; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import pers.xanadu.slimefunrecipe.config.Lang; +import pers.xanadu.slimefunrecipe.gui.GUISize; + +import static pers.xanadu.slimefunrecipe.Main.langF; +import static pers.xanadu.slimefunrecipe.Main.reloadAll; +import static pers.xanadu.slimefunrecipe.config.Config.enable; +import static pers.xanadu.slimefunrecipe.config.Lang.*; +import static pers.xanadu.slimefunrecipe.manager.GuiManager.openSizedSlimeInv; +import static pers.xanadu.slimefunrecipe.manager.GuiManager.openSlimeInv; +import static pers.xanadu.slimefunrecipe.manager.ItemManager.isEmpty; + +public class MainCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command cmd, String name, String[] args) { + if(args.length == 0){ + sendCommandTips(sender); + return false; + } + if(!sender.hasPermission("slimefunrecipe.admin")){ + sendFeedback(sender, command_no_permission); + return false; + } + switch (args[0].toLowerCase()){ + case "reload" : { + if(args.length != 1){ + sendCommandTips(sender); + return false; + } + reloadAll(); + sendFeedback(sender,Lang.command_reload_config); + return true; + } + case "edit" : { + if(!enable){ + sendFeedback(sender,plugin_fun_not_enable); + return false; + } + if (!(sender instanceof Player)) { + sendFeedback(sender, Lang.command_only_player); + return false; + } + Player p = (Player) sender; + ItemStack item = p.getItemInHand(); + if(isEmpty(item)){ + sendFeedback(sender, command_item_error); + return false; + } + SlimefunItem si = SlimefunItem.getByItem(item); + if(si==null) { + sendFeedback(sender, command_item_error); + return false; + } + if(args.length == 1){ + openSlimeInv(p, si); + return true; + } + else if(args.length == 2){ + openSizedSlimeInv(p,si, GUISize.of(args[1])); + return true; + } + else{ + sendCommandTips(sender); + return false; + } + } + default : { + sendCommandTips(sender); + return false; + } + } + } + private static void sendCommandTips(CommandSender sender){ + for(String str : CommandTips){ + sender.sendMessage(str); + } + } +} diff --git a/src/main/java/xanadu/slimefunrecipe/commands/TabCompleter.java b/src/main/java/pers/xanadu/slimefunrecipe/commands/TabCompleter.java similarity index 62% rename from src/main/java/xanadu/slimefunrecipe/commands/TabCompleter.java rename to src/main/java/pers/xanadu/slimefunrecipe/commands/TabCompleter.java index ed8b29a..e98e134 100644 --- a/src/main/java/xanadu/slimefunrecipe/commands/TabCompleter.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/commands/TabCompleter.java @@ -1,26 +1,36 @@ -package xanadu.slimefunrecipe.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class TabCompleter implements org.bukkit.command.TabCompleter { - private static final List arguments_1 = Arrays.asList("reload","edit"); - @Override - public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { - - List result = new ArrayList<>(); - if (args.length == 1) { - for (String s : arguments_1) { - if (s.toLowerCase().startsWith(args[0].toLowerCase())) { - result.add(s); - } - } - return result; - } - return null; - } -} +package pers.xanadu.slimefunrecipe.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class TabCompleter implements org.bukkit.command.TabCompleter { + private static final List arguments_1 = Arrays.asList("reload","edit"); + private static final List arguments_2 = Arrays.asList("1x4","3x3","6x6"); + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + List result = new ArrayList<>(); + if (args.length == 1) { + for (String s : arguments_1) { + if (s.toLowerCase().startsWith(args[0].toLowerCase())) { + result.add(s); + } + } + return result; + } + else if(args.length==2){ + if (args[0].equalsIgnoreCase("edit")) { + for(String s : arguments_2){ + if(s.toLowerCase().startsWith(args[1].toLowerCase())){ + result.add(s); + } + } + } + return result; + } + return null; + } +} diff --git a/src/main/java/xanadu/slimefunrecipe/config/Config.java b/src/main/java/pers/xanadu/slimefunrecipe/config/Config.java similarity index 84% rename from src/main/java/xanadu/slimefunrecipe/config/Config.java rename to src/main/java/pers/xanadu/slimefunrecipe/config/Config.java index 96b66b6..5bd8663 100644 --- a/src/main/java/xanadu/slimefunrecipe/config/Config.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/config/Config.java @@ -1,38 +1,36 @@ -package xanadu.slimefunrecipe.config; - -import org.bukkit.configuration.file.FileConfiguration; - -import java.lang.reflect.Field; -import java.lang.reflect.Type; -import java.util.Iterator; - -import static xanadu.slimefunrecipe.config.Lang.error; - -public class Config { - public static String version; - public static String lang; - public static boolean enable; - - public static void reload(FileConfiguration file){ - Field[] fields = Config.class.getFields(); - for(Field field : fields){ - Type type = field.getType(); - if(type.equals(java.util.List.class) || type.equals(java.lang.String.class)){ - try{ - field.set(null,""); - }catch (Exception ignored){} - } - } - Iterator it = file.getKeys(true).iterator(); - while (it.hasNext()){ - String str = it.next(); - try{ - if(file.isConfigurationSection(str)) continue; - Config.class.getField(str.replace(".","_")).set(null, file.get(str)); - }catch (Exception e){ - error("Config loading error! Key: "+str); - } - } - - } -} +package pers.xanadu.slimefunrecipe.config; + +import org.bukkit.configuration.file.FileConfiguration; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.util.Iterator; + +public class Config { + public static String version; + public static String lang; + public static boolean enable; + + public static void reload(FileConfiguration file){ + Field[] fields = Config.class.getFields(); + for(Field field : fields){ + Type type = field.getType(); + if(type.equals(java.util.List.class) || type.equals(java.lang.String.class)){ + try{ + field.set(null,""); + }catch (Exception ignored){} + } + } + Iterator it = file.getKeys(true).iterator(); + while (it.hasNext()){ + String str = it.next(); + try{ + if(file.isConfigurationSection(str)) continue; + Config.class.getField(str.replace(".","_")).set(null, file.get(str)); + }catch (Exception e){ + Lang.error("Config loading error! Key: "+str); + } + } + + } +} diff --git a/src/main/java/xanadu/slimefunrecipe/config/Lang.java b/src/main/java/pers/xanadu/slimefunrecipe/config/Lang.java similarity index 96% rename from src/main/java/xanadu/slimefunrecipe/config/Lang.java rename to src/main/java/pers/xanadu/slimefunrecipe/config/Lang.java index 6de919d..0f57243 100644 --- a/src/main/java/xanadu/slimefunrecipe/config/Lang.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/config/Lang.java @@ -1,128 +1,130 @@ -package xanadu.slimefunrecipe.config; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; - -import java.lang.reflect.Field; -import java.util.Iterator; -import java.util.List; - -public class Lang { - public static String version; - public static String plugin_prefix; - public static String plugin_file_save_error; - public static String plugin_fun_not_enable; - public static String plugin_data_parsing_error; - public static String plugin_recipes_loaded; - public static String plugin_checking_update; - public static String plugin_check_update_fail; - public static String plugin_out_of_date; - public static String plugin_up_to_date; - public static String command_no_permission; - public static String command_reload_config; - public static String command_only_player; - public static String command_item_error; - public static String gui_title; - public static String gui_button_verify; - public static String gui_button_cancel; - public static String gui_recipe_cancel; - public static String gui_recipe_saved; - public static String gui_RecipeType_null_name; - public static String gui_RecipeType_null_lore; - public static String item_not_slimefun; - public static String item_unknown_recipe_type; - public static List CommandTips; - public static void reload(FileConfiguration file){ - Field[] fields = Lang.class.getFields(); - for(Field field : fields){ - try{ - field.set(null,""); - }catch (Exception ignored){} - } - Iterator it = file.getKeys(true).iterator(); - while (it.hasNext()){ - String str = it.next(); - try{ - if(file.isConfigurationSection(str)) continue; - Lang.class.getField(str.replace(".","_")).set(null, file.get(str)); - }catch (Exception e){ - error("Language loading error! Key: "+str); - } - } -// while (it.hasNext()){ -// String str = it.next(); -// try{ -// if(!file.isString(str)) continue; -// Lang.class.getField(str.replace(".","_")).set(null,file.getString(str)); -// }catch (Exception e){ -// error("Language loading error! Key: "+str); -// } -// } - } - public static void sendMessage(String str) { - if(str == null) { - Bukkit.getConsoleSender().sendMessage(Lang.plugin_prefix + "null"); - return; - } - if(str.contains("\\n")){ - String[] strings = str.split("\\\\n"); - for(String s : strings){ - sendMessage(s); - } - } - else Bukkit.getConsoleSender().sendMessage(Lang.plugin_prefix + str); - } - public static void info(String str){ - if(str == null) { - Bukkit.getConsoleSender().sendMessage("§a[SlimefunRecipe] " + "null"); - return; - } - if(str.contains("\\n")){ - String[] strings = str.split("\\\\n"); - for(String s : strings){ - info(s); - } - } - else Bukkit.getConsoleSender().sendMessage("§a[SlimefunRecipe] "+str); - } - public static void warn(String str){ - if(str == null) { - Bukkit.getConsoleSender().sendMessage("§e[SlimefunRecipe] " + "null"); - return; - } - if(str.contains("\\n")){ - String[] strings = str.split("\\\\n"); - for(String s : strings){ - warn(s); - } - } - else Bukkit.getConsoleSender().sendMessage("§e[SlimefunRecipe] "+str); - } - public static void error(String str){ - if(str == null) { - Bukkit.getConsoleSender().sendMessage("§c[SlimefunRecipe] " + "null"); - return; - } - if(str.contains("\\n")){ - String[] strings = str.split("\\\\n"); - for(String s : strings){ - error(s); - } - } - else Bukkit.getConsoleSender().sendMessage("§c[SlimefunRecipe] "+str); - } - public static void sendFeedback(CommandSender sender, String str){ - if(str == null) { - sender.sendMessage(Lang.plugin_prefix + "null"); - return; - } - if(str.contains("\\n")){ - String[] strings = str.split("\\\\n"); - for(String s : strings){ - sender.sendMessage(Lang.plugin_prefix + s); - } - } - else sender.sendMessage(Lang.plugin_prefix + str); - } -} +package pers.xanadu.slimefunrecipe.config; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; + +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.List; + +public class Lang { + public static String version; + public static String plugin_prefix; + public static String plugin_file_save_error; + public static String plugin_fun_not_enable; + public static String plugin_data_parsing_error; + public static String plugin_recipes_loaded; + public static String plugin_checking_update; + public static String plugin_check_update_fail; + public static String plugin_out_of_date; + public static String plugin_up_to_date; + public static String plugin_wrong_file_version; + public static String command_no_permission; + public static String command_reload_config; + public static String command_only_player; + public static String command_item_error; + public static String command_unknown_item_size; + public static String gui_title; + public static String gui_button_verify; + public static String gui_button_cancel; + public static String gui_recipe_cancel; + public static String gui_recipe_saved; + public static String gui_RecipeType_null_name; + public static String gui_RecipeType_null_lore; + public static String item_not_slimefun; + public static String item_unknown_recipe_type; + public static List CommandTips; + public static void reload(FileConfiguration file){ + Field[] fields = Lang.class.getFields(); + for(Field field : fields){ + try{ + field.set(null,""); + }catch (Exception ignored){} + } + Iterator it = file.getKeys(true).iterator(); + while (it.hasNext()){ + String str = it.next(); + try{ + if(file.isConfigurationSection(str)) continue; + Lang.class.getField(str.replace(".","_")).set(null, file.get(str)); + }catch (Exception e){ + error("Language loading error! Key: "+str); + } + } +// while (it.hasNext()){ +// String str = it.next(); +// try{ +// if(!file.isString(str)) continue; +// Lang.class.getField(str.replace(".","_")).set(null,file.getString(str)); +// }catch (Exception e){ +// error("Language loading error! Key: "+str); +// } +// } + } + public static void sendMessage(String str) { + if(str == null) { + Bukkit.getConsoleSender().sendMessage(Lang.plugin_prefix + "null"); + return; + } + if(str.contains("\\n")){ + String[] strings = str.split("\\\\n"); + for(String s : strings){ + sendMessage(s); + } + } + else Bukkit.getConsoleSender().sendMessage(Lang.plugin_prefix + str); + } + public static void info(String str){ + if(str == null) { + Bukkit.getConsoleSender().sendMessage("§a[SlimefunRecipe] " + "null"); + return; + } + if(str.contains("\\n")){ + String[] strings = str.split("\\\\n"); + for(String s : strings){ + info(s); + } + } + else Bukkit.getConsoleSender().sendMessage("§a[SlimefunRecipe] "+str); + } + public static void warn(String str){ + if(str == null) { + Bukkit.getConsoleSender().sendMessage("§e[SlimefunRecipe] " + "null"); + return; + } + if(str.contains("\\n")){ + String[] strings = str.split("\\\\n"); + for(String s : strings){ + warn(s); + } + } + else Bukkit.getConsoleSender().sendMessage("§e[SlimefunRecipe] "+str); + } + public static void error(String str){ + if(str == null) { + Bukkit.getConsoleSender().sendMessage("§c[SlimefunRecipe] " + "null"); + return; + } + if(str.contains("\\n")){ + String[] strings = str.split("\\\\n"); + for(String s : strings){ + error(s); + } + } + else Bukkit.getConsoleSender().sendMessage("§c[SlimefunRecipe] "+str); + } + public static void sendFeedback(CommandSender sender, String str){ + if(str == null) { + sender.sendMessage(Lang.plugin_prefix + "null"); + return; + } + if(str.contains("\\n")){ + String[] strings = str.split("\\\\n"); + for(String s : strings){ + sender.sendMessage(Lang.plugin_prefix + s); + } + } + else sender.sendMessage(Lang.plugin_prefix + str); + } +} diff --git a/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI.java b/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI.java new file mode 100644 index 0000000..e718da1 --- /dev/null +++ b/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI.java @@ -0,0 +1,83 @@ +package pers.xanadu.slimefunrecipe.gui; + +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import pers.xanadu.slimefunrecipe.config.Lang; +import pers.xanadu.slimefunrecipe.manager.ItemManager; +import pers.xanadu.slimefunrecipe.utils.GuiUtils; + +import java.util.Vector; + +public abstract class GUI extends ChestMenu { + protected static final Vector items = new Vector<>(); + protected int index; + protected int chosen; + protected int capacity; + protected GUI(String title) { + super(title); + index=1; + chosen=-1; + capacity=0; + initVector(); + } + public void initVector(){ + items.clear(); + for(RecipeType recipeType : ItemManager.recipeTypes){ + ItemStack item = recipeType.toItem(); + if(item == null) items.add(new CustomItemStack(Material.BARRIER, Lang.gui_RecipeType_null_name,Lang.gui_RecipeType_null_lore)); + else items.add(item); + } + } + public int getPage(){ + return index; + } + public int getCapacity(){ + return capacity; + } + public void setPage(final Player p, int to){ + if(to<1||to>getSize()) return; + index=to; + fillPagedItems(to); + updateButton(p,to); + } + public void prev(final Player p){ + if(index==1) return; + --index; + fillPagedItems(index); + updateButton(p,index); + } + public void next(final Player p){ + if(index==getSize()) return; + ++index; + fillPagedItems(index); + updateButton(p,index); + } + public int getChosen(){ + return chosen; + } + public boolean setChosen(int slot){ + return false; + } + protected void updateChosen(){ + + } + protected void fillPagedItems(int page){ + + } + protected void updateButton(final Player p, int page){ + this.replaceExistingItem(getPrevPageButtonIndex(), ChestMenuUtils.getPreviousButton(p,page,getSize())); + this.replaceExistingItem(getNextPageButtonIndex(), ChestMenuUtils.getNextButton(p,page,getSize())); + } + public abstract int getSize(); + public abstract int getCancelButtonIndex(); + public abstract int getVerifyButtonIndex(); + public abstract int getMachineIndex(); + public abstract int getItemShowIndex(); + public abstract int getPrevPageButtonIndex(); + public abstract int getNextPageButtonIndex(); +} diff --git a/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI1x4.java b/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI1x4.java new file mode 100644 index 0000000..d59d62b --- /dev/null +++ b/src/main/java/pers/xanadu/slimefunrecipe/gui/GUI1x4.java @@ -0,0 +1,58 @@ +package pers.xanadu.slimefunrecipe.gui; + +import pers.xanadu.slimefunrecipe.utils.GuiUtils; + +public class GUI1x4 extends GUI{ + public GUI1x4(String title) { + super(title); + capacity = 4; + } + @Override + public int getChosen(){ + return chosen; + } + @Override + public boolean setChosen(int slot){ + int j=(index-1)*9+slot-36; + if(j>=items.size()) return false; + chosen=j; + updateChosen(); + return true; + } + @Override + protected void updateChosen(){ + for(int i=0;i<9;i++){ + int j=(index-1)*9+i; + this.replaceExistingItem(i+45,j==chosen? GuiUtils.getItem_chosen() : GuiUtils.getItem_not_chosen()); + } + } + @Override + protected void fillPagedItems(int page){ + for(int i=0;i<9;i++){ + int j=(page-1)*9+i; + this.replaceExistingItem(i+36,j=items.size()) return false; + chosen=j; + updateChosen(); + return true; + } + @Override + protected void updateChosen(){ + for(int i=0;i<9;i++){ + int j=(index-1)*9+i; + this.replaceExistingItem(i+45,j==chosen? GuiUtils.getItem_chosen() : GuiUtils.getItem_not_chosen()); + } + } + @Override + protected void fillPagedItems(int page){ + for(int i=0;i<9;i++){ + int j=(page-1)*9+i; + this.replaceExistingItem(i+36,j=items.size()) return false; + chosen=j; + updateChosen(); + return true; + } + @Override + protected void updateChosen(){ + for(int i=0;i<6;i++){ + int j=(index-1)*6+i; + this.replaceExistingItem(i*9+6,j==chosen? GuiUtils.getItem_chosen() : GuiUtils.getItem_not_chosen()); + } + } + @Override + protected void fillPagedItems(int page){ + for(int i=0;i<6;i++){ + int j=(page-1)*6+i; + this.replaceExistingItem(i*9+7,j { + player.closeInventory(); + sendFeedback(player, gui_recipe_cancel); + return false; + }); + menu.replaceExistingItem(menu.getVerifyButtonIndex(), new CustomItemStack(Material.GREEN_STAINED_GLASS_PANE, gui_button_verify)); + menu.addMenuClickHandler(menu.getPrevPageButtonIndex(),(player,slot,item,action)->{ + menu.prev(player); + return false; + }); + menu.addMenuClickHandler(menu.getNextPageButtonIndex(),(player,slot,item,action)->{ + menu.next(player); + return false; + }); + } + public static void handle_3x3(final GUI menu,final SlimefunItem si){ + for (int i : black_border_3x3) { + menu.addItem(i, GuiUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); + } + for (int i : gray_border_3x3) { + menu.addItem(i, GuiUtils.getGray_background(), ChestMenuUtils.getEmptyClickHandler()); + } + for(int i=45;i<54;i++){ + menu.addItem(i,GuiUtils.getItem_not_chosen(),ChestMenuUtils.getEmptyClickHandler()); + } + menu.addMenuClickHandler(menu.getVerifyButtonIndex(), (player, slot, item1, action) -> { + ItemStack[] recipe = new ItemStack[9]; + for(int i=0;i<9;i++){ + ItemStack item = menu.getItemInSlot(item_slot_3x3[i]); + if (isEmpty(item)) recipe[i] = null; + else recipe[i] = item; + } + int chosen = menu.getChosen(); + if(chosen!=-1) si.setRecipeType(ItemManager.recipeTypes.get(chosen)); + si.setRecipe(recipe); + if(!saveRecipe(recipe,si)){ + error(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + player.sendMessage(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + } + else sendFeedback(player, gui_recipe_saved); + player.closeInventory(); + return false; + }); + for(int i=36;i<45;i++){ + menu.addMenuClickHandler(i,(player,slot,item,action)->{ + if(menu.setChosen(slot)) menu.replaceExistingItem(menu.getMachineIndex(),item); + return false; + }); + } + ItemStack[] recipe = si.getRecipe(); + int size = Math.min(recipe.length,9); + menu.addMenuOpeningHandler(player -> { + for(int i=0;i { + ItemStack[] recipe = new ItemStack[4]; + for(int i=0;i<4;i++){ + ItemStack item = menu.getItemInSlot(item_slot_1x4[i]); + if (isEmpty(item)) recipe[i] = null; + else recipe[i] = item; + } + int chosen = menu.getChosen(); + if(chosen!=-1) si.setRecipeType(ItemManager.recipeTypes.get(chosen)); + setRecipe(si,recipe); + if(!saveRecipe(recipe,si)){ + error(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + player.sendMessage(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + } + else sendFeedback(player, gui_recipe_saved); + player.closeInventory(); + return false; + }); + for(int i=36;i<45;i++){ + menu.addMenuClickHandler(i,(player,slot,item,action)->{ + if(menu.setChosen(slot)) menu.replaceExistingItem(menu.getMachineIndex(),item); + return false; + }); + } + ItemStack[] recipe = si.getRecipe(); + int size = Math.min(recipe.length,4); + menu.addMenuOpeningHandler(player -> { + for(int i=0;i { + ItemStack[] recipe = new ItemStack[36]; + for(int i=0;i<36;i++){ + ItemStack item = menu.getItemInSlot(item_slot_6x6[i]); + if (isEmpty(item)) recipe[i] = null; + else recipe[i] = item; + } + int chosen = menu.getChosen(); + if(chosen!=-1) si.setRecipeType(ItemManager.recipeTypes.get(chosen)); + setRecipe(si,recipe); + if(!saveRecipe(recipe,si)){ + error(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + player.sendMessage(plugin_file_save_error.replaceAll("\\{file_name}", Main.dataF.getName())); + } + else sendFeedback(player, gui_recipe_saved); + player.closeInventory(); + return false; + }); + for(int i=0;i<6;i++){ + menu.addMenuClickHandler(i*9+7,(player,slot,item,action)->{ + if(menu.setChosen(slot)) menu.replaceExistingItem(menu.getMachineIndex(),item); + return false; + }); + } + ItemStack[] recipe = si.getRecipe(); + int size = Math.min(recipe.length,36); + menu.addMenuOpeningHandler(player -> { + for(int i=0;i mp = new HashMap<>(); + public static final Vector recipeTypes = new Vector<>(); + public static ItemStack readAsItem(final ConfigurationSection section,final String path){ + String nbt = section.getString(path); + if (nbt == null) return null; + return section.getItemStack(path); + } + public static boolean saveRecipe(final ItemStack[] recipe,final SlimefunItem si){ + YamlConfiguration yaml = new YamlConfiguration(); + yaml.set("RecipeType",si.getRecipeType().getKey().getKey()); + yaml.set("size",recipe.length); + ConfigurationSection section = yaml.createSection("data"); + for(int i=0;i lores = meta.getLore(); + if (lores != null) { + lores.add(0,lore); + meta.setLore(lores); + } + else { + meta.setLore(Collections.singletonList(lore)); + } + item.setItemMeta(meta); + } + } + public static boolean isEmpty(final ItemStack item){ + if(item == null || item.getType() == Material.AIR) return true; + return false; + } + public static void setRecipe(final SlimefunItem si,final ItemStack[] recipe){ + if(recipe.length == 9){ + si.setRecipe(recipe); + } + else{ + try { + Class clazz = Class.forName("io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem"); + Field si_recipe = clazz.getDeclaredField("recipe"); + si_recipe.setAccessible(true); + si_recipe.set(si,recipe); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + } + public static RecipeType getByName(final String str){ + SlimeRecipeType slimeRecipeType = SlimeRecipeType.getByName(str); + if(slimeRecipeType == SlimeRecipeType._unknown) return null; + return mp.get(slimeRecipeType); + } + public static void initRecipeType(){ + recipeTypes.clear(); + registerAll(RecipeType.class); + if(Bukkit.getPluginManager().getPlugin("InfinityExpansion")!=null){ + registerRecipeType(SlimeRecipeType.infinity_forge,InfinityWorkbench.TYPE); + final RecipeType mob_data_infuser = new MachineRecipeType("mob_data_infuser", MobData.INFUSER); + registerRecipeType(SlimeRecipeType.mob_data_infuser,mob_data_infuser); + registerRecipeType(SlimeRecipeType.storage_forge,StorageForge.TYPE); + } + if(Bukkit.getPluginManager().getPlugin("ExoticGarden")!=null){ + registerAll(ExoticGardenRecipeTypes.class); + } + if(Bukkit.getPluginManager().getPlugin("TranscEndence")!=null){ + registerAll(TERecipeType.class); + } + if(Bukkit.getPluginManager().getPlugin("FlowerPower")!=null){ + registerRecipeType(SlimeRecipeType.magic_basin, MagicBasin.BASIN_RECIPE); + } + } + private static void registerAll(final Class clazz){ + Field[] fields = clazz.getFields(); + for(Field field : fields){ + Type type = field.getType(); + if(!type.equals(RecipeType.class)) continue; + final SlimeRecipeType slimeRecipeType = SlimeRecipeType.getByName(field.getName()); + if(slimeRecipeType==SlimeRecipeType._unknown) continue; + try { + registerRecipeType(slimeRecipeType,(RecipeType) field.get(null)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + } + private static void registerRecipeType(final SlimeRecipeType my_type,final RecipeType recipeType){ + mp.put(my_type,recipeType); + recipeTypes.add(recipeType); + } + public static ItemStack read(ConfigurationSection section, String str, String str2, Material material){ + if(material == null) return new ItemStack(Material.AIR); + return new ItemStack(material,1); + } +} diff --git a/src/main/java/xanadu/slimefunrecipe/metrics/Metrics.java b/src/main/java/pers/xanadu/slimefunrecipe/metrics/Metrics.java similarity index 97% rename from src/main/java/xanadu/slimefunrecipe/metrics/Metrics.java rename to src/main/java/pers/xanadu/slimefunrecipe/metrics/Metrics.java index 190045a..8c61f0b 100644 --- a/src/main/java/xanadu/slimefunrecipe/metrics/Metrics.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/metrics/Metrics.java @@ -1,849 +1,849 @@ -package xanadu.slimefunrecipe.metrics; - -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.Method; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.logging.Level; -import java.util.stream.Collectors; -import java.util.zip.GZIPOutputStream; -import javax.net.ssl.HttpsURLConnection; -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; - -public class Metrics { - - private final Plugin plugin; - - private final MetricsBase metricsBase; - - /** - * Creates a new Metrics instance. - * - * @param plugin Your plugin instance. - * @param serviceId The id of the service. It can be found at What is my plugin id? - */ - public Metrics(JavaPlugin plugin, int serviceId) { - this.plugin = plugin; - // Get the config file - File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); - File configFile = new File(bStatsFolder, "config.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); - if (!config.isSet("serverUuid")) { - config.addDefault("enabled", true); - config.addDefault("serverUuid", UUID.randomUUID().toString()); - config.addDefault("logFailedRequests", false); - config.addDefault("logSentData", false); - config.addDefault("logResponseStatusText", false); - // Inform the server owners about bStats - config - .options() - .header( - "bStats (https://bStats.org) collects some basic information for plugin authors, like how\n" - + "many people use their plugin and their total player count. It's recommended to keep bStats\n" - + "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n" - + "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n" - + "anonymous.") - .copyDefaults(true); - try { - config.save(configFile); - } catch (IOException ignored) { - } - } - // Load the data - boolean enabled = config.getBoolean("enabled", true); - String serverUUID = config.getString("serverUuid"); - boolean logErrors = config.getBoolean("logFailedRequests", false); - boolean logSentData = config.getBoolean("logSentData", false); - boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false); - metricsBase = - new MetricsBase( - "bukkit", - serverUUID, - serviceId, - enabled, - this::appendPlatformData, - this::appendServiceData, - submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask), - plugin::isEnabled, - (message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error), - (message) -> this.plugin.getLogger().log(Level.INFO, message), - logErrors, - logSentData, - logResponseStatusText); - } - - /** - * Adds a custom chart. - * - * @param chart The chart to add. - */ - public void addCustomChart(CustomChart chart) { - metricsBase.addCustomChart(chart); - } - - private void appendPlatformData(JsonObjectBuilder builder) { - builder.appendField("playerAmount", getPlayerAmount()); - builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0); - builder.appendField("bukkitVersion", Bukkit.getVersion()); - builder.appendField("bukkitName", Bukkit.getName()); - builder.appendField("javaVersion", System.getProperty("java.version")); - builder.appendField("osName", System.getProperty("os.name")); - builder.appendField("osArch", System.getProperty("os.arch")); - builder.appendField("osVersion", System.getProperty("os.version")); - builder.appendField("coreCount", Runtime.getRuntime().availableProcessors()); - } - - private void appendServiceData(JsonObjectBuilder builder) { - builder.appendField("pluginVersion", plugin.getDescription().getVersion()); - } - - private int getPlayerAmount() { - try { - // Around MC 1.8 the return type was changed from an array to a collection, - // This fixes java.lang.NoSuchMethodError: - // org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; - Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); - return onlinePlayersMethod.getReturnType().equals(Collection.class) - ? ((Collection) onlinePlayersMethod.invoke(Bukkit.getServer())).size() - : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; - } catch (Exception e) { - // Just use the new method if the reflection failed - return Bukkit.getOnlinePlayers().size(); - } - } - - public static class MetricsBase { - - /** The version of the Metrics class. */ - public static final String METRICS_VERSION = "3.0.0"; - - private static final ScheduledExecutorService scheduler = - Executors.newScheduledThreadPool(1, task -> new Thread(task, "bStats-Metrics")); - - private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s"; - - private final String platform; - - private final String serverUuid; - - private final int serviceId; - - private final Consumer appendPlatformDataConsumer; - - private final Consumer appendServiceDataConsumer; - - private final Consumer submitTaskConsumer; - - private final Supplier checkServiceEnabledSupplier; - - private final BiConsumer errorLogger; - - private final Consumer infoLogger; - - private final boolean logErrors; - - private final boolean logSentData; - - private final boolean logResponseStatusText; - - private final Set customCharts = new HashSet<>(); - - private final boolean enabled; - - /** - * Creates a new MetricsBase class instance. - * - * @param platform The platform of the service. - * @param serviceId The id of the service. - * @param serverUuid The server uuid. - * @param enabled Whether or not data sending is enabled. - * @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and - * appends all platform-specific data. - * @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and - * appends all service-specific data. - * @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be - * used to delegate the data collection to a another thread to prevent errors caused by - * concurrency. Can be {@code null}. - * @param checkServiceEnabledSupplier A supplier to check if the service is still enabled. - * @param errorLogger A consumer that accepts log message and an error. - * @param infoLogger A consumer that accepts info log messages. - * @param logErrors Whether or not errors should be logged. - * @param logSentData Whether or not the sent data should be logged. - * @param logResponseStatusText Whether or not the response status text should be logged. - */ - public MetricsBase( - String platform, - String serverUuid, - int serviceId, - boolean enabled, - Consumer appendPlatformDataConsumer, - Consumer appendServiceDataConsumer, - Consumer submitTaskConsumer, - Supplier checkServiceEnabledSupplier, - BiConsumer errorLogger, - Consumer infoLogger, - boolean logErrors, - boolean logSentData, - boolean logResponseStatusText) { - this.platform = platform; - this.serverUuid = serverUuid; - this.serviceId = serviceId; - this.enabled = enabled; - this.appendPlatformDataConsumer = appendPlatformDataConsumer; - this.appendServiceDataConsumer = appendServiceDataConsumer; - this.submitTaskConsumer = submitTaskConsumer; - this.checkServiceEnabledSupplier = checkServiceEnabledSupplier; - this.errorLogger = errorLogger; - this.infoLogger = infoLogger; - this.logErrors = logErrors; - this.logSentData = logSentData; - this.logResponseStatusText = logResponseStatusText; - checkRelocation(); - if (enabled) { - // WARNING: Removing the option to opt-out will get your plugin banned from bStats - startSubmitting(); - } - } - - public void addCustomChart(CustomChart chart) { - this.customCharts.add(chart); - } - - private void startSubmitting() { - final Runnable submitTask = - () -> { - if (!enabled || !checkServiceEnabledSupplier.get()) { - // Submitting data or service is disabled - scheduler.shutdown(); - return; - } - if (submitTaskConsumer != null) { - submitTaskConsumer.accept(this::submitData); - } else { - this.submitData(); - } - }; - // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution - // of requests on the - // bStats backend. To circumvent this problem, we introduce some randomness into the initial - // and second delay. - // WARNING: You must not modify and part of this Metrics class, including the submit delay or - // frequency! - // WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it! - long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3)); - long secondDelay = (long) (1000 * 60 * (Math.random() * 30)); - scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS); - scheduler.scheduleAtFixedRate( - submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS); - } - - private void submitData() { - final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder(); - appendPlatformDataConsumer.accept(baseJsonBuilder); - final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder(); - appendServiceDataConsumer.accept(serviceJsonBuilder); - JsonObjectBuilder.JsonObject[] chartData = - customCharts.stream() - .map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors)) - .filter(Objects::nonNull) - .toArray(JsonObjectBuilder.JsonObject[]::new); - serviceJsonBuilder.appendField("id", serviceId); - serviceJsonBuilder.appendField("customCharts", chartData); - baseJsonBuilder.appendField("service", serviceJsonBuilder.build()); - baseJsonBuilder.appendField("serverUUID", serverUuid); - baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION); - JsonObjectBuilder.JsonObject data = baseJsonBuilder.build(); - scheduler.execute( - () -> { - try { - // Send the data - sendData(data); - } catch (Exception e) { - // Something went wrong! :( - if (logErrors) { - errorLogger.accept("Could not submit bStats metrics data", e); - } - } - }); - } - - private void sendData(JsonObjectBuilder.JsonObject data) throws Exception { - if (logSentData) { - infoLogger.accept("Sent bStats metrics data: " + data.toString()); - } - String url = String.format(REPORT_URL, platform); - HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); - // Compress the data to save bandwidth - byte[] compressedData = compress(data.toString()); - connection.setRequestMethod("POST"); - connection.addRequestProperty("Accept", "application/json"); - connection.addRequestProperty("Connection", "close"); - connection.addRequestProperty("Content-Encoding", "gzip"); - connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestProperty("User-Agent", "Metrics-Service/1"); - connection.setDoOutput(true); - try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { - outputStream.write(compressedData); - } - StringBuilder builder = new StringBuilder(); - try (BufferedReader bufferedReader = - new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String line; - while ((line = bufferedReader.readLine()) != null) { - builder.append(line); - } - } - if (logResponseStatusText) { - infoLogger.accept("Sent data to bStats and received response: " + builder); - } - } - - /** Checks that the class was properly relocated. */ - private void checkRelocation() { - // You can use the property to disable the check in your test environment - if (System.getProperty("bstats.relocatecheck") == null - || !System.getProperty("bstats.relocatecheck").equals("false")) { - // Maven's Relocate is clever and changes strings, too. So we have to use this little - // "trick" ... :D - final String defaultPackage = - new String(new byte[] {'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'}); - final String examplePackage = - new String(new byte[] {'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); - // We want to make sure no one just copy & pastes the example and uses the wrong package - // names - if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage) - || MetricsBase.class.getPackage().getName().startsWith(examplePackage)) { - throw new IllegalStateException("bStats Metrics class has not been relocated correctly!"); - } - } - } - - /** - * Gzips the given string. - * - * @param str The string to gzip. - * @return The gzipped string. - */ - private static byte[] compress(final String str) throws IOException { - if (str == null) { - return null; - } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) { - gzip.write(str.getBytes(StandardCharsets.UTF_8)); - } - return outputStream.toByteArray(); - } - } - - public static class DrilldownPie extends CustomChart { - - private final Callable>> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public DrilldownPie(String chartId, Callable>> callable) { - super(chartId); - this.callable = callable; - } - - @Override - public JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map> map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean reallyAllSkipped = true; - for (Map.Entry> entryValues : map.entrySet()) { - JsonObjectBuilder valueBuilder = new JsonObjectBuilder(); - boolean allSkipped = true; - for (Map.Entry 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(); - } - } - - public static class AdvancedPie extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedPie(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class MultiLineChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public MultiLineChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class SimpleBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimpleBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - for (Map.Entry entry : map.entrySet()) { - valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()}); - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public abstract static class CustomChart { - - private final String chartId; - - protected CustomChart(String chartId) { - if (chartId == null) { - throw new IllegalArgumentException("chartId must not be null"); - } - this.chartId = chartId; - } - - public JsonObjectBuilder.JsonObject getRequestJsonObject( - BiConsumer errorLogger, boolean logErrors) { - JsonObjectBuilder builder = new JsonObjectBuilder(); - builder.appendField("chartId", chartId); - try { - JsonObjectBuilder.JsonObject data = getChartData(); - if (data == null) { - // If the data is null we don't send the chart. - return null; - } - builder.appendField("data", data); - } catch (Throwable t) { - if (logErrors) { - errorLogger.accept("Failed to get data for custom chart with id " + chartId, t); - } - return null; - } - return builder.build(); - } - - protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception; - } - - public static class SimplePie extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SimplePie(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - String value = callable.call(); - if (value == null || value.isEmpty()) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("value", value).build(); - } - } - - public static class AdvancedBarChart extends CustomChart { - - private final Callable> callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public AdvancedBarChart(String chartId, Callable> callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); - Map map = callable.call(); - if (map == null || map.isEmpty()) { - // Null = skip the chart - return null; - } - boolean allSkipped = true; - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().length == 0) { - // Skip this invalid - continue; - } - allSkipped = false; - valuesBuilder.appendField(entry.getKey(), entry.getValue()); - } - if (allSkipped) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); - } - } - - public static class SingleLineChart extends CustomChart { - - private final Callable callable; - - /** - * Class constructor. - * - * @param chartId The id of the chart. - * @param callable The callable which is used to request the chart data. - */ - public SingleLineChart(String chartId, Callable callable) { - super(chartId); - this.callable = callable; - } - - @Override - protected JsonObjectBuilder.JsonObject getChartData() throws Exception { - int value = callable.call(); - if (value == 0) { - // Null = skip the chart - return null; - } - return new JsonObjectBuilder().appendField("value", value).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; - } - } - } +package pers.xanadu.slimefunrecipe.metrics; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Method; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Supplier; +import java.util.logging.Level; +import java.util.stream.Collectors; +import java.util.zip.GZIPOutputStream; +import javax.net.ssl.HttpsURLConnection; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; + +public class Metrics { + + private final Plugin plugin; + + private final MetricsBase metricsBase; + + /** + * Creates a new Metrics instance. + * + * @param plugin Your plugin instance. + * @param serviceId The id of the service. It can be found at What is my plugin id? + */ + public Metrics(JavaPlugin plugin, int serviceId) { + this.plugin = plugin; + // Get the config file + File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); + File configFile = new File(bStatsFolder, "config.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile); + if (!config.isSet("serverUuid")) { + config.addDefault("enabled", true); + config.addDefault("serverUuid", UUID.randomUUID().toString()); + config.addDefault("logFailedRequests", false); + config.addDefault("logSentData", false); + config.addDefault("logResponseStatusText", false); + // Inform the server owners about bStats + config + .options() + .header( + "bStats (https://bStats.org) collects some basic information for plugin authors, like how\n" + + "many people use their plugin and their total player count. It's recommended to keep bStats\n" + + "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n" + + "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n" + + "anonymous.") + .copyDefaults(true); + try { + config.save(configFile); + } catch (IOException ignored) { + } + } + // Load the data + boolean enabled = config.getBoolean("enabled", true); + String serverUUID = config.getString("serverUuid"); + boolean logErrors = config.getBoolean("logFailedRequests", false); + boolean logSentData = config.getBoolean("logSentData", false); + boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false); + metricsBase = + new MetricsBase( + "bukkit", + serverUUID, + serviceId, + enabled, + this::appendPlatformData, + this::appendServiceData, + submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask), + plugin::isEnabled, + (message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error), + (message) -> this.plugin.getLogger().log(Level.INFO, message), + logErrors, + logSentData, + logResponseStatusText); + } + + /** + * Adds a custom chart. + * + * @param chart The chart to add. + */ + public void addCustomChart(CustomChart chart) { + metricsBase.addCustomChart(chart); + } + + private void appendPlatformData(JsonObjectBuilder builder) { + builder.appendField("playerAmount", getPlayerAmount()); + builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0); + builder.appendField("bukkitVersion", Bukkit.getVersion()); + builder.appendField("bukkitName", Bukkit.getName()); + builder.appendField("javaVersion", System.getProperty("java.version")); + builder.appendField("osName", System.getProperty("os.name")); + builder.appendField("osArch", System.getProperty("os.arch")); + builder.appendField("osVersion", System.getProperty("os.version")); + builder.appendField("coreCount", Runtime.getRuntime().availableProcessors()); + } + + private void appendServiceData(JsonObjectBuilder builder) { + builder.appendField("pluginVersion", plugin.getDescription().getVersion()); + } + + private int getPlayerAmount() { + try { + // Around MC 1.8 the return type was changed from an array to a collection, + // This fixes java.lang.NoSuchMethodError: + // org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; + Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); + return onlinePlayersMethod.getReturnType().equals(Collection.class) + ? ((Collection) onlinePlayersMethod.invoke(Bukkit.getServer())).size() + : ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; + } catch (Exception e) { + // Just use the new method if the reflection failed + return Bukkit.getOnlinePlayers().size(); + } + } + + public static class MetricsBase { + + /** The version of the Metrics class. */ + public static final String METRICS_VERSION = "3.0.0"; + + private static final ScheduledExecutorService scheduler = + Executors.newScheduledThreadPool(1, task -> new Thread(task, "bStats-Metrics")); + + private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s"; + + private final String platform; + + private final String serverUuid; + + private final int serviceId; + + private final Consumer appendPlatformDataConsumer; + + private final Consumer appendServiceDataConsumer; + + private final Consumer submitTaskConsumer; + + private final Supplier checkServiceEnabledSupplier; + + private final BiConsumer errorLogger; + + private final Consumer infoLogger; + + private final boolean logErrors; + + private final boolean logSentData; + + private final boolean logResponseStatusText; + + private final Set customCharts = new HashSet<>(); + + private final boolean enabled; + + /** + * Creates a new MetricsBase class instance. + * + * @param platform The platform of the service. + * @param serviceId The id of the service. + * @param serverUuid The server uuid. + * @param enabled Whether or not data sending is enabled. + * @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and + * appends all platform-specific data. + * @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and + * appends all service-specific data. + * @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be + * used to delegate the data collection to a another thread to prevent errors caused by + * concurrency. Can be {@code null}. + * @param checkServiceEnabledSupplier A supplier to check if the service is still enabled. + * @param errorLogger A consumer that accepts log message and an error. + * @param infoLogger A consumer that accepts info log messages. + * @param logErrors Whether or not errors should be logged. + * @param logSentData Whether or not the sent data should be logged. + * @param logResponseStatusText Whether or not the response status text should be logged. + */ + public MetricsBase( + String platform, + String serverUuid, + int serviceId, + boolean enabled, + Consumer appendPlatformDataConsumer, + Consumer appendServiceDataConsumer, + Consumer submitTaskConsumer, + Supplier checkServiceEnabledSupplier, + BiConsumer errorLogger, + Consumer infoLogger, + boolean logErrors, + boolean logSentData, + boolean logResponseStatusText) { + this.platform = platform; + this.serverUuid = serverUuid; + this.serviceId = serviceId; + this.enabled = enabled; + this.appendPlatformDataConsumer = appendPlatformDataConsumer; + this.appendServiceDataConsumer = appendServiceDataConsumer; + this.submitTaskConsumer = submitTaskConsumer; + this.checkServiceEnabledSupplier = checkServiceEnabledSupplier; + this.errorLogger = errorLogger; + this.infoLogger = infoLogger; + this.logErrors = logErrors; + this.logSentData = logSentData; + this.logResponseStatusText = logResponseStatusText; + checkRelocation(); + if (enabled) { + // WARNING: Removing the option to opt-out will get your plugin banned from bStats + startSubmitting(); + } + } + + public void addCustomChart(CustomChart chart) { + this.customCharts.add(chart); + } + + private void startSubmitting() { + final Runnable submitTask = + () -> { + if (!enabled || !checkServiceEnabledSupplier.get()) { + // Submitting data or service is disabled + scheduler.shutdown(); + return; + } + if (submitTaskConsumer != null) { + submitTaskConsumer.accept(this::submitData); + } else { + this.submitData(); + } + }; + // Many servers tend to restart at a fixed time at xx:00 which causes an uneven distribution + // of requests on the + // bStats backend. To circumvent this problem, we introduce some randomness into the initial + // and second delay. + // WARNING: You must not modify and part of this Metrics class, including the submit delay or + // frequency! + // WARNING: Modifying this code will get your plugin banned on bStats. Just don't do it! + long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3)); + long secondDelay = (long) (1000 * 60 * (Math.random() * 30)); + scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS); + scheduler.scheduleAtFixedRate( + submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS); + } + + private void submitData() { + final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder(); + appendPlatformDataConsumer.accept(baseJsonBuilder); + final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder(); + appendServiceDataConsumer.accept(serviceJsonBuilder); + JsonObjectBuilder.JsonObject[] chartData = + customCharts.stream() + .map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors)) + .filter(Objects::nonNull) + .toArray(JsonObjectBuilder.JsonObject[]::new); + serviceJsonBuilder.appendField("id", serviceId); + serviceJsonBuilder.appendField("customCharts", chartData); + baseJsonBuilder.appendField("service", serviceJsonBuilder.build()); + baseJsonBuilder.appendField("serverUUID", serverUuid); + baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION); + JsonObjectBuilder.JsonObject data = baseJsonBuilder.build(); + scheduler.execute( + () -> { + try { + // Send the data + sendData(data); + } catch (Exception e) { + // Something went wrong! :( + if (logErrors) { + errorLogger.accept("Could not submit bStats metrics data", e); + } + } + }); + } + + private void sendData(JsonObjectBuilder.JsonObject data) throws Exception { + if (logSentData) { + infoLogger.accept("Sent bStats metrics data: " + data.toString()); + } + String url = String.format(REPORT_URL, platform); + HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); + // Compress the data to save bandwidth + byte[] compressedData = compress(data.toString()); + connection.setRequestMethod("POST"); + connection.addRequestProperty("Accept", "application/json"); + connection.addRequestProperty("Connection", "close"); + connection.addRequestProperty("Content-Encoding", "gzip"); + connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length)); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("User-Agent", "Metrics-Service/1"); + connection.setDoOutput(true); + try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { + outputStream.write(compressedData); + } + StringBuilder builder = new StringBuilder(); + try (BufferedReader bufferedReader = + new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + while ((line = bufferedReader.readLine()) != null) { + builder.append(line); + } + } + if (logResponseStatusText) { + infoLogger.accept("Sent data to bStats and received response: " + builder); + } + } + + /** Checks that the class was properly relocated. */ + private void checkRelocation() { + // You can use the property to disable the check in your test environment + if (System.getProperty("bstats.relocatecheck") == null + || !System.getProperty("bstats.relocatecheck").equals("false")) { + // Maven's Relocate is clever and changes strings, too. So we have to use this little + // "trick" ... :D + final String defaultPackage = + new String(new byte[] {'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'}); + final String examplePackage = + new String(new byte[] {'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); + // We want to make sure no one just copy & pastes the example and uses the wrong package + // names + if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage) + || MetricsBase.class.getPackage().getName().startsWith(examplePackage)) { + throw new IllegalStateException("bStats Metrics class has not been relocated correctly!"); + } + } + } + + /** + * Gzips the given string. + * + * @param str The string to gzip. + * @return The gzipped string. + */ + private static byte[] compress(final String str) throws IOException { + if (str == null) { + return null; + } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) { + gzip.write(str.getBytes(StandardCharsets.UTF_8)); + } + return outputStream.toByteArray(); + } + } + + public static class DrilldownPie extends CustomChart { + + private final Callable>> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public DrilldownPie(String chartId, Callable>> callable) { + super(chartId); + this.callable = callable; + } + + @Override + public JsonObjectBuilder.JsonObject getChartData() throws Exception { + JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); + Map> map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean reallyAllSkipped = true; + for (Map.Entry> entryValues : map.entrySet()) { + JsonObjectBuilder valueBuilder = new JsonObjectBuilder(); + boolean allSkipped = true; + for (Map.Entry 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(); + } + } + + public static class AdvancedPie extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public AdvancedPie(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 0) { + // Skip this invalid + continue; + } + allSkipped = false; + valuesBuilder.appendField(entry.getKey(), entry.getValue()); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); + } + } + + public static class MultiLineChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public MultiLineChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == 0) { + // Skip this invalid + continue; + } + allSkipped = false; + valuesBuilder.appendField(entry.getKey(), entry.getValue()); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); + } + } + + public static class SimpleBarChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SimpleBarChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + for (Map.Entry entry : map.entrySet()) { + valuesBuilder.appendField(entry.getKey(), new int[] {entry.getValue()}); + } + return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); + } + } + + public abstract static class CustomChart { + + private final String chartId; + + protected CustomChart(String chartId) { + if (chartId == null) { + throw new IllegalArgumentException("chartId must not be null"); + } + this.chartId = chartId; + } + + public JsonObjectBuilder.JsonObject getRequestJsonObject( + BiConsumer errorLogger, boolean logErrors) { + JsonObjectBuilder builder = new JsonObjectBuilder(); + builder.appendField("chartId", chartId); + try { + JsonObjectBuilder.JsonObject data = getChartData(); + if (data == null) { + // If the data is null we don't send the chart. + return null; + } + builder.appendField("data", data); + } catch (Throwable t) { + if (logErrors) { + errorLogger.accept("Failed to get data for custom chart with id " + chartId, t); + } + return null; + } + return builder.build(); + } + + protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception; + } + + public static class SimplePie extends CustomChart { + + private final Callable callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SimplePie(String chartId, Callable callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + String value = callable.call(); + if (value == null || value.isEmpty()) { + // Null = skip the chart + return null; + } + return new JsonObjectBuilder().appendField("value", value).build(); + } + } + + public static class AdvancedBarChart extends CustomChart { + + private final Callable> callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public AdvancedBarChart(String chartId, Callable> callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + JsonObjectBuilder valuesBuilder = new JsonObjectBuilder(); + Map map = callable.call(); + if (map == null || map.isEmpty()) { + // Null = skip the chart + return null; + } + boolean allSkipped = true; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue().length == 0) { + // Skip this invalid + continue; + } + allSkipped = false; + valuesBuilder.appendField(entry.getKey(), entry.getValue()); + } + if (allSkipped) { + // Null = skip the chart + return null; + } + return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build(); + } + } + + public static class SingleLineChart extends CustomChart { + + private final Callable callable; + + /** + * Class constructor. + * + * @param chartId The id of the chart. + * @param callable The callable which is used to request the chart data. + */ + public SingleLineChart(String chartId, Callable callable) { + super(chartId); + this.callable = callable; + } + + @Override + protected JsonObjectBuilder.JsonObject getChartData() throws Exception { + int value = callable.call(); + if (value == 0) { + // Null = skip the chart + return null; + } + return new JsonObjectBuilder().appendField("value", value).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/xanadu/slimefunrecipe/utils/GuiUtils.java b/src/main/java/pers/xanadu/slimefunrecipe/utils/GuiUtils.java similarity index 93% rename from src/main/java/xanadu/slimefunrecipe/utils/GuiUtils.java rename to src/main/java/pers/xanadu/slimefunrecipe/utils/GuiUtils.java index 026509e..1cff2de 100644 --- a/src/main/java/xanadu/slimefunrecipe/utils/GuiUtils.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/utils/GuiUtils.java @@ -1,26 +1,26 @@ -package xanadu.slimefunrecipe.utils; - -import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; -import org.bukkit.Material; -import org.bukkit.inventory.ItemStack; - -public class GuiUtils { - private static final ItemStack item_chosen = new CustomItemStack(Material.BLUE_STAINED_GLASS_PANE," "); - private static final ItemStack item_not_chosen = new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE," "); - private static final ItemStack black_background = new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE," "); - private static final ItemStack gray_background = new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE," "); - public static ItemStack getItem_chosen(){ - return item_chosen; - } - public static ItemStack getItem_not_chosen(){ - return item_not_chosen; - } - public static ItemStack getBackground(){ - return black_background; - } - public static ItemStack getGray_background(){ - return gray_background; - } - - -} +package pers.xanadu.slimefunrecipe.utils; + +import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +public class GuiUtils { + private static final ItemStack item_chosen = new CustomItemStack(Material.BLUE_STAINED_GLASS_PANE," "); + private static final ItemStack item_not_chosen = new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE," "); + private static final ItemStack black_background = new CustomItemStack(Material.BLACK_STAINED_GLASS_PANE," "); + private static final ItemStack gray_background = new CustomItemStack(Material.GRAY_STAINED_GLASS_PANE," "); + public static ItemStack getItem_chosen(){ + return item_chosen; + } + public static ItemStack getItem_not_chosen(){ + return item_not_chosen; + } + public static ItemStack getBackground(){ + return black_background; + } + public static ItemStack getGray_background(){ + return gray_background; + } + + +} diff --git a/src/main/java/xanadu/slimefunrecipe/utils/VersionUpdater.java b/src/main/java/pers/xanadu/slimefunrecipe/utils/VersionUpdater.java similarity index 64% rename from src/main/java/xanadu/slimefunrecipe/utils/VersionUpdater.java rename to src/main/java/pers/xanadu/slimefunrecipe/utils/VersionUpdater.java index 15f1fdf..98cc147 100644 --- a/src/main/java/xanadu/slimefunrecipe/utils/VersionUpdater.java +++ b/src/main/java/pers/xanadu/slimefunrecipe/utils/VersionUpdater.java @@ -1,42 +1,40 @@ -package xanadu.slimefunrecipe.utils; - -import org.bukkit.scheduler.BukkitRunnable; -import xanadu.slimefunrecipe.config.Lang; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; - -import static xanadu.slimefunrecipe.Main.plugin; -import static xanadu.slimefunrecipe.config.Lang.*; - -public class VersionUpdater { - public static void checkUpdate(){ - new BukkitRunnable(){ - @Override - public void run(){ - try { - URLConnection conn = new URL("https://api.github.com/repos/iXanadu13/SlimeFunRecipe/releases/latest").openConnection(); - conn.setConnectTimeout(20000); - conn.setReadTimeout(60000); - InputStream is = conn.getInputStream(); - String line = new BufferedReader(new InputStreamReader(is)).readLine(); - is.close(); - String newVer = line.substring(line.indexOf("\"tag_name\"") + 13, line.indexOf("\"target_commitish\"") - 2); - String localVer = plugin.getDescription().getVersion(); - if (!localVer.equals(newVer)) { - warn(Lang.plugin_out_of_date.replace("{0}",localVer).replace("{1}",newVer)); - } - else{ - info(Lang.plugin_up_to_date.replace("{1}",newVer)); - } - } catch (Exception e) { - warn(plugin_check_update_fail); - } - } - }.runTaskAsynchronously(plugin); - info(Lang.plugin_checking_update); - } -} +package pers.xanadu.slimefunrecipe.utils; + +import org.bukkit.scheduler.BukkitRunnable; +import pers.xanadu.slimefunrecipe.Main; +import pers.xanadu.slimefunrecipe.config.Lang; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; + +public class VersionUpdater { + public static void checkUpdate(){ + new BukkitRunnable(){ + @Override + public void run(){ + try { + URLConnection conn = new URL("https://api.github.com/repos/iXanadu13/SlimeFunRecipe/releases/latest").openConnection(); + conn.setConnectTimeout(20000); + conn.setReadTimeout(60000); + InputStream is = conn.getInputStream(); + String line = new BufferedReader(new InputStreamReader(is)).readLine(); + is.close(); + String newVer = line.substring(line.indexOf("\"tag_name\"") + 13, line.indexOf("\"target_commitish\"") - 2); + String localVer = Main.plugin.getDescription().getVersion(); + if (!localVer.equals(newVer)) { + Lang.warn(Lang.plugin_out_of_date.replace("{0}",localVer).replace("{1}",newVer)); + } + else{ + Lang.info(Lang.plugin_up_to_date.replace("{1}",newVer)); + } + } catch (Exception e) { + Lang.warn(Lang.plugin_check_update_fail); + } + } + }.runTaskAsynchronously(Main.plugin); + Lang.info(Lang.plugin_checking_update); + } +} diff --git a/src/main/java/xanadu/slimefunrecipe/GUI.java b/src/main/java/xanadu/slimefunrecipe/GUI.java deleted file mode 100644 index 0d38625..0000000 --- a/src/main/java/xanadu/slimefunrecipe/GUI.java +++ /dev/null @@ -1,73 +0,0 @@ -package xanadu.slimefunrecipe; - -import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; -import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; -import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; -import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import xanadu.slimefunrecipe.config.Lang; -import xanadu.slimefunrecipe.manager.ItemManager; -import xanadu.slimefunrecipe.utils.GuiUtils; - -import java.util.Vector; - -public class GUI extends ChestMenu { - private static final Vector items = new Vector<>(); - private int index; - private int chosen; - public GUI(String title) { - super(title); - index=1; - chosen=-1; - initVector(); - } - public void initVector(){ - items.clear(); - for(RecipeType recipeType : ItemManager.recipeTypes){ - ItemStack item = recipeType.toItem(); - if(item == null) items.add(new CustomItemStack(Material.BARRIER, Lang.gui_RecipeType_null_name,Lang.gui_RecipeType_null_lore)); - else items.add(item); - } - } - public int getPage(){ - return index; - } - public int getChosen(){ - return chosen; - } - public boolean setChosen(int slot){ - int j=(index-1)*9+slot-36; - if(j>=items.size()) return false; - chosen=j; - updateChosen(); - return true; - } - public void setPage(Player p,int to){ - if(to<1||to>getSize()) return; - index=to; - fillPagedItems(to); - updateButton(p,to); - } - private void updateChosen(){ - for(int i=0;i<9;i++){ - int j=(index-1)*9+i; - this.replaceExistingItem(i+45,j==chosen? GuiUtils.getItem_chosen() : GuiUtils.getItem_not_chosen()); - } - } - private void fillPagedItems(int page){ - for(int i=0;i<9;i++){ - int j=(page-1)*9+i; - this.replaceExistingItem(i+36,j { - ItemStack[] recipe = new ItemStack[9]; - for(int i=0;i<9;i++){ - ItemStack item = menu.getItemInSlot(item_slot[i]); - if (isEmpty(item)) recipe[i] = null; - else recipe[i] = item; - } - int chosen = menu.getChosen(); - if(chosen!=-1) si.setRecipeType(ItemManager.recipeTypes.get(chosen)); - si.setRecipe(recipe); - if(!saveRecipe(recipe,si)){ - error(plugin_file_save_error.replaceAll("\\{file_name}",dataF.getName())); - p.sendMessage(plugin_file_save_error.replaceAll("\\{file_name}",dataF.getName())); - } - else sendFeedback(player, gui_recipe_saved); - player.closeInventory(); - return false; - }); - menu.replaceExistingItem(0, new CustomItemStack(Material.RED_STAINED_GLASS_PANE, gui_button_cancel)); - menu.addMenuClickHandler(0, (player, slot, item, action) -> { - player.closeInventory(); - sendFeedback(player, gui_recipe_cancel); - return false; - }); - menu.addMenuClickHandler(28,(player,slot,item,action)->{ - if(menu.getPage()==1) return false; - menu.setPage(player,menu.getPage()-1); - return false; - }); - menu.addMenuClickHandler(34,(player,slot,item,action)->{ - if(menu.getPage()==menu.getSize()) return false; - menu.setPage(player,menu.getPage()+1); - return false; - }); - for(int i=36;i<45;i++){ - menu.addMenuClickHandler(i,(player,slot,item,action)->{ - if(menu.setChosen(slot)) menu.replaceExistingItem(10,item); - return false; - }); - } - menu.addMenuOpeningHandler(player -> { - for(int i=0;i<9;i++){ - menu.replaceExistingItem(item_slot[i], si.getRecipe()[i]); - } - }); - menu.open(p); - menu.setPage(p,1); - } - -} diff --git a/src/main/java/xanadu/slimefunrecipe/manager/ItemManager.java b/src/main/java/xanadu/slimefunrecipe/manager/ItemManager.java deleted file mode 100644 index a040f5f..0000000 --- a/src/main/java/xanadu/slimefunrecipe/manager/ItemManager.java +++ /dev/null @@ -1,89 +0,0 @@ -package xanadu.slimefunrecipe.manager; - -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; -import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import xanadu.slimefunrecipe.Main; -import xanadu.slimefunrecipe.SlimeRecipeType; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Type; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Vector; - -import static xanadu.slimefunrecipe.Main.dataF; - -public class ItemManager { - public static final HashMap mp = new HashMap<>(); - public static final Vector recipeTypes = new Vector<>(); - public static ItemStack readAsItem(ConfigurationSection section, String path){ - String nbt = section.getString(path); - if (nbt == null) return null; - return section.getItemStack(path); - } - public static boolean saveRecipe(ItemStack[] recipe, SlimefunItem si){ - YamlConfiguration yaml = new YamlConfiguration(); - yaml.set("RecipeType",si.getRecipeType().getKey().getKey()); - ConfigurationSection section = yaml.createSection("data"); - for(int i=0;i<9;i++){ - section.set(String.valueOf(i+1),recipe[i]); - } - Main.data.set(si.getId(),yaml); - try { - Main.data.save(dataF); - } catch (IOException e) { - return false; - } - return true; - } - public static void addLore(ItemStack item, String lore){ - ItemMeta meta = item.getItemMeta(); - if(meta != null) { - List lores = meta.getLore(); - if (lores != null) { - lores.add(0,lore); - meta.setLore(lores); - } - else { - meta.setLore(Collections.singletonList(lore)); - } - item.setItemMeta(meta); - } - } - public static boolean isEmpty(ItemStack item){ - if(item == null || item.getType() == Material.AIR) return true; - return false; - } - public static RecipeType getByName(String str){ - SlimeRecipeType slimeRecipeType = SlimeRecipeType.getByName(str); - if(slimeRecipeType == SlimeRecipeType._unknown) return null; - return mp.get(slimeRecipeType); - } - public static void initRecipeType(){ - recipeTypes.clear(); - Field[] fields = RecipeType.class.getFields(); - for(Field field : fields){ - Type type = field.getType(); - if(!type.equals(RecipeType.class)) continue; - SlimeRecipeType slimeRecipeType = SlimeRecipeType.getByName(field.getName()); - if(slimeRecipeType==SlimeRecipeType._unknown) continue; - try { - mp.put(slimeRecipeType, (RecipeType) field.get(null)); - recipeTypes.add((RecipeType) field.get(null)); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - } - public static ItemStack read(ConfigurationSection section, String str, String str2, Material material){ - if(material == null) return new ItemStack(Material.AIR); - return new ItemStack(material,1); - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1028063..93219ff 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,5 +1,5 @@ #File version, do not edit -version: 1.0.0 +version: 1.1.0 #[English/Chinese] lang: English diff --git a/src/main/resources/lang/Chinese.yml b/src/main/resources/lang/Chinese.yml index aca9ed8..9c2ade7 100644 --- a/src/main/resources/lang/Chinese.yml +++ b/src/main/resources/lang/Chinese.yml @@ -1,4 +1,4 @@ -version: 1.0.0 +version: 1.1.0 plugin: prefix: '§7[§eSlimefunRecipe§7]§r ' file_save_error: '文件 {file_name} 保存失败!' @@ -9,16 +9,17 @@ plugin: check_update_fail: '检查更新失败,请确认您的网络配置' out_of_date: '您正在使用的插件版本: v{0}\n检测到新版插件v{1}已发布,请尽快更新\n下载地址:https://www.spigotmc.org/resources/slimefunrecipe.109617/' up_to_date: '您正在使用最新版的插件(v{1})...' + wrong_file_version: '{file_name} 版本与插件不对应,请更新文件' command: no_permission: "§4你没有使用该命令的权限" reload_config: '§a配置文件已重载' only_player: '§c这个指令只能由玩家执行' item_error: '§c错误:你需要拿着Slimefun物品' + unknown_item_size: '§c未知的物品配方格式!' gui: title: '§b正在编辑粘液物品: %item%' button_verify: '§a确定' button_cancel: '§c取消' - empty_recipe: '§c合成配方不能全为空!' recipe_saved: '§a配方修改成功,§c重启服务器后§a将生效' recipe_cancel: '§c配方修改取消!' RecipeType_null_name: '§c无配方' diff --git a/src/main/resources/lang/English.yml b/src/main/resources/lang/English.yml index bef1e01..fbb2b37 100644 --- a/src/main/resources/lang/English.yml +++ b/src/main/resources/lang/English.yml @@ -1,4 +1,4 @@ -version: 1.0.0 +version: 1.1.0 plugin: prefix: '§7[§eSlimefunRecipe§7]§r ' file_save_error: 'File {file_name} save error!' @@ -9,16 +9,17 @@ plugin: check_update_fail: 'Checking update fail. Please check your network settings.' out_of_date: 'Your version: v{0}\nNew version: v{1}\nDownload Link: https://www.spigotmc.org/resources/slimefunrecipe.109617/' up_to_date: 'You are using the latest version (v{1})...' + wrong_file_version: 'File {file_name} is out of date,please update it.' command: no_permission: "§4I'm Sorry,but you do not have permission to do that." reload_config: '§aConfig reload complete.' only_player: '§cOnly player can do this.' item_error: '§cYou must hold slimefun items when use this command' + unknown_item_size: '§cUnknown item recipe size!' gui: title: '§bEdit Recipe: %item%' button_verify: '§aVerify' button_cancel: '§cCancel' - empty_recipe: '§cThe recipe cannot be empty!' recipe_saved: '§aNew recipe has been saved. §cYou need to restart the server to apply the change!' recipe_cancel: '§cYour changes are unsaved.' RecipeType_null_name: '§cNo recipe' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 662af96..4f43442 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,8 +1,17 @@ name: SlimeFunRecipe -version: 1.0.0 -main: xanadu.slimefunrecipe.Main +version: 1.1.0 +main: pers.xanadu.slimefunrecipe.Main api-version: 1.13 -depend: [Slimefun] +depend: + - Slimefun +softdepend: + - DynaTech + - ExoticGarden + - FlowerPower + - FluffyMachines + - InfinityExpansion + - SoulJars + - TranscEndence commands: slimefunrecipe: description: 'Edit the recipe of Slimefun items' @@ -12,4 +21,6 @@ commands: permissions: slimefunrecipe.admin: description: 'Use all commands of SlimefunRecipe' - default: op \ No newline at end of file + default: op + +website: https://www.spigotmc.org/resources/slimefunrecipe.109617/ \ No newline at end of file