From ce00fcf59669fb61a9d24c8836d0fd099dbd3caf Mon Sep 17 00:00:00 2001 From: Niklas Date: Sun, 28 Jun 2020 02:45:33 +0200 Subject: [PATCH] reimplemented block componenets --- .../kapelle/multiblock/BlockComponent.java | 70 ------------------- .../multiblock/BlockComponentMode.java | 5 -- .../multiblock/MultiblockListener.java | 3 +- .../multiblock/MultiblockStructure.java | 11 +-- .../multiblock/block_component/AnyBlock.java | 21 ++++++ .../block_component/IBlockComponent.java | 10 +++ .../{ => block_component}/ICheckFunction.java | 2 +- .../block_component/LambdaComponent.java | 16 +++++ .../block_component/MultipleBlocks.java | 19 +++++ .../block_component/SingleBlock.java | 17 +++++ 10 files changed, 92 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/org/kapelle/multiblock/BlockComponent.java delete mode 100644 src/main/java/org/kapelle/multiblock/BlockComponentMode.java create mode 100644 src/main/java/org/kapelle/multiblock/block_component/AnyBlock.java create mode 100644 src/main/java/org/kapelle/multiblock/block_component/IBlockComponent.java rename src/main/java/org/kapelle/multiblock/{ => block_component}/ICheckFunction.java (76%) create mode 100644 src/main/java/org/kapelle/multiblock/block_component/LambdaComponent.java create mode 100644 src/main/java/org/kapelle/multiblock/block_component/MultipleBlocks.java create mode 100644 src/main/java/org/kapelle/multiblock/block_component/SingleBlock.java diff --git a/src/main/java/org/kapelle/multiblock/BlockComponent.java b/src/main/java/org/kapelle/multiblock/BlockComponent.java deleted file mode 100644 index a9870ab..0000000 --- a/src/main/java/org/kapelle/multiblock/BlockComponent.java +++ /dev/null @@ -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 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 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; - - } - } - -} diff --git a/src/main/java/org/kapelle/multiblock/BlockComponentMode.java b/src/main/java/org/kapelle/multiblock/BlockComponentMode.java deleted file mode 100644 index c8c1460..0000000 --- a/src/main/java/org/kapelle/multiblock/BlockComponentMode.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.kapelle.multiblock; - -public enum BlockComponentMode { - ANY, AIR, SET, CUSTOM -} diff --git a/src/main/java/org/kapelle/multiblock/MultiblockListener.java b/src/main/java/org/kapelle/multiblock/MultiblockListener.java index 8e0aca3..1e40cf7 100644 --- a/src/main/java/org/kapelle/multiblock/MultiblockListener.java +++ b/src/main/java/org/kapelle/multiblock/MultiblockListener.java @@ -6,12 +6,13 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.java.JavaPlugin; 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. */ public abstract class MultiblockListener implements Listener { - protected final BlockComponent coreBlock; + protected final IBlockComponent coreBlock; protected final BlockVector coreBlockOffset; protected final MultiblockStructure structure; diff --git a/src/main/java/org/kapelle/multiblock/MultiblockStructure.java b/src/main/java/org/kapelle/multiblock/MultiblockStructure.java index dc87ba4..fba5f1e 100644 --- a/src/main/java/org/kapelle/multiblock/MultiblockStructure.java +++ b/src/main/java/org/kapelle/multiblock/MultiblockStructure.java @@ -2,6 +2,7 @@ package org.kapelle.multiblock; import org.bukkit.Location; import org.bukkit.util.BlockVector; +import org.kapelle.multiblock.block_component.IBlockComponent; import java.util.List; @@ -11,7 +12,7 @@ import java.util.List; */ public class MultiblockStructure { - List>> structure; + List>> structure; /** * Create a multiblock structure. @@ -19,15 +20,15 @@ public class MultiblockStructure { * So you describe the layers from bottom to top. * @param structure structure in the format: [y][z][x] */ - public MultiblockStructure(List>> structure){ + public MultiblockStructure(List>> structure){ this.structure = structure; } - public BlockComponent getComponent(BlockVector offset){ + public IBlockComponent getComponent(BlockVector offset){ 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); } @@ -42,7 +43,7 @@ public class MultiblockStructure { for (int z = 0; z < structure.get(y).size(); z++) { 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)); if(!currentComponent.isValidBlock(globalBlockLocation)){ diff --git a/src/main/java/org/kapelle/multiblock/block_component/AnyBlock.java b/src/main/java/org/kapelle/multiblock/block_component/AnyBlock.java new file mode 100644 index 0000000..ce69fd4 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/block_component/AnyBlock.java @@ -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; + } + +} diff --git a/src/main/java/org/kapelle/multiblock/block_component/IBlockComponent.java b/src/main/java/org/kapelle/multiblock/block_component/IBlockComponent.java new file mode 100644 index 0000000..98edaa5 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/block_component/IBlockComponent.java @@ -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); +} diff --git a/src/main/java/org/kapelle/multiblock/ICheckFunction.java b/src/main/java/org/kapelle/multiblock/block_component/ICheckFunction.java similarity index 76% rename from src/main/java/org/kapelle/multiblock/ICheckFunction.java rename to src/main/java/org/kapelle/multiblock/block_component/ICheckFunction.java index b25baf3..0c892ce 100644 --- a/src/main/java/org/kapelle/multiblock/ICheckFunction.java +++ b/src/main/java/org/kapelle/multiblock/block_component/ICheckFunction.java @@ -1,4 +1,4 @@ -package org.kapelle.multiblock; +package org.kapelle.multiblock.block_component; import org.bukkit.Location; diff --git a/src/main/java/org/kapelle/multiblock/block_component/LambdaComponent.java b/src/main/java/org/kapelle/multiblock/block_component/LambdaComponent.java new file mode 100644 index 0000000..1a3d1b6 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/block_component/LambdaComponent.java @@ -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); + } +} diff --git a/src/main/java/org/kapelle/multiblock/block_component/MultipleBlocks.java b/src/main/java/org/kapelle/multiblock/block_component/MultipleBlocks.java new file mode 100644 index 0000000..323881c --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/block_component/MultipleBlocks.java @@ -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 validMaterial; + + public MultipleBlocks(Set validMaterial){ + this.validMaterial = validMaterial; + } + + @Override + public boolean isValidBlock(Location location) { + return this.validMaterial.contains(location.getBlock().getType()); + } +} diff --git a/src/main/java/org/kapelle/multiblock/block_component/SingleBlock.java b/src/main/java/org/kapelle/multiblock/block_component/SingleBlock.java new file mode 100644 index 0000000..9cab412 --- /dev/null +++ b/src/main/java/org/kapelle/multiblock/block_component/SingleBlock.java @@ -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; + } +}