end433

for DEV

Jan 30th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | Source Code | 0 0
  1. //Code of eventHandler
  2. public class StMenuListener implements Listener {
  3.  
  4.     private final ConfigManager configManager;
  5.  
  6.     public StMenuListener(ConfigManager configManager) {
  7.         this.configManager = configManager;
  8.     }
  9.  
  10.     @EventHandler
  11.  
  12.     public void onInventoryDrag(InventoryDragEvent event) {
  13.         String stMenuTitle = configManager.getString("lang.yml", "st.st-title");
  14.         String stMenuColor = TextColorUtil.colorize(stMenuTitle);
  15.  
  16.         if (ChatColor.stripColor(event.getView().getTitle()).equals(ChatColor.stripColor(stMenuColor))) {
  17.             event.setCancelled(true);
  18.         }
  19.     }
  20. }
  21.  
  22.  
  23. //ConfigManager
  24. public class ConfigManager {
  25.  
  26.     private static FileConfiguration getConfig(String fileName) {
  27.         File pluginFolder = new File("plugins/EndiumMMO");
  28.         if (!pluginFolder.exists()) {
  29.             boolean created = pluginFolder.mkdirs();
  30.             if (!created) {
  31.                 System.out.println("Unable to create: " + pluginFolder);
  32.             }
  33.         }
  34.  
  35.         File file = new File("plugins/EndiumMMO", fileName);
  36.         if (!file.exists()) {
  37.             try {
  38.                 file.createNewFile();
  39.             } catch (IOException e) {
  40.                 System.out.println("Failed to create " + fileName);
  41.             }
  42.         }
  43.         return YamlConfiguration.loadConfiguration(file);
  44.     }
  45.  
  46.     public static String getString(String fileName, String path) {
  47.         FileConfiguration config = getConfig(fileName);
  48.         return config != null ? config.getString(path) : null;
  49.     }
  50.  
  51.     public static int getInt(String fileName, String path) {
  52.         FileConfiguration config = getConfig(fileName);
  53.         return config != null ? config.getInt(path) : 0;
  54.     }
  55.  
  56.     public static boolean getBoolean(String fileName, String path) {
  57.         FileConfiguration config = getConfig(fileName);
  58.         return config != null && config.getBoolean(path);
  59.     }
  60.  
  61.     public static List<String> getStringList(String fileName, String path) {
  62.         FileConfiguration config = getConfig(fileName);
  63.         return config != null ? config.getStringList(path) : null;
  64.     }
  65.  
  66.     public static List<Integer> getIntegerList(String fileName, String path) {
  67.         FileConfiguration config = getConfig(fileName);
  68.         return config != null ? config.getIntegerList(path) : null;
  69.     }
  70.  
  71.     public static void setValue(String fileName, String path, Object value) {
  72.         File file = new File("plugins/EndiumMMO", fileName);
  73.         if (!file.exists()) {
  74.             try {
  75.                 file.createNewFile();
  76.             } catch (IOException e) {
  77.                 System.out.println("Failed to create " + fileName);
  78.             }
  79.         }
  80.         FileConfiguration config = YamlConfiguration.loadConfiguration(file);
  81.  
  82.         config.set(path, value);
  83.  
  84.         try {
  85.             config.save(file);
  86.         } catch (IOException e) {
  87.             e.printStackTrace();
  88.         }
  89.     }
  90.  
  91.     public static void setIntListInline(String fileName, String path, List<Integer> list) {
  92.         String inlineList = list.toString();
  93.  
  94.         setValue(fileName, path, inlineList);
  95.     }
  96.  
  97.     public static List<Integer> getIntListInline(String fileName, String path) {
  98.         String listString = getString(fileName, path);
  99.         if (listString == null || listString.isEmpty()) {
  100.             return null;
  101.         }
  102.  
  103.         return Arrays.stream(listString.replace("[", "").replace("]", "").split(","))
  104.                 .map(String::trim)
  105.                 .map(Integer::parseInt)
  106.                 .collect(Collectors.toList());
  107.     }
  108. }
  109.  
  110.  
  111. //TextColorUtil
  112. public class TextColorUtil {
  113.     public static String colorize(String text) {
  114.         return ChatColor.translateAlternateColorCodes('&', text);
  115.     }
  116.  
  117.     public static String removeColors(String text) {
  118.         return ChatColor.stripColor(text);
  119.     }
  120. }
  121.  
  122.  
  123. //GUI
  124. public class StatisticGUI {
  125.  
  126.     public StatisticGUI(Player player, Player target) {
  127.  
  128.         ConfigManager configManager = new ConfigManager();
  129.         String targetName = target.getName();
  130.         int sizeMenu = ConfigManager.getInt("guis/st.yml", "st.menu-size");
  131.         String stMenuTitle = ConfigManager.getString("lang.yml", "st.st-title").replace("{player}", targetName);
  132.         String stMenuColor = TextColorUtil.colorize(stMenuTitle);
  133.         Inventory inventory = Bukkit.createInventory(null, sizeMenu, stMenuColor);
  134.  
  135.         List<Integer> fillMaterialList = configManager.getIntListInline("guis/st.yml", "st.material-slots");
  136.         String materialFill = configManager.getString("guis/st.yml", "st.material-fill");
  137.         Material materialConfigFill = Material.valueOf(materialFill);
  138.         ItemStack materialStack = new ItemStack(materialConfigFill, 1);
  139.  
  140.         for (int slot : fillMaterialList) {
  141.             if (slot >= 0 && slot < inventory.getSize()) {
  142.                 inventory.setItem(slot, materialStack);
  143.             }
  144.  
  145.             player.openInventory(inventory);
  146.         }
  147.     }
  148. }
  149.  
Add Comment
Please, Sign In to add comment