Advertisement
JackOUT

Untitled

Nov 23rd, 2022
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.73 KB | None | 0 0
  1. package games.coob.laserturrets.model;
  2.  
  3. import games.coob.laserturrets.settings.TurretSettings;
  4. import lombok.AccessLevel;
  5. import lombok.Getter;
  6. import lombok.RequiredArgsConstructor;
  7. import org.bukkit.Location;
  8. import org.bukkit.entity.EntityType;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.mineacademy.fo.SerializeUtil;
  11. import org.mineacademy.fo.Valid;
  12. import org.mineacademy.fo.collection.SerializedMap;
  13. import org.mineacademy.fo.model.ConfigSerializable;
  14. import org.mineacademy.fo.model.Tuple;
  15. import org.mineacademy.fo.remain.CompMaterial;
  16.  
  17. import javax.annotation.Nullable;
  18. import java.util.*;
  19.  
  20. @Getter
  21. public class TurretData implements ConfigSerializable { // TODO create ammo
  22.  
  23.     private Location location;
  24.  
  25.     private CompMaterial material;
  26.  
  27.     private String id;
  28.  
  29.     private String type;
  30.  
  31.     private UUID owner;
  32.  
  33.     private boolean broken;
  34.  
  35.     private List<ItemStack> currentLoot = new ArrayList<>();
  36.  
  37.     private Set<UUID> playerBlacklist = new HashSet<>();
  38.  
  39.     private Set<EntityType> mobBlacklist = new HashSet<>();
  40.  
  41.     private boolean mobWhitelistEnabled;
  42.  
  43.     private boolean playerWhitelistEnabled;
  44.  
  45.     private List<TurretData.TurretLevel> turretLevels = new ArrayList<>();
  46.  
  47.     private double currentHealth;
  48.  
  49.     private int currentLevel;
  50.  
  51.     public void setLocation(final Location location) {
  52.         this.location = location;
  53.     }
  54.  
  55.     public void setMaterial(final CompMaterial material) {
  56.         this.material = material;
  57.     }
  58.  
  59.     public void setType(final String type) {
  60.         this.type = type;
  61.     }
  62.  
  63.     public void setId(final String id) {
  64.         this.id = id;
  65.     }
  66.  
  67.     public void setOwner(final UUID uuid) {
  68.         this.owner = uuid;
  69.     }
  70.  
  71.     public void setBroken(final boolean broken) {
  72.         this.broken = broken;
  73.  
  74.         if (this.isBroken())
  75.             this.currentLoot = randomItemPercentageList(this.getLevel(this.currentLevel).getLootChances());
  76.     }
  77.  
  78.     private List<ItemStack> randomItemPercentageList(final List<Tuple<ItemStack, Double>> lootChanceList) {
  79.         final List<ItemStack> items = new ArrayList<>();
  80.  
  81.         for (final Tuple<ItemStack, Double> lootChance : lootChanceList) {
  82.             if (lootChance != null) {
  83.                 final Random random = new Random();
  84.                 final double randomPercentage = random.nextDouble();
  85.  
  86.                 if (lootChance.getValue() >= randomPercentage)
  87.                     items.add(lootChance.getKey());
  88.             }
  89.         }
  90.  
  91.         return items;
  92.     }
  93.  
  94.     public void setCurrentLevel(final int level) {
  95.         this.currentLevel = level;
  96.     }
  97.  
  98.     public void setCurrentLoot(final @Nullable List<ItemStack> items) {
  99.         this.currentLoot = items;
  100.     }
  101.  
  102.     public void addPlayerToBlacklist(final UUID uuid) {
  103.         this.playerBlacklist.add(uuid);
  104.     }
  105.  
  106.     public void removePlayerFromBlacklist(final UUID uuid) {
  107.         if (this.playerBlacklist != null)
  108.             this.playerBlacklist.remove(uuid);
  109.     }
  110.  
  111.     public boolean isPlayerListedAsAlly(final UUID uuid) {
  112.         if (this.playerBlacklist != null)
  113.             return this.playerBlacklist.contains(uuid);
  114.         else return false;
  115.     }
  116.  
  117.     public void setPlayerBlacklist(final Set<UUID> playerBlacklist) {
  118.         this.playerBlacklist = playerBlacklist;
  119.     }
  120.  
  121.     public void setMobWhitelistEnabled(final boolean whitelist) {
  122.         this.mobWhitelistEnabled = whitelist;
  123.     }
  124.  
  125.     public void setPlayerWhitelistEnabled(final boolean whitelist) {
  126.         this.playerWhitelistEnabled = whitelist;
  127.     }
  128.  
  129.     public void setCurrentHealth(final double health) {
  130.         this.currentHealth = health;
  131.     }
  132.  
  133.     public void addMobToBlacklist(final EntityType entityType) {
  134.         this.mobBlacklist.add(entityType);
  135.     }
  136.  
  137.     public void removeMobFromBlacklist(final EntityType entityType) {
  138.         this.mobBlacklist.remove(entityType);
  139.     }
  140.  
  141.     public boolean isMobListedAsAlly(final EntityType entityType) {
  142.         return this.mobBlacklist.contains(entityType);
  143.     }
  144.  
  145.     public void setMobBlacklist(final @Nullable Set<EntityType> entityTypes) {
  146.         this.mobBlacklist = entityTypes;
  147.     }
  148.  
  149.     public TurretLevel getLevel(final int level) {
  150.         final boolean outOfBounds = level <= 0 || level >= this.turretLevels.size() + 1;
  151.  
  152.         if (!outOfBounds)
  153.             return this.turretLevels.get(level - 1);
  154.  
  155.         return null;
  156.     }
  157.  
  158.     public TurretLevel addLevel() {
  159.         final TurretData.TurretLevel level = new TurretData.TurretLevel(this);
  160.  
  161.         turretLevels.add(level);
  162.  
  163.         return this.turretLevels.get(this.turretLevels.size() - 1);
  164.     }
  165.  
  166.     public void createLevel(final String turretType) {
  167.         final TurretData.TurretLevel level = addLevel();
  168.         final List<TurretSettings.LevelData> levels = TurretSettings.findTurretSettings(turretType).getLevels();
  169.  
  170.         levels.get(levels.size() - 1).setLevelData(level);
  171.     }
  172.  
  173.     public void removeLevel(final int level) {
  174.         Valid.checkBoolean(getLevels() >= level, "Cannot remove level " + level + " because the turret only has " + getLevels() + " levels.");
  175.  
  176.         turretLevels.remove(level - 1);
  177.     }
  178.  
  179.     public void setTurretLevels(final List<TurretData.TurretLevel> levels) {
  180.         this.turretLevels = levels;
  181.     }
  182.  
  183.     public int getLevels() {
  184.         return this.turretLevels.size();
  185.     }
  186.  
  187.     private String toHash(final Location location, final CompMaterial material) {
  188.         return SerializeUtil.serializeLoc(location) + " | " + material;
  189.     }
  190.  
  191.     @Override
  192.     public SerializedMap serialize() {
  193.         final SerializedMap map = new SerializedMap();
  194.  
  195.         map.put("Block", toHash(this.location, this.material));
  196.         map.put("Id", this.id);
  197.         map.put("Type", this.type);
  198.         map.put("Owner", this.owner);
  199.         map.putIf("Player_Blacklist", this.playerBlacklist);
  200.         map.putIf("Mob_Blacklist", this.mobBlacklist);
  201.         map.put("Use_Player_Whitelist", this.playerWhitelistEnabled);
  202.         map.put("Use_Mob_Whitelist", this.mobWhitelistEnabled);
  203.         map.put("Current_Health", this.currentHealth);
  204.         map.put("Current_Level", this.currentLevel);
  205.         map.put("Destroyed", this.broken);
  206.         map.put("Levels", this.turretLevels);
  207.         map.putIf("Current_Loot", this.currentLoot);
  208.  
  209.         return map;
  210.     }
  211.  
  212.     public static TurretData deserialize(final SerializedMap map) {
  213.         final TurretData turretData = new TurretData();
  214.  
  215.         final String hash = map.getString("Block");
  216.         final String id = map.getString("Id");
  217.         final String type = map.getString("Type");
  218.         final UUID owner = map.getUUID("Owner");
  219.         final Set<UUID> blacklist = map.getSet("Player_Blacklist", UUID.class);
  220.         final Set<EntityType> entityTypes = map.getSet("Mob_Blacklist", EntityType.class);
  221.         final boolean playerWhitelist = map.getBoolean("Use_Player_Whitelist", false);
  222.         final boolean mobWhitelist = map.getBoolean("Use_Mob_Whitelist", false); // Add default value to load it if the key doesn't exist
  223.         final Integer level = map.getInteger("Current_Level");
  224.         final boolean destroyed = map.getBoolean("Destroyed", false);
  225.         final List<TurretLevel> levels = map.getList("Levels", TurretLevel.class, turretData);
  226.         final double currentHealth = map.getDouble("Current_Health");
  227.         final List<ItemStack> currentLoot = map.getList("Current_Loot", ItemStack.class);
  228.  
  229.         final String[] split = hash.split(" \\| ");
  230.         final Location location = SerializeUtil.deserializeLocation(split[0]);
  231.         final CompMaterial material = CompMaterial.valueOf(split[1]);
  232.  
  233.         turretData.setMaterial(material);
  234.         turretData.setLocation(location);
  235.         turretData.setId(id);
  236.         turretData.setType(type);
  237.         turretData.setOwner(owner);
  238.         turretData.setPlayerBlacklist(blacklist);
  239.         turretData.setMobBlacklist(entityTypes);
  240.         turretData.setPlayerWhitelistEnabled(playerWhitelist);
  241.         turretData.setMobWhitelistEnabled(mobWhitelist);
  242.         turretData.setCurrentLevel(level);
  243.         turretData.setCurrentHealth(currentHealth);
  244.         turretData.setBroken(destroyed);
  245.         turretData.setTurretLevels(levels);
  246.         turretData.setCurrentLoot(currentLoot);
  247.  
  248.         return turretData;
  249.     }
  250.  
  251.     @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
  252.     public final static class TurretLevel implements ConfigSerializable {
  253.  
  254.         private final TurretData turretData;
  255.  
  256.         @Getter
  257.         private double price;
  258.  
  259.         @Getter
  260.         private List<Tuple<ItemStack, Double>> lootChances = new ArrayList<>();
  261.  
  262.         @Getter
  263.         private int range;
  264.  
  265.         @Getter
  266.         private boolean laserEnabled;
  267.  
  268.         @Getter
  269.         private double laserDamage;
  270.  
  271.         @Getter
  272.         private int maxHealth;
  273.  
  274.         public void setPrice(final double price) {
  275.             this.price = price;
  276.         }
  277.  
  278.         public void setRange(final int range) {
  279.             this.range = range;
  280.         }
  281.  
  282.         public void setLaserEnabled(final boolean laserEnabled) {
  283.             this.laserEnabled = laserEnabled;
  284.         }
  285.  
  286.         public void setLaserDamage(final double laserDamage) {
  287.             this.laserDamage = laserDamage;
  288.         }
  289.  
  290.         public void setLootChances(final @Nullable List<Tuple<ItemStack, Double>> lootChances) {
  291.             this.lootChances = lootChances;
  292.         }
  293.  
  294.         public void setMaxHealth(final int health) {
  295.             this.maxHealth = health;
  296.         }
  297.  
  298.         public static TurretLevel deserialize(final SerializedMap map, final TurretData turretData) { // TODO cannot deserialize because there are null values in the map
  299.             final double price = map.getDouble("Price");
  300.             final List<Tuple<ItemStack, Double>> lootChances = map.getTupleList("Loot_Chances", ItemStack.class, Double.class);
  301.             final int range = map.getInteger("Range");
  302.             final boolean laserEnabled = map.getBoolean("Laser_Enabled");
  303.             final double laserDamage = map.getDouble("Laser_Damage");
  304.             final int maxHealth = map.getInteger("Max_Health");
  305.  
  306.             final TurretLevel level = new TurretLevel(turretData);
  307.  
  308.             level.setPrice(price);
  309.             level.setLootChances(lootChances);
  310.             level.setRange(range);
  311.             level.setLaserEnabled(laserEnabled);
  312.             level.setLaserDamage(laserDamage);
  313.             level.setMaxHealth(maxHealth);
  314.  
  315.             return level;
  316.         }
  317.  
  318.         @Override
  319.         public SerializedMap serialize() {
  320.             final SerializedMap map = new SerializedMap();
  321.  
  322.             map.put("Price", this.price);
  323.             map.put("Range", this.range);
  324.             map.put("Max_Health", this.maxHealth);
  325.             map.put("Laser_Enabled", this.laserEnabled);
  326.             map.put("Laser_Damage", this.laserDamage);
  327.             map.putIf("Loot_Chances", this.lootChances);
  328.  
  329.             return map;
  330.         }
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement