JackOUT

Untitled

Apr 2nd, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.78 KB | None | 0 0
  1. package games.coob.laserturrets;
  2.  
  3. import games.coob.laserturrets.database.TurretsDatabase;
  4. import games.coob.laserturrets.hook.HookSystem;
  5. import games.coob.laserturrets.hook.VaultHook;
  6. import games.coob.laserturrets.model.TurretData;
  7. import games.coob.laserturrets.sequence.Sequence;
  8. import games.coob.laserturrets.settings.Settings;
  9. import games.coob.laserturrets.settings.TurretSettings;
  10. import games.coob.laserturrets.settings.TurretType;
  11. import games.coob.laserturrets.task.*;
  12. import games.coob.laserturrets.util.Hologram;
  13. import games.coob.laserturrets.util.SkullCreator;
  14. import games.coob.laserturrets.util.TurretUtil;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.mineacademy.fo.Common;
  17. import org.mineacademy.fo.ReflectionUtil;
  18. import org.mineacademy.fo.Valid;
  19. import org.mineacademy.fo.exception.CommandException;
  20. import org.mineacademy.fo.plugin.SimplePlugin;
  21.  
  22. import java.io.File;
  23. import java.io.FileWriter;
  24. import java.io.IOException;
  25. import java.nio.file.Files;
  26. import java.util.Arrays;
  27. import java.util.function.Function;
  28.  
  29. /**
  30.  * PluginTemplate is a simple template you can use every time you make
  31.  * a new plugin. This will save you time because you no longer have to
  32.  * recreate the same skeleton and features each time.
  33.  * <p>
  34.  * It uses Foundation for fast and efficient development process.
  35.  */
  36. public final class LaserTurrets extends SimplePlugin {
  37.  
  38.     /**
  39.      * Automatically perform login ONCE when the plugin starts.
  40.      */
  41.     @Override
  42.     protected void onPluginStart() {
  43.         if (folderContainsOldTurretFiles("turrets")) {
  44.             moveTurretsFolder();
  45.             createReadmeFile();
  46.         }
  47.  
  48.         for (final String type : getTypes()) {
  49.             final TurretType turretType = findEnum(TurretType.class, type, null, "No such such turret type. Available: " + Arrays.toString(getTypes()) + ".");
  50.             TurretSettings.createTurretType(type, turretType);
  51.  
  52.             final TurretSettings settings = TurretSettings.findByName(type);
  53.  
  54.             if (settings.getToolItem() == null) {
  55.                 final ItemStack itemStack = SkullCreator.itemFromBase64(settings.getHeadTexture());
  56.                 settings.setToolItem(itemStack);
  57.             }
  58.         }
  59.  
  60.         if (!VaultHook.setupEconomy(getServer()) && Settings.CurrencySection.USE_VAULT) {
  61.             Common.log("Disabled due to no Vault dependency found (an economy plugin is also required)!", getDescription().getName());
  62.             getServer().getPluginManager().disablePlugin(this);
  63.             return;
  64.         }
  65.  
  66.         if (Settings.DatabaseSection.ENABLE_MYSQL)
  67.             TurretsDatabase.getInstance().connect(Settings.DatabaseSection.HOST, Settings.DatabaseSection.PORT, Settings.DatabaseSection.DATABASE, Settings.DatabaseSection.USER, Settings.DatabaseSection.PASSWORD);
  68.  
  69.         new UpdateChecker(this, 105494).getVersion(version -> {
  70.             if (!this.getDescription().getVersion().equals(version))
  71.                 Common.log("There is a new update available (v" + version + ").");
  72.         });
  73.     }
  74.  
  75.     private void moveTurretsFolder() {
  76.         final File turretsFolder = new File(this.getDataFolder(), "turrets");
  77.         final File oldTurretsFolder = new File(this.getDataFolder(), "old-turrets");
  78.  
  79.         if (oldTurretsFolder.exists())
  80.             return;
  81.  
  82.         if (!oldTurretsFolder.mkdir()) {
  83.             Common.log("Error: failed to create old-turrets folder");
  84.             return;
  85.         }
  86.  
  87.         for (final File file : turretsFolder.listFiles()) {
  88.             try {
  89.                 Files.move(file.toPath(), new File(oldTurretsFolder, file.getName()).toPath());
  90.             } catch (final IOException e) {
  91.                 Common.log("Error moving file " + file.getName() + ": " + e.getMessage());
  92.             }
  93.         }
  94.  
  95.         Common.log("All files from turrets folder moved to old-turrets folder");
  96.     }
  97.  
  98.     public boolean folderContainsOldTurretFiles(final String folderName) {
  99.         final File pluginDataFolder = new File(this.getDataFolder(), folderName);
  100.  
  101.         if (!pluginDataFolder.isDirectory()) {
  102.             return false;
  103.         }
  104.  
  105.         for (final File file : pluginDataFolder.listFiles()) {
  106.             if (file.getName().contains("turret")) {
  107.                 return true;
  108.             }
  109.         }
  110.  
  111.         return false;
  112.     }
  113.  
  114.  
  115.     public void createReadmeFile() {
  116.         final File oldTurretsFolder = new File(this.getDataFolder(), "old-turrets");
  117.  
  118.         if (!oldTurretsFolder.exists() || !oldTurretsFolder.isDirectory()) {
  119.             Common.log("Error: old-turrets folder not found or is not a directory");
  120.             return;
  121.         }
  122.  
  123.         final File readmeFile = new File(oldTurretsFolder, "README.txt");
  124.  
  125.         try (FileWriter writer = new FileWriter(readmeFile)) {
  126.             writer.write("Update to v2.0.0 information\n\n");
  127.             writer.write("This folder contains all the files that were previously in the 'turrets' folder. ");
  128.             writer.write("These files have been moved here because I've made some new changes and recoded some parts of the plugin.\n\n");
  129.             writer.write("If you would like to maintain your previous settings, you can copy paste the specific sections from the 'old-turrets' to the files in the 'turrets' folder. ");
  130.             writer.write("Feel free to delete this folder if you no longer need these files.\n\n");
  131.             writer.write("You may use a YAML validator to check if your files are correctly formatted and prevent the plugin from breaking. ");
  132.             writer.write("If you encounter any issues, you can contact me on our discord server (https://discord.gg/2rgvQbHsSW).\n\n");
  133.             writer.write("NOTE: This update does not impact already created turrets, only the settings have been modified.");
  134.         } catch (final IOException e) {
  135.             Common.log("Error creating README file: " + e.getMessage());
  136.             return;
  137.         }
  138.  
  139.         Common.log("README file created in the old-turrets folder");
  140.     }
  141.  
  142.     @Override
  143.     protected void onPluginReload() {
  144.         Sequence.reload();
  145.         Hologram.deleteAll();
  146.     }
  147.  
  148.     @Override
  149.     protected void onPluginStop() {
  150.         Sequence.reload();
  151.         Hologram.deleteAll();
  152.     }
  153.  
  154.     public String[] getTypes() { // TODO get from database
  155.         return new String[]{
  156.                 "arrow", "beam", "fireball"
  157.         };
  158.     }
  159.  
  160.     /**
  161.      * Automatically perform login when the plugin starts and each time it is reloaded.
  162.      */
  163.     @Override
  164.     protected void onReloadablesStart() {
  165.         TurretData.loadTurrets(); // TODO fix error
  166.         //
  167.         // Add your own plugin parts to load automatically here
  168.         // Please see @AutoRegister for parts you do not have to register manually
  169.         //
  170.         // Load our dependency system
  171.         try {
  172.             HookSystem.loadDependencies();
  173.         } catch (final Throwable throwable) {
  174.             Common.throwError(throwable, "Error while loading " + this.getDataFolder().getName() + " dependencies!");
  175.         }
  176.  
  177.         Common.runTimer(20, new ArrowTask());
  178.         Common.runTimer(25, new FireballTask());
  179.         Common.runTimer(30, new BeamTask());
  180.         Common.runTimer(2, new LaserPointerTask());
  181.  
  182.         Common.runLater(() -> {
  183.             for (final TurretData turretData : TurretData.getRegisteredTurrets()) {
  184.                 TurretUtil.updateHologramAndTexture(turretData);
  185.             }
  186.         });
  187.  
  188.         if (Settings.TurretSection.DISPLAY_HOLOGRAM)
  189.             Common.runTimer(20, new HologramTask());
  190.     }
  191.  
  192.     private <T extends Enum<T>> T findEnum(final Class<T> enumType, final String name, final Function<T, Boolean> condition, final String falseMessage) throws CommandException {
  193.         T found = null;
  194.  
  195.         try {
  196.             found = ReflectionUtil.lookupEnum(enumType, name);
  197.  
  198.             if (!condition.apply(found))
  199.                 found = null;
  200.  
  201.         } catch (final Throwable t) {
  202.             // Not found, pass through below to error out
  203.         }
  204.  
  205.         Valid.checkNotNull(found, falseMessage.replace("{enum}", name).replace("{available}", Common.join(enumType.getEnumConstants())));
  206.         return found;
  207.     }
  208.  
  209.     /* ------------------------------------------------------------------------------- */
  210.     /* Static */
  211.     /* ------------------------------------------------------------------------------- */
  212.  
  213.     /**
  214.      * Return the instance of this plugin, which simply refers to a static
  215.      * field already created for you in SimplePlugin but casts it to your
  216.      * specific plugin instance for your convenience.
  217.      *
  218.      * @return
  219.      */
  220.     public static LaserTurrets getInstance() {
  221.         return (LaserTurrets) SimplePlugin.getInstance();
  222.     }
  223. }
  224.  
Add Comment
Please, Sign In to add comment