reimplemented block componenets

This commit is contained in:
Niklas 2020-06-28 02:45:33 +02:00
parent ecda11b620
commit ce00fcf596
10 changed files with 92 additions and 82 deletions

View File

@ -1,70 +0,0 @@
package org.kapelle.multiblock;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Represents a single position in a multiblock.
* Can be a single type of block or multiple types.
* Custom valid check functions are supported,
*/
public class BlockComponent {
private final BlockComponentMode mode;
private final Set<Material> blocks;
private ICheckFunction customCheckFunction;
public BlockComponent(BlockComponentMode mode){
if(mode == BlockComponentMode.SET){
throw new RuntimeException("Component is impossible to satisfy because no valid blocks are given");
}
this.mode = mode;
this.blocks = Collections.emptySet();
}
public BlockComponent(BlockComponentMode mode, Set<Material> validBlocks){
this.mode = mode;
this.blocks = validBlocks;
}
public BlockComponent(ICheckFunction customCheckFunction){
this.mode = BlockComponentMode.CUSTOM;
this.blocks = Collections.emptySet();
this.customCheckFunction = customCheckFunction;
}
public BlockComponent(Material type){
this.blocks = new HashSet<>();
this.blocks.add(type);
this.mode = BlockComponentMode.SET;
}
public boolean isValidBlock(Location location){
if(this.mode == BlockComponentMode.CUSTOM){
return this.customCheckFunction.apply(location);
}else{
return isValidType(location.getBlock().getType());
}
}
private boolean isValidType(Material type){
switch (this.mode){
case AIR:
return type == Material.AIR;
case ANY:
return true;
case SET:
return blocks.contains(type);
default:
return false;
}
}
}

View File

@ -1,5 +0,0 @@
package org.kapelle.multiblock;
public enum BlockComponentMode {
ANY, AIR, SET, CUSTOM
}

View File

@ -6,12 +6,13 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BlockVector; import org.bukkit.util.BlockVector;
import org.kapelle.multiblock.block_component.IBlockComponent;
/** /**
* Listens for block placements and checks if it is part of a the multiblock structure. * Listens for block placements and checks if it is part of a the multiblock structure.
*/ */
public abstract class MultiblockListener implements Listener { public abstract class MultiblockListener implements Listener {
protected final BlockComponent coreBlock; protected final IBlockComponent coreBlock;
protected final BlockVector coreBlockOffset; protected final BlockVector coreBlockOffset;
protected final MultiblockStructure structure; protected final MultiblockStructure structure;

View File

@ -2,6 +2,7 @@ package org.kapelle.multiblock;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.util.BlockVector; import org.bukkit.util.BlockVector;
import org.kapelle.multiblock.block_component.IBlockComponent;
import java.util.List; import java.util.List;
@ -11,7 +12,7 @@ import java.util.List;
*/ */
public class MultiblockStructure { public class MultiblockStructure {
List<List<List<BlockComponent>>> structure; List<List<List<IBlockComponent>>> structure;
/** /**
* Create a multiblock structure. * Create a multiblock structure.
@ -19,15 +20,15 @@ public class MultiblockStructure {
* So you describe the layers from bottom to top. * So you describe the layers from bottom to top.
* @param structure structure in the format: [y][z][x] * @param structure structure in the format: [y][z][x]
*/ */
public MultiblockStructure(List<List<List<BlockComponent>>> structure){ public MultiblockStructure(List<List<List<IBlockComponent>>> structure){
this.structure = structure; this.structure = structure;
} }
public BlockComponent getComponent(BlockVector offset){ public IBlockComponent getComponent(BlockVector offset){
return getComponent(offset.getBlockX(),offset.getBlockY(),offset.getBlockZ()); return getComponent(offset.getBlockX(),offset.getBlockY(),offset.getBlockZ());
} }
public BlockComponent getComponent(int x, int y, int z){ public IBlockComponent getComponent(int x, int y, int z){
return structure.get(y).get(z).get(x); return structure.get(y).get(z).get(x);
} }
@ -42,7 +43,7 @@ public class MultiblockStructure {
for (int z = 0; z < structure.get(y).size(); z++) { for (int z = 0; z < structure.get(y).size(); z++) {
for (int x = 0; x < structure.get(y).get(z).size() ; x++) { for (int x = 0; x < structure.get(y).get(z).size() ; x++) {
BlockComponent currentComponent = getComponent(x,y,z); IBlockComponent currentComponent = getComponent(x,y,z);
Location globalBlockLocation = location.clone().subtract(offset).add(new BlockVector(x,y,z)); Location globalBlockLocation = location.clone().subtract(offset).add(new BlockVector(x,y,z));
if(!currentComponent.isValidBlock(globalBlockLocation)){ if(!currentComponent.isValidBlock(globalBlockLocation)){

View File

@ -0,0 +1,21 @@
package org.kapelle.multiblock.block_component;
import org.bukkit.Location;
public class AnyBlock implements IBlockComponent{
private static final AnyBlock singleton = new AnyBlock();
private AnyBlock(){
}
@Override
public boolean isValidBlock(Location location) {
return true;
}
public static AnyBlock getInstance(){
return AnyBlock.singleton;
}
}

View File

@ -0,0 +1,10 @@
package org.kapelle.multiblock.block_component;
import org.bukkit.Location;
/**
* Represents a single position in a multiblock.
*/
public interface IBlockComponent {
public boolean isValidBlock(Location location);
}

View File

@ -1,4 +1,4 @@
package org.kapelle.multiblock; package org.kapelle.multiblock.block_component;
import org.bukkit.Location; import org.bukkit.Location;

View File

@ -0,0 +1,16 @@
package org.kapelle.multiblock.block_component;
import org.bukkit.Location;
public class LambdaComponent implements IBlockComponent{
private final ICheckFunction checkFunction;
public LambdaComponent(ICheckFunction checkFunction){
this.checkFunction = checkFunction;
}
@Override
public boolean isValidBlock(Location location) {
return checkFunction.apply(location);
}
}

View File

@ -0,0 +1,19 @@
package org.kapelle.multiblock.block_component;
import org.bukkit.Location;
import org.bukkit.Material;
import java.util.Set;
public class MultipleBlocks implements IBlockComponent{
private final Set<Material> validMaterial;
public MultipleBlocks(Set<Material> validMaterial){
this.validMaterial = validMaterial;
}
@Override
public boolean isValidBlock(Location location) {
return this.validMaterial.contains(location.getBlock().getType());
}
}

View File

@ -0,0 +1,17 @@
package org.kapelle.multiblock.block_component;
import org.bukkit.Location;
import org.bukkit.Material;
public class SingleBlock implements IBlockComponent{
private final Material material;
public SingleBlock(Material material){
this.material = material;
}
@Override
public boolean isValidBlock(Location location) {
return location.getBlock().getType() == this.material;
}
}