Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games.coob.core.block;
- import lombok.Getter;
- import org.bukkit.Location;
- import org.bukkit.block.Block;
- import org.mineacademy.fo.Valid;
- import org.mineacademy.fo.collection.SerializedMap;
- import org.mineacademy.fo.constants.FoConstants;
- import org.mineacademy.fo.remain.CompMaterial;
- import org.mineacademy.fo.settings.YamlConfig;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class BlockRegistry extends YamlConfig {
- @Getter
- private static final BlockRegistry instance = new BlockRegistry();
- private Set<BlockData> registeredBlocks = new HashSet<>();
- private BlockRegistry() {
- this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
- }
- @Override
- protected void onLoadFinish() {
- this.registeredBlocks = this.getSet("Blocks", BlockData.class);
- }
- @Override
- protected SerializedMap serialize() {
- return SerializedMap.of("Blocks", this.registeredBlocks);
- }
- public void register(final Block block) {
- final BlockData blockData = new BlockData();
- Valid.checkBoolean(!this.registeredBlocks.contains(blockData), block + " has already been registered");
- blockData.setLevel(1);
- blockData.setLocation(block.getLocation());
- blockData.setMaterial(CompMaterial.fromMaterial(block.getType()));
- this.registeredBlocks.add(blockData);
- this.save();
- }
- public void unregister(final Block block) {
- // synchronized (registeredBlocks) { // synchronized is used for anyscronous processing (Common.runLaterAsync)
- this.registeredBlocks.removeIf(blockData -> blockData.getLocation().getBlock().equals(block));
- this.save();
- }
- public boolean isRegistered(final Block block) {
- for (final BlockData blockData : this.registeredBlocks) {
- if (blockData.getLocation().equals(block.getLocation()))
- return true;
- }
- return false;
- }
- public void increaseLevel(final Block block) {
- for (final BlockData blockData : this.registeredBlocks)
- if (blockData.getLocation().equals(block.getLocation()))
- blockData.increaseLevel();
- this.save();
- }
- public int getLevel(final Block block) {
- for (final BlockData blockData : this.registeredBlocks)
- if (blockData.getLocation().equals(block.getLocation()))
- return blockData.getLevel();
- return 0;
- }
- public List<Location> getLocations() {
- final List<Location> locations = new ArrayList<>();
- for (final BlockData blockData : this.registeredBlocks)
- locations.add(blockData.getLocation());
- return locations;
- }
- /*
- private final Set<String> registeredBlocks = new HashSet<>(); TODO 1
- @Override
- protected void onLoadFinish() {
- for (final String hash : this.getStringList("Blocks")) {
- final Tuple<Location, CompMaterial> tuple = fromHash(hash);
- final Location location = tuple.getKey();
- final CompMaterial material = tuple.getValue();
- final Block block = location.getBlock();
- if (block.getType() == material.getMaterial() && block.getData() == material.getData())
- this.registeredBlocks.add(hash);
- }
- }
- @Override
- protected SerializedMap serialize() {
- return SerializedMap.of("Blocks", this.registeredBlocks);
- }
- public void register(final Block block) {
- final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
- Valid.checkBoolean(!this.registeredBlocks.contains(hash), block + " has already been registered");
- this.registeredBlocks.add(hash);
- this.save();
- }
- public void unregister(final Block block) {
- final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
- Valid.checkBoolean(this.registeredBlocks.contains(hash), block + " has not been registered");
- this.registeredBlocks.remove(hash);
- this.save();
- }
- public boolean isRegistered(final Block block) {
- final String hash = this.toHash(block.getLocation(), CompMaterial.fromMaterial(block.getType()));
- return this.registeredBlocks.contains(hash);
- }
- public List<Location> getLocations() { // TODO 2
- final List<Location> locations = new ArrayList<>();
- for (final String hash : this.registeredBlocks)
- locations.add(this.fromHash(hash).getKey());
- return locations;
- }
- private Tuple<Location, CompMaterial> fromHash(final String hash) { // TODO 3
- final String[] split = hash.split(" \\| ");
- final Location location = SerializeUtil.deserializeLocation(split[0]);
- final CompMaterial material = CompMaterial.valueOf(split[1]);
- return new Tuple<>(location, material);
- }
- private String toHash(final Location location, final CompMaterial material) {
- return SerializeUtil.serializeLoc(location) + " | " + material;
- }
- */
- }
Add Comment
Please, Sign In to add comment