added strcuture constraints

This commit is contained in:
Niklas 2020-06-28 15:05:23 +02:00
parent ce00fcf596
commit 46e97dcb43
3 changed files with 64 additions and 2 deletions

View File

@ -3,7 +3,10 @@ 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 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; import java.util.List;
/** /**
@ -12,7 +15,8 @@ import java.util.List;
*/ */
public class MultiblockStructure { public class MultiblockStructure {
List<List<List<IBlockComponent>>> structure; private final List<List<List<IBlockComponent>>> structure;
private final List<IConstraint> constraints = new ArrayList<>();
/** /**
* Create a multiblock structure. * Create a multiblock structure.
@ -32,6 +36,10 @@ public class MultiblockStructure {
return structure.get(y).get(z).get(x); 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. * Checks if the multiblock is valid at the given location.
* @param location Location of a block in a multiblock structure. * @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++) { for (int x = 0; x < structure.get(y).get(z).size() ; x++) {
IBlockComponent 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 = getBlockLocation(location,offset,new BlockVector(x,y,z));
if(!currentComponent.isValidBlock(globalBlockLocation)){ if(!currentComponent.isValidBlock(globalBlockLocation)){
return false; return false;
@ -53,6 +61,17 @@ public class MultiblockStructure {
} }
} }
for (IConstraint constraint:constraints) {
if(!constraint.isValid(location,offset,this)){
return false;
}
}
return true; return true;
} }
public void addConstraint(IConstraint constraint){
this.constraints.add(constraint);
}
} }

View File

@ -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);
}

View File

@ -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<BlockVector> positions;
public SameBlockConstraint(List<BlockVector> 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;
}
}