diff --git a/src/main/java/org/kapelle/multiblock/MultiblockStructure.java b/src/main/java/org/kapelle/multiblock/MultiblockStructure.java index fba5f1e..16a2033 100644 --- a/src/main/java/org/kapelle/multiblock/MultiblockStructure.java +++ b/src/main/java/org/kapelle/multiblock/MultiblockStructure.java @@ -3,7 +3,10 @@ package org.kapelle.multiblock; import org.bukkit.Location; import org.bukkit.util.BlockVector; import org.kapelle.multiblock.block_component.IBlockComponent; +import org.kapelle.multiblock.block_component.ICheckFunction; +import org.kapelle.multiblock.constraint.IConstraint; +import java.util.ArrayList; import java.util.List; /** @@ -12,7 +15,8 @@ import java.util.List; */ public class MultiblockStructure { - List>> structure; + private final List>> structure; + private final List constraints = new ArrayList<>(); /** * Create a multiblock structure. @@ -32,6 +36,10 @@ public class MultiblockStructure { return structure.get(y).get(z).get(x); } + public Location getBlockLocation(Location location, BlockVector offset, BlockVector localPosition){ + return location.clone().subtract(offset).add(localPosition); + } + /** * Checks if the multiblock is valid at the given location. * @param location Location of a block in a multiblock structure. @@ -44,7 +52,7 @@ public class MultiblockStructure { for (int x = 0; x < structure.get(y).get(z).size() ; x++) { IBlockComponent currentComponent = getComponent(x,y,z); - Location globalBlockLocation = location.clone().subtract(offset).add(new BlockVector(x,y,z)); + Location globalBlockLocation = getBlockLocation(location,offset,new BlockVector(x,y,z)); if(!currentComponent.isValidBlock(globalBlockLocation)){ return false; @@ -53,6 +61,17 @@ public class MultiblockStructure { } } + for (IConstraint constraint:constraints) { + if(!constraint.isValid(location,offset,this)){ + return false; + } + } + return true; } + + public void addConstraint(IConstraint constraint){ + this.constraints.add(constraint); + } + } diff --git a/src/main/java/org/kapelle/multiblock/constraint/IConstraint.java b/src/main/java/org/kapelle/multiblock/constraint/IConstraint.java new file mode 100644 index 0000000..c6a6d31 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/constraint/IConstraint.java @@ -0,0 +1,9 @@ +package org.kapelle.multiblock.constraint; + +import org.bukkit.Location; +import org.bukkit.util.BlockVector; +import org.kapelle.multiblock.MultiblockStructure; + +public interface IConstraint { + public boolean isValid(Location coreBlockLocation, BlockVector offset, MultiblockStructure structure); +} diff --git a/src/main/java/org/kapelle/multiblock/constraint/SameBlockConstraint.java b/src/main/java/org/kapelle/multiblock/constraint/SameBlockConstraint.java new file mode 100644 index 0000000..38d4398 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/constraint/SameBlockConstraint.java @@ -0,0 +1,34 @@ +package org.kapelle.multiblock.constraint; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.util.BlockVector; +import org.kapelle.multiblock.MultiblockStructure; + +import java.security.InvalidParameterException; +import java.util.List; + +public class SameBlockConstraint implements IConstraint{ + + private final List positions; + + public SameBlockConstraint(List positions){ + this.positions = positions; + if (positions.size() <= 1){ + throw new InvalidParameterException("position need at least 2 positions"); + } + } + + @Override + public boolean isValid(Location coreBlockLocation, BlockVector offset, MultiblockStructure structure) { + Material material = structure.getBlockLocation(coreBlockLocation,offset,positions.get(0)).getBlock().getType(); + + for (BlockVector pos : positions) { + if(structure.getBlockLocation(coreBlockLocation,offset,pos).getBlock().getType() != material){ + return false; + } + } + + return true; + } +}