JackOUT

Untitled

Feb 18th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package games.coob.laserturrets;
  2.  
  3. import games.coob.laserturrets.database.TurretsDatabase;
  4. import games.coob.laserturrets.hook.VaultHook;
  5. import games.coob.laserturrets.model.TurretData;
  6. import games.coob.laserturrets.model.TurretRegistry;
  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 org.bukkit.block.Block;
  15. import org.bukkit.block.BlockFace;
  16. import org.bukkit.block.Skull;
  17. import org.mineacademy.fo.Common;
  18. import org.mineacademy.fo.MinecraftVersion;
  19. import org.mineacademy.fo.ReflectionUtil;
  20. import org.mineacademy.fo.Valid;
  21. import org.mineacademy.fo.exception.CommandException;
  22. import org.mineacademy.fo.plugin.SimplePlugin;
  23. import org.mineacademy.fo.remain.CompMaterial;
  24.  
  25. import java.util.Arrays;
  26. import java.util.function.Function;
  27.  
  28. /**
  29. * PluginTemplate is a simple template you can use every time you make
  30. * a new plugin. This will save you time because you no longer have to
  31. * recreate the same skeleton and features each time.
  32. * <p>
  33. * It uses Foundation for fast and efficient development process.
  34. */
  35. public final class LaserTurrets extends SimplePlugin { // TODO use HookManager.deposit(); instead of vault hook
  36.  
  37. /**
  38. * Automatically perform login ONCE when the plugin starts.
  39. */
  40. @Override
  41. protected void onPluginStart() {
  42. Common.runLater(TurretRegistry::getInstance);
  43.  
  44. for (final String type : getTypes()) {
  45. final TurretType turretType = findEnum(TurretType.class, type, null, "No such such turret type. Available: " + Arrays.toString(getTypes()) + ".");
  46. TurretSettings.createTurretType(type, turretType);
  47. }
  48.  
  49. if (!VaultHook.setupEconomy(getServer()) && Settings.CurrencySection.USE_VAULT) {
  50. Common.log("[LaserTurrets] - Disabled due to no Vault dependency found (an economy plugin is also required)!", getDescription().getName());
  51. getServer().getPluginManager().disablePlugin(this);
  52. return;
  53. }
  54.  
  55. if (Settings.DatabaseSection.ENABLE_MYSQL)
  56. TurretsDatabase.getInstance().connect(Settings.DatabaseSection.HOST, Settings.DatabaseSection.PORT, Settings.DatabaseSection.DATABASE, Settings.DatabaseSection.USER, Settings.DatabaseSection.PASSWORD);
  57. }
  58.  
  59. private <T extends Enum<T>> T findEnum(final Class<T> enumType, final String name, final Function<T, Boolean> condition, final String falseMessage) throws CommandException {
  60. T found = null;
  61.  
  62. try {
  63. found = ReflectionUtil.lookupEnum(enumType, name);
  64.  
  65. if (!condition.apply(found))
  66. found = null;
  67.  
  68. } catch (final Throwable t) {
  69. // Not found, pass through below to error out
  70. }
  71.  
  72. Valid.checkNotNull(found, falseMessage.replace("{enum}", name).replace("{available}", Common.join(enumType.getEnumConstants())));
  73. return found;
  74. }
  75.  
  76. @Override
  77. protected void onPluginReload() {
  78. TurretRegistry.getInstance().save();
  79. Sequence.reload();
  80. Hologram.deleteAll();
  81. }
  82.  
  83. @Override
  84. protected void onPluginStop() {
  85. TurretRegistry.getInstance().save();
  86. Sequence.reload();
  87. Hologram.deleteAll();
  88. }
  89.  
  90. public String[] getTypes() { // TODO get from database
  91. return new String[]{
  92. "arrow", "beam", "fireball"
  93. };
  94. }
  95.  
  96. /**
  97. * Automatically perform login when the plugin starts and each time it is reloaded.
  98. */
  99. @Override
  100. protected void onReloadablesStart() {
  101. //
  102. // Add your own plugin parts to load automatically here
  103. // Please see @AutoRegister for parts you do not have to register manually
  104. //
  105.  
  106. Common.runTimer(20, new ArrowTask());
  107. Common.runTimer(25, new FireballTask());
  108. Common.runTimer(2, new LaserPointerTask());
  109.  
  110. Common.runLater(() -> {
  111. for (final TurretData turretData : TurretRegistry.getInstance().getRegisteredTurrets()) {
  112. final String type = turretData.getType();
  113. final TurretSettings settings = TurretSettings.findByName(type);
  114. final Block skullBlock = turretData.getLocation().getBlock().getRelative(BlockFace.UP);
  115.  
  116. if (CompMaterial.isSkull(skullBlock.getType())) {
  117. final Skull state = (Skull) skullBlock.getState();
  118. SkullCreator.mutateBlockState(state, settings.getBase64Texture());
  119. state.update(false, false);
  120. }
  121.  
  122. if (Settings.TurretSection.DISPLAY_HOLOGRAM)
  123. TurretRegistry.getInstance().updateHologram(turretData);
  124. }
  125.  
  126. TurretRegistry.getInstance().save();
  127. });
  128.  
  129. if (Settings.TurretSection.DISPLAY_HOLOGRAM)
  130. Common.runTimer(20, new HologramTask());
  131.  
  132. if (MinecraftVersion.atLeast(MinecraftVersion.V.v1_9))
  133. Common.runTimer(30, new BeamTask());
  134. }
  135.  
  136. /* ------------------------------------------------------------------------------- */
  137. /* Static */
  138. /* ------------------------------------------------------------------------------- */
  139.  
  140. /**
  141. * Return the instance of this plugin, which simply refers to a static
  142. * field already created for you in SimplePlugin but casts it to your
  143. * specific plugin instance for your convenience.
  144. *
  145. * @return
  146. */
  147. public static LaserTurrets getInstance() {
  148. return (LaserTurrets) SimplePlugin.getInstance();
  149. }
  150. }
  151.  
Add Comment
Please, Sign In to add comment