Advertisement
williambriggs

RandomThings

Aug 11th, 2022
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package me.will.randomthings;
  2.  
  3. import com.moandjiezana.toml.Toml;
  4. import me.will.randomthings.commands.ReloadCommand;
  5. import me.will.randomthings.listeners.BlockBreak;
  6. import org.bukkit.plugin.PluginManager;
  7.  
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.util.Objects;
  14.  
  15. public final class RandomThings extends JavaPlugin {
  16.  
  17.     private static RandomThings plugin;
  18.     public static Toml config;
  19.  
  20.     @Override
  21.     public void onEnable() {
  22.         // Plugin startup logic
  23.         plugin = this;
  24.         registerListeners();
  25.  
  26.         config = loadConfig();
  27.  
  28.  
  29.  
  30.         Objects.requireNonNull(this.getCommand("randomthings")).setExecutor(new ReloadCommand());
  31.  
  32.     }
  33.  
  34.     @SuppressWarnings("ResultOfMethodCallIgnored")
  35.     public Toml loadConfig() {
  36.         if(!getDataFolder().exists()) {
  37.             getDataFolder().mkdirs(); // Creating the directory as it may not exist
  38.         }
  39.         File file = new File(getDataFolder(), "config.toml"); // Assign a variable to the file
  40.         if(!file.exists()) {
  41.             try {
  42.                 Files.copy(Objects.requireNonNull(getResource("config.toml")), file.toPath()); // Copy the file out of our jar into our plugins Data Folder
  43.             } catch (IOException ex) {
  44.                 ex.printStackTrace();
  45.             }
  46.         }
  47.         return new Toml(
  48.                 new Toml().read(getResource("config.toml"))) // We'll use our internal file as default to prevent errors
  49.                 .read(file); // Reads the file from the Data Folder of our plugin
  50.     }
  51.  
  52.     public Toml getTomlConfig() {
  53.  
  54.         reloadPluginConfig();
  55.         return config;
  56.     }
  57.  
  58.     public Toml reloadPluginConfig() {
  59.         File file = new File(getDataFolder(), "config.toml");
  60.         return new Toml(
  61.                 new Toml().read(getResource("config.toml"))) // We'll use our internal file as default to prevent errors
  62.                 .read(file);
  63.     }
  64.  
  65.     private void registerListeners() {
  66.         PluginManager pm = getServer().getPluginManager();
  67.         pm.registerEvents(new BlockBreak(), this);
  68.     }
  69.  
  70.     public static RandomThings getPlugin() {
  71.         return plugin;
  72.     }
  73.  
  74.  
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement