UltimateStacker/UltimateStacker/src/main/java/com.craftaro.ultimatestacker/stackable/block/BlockStackManagerImpl.java

63 lines
2.0 KiB
Java

package com.craftaro.ultimatestacker.stackable.block;
import com.craftaro.ultimatestacker.api.stack.block.BlockStack;
import com.craftaro.ultimatestacker.api.stack.block.BlockStackManager;
import com.songoda.core.compatibility.CompatibleMaterial;
import org.bukkit.Location;
import org.bukkit.block.Block;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class BlockStackManagerImpl implements BlockStackManager {
private final Map<Location, BlockStack> registeredBlocks = new HashMap<>();
public void addBlocks(Map<Location, BlockStack> blocks) {
this.registeredBlocks.putAll(blocks);
}
public BlockStack addBlock(BlockStack blockStack) {
this.registeredBlocks.put(roundLocation(blockStack.getLocation()), blockStack);
return blockStack;
}
public BlockStack removeBlock(Location location) {
return registeredBlocks.remove(roundLocation(location));
}
public BlockStack getBlock(Location location) {
return this.registeredBlocks.get(location);
}
public BlockStack getBlock(Block block, CompatibleMaterial material) {
return this.getBlock(block.getLocation());
}
public BlockStack createBlock(Location location, CompatibleMaterial material) {
return this.registeredBlocks.computeIfAbsent(location, b -> new BlockStackImpl(material, location));
}
public BlockStack createBlock(Block block) {
return this.createBlock(block.getLocation(), CompatibleMaterial.getMaterial(block));
}
public boolean isBlock(Location location) {
return this.registeredBlocks.get(location) != null;
}
public Collection<BlockStack> getStacks() {
return Collections.unmodifiableCollection(this.registeredBlocks.values());
}
private Location roundLocation(Location location) {
location = location.clone();
location.setX(location.getBlockX());
location.setY(location.getBlockY());
location.setZ(location.getBlockZ());
return location;
}
}