Advertisement
JackOUT

Untitled

Dec 8th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. package games.coob.skywars.model;
  2.  
  3. import games.coob.skywars.util.Constants;
  4. import lombok.AccessLevel;
  5. import lombok.Getter;
  6. import lombok.NonNull;
  7. import lombok.RequiredArgsConstructor;
  8. import org.bukkit.Material;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.inventory.ItemStack;
  11. import org.bukkit.inventory.PlayerInventory;
  12. import org.bukkit.potion.PotionEffect;
  13. import org.bukkit.potion.PotionEffectType;
  14. import org.mineacademy.fo.Common;
  15. import org.mineacademy.fo.FileUtil;
  16. import org.mineacademy.fo.PlayerUtil;
  17. import org.mineacademy.fo.Valid;
  18. import org.mineacademy.fo.collection.SerializedMap;
  19. import org.mineacademy.fo.model.ConfigSerializable;
  20. import org.mineacademy.fo.remain.CompMaterial;
  21. import org.mineacademy.fo.settings.YamlConfig;
  22.  
  23. import javax.annotation.Nullable;
  24. import java.io.File;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30.  
  31. public final class SkyWarsKit extends YamlConfig {
  32.  
  33. /**
  34. * The list of loaded kits
  35. */
  36. private static final List<SkyWarsKit> loadedKits = new ArrayList<>();
  37.  
  38. /**
  39. * The kit name
  40. */
  41. @Getter
  42. private final String name;
  43.  
  44. /**
  45. * The icon to display in the menu for this kit
  46. */
  47. private ItemStack icon;
  48.  
  49. /**
  50. * The permission to use this kit
  51. */
  52. @Getter
  53. private String permission;
  54.  
  55. /**
  56. * The rarity of this kit
  57. */
  58. @Getter
  59. private String rarity;
  60.  
  61. /**
  62. * The tiers that players can upgrade into
  63. */
  64. private List<SkyWarsKitTier> tiers;
  65.  
  66. /**
  67. * Create a new arena kit by the name
  68. *
  69. * @param name
  70. */
  71. private SkyWarsKit(final String name) {
  72. this.name = name;
  73.  
  74. loadConfiguration(NO_DEFAULT, "kits/" + name + ".yml");
  75. }
  76.  
  77. /**
  78. * @see YamlConfig#onLoadFinish()
  79. */
  80. @Override
  81. protected void onLoadFinish() {
  82. this.icon = get("Icon", ItemStack.class);
  83. this.tiers = getList("Tiers", SkyWarsKitTier.class, this);
  84. this.permission = getString("Permission");
  85. this.rarity = getString("Rarity", "&fCOMMON");
  86. }
  87.  
  88. /**
  89. * Assign this kit to the given player
  90. *
  91. * @param player
  92. */
  93. public void assignTo(final Player player) {
  94. final SkyWarsPlayer cache = SkyWarsPlayer.getCache(player);
  95. Valid.checkBoolean(cache.hasArena() && cache.getMode() == ArenaJoinMode.PLAYING, "Kits may only be selected when playing an arena");
  96.  
  97. final Arena arena = cache.getArena();
  98. Valid.checkBoolean(canAssign(player), "Player " + player.getName() + " may not be assigned kit " + name);
  99. Valid.checkBoolean(arena.hasKits(), "SkyWars arena " + arena.getName() + " does not support kits!");
  100.  
  101. int tier = cache.getTier(this);
  102. SkyWarsKitTier kitTier = getTier(tier);
  103.  
  104. // Search for lower tiers if the one we want is not loaded/setup
  105. while (kitTier == null && tier > 1) {
  106. tier--;
  107.  
  108. kitTier = getTier(tier);
  109. }
  110.  
  111. Valid.checkNotNull(kitTier, "Could not find tier of kit " + name + " for player " + player.getName());
  112.  
  113. kitTier.applyFor(player, true);
  114. cache.setSkyWarsKit(this);
  115. }
  116.  
  117. public static void assignOnStart(final Player player, final SkyWarsKit skyWarsKit) {
  118. final SkyWarsPlayer cache = SkyWarsPlayer.getCache(player);
  119.  
  120. if (skyWarsKit != null && cache.getSelectedKit().equals(skyWarsKit.getName()))
  121. skyWarsKit.assignTo(player);
  122. }
  123.  
  124. /**
  125. * Return if the player may get this kit
  126. *
  127. * @param player
  128. * @return
  129. */
  130. public boolean canAssign(final Player player) {
  131. return permission == null || PlayerUtil.hasPerm(player, permission);
  132.  
  133. //if (permission != null && !PlayerUtil.hasPerm(player, permission))
  134. //return false;
  135. }
  136.  
  137. /**
  138. * Set the kit icon
  139. *
  140. * @param icon the icon to set
  141. */
  142. public void setIcon(final ItemStack icon) {
  143. this.icon = icon;
  144.  
  145. save();
  146. }
  147.  
  148. /**
  149. * Return the icon, or the default one if not set
  150. *
  151. * @return
  152. */
  153. public ItemStack getIcon() {
  154. return icon != null && icon.getType() != Material.AIR ? icon : new ItemStack(CompMaterial.IRON_SWORD.getMaterial());
  155. }
  156.  
  157. /**
  158. * Set the permission to use this kit, set to null to allow everyone
  159. *
  160. * @param permission the permission to set
  161. */
  162. public void setPermission(final String permission) {
  163. this.permission = permission;
  164.  
  165. save();
  166. }
  167.  
  168. /**
  169. * Set the rarity of this kit
  170. */
  171. public void setRarity(final String rarity) {
  172. this.rarity = rarity;
  173.  
  174. save();
  175. }
  176.  
  177. /**
  178. * Return arena tier from the given tier number
  179. *
  180. * @param tier
  181. * @return
  182. */
  183. @Nullable
  184. public SkyWarsKitTier getTier(final int tier) {
  185. return hasTier(tier) ? tiers.get(tier - 1) : null;
  186. }
  187.  
  188. /**
  189. * Return if the given tier is contained within this kit
  190. *
  191. * @param tier
  192. * @return
  193. */
  194. public boolean hasTier(final int tier) {
  195. return tiers.size() >= tier;
  196. }
  197.  
  198. /**
  199. * Add a new kit tier
  200. *
  201. * @return
  202. */
  203. public SkyWarsKitTier addTier() {
  204. final SkyWarsKitTier tier = new SkyWarsKitTier(this);
  205.  
  206. tiers.add(tier);
  207. save();
  208.  
  209. return tiers.get(tiers.size() - 1);
  210. }
  211.  
  212. /**
  213. * Removes a tier from this kit
  214. *
  215. * @param tier the tier level
  216. */
  217. public void removeTier(final int tier) {
  218. Valid.checkBoolean(getTiers() >= tier, "Cannot remove tier " + tier + " because the kit " + name + " only has " + tiers.size() + " tiers");
  219.  
  220. tiers.remove(tier - 1);
  221. save();
  222. }
  223.  
  224. /**
  225. * Return how many tiers this kit has
  226. *
  227. * @return
  228. */
  229. public int getTiers() {
  230. return tiers.size();
  231. }
  232.  
  233. @Override
  234. public void save() {
  235. final SerializedMap map = SerializedMap.ofArray();
  236. // Enable null values
  237. map.asMap().put("Tiers", tiers);
  238. map.asMap().put("Icon", icon);
  239. map.asMap().put("Permission", permission);
  240. map.asMap().put("Rarity", rarity);
  241.  
  242. for (final Map.Entry<String, Object> entry : map.entrySet())
  243. setNoSave(entry.getKey(), entry.getValue());
  244.  
  245. super.save();
  246. }
  247.  
  248. @Override
  249. public boolean equals(final Object object) {
  250. return object instanceof SkyWarsKit && ((SkyWarsKit) object).getName().equals(this.name);
  251. }
  252.  
  253. // ------–------–------–------–------–------–------–------–------–------–------–------–
  254. // Static
  255. // ------–------–------–------–------–------–------–------–------–------–------–------–
  256.  
  257. /**
  258. * Load all kits in the plugin
  259. */
  260. public static void loadKits() {
  261. loadedKits.clear();
  262.  
  263. final File[] kitFiles = FileUtil.getFiles("kits", "yml");
  264.  
  265. for (final File kitfile : kitFiles) {
  266. final String name = FileUtil.getFileName(kitfile);
  267.  
  268. loadOrCreateKit(name);
  269. }
  270. }
  271.  
  272. /**
  273. * Load or creates a new kit
  274. *
  275. * @param name
  276. * @return
  277. */
  278. public static SkyWarsKit loadOrCreateKit(final String name) {
  279. Valid.checkBoolean(!isKitLoaded(name), "Kit " + name + " is already loaded: " + getKitNames());
  280.  
  281. try {
  282. final SkyWarsKit skyWarsKit = new SkyWarsKit(name);
  283. loadedKits.add(skyWarsKit);
  284.  
  285. Common.log("[+] Loaded kit " + skyWarsKit.getName());
  286. return skyWarsKit;
  287.  
  288. } catch (final Throwable t) {
  289. Common.throwError(t, "Failed to load kit " + name);
  290. }
  291.  
  292. return null;
  293. }
  294.  
  295. /**
  296. * Permanently delete a kit
  297. *
  298. * @param skyWarsKit
  299. */
  300. public static void removeKit(@NonNull final SkyWarsKit skyWarsKit) {
  301. Valid.checkBoolean(isKitLoaded(skyWarsKit.getName()), "Kit " + skyWarsKit.getName() + " not loaded. Available: " + getKitNames());
  302.  
  303. skyWarsKit.delete();
  304.  
  305. loadedKits.remove(skyWarsKit);
  306. }
  307.  
  308. /**
  309. * Return true if the given kit exists
  310. *
  311. * @param name
  312. * @return
  313. */
  314. public static boolean isKitLoaded(final String name) {
  315. return findKit(name) != null;
  316. }
  317.  
  318. /**
  319. * Find a kit by name
  320. *
  321. * @param name
  322. * @return
  323. */
  324. public static SkyWarsKit findKit(@NonNull final String name) {
  325. for (final SkyWarsKit skyWarsKit : loadedKits)
  326. if (skyWarsKit.getName().equalsIgnoreCase(name))
  327. return skyWarsKit;
  328.  
  329. return null;
  330. }
  331.  
  332. /**
  333. * Get all loaded kits
  334. *
  335. * @return
  336. */
  337. public static List<SkyWarsKit> getKits() {
  338. return Collections.unmodifiableList(loadedKits);
  339. }
  340.  
  341. /**
  342. * Get all loaded kit names
  343. *
  344. * @return
  345. */
  346. public static List<String> getKitNames() {
  347. return Common.convert(loadedKits, SkyWarsKit::getName);
  348. }
  349.  
  350. // ------–------–------–------–------–------–------–------–------–------–------–------–
  351. // Kits
  352. // ------–------–------–------–------–------–------–------–------–------–------–------–
  353.  
  354. /**
  355. * Represents the tier data kit
  356. */
  357. @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
  358. public final static class SkyWarsKitTier implements ConfigSerializable {
  359.  
  360. /**
  361. * The kit in which this tier is set
  362. */
  363. private final SkyWarsKit skyWarsKit;
  364.  
  365. /**
  366. * Price to obtain this tier
  367. */
  368. @Getter
  369. private double price;
  370.  
  371. /**
  372. * The inventory content of this tier
  373. */
  374. @Getter
  375. private ItemStack[] content = new ItemStack[36];
  376.  
  377. /**
  378. * The armor content of this tier
  379. */
  380. @Getter
  381. private ItemStack[] armorContent = new ItemStack[4];
  382.  
  383. /**
  384. * The potion effects of this tier name:amplifier
  385. */
  386. private SerializedMap potionEffects = new SerializedMap();
  387.  
  388. /**
  389. * Set the price for this tier
  390. *
  391. * @param price the price to set
  392. */
  393. public void setPrice(final double price) {
  394. this.price = price;
  395.  
  396. skyWarsKit.save();
  397. }
  398.  
  399. /**
  400. * Set both the content and armor content
  401. *
  402. * @param content
  403. * @param armor
  404. */
  405. public void setAllContent(final ItemStack[] content, final ItemStack[] armor) {
  406. this.content = content;
  407. this.armorContent = armor;
  408.  
  409. skyWarsKit.save();
  410. }
  411.  
  412. /**
  413. * Set inventory content
  414. *
  415. * @param content the content to set
  416. */
  417. public void setContent(final ItemStack[] content) {
  418. this.content = content;
  419.  
  420. skyWarsKit.save();
  421. }
  422.  
  423. /**
  424. * Set armor content
  425. *
  426. * @param armorContent the armorContent to set
  427. */
  428. public void setArmorContent(final ItemStack[] armorContent) {
  429. this.armorContent = armorContent;
  430.  
  431. skyWarsKit.save();
  432. }
  433.  
  434. /**
  435. * Get the potion effect or 0 if not set
  436. *
  437. * @param type
  438. * @return
  439. */
  440. public int getPotionEffect(final PotionEffectType type) {
  441. return potionEffects.getInteger(type.getName(), 0);
  442. }
  443.  
  444. /**
  445. * Return true if the tier is completely empty
  446. *
  447. * @return
  448. */
  449. public boolean isEmpty() {
  450. return Valid.isNullOrEmpty(content) && Valid.isNullOrEmpty(armorContent) && potionEffects.isEmpty();
  451. }
  452.  
  453. /**
  454. * Set potion effects
  455. */
  456. public void setPotionEffect(final PotionEffectType type, final int level) {
  457. final String name = type.getName();
  458.  
  459. if (level == 0 && potionEffects.containsKey(name))
  460. potionEffects.asMap().remove(name);
  461. else
  462. potionEffects.override(type.getName(), level);
  463.  
  464. skyWarsKit.save();
  465. }
  466.  
  467. /**
  468. * Apply the tier for the given player
  469. *
  470. * @param player
  471. * @param giveInventory
  472. */
  473. public void applyFor(final Player player, final boolean giveInventory) {
  474. if (giveInventory) {
  475. final PlayerInventory inventory = player.getInventory();
  476.  
  477. inventory.setContents(content);
  478. inventory.setArmorContents(armorContent);
  479. }
  480.  
  481. for (final Entry<String, Object> effect : potionEffects.asMap().entrySet())
  482. player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(effect.getKey()), Integer.MAX_VALUE, (int) effect.getValue() - 1), true);
  483. }
  484.  
  485. /**
  486. * Create a new tier from saved config section
  487. *
  488. * @param map
  489. * @param skyWarsKit
  490. * @return
  491. */
  492. public static SkyWarsKitTier deserialize(final SerializedMap map, final SkyWarsKit skyWarsKit) {
  493. final SkyWarsKitTier tier = new SkyWarsKitTier(skyWarsKit);
  494.  
  495. final List<ItemStack> content = map.getList("Content", ItemStack.class);
  496. final List<ItemStack> armor = map.getList("Armor_Content", ItemStack.class);
  497.  
  498. tier.price = map.getDouble("Price", Constants.Defaults.TIER_UPGRADE_PRICE);
  499. tier.content = content.toArray(new ItemStack[content.size()]);
  500. tier.armorContent = armor.toArray(new ItemStack[armor.size()]);
  501.  
  502. tier.potionEffects = map.getMap("Potion_Effects");
  503.  
  504. return tier;
  505. }
  506.  
  507. /**
  508. * @see ConfigSerializable#serialize()
  509. */
  510. @Override
  511. public SerializedMap serialize() {
  512. return SerializedMap.ofArray(
  513. "Price", price,
  514. "Content", content,
  515. "Armor_Content", armorContent,
  516. "Potion_Effects", potionEffects);
  517. }
  518.  
  519. /**
  520. * @see Object#toString()
  521. */
  522. @Override
  523. public String toString() {
  524. return "Tier {content= " + Common.join(content, ", ", (item) -> item.getType().toString()) + "; armor=" + Common.join(armorContent, ", ", (item) -> item.getType().toString()) + "}";
  525. }
  526. }
  527. }
  528.  
  529.  
  530.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement