Advertisement
JackOUT

Untitled

Oct 4th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.50 KB | None | 0 0
  1. package games.coob.laserturrets.settings;
  2.  
  3. import games.coob.laserturrets.model.TurretData;
  4. import lombok.*;
  5. import org.bukkit.entity.EntityType;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.mineacademy.fo.Valid;
  8. import org.mineacademy.fo.collection.SerializedMap;
  9. import org.mineacademy.fo.model.ConfigSerializable;
  10. import org.mineacademy.fo.model.Tuple;
  11. import org.mineacademy.fo.settings.ConfigItems;
  12. import org.mineacademy.fo.settings.YamlConfig;
  13.  
  14. import java.util.*;
  15.  
  16. @Getter
  17. public class TurretSettings extends YamlConfig {
  18.  
  19.     private static final ConfigItems<TurretSettings> loadedTurretSettings = ConfigItems.fromFolder("turrets", TurretSettings.class);
  20.  
  21.     private List<LevelData> levels = new ArrayList<>();
  22.  
  23.     private Set<UUID> playerBlacklist = new HashSet<>();
  24.  
  25.     private Set<EntityType> mobBlacklist = new HashSet<>();
  26.  
  27.     private TurretSettings(final String turretType) {
  28.         this.setPathPrefix("Turret_Settings");
  29.         this.loadConfiguration("turret-data.yml", "turrets/" + turretType + "-turrets.yml");
  30.     }
  31.  
  32.     @Override
  33.     protected boolean saveComments() {
  34.         return false;
  35.     }
  36.  
  37.     @Override
  38.     protected void onLoad() {
  39.         if (!this.levels.isEmpty()) {
  40.             this.save();
  41.             System.out.println("hi");
  42.  
  43.             return;
  44.         }
  45.  
  46.         this.playerBlacklist = this.getSet("Player_Blacklist", UUID.class);
  47.         this.mobBlacklist = this.getSet("Mob_Blacklist", EntityType.class);
  48.         this.levels = this.loadLevels();
  49.     }
  50.  
  51.     @Override
  52.     protected void onSave() {
  53.         this.set("Player_Blacklist", this.playerBlacklist);
  54.         this.set("Mob_Blacklist", this.mobBlacklist);
  55.         this.set("Levels", this.levels);
  56.     }
  57.  
  58.     private List<LevelData> loadLevels() {
  59.         final List<LevelData> levels = new ArrayList<>();
  60.  
  61.         for (final Map.Entry<Integer, Object> entry : this.getMap("Levels", Integer.class, Object.class).entrySet()) {
  62.             final int level = entry.getKey();
  63.             final SerializedMap levelSettings = SerializedMap.of(entry.getValue());
  64.  
  65.             levels.add(LevelData.deserialize(levelSettings, level));
  66.         }
  67.  
  68.         return levels;
  69.     }
  70.  
  71.     public void setSettingsRange(final LevelData levelData, final int range) {
  72.         levelData.setRange(range);
  73.  
  74.         this.save();
  75.     }
  76.  
  77.     public int getLevelsSize() {
  78.         return this.levels.size();
  79.     }
  80.  
  81.     public LevelData addLevel() {
  82.         final List<TurretSettings.LevelData> levels = this.levels;
  83.         final LevelData level = new LevelData(levels.size() + 1);
  84.  
  85.         level.setLevelSettings(levels.get(levels.size() - 1), level);
  86.         levels.add(level);
  87.         this.save();
  88.  
  89.         return levels.get(levels.size() - 1);
  90.     }
  91.  
  92.     public void setLevelPrice(final LevelData levelData, final double price) {
  93.         levelData.setPrice(price);
  94.  
  95.         this.save();
  96.     }
  97.  
  98.     public LevelData getLevel(final int level) {
  99.         final boolean outOfBounds = level <= 0 || level >= this.levels.size() + 1;
  100.  
  101.         if (!outOfBounds)
  102.             return this.levels.get(level - 1);
  103.  
  104.         return null;
  105.     }
  106.  
  107.     public void removeLevel(final int settingsLevel) {
  108.         this.levels.remove(settingsLevel - 1);
  109.         this.save();
  110.     }
  111.  
  112.     public void setLaserEnabled(final LevelData levelData, final boolean laserEnabled) {
  113.         levelData.setLaserEnabled(laserEnabled);
  114.  
  115.         this.save();
  116.     }
  117.  
  118.     public void setLaserDamage(final LevelData levelData, final double damage) {
  119.         levelData.setLaserDamage(damage);
  120.  
  121.         this.save();
  122.     }
  123.  
  124.     public void setLootChances(final LevelData levelData, final List<Tuple<ItemStack, Double>> lootChances) {
  125.         levelData.setLootChances(lootChances);
  126.  
  127.         this.save();
  128.     }
  129.  
  130.     public void addPlayerToBlacklist(final UUID uuid) {
  131.         this.playerBlacklist.add(uuid);
  132.  
  133.         this.save();
  134.     }
  135.  
  136.     public void removePlayerFromBlacklist(final UUID uuid) {
  137.         if (this.playerBlacklist != null)
  138.             this.playerBlacklist.remove(uuid);
  139.  
  140.         this.save();
  141.     }
  142.  
  143.     public void addMobToBlacklist(final EntityType entityType) {
  144.         this.mobBlacklist.add(entityType);
  145.  
  146.         this.save();
  147.     }
  148.  
  149.     public void removeMobFromBlacklist(final EntityType entityType) {
  150.         if (this.mobBlacklist != null)
  151.             this.mobBlacklist.remove(entityType);
  152.  
  153.         this.save();
  154.     }
  155.  
  156.     @Getter
  157.     @Setter
  158.     @ToString
  159.     @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
  160.     public static class LevelData implements ConfigSerializable {
  161.  
  162.         private final int level;
  163.  
  164.         private double price;
  165.  
  166.         private boolean laserEnabled;
  167.  
  168.         private double laserDamage;
  169.  
  170.         private int range;
  171.  
  172.         private int health;
  173.  
  174.         private List<Tuple<ItemStack, Double>> lootChances;
  175.  
  176.         public void setLevelData(final TurretData.TurretLevel turretLevel) {
  177.             turretLevel.setRange(this.range);
  178.             turretLevel.setPrice(this.price);
  179.             turretLevel.setLaserEnabled(this.laserEnabled);
  180.             turretLevel.setLaserDamage(this.laserDamage);
  181.             turretLevel.setMaxHealth(this.health);
  182.             turretLevel.setLootChances(this.lootChances);
  183.         }
  184.  
  185.         public void setLevelSettings(final LevelData levelData, final LevelData newLevel) {
  186.             newLevel.setRange(levelData.getRange());
  187.             newLevel.setPrice(levelData.getPrice());
  188.             newLevel.setLaserEnabled(levelData.isLaserEnabled());
  189.             newLevel.setLaserDamage(levelData.getLaserDamage());
  190.             newLevel.setHealth(levelData.getHealth());
  191.             newLevel.setLootChances(levelData.getLootChances());
  192.  
  193.         }
  194.  
  195.         @Override
  196.         public SerializedMap serialize() {
  197.             final SerializedMap map = new SerializedMap();
  198.  
  199.             map.put("Range", this.range);
  200.             map.put("Price", this.price);
  201.             map.put("Health", this.health);
  202.             map.put("Enable_Laser_Pointer", this.laserEnabled);
  203.             map.put("Laser_Pointer_Damage", this.laserDamage);
  204.             map.putIf("Loot_Drops", this.lootChances);
  205.  
  206.             return map;
  207.         }
  208.  
  209.         public static LevelData deserialize(final SerializedMap map, final int level) {
  210.             final LevelData levelData = new LevelData(level);
  211.  
  212.             map.setRemoveOnGet(true);
  213.  
  214.             levelData.range = map.getInteger("Range");
  215.             Valid.checkNotNull(levelData.range, "Missing 'Range' key from level: " + map);
  216.  
  217.             levelData.health = map.getInteger("Health");
  218.             Valid.checkNotNull(levelData.health, "Missing 'Health' key from level: " + map);
  219.  
  220.             levelData.price = map.getDouble("Price");
  221.             Valid.checkNotNull(levelData.price, "Missing 'Price' key from level: " + map);
  222.  
  223.             levelData.laserEnabled = map.getBoolean("Enable_Laser_Pointer");
  224.             Valid.checkNotNull(levelData.laserEnabled, "Missing 'Enable_Laser_Pointers' key from level: " + map);
  225.  
  226.             levelData.laserDamage = map.getDouble("Laser_Pointer_Damage");
  227.             Valid.checkNotNull(levelData.price, "Missing 'Laser_Pointer_Damage' key from level: " + map);
  228.  
  229.             levelData.lootChances = map.getTupleList("Loot_Drops", ItemStack.class, Double.class);
  230.             Valid.checkNotNull(levelData.lootChances, "Missing 'Loot_Drops' key from level: " + map);
  231.  
  232.             Valid.checkBoolean(map.isEmpty(), "Found unrecognized level settings: " + map);
  233.  
  234.             return levelData;
  235.         }
  236.     }
  237.  
  238.     // -----------------------------------------------------------------
  239.     // Static
  240.     // -----------------------------------------------------------------
  241.  
  242.     public static void createSettings(@NonNull final String turretType) {
  243.         loadedTurretSettings.loadOrCreateItem(turretType, () -> new TurretSettings(turretType));
  244.     }
  245.  
  246.     public static void loadTurretSettings() {
  247.         loadedTurretSettings.loadItems();
  248.     }
  249.  
  250.     public static Collection<TurretSettings> getTurretSettings() {
  251.         return loadedTurretSettings.getItems();
  252.     }
  253.  
  254.     public static Set<String> getTurretSettingTypes() {
  255.         return loadedTurretSettings.getItemNames();
  256.     }
  257.  
  258.     public static boolean isTurretSettingLoaded(final String type) {
  259.         return loadedTurretSettings.isItemLoaded(type);
  260.     }
  261.  
  262.     public static TurretSettings findTurretSettings(final String type) {
  263.         return loadedTurretSettings.findItem(type);
  264.     }
  265. }
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement