JackOUT

Untitled

Jan 8th, 2022 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. package games.coob.core.block;
  2.  
  3. import lombok.Getter;
  4. import org.bukkit.Location;
  5. import org.bukkit.block.Block;
  6. import org.mineacademy.fo.Valid;
  7. import org.mineacademy.fo.collection.SerializedMap;
  8. import org.mineacademy.fo.constants.FoConstants;
  9. import org.mineacademy.fo.remain.CompMaterial;
  10. import org.mineacademy.fo.settings.YamlConfig;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.HashSet;
  14. import java.util.List;
  15. import java.util.Set;
  16.  
  17. public class BlockRegistry extends YamlConfig {
  18.  
  19.     @Getter
  20.     private static final BlockRegistry instance = new BlockRegistry();
  21.  
  22.     private Set<BlockData> registeredBlocks = new HashSet<>();
  23.  
  24.     private BlockRegistry() {
  25.         this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
  26.     }
  27.  
  28.     @Override
  29.     protected void onLoadFinish() {
  30.         this.registeredBlocks = this.getSet("Blocks", BlockData.class);
  31.     }
  32.  
  33.     @Override
  34.     protected SerializedMap serialize() {
  35.         return SerializedMap.of("Blocks", this.registeredBlocks);
  36.     }
  37.  
  38.     public void register(final Block block) {
  39.         final BlockData blockData = new BlockData();
  40.  
  41.         Valid.checkBoolean(!this.registeredBlocks.contains(blockData), block + " has already been registered");
  42.  
  43.         blockData.setLevel(1);
  44.         blockData.setLocation(block.getLocation());
  45.         blockData.setMaterial(CompMaterial.fromMaterial(block.getType()));
  46.  
  47.         this.registeredBlocks.add(blockData);
  48.         this.save();
  49.     }
  50.  
  51.     public void unregister(final Block block) {
  52.         // synchronized (registeredBlocks) { // synchronized is used for anyscronous processing (Common.runLaterAsync)
  53.         this.registeredBlocks.removeIf(blockData -> blockData.getLocation().getBlock().equals(block));
  54.  
  55.         this.save();
  56.     }
  57.  
  58.     public boolean isRegistered(final Block block) {
  59.         for (final BlockData blockData : this.registeredBlocks) {
  60.             if (blockData.getLocation().equals(block.getLocation()))
  61.                 return true;
  62.         }
  63.  
  64.         return false;
  65.     }
  66.  
  67.     public void increaseLevel(final Block block) {
  68.         for (final BlockData blockData : this.registeredBlocks)
  69.             if (blockData.getLocation().equals(block.getLocation()))
  70.                 blockData.increaseLevel();
  71.  
  72.         this.save();
  73.     }
  74.  
  75.     public int getLevel(final Block block) {
  76.         for (final BlockData blockData : this.registeredBlocks)
  77.             if (blockData.getLocation().equals(block.getLocation()))
  78.                 return blockData.getLevel();
  79.  
  80.         return 0;
  81.     }
  82.  
  83.     public List<Location> getLocations() {
  84.         final List<Location> locations = new ArrayList<>();
  85.  
  86.         for (final BlockData blockData : this.registeredBlocks)
  87.             locations.add(blockData.getLocation());
  88.  
  89.         return locations;
  90.     }
  91.  
  92.     /*
  93.     private final Set<String> registeredBlocks = new HashSet<>(); TODO 1
  94.  
  95.     @Override
  96.     protected void onLoadFinish() {
  97.         for (final String hash : this.getStringList("Blocks")) {
  98.             final Tuple<Location, CompMaterial> tuple = fromHash(hash);
  99.  
  100.             final Location location = tuple.getKey();
  101.             final CompMaterial material = tuple.getValue();
  102.  
  103.             final Block block = location.getBlock();
  104.  
  105.             if (block.getType() == material.getMaterial() && block.getData() == material.getData())
  106.                 this.registeredBlocks.add(hash);
  107.         }
  108.     }
  109.  
  110.     @Override
  111.     protected SerializedMap serialize() {
  112.         return SerializedMap.of("Blocks", this.registeredBlocks);
  113.     }
  114.  
  115.     public void register(final Block block) {
  116.         final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
  117.         Valid.checkBoolean(!this.registeredBlocks.contains(hash), block + " has already been registered");
  118.  
  119.         this.registeredBlocks.add(hash);
  120.         this.save();
  121.     }
  122.  
  123.     public void unregister(final Block block) {
  124.         final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
  125.         Valid.checkBoolean(this.registeredBlocks.contains(hash), block + " has not been registered");
  126.  
  127.         this.registeredBlocks.remove(hash);
  128.         this.save();
  129.     }
  130.  
  131.     public boolean isRegistered(final Block block) {
  132.         final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
  133.  
  134.         return this.registeredBlocks.contains(hash);
  135.     }
  136.  
  137.     public List<Location> getLocations() { // TODO 2
  138.         final List<Location> locations = new ArrayList<>();
  139.  
  140.         for (final String hash : this.registeredBlocks)
  141.             locations.add(this.fromHash(hash).getKey());
  142.  
  143.         return locations;
  144.     }
  145.  
  146.     private Tuple<Location, CompMaterial> fromHash(final String hash) { // TODO 3
  147.         final String[] split = hash.split(" \\| ");
  148.         final Location location = SerializeUtil.deserializeLocation(split[0]);
  149.         final CompMaterial material = CompMaterial.valueOf(split[1]);
  150.  
  151.         return new Tuple<>(location, material);
  152.     }
  153.  
  154.     private String toHash(final Location location, final CompMaterial material) {
  155.         return SerializeUtil.serializeLoc(location) + " | " + material;
  156.     }
  157.      */
  158. }
  159.  
Add Comment
Please, Sign In to add comment