|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Kevin Phoenix |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +package in.twizmwaz.cardinal.module.region.type; |
| 27 | + |
| 28 | +import in.twizmwaz.cardinal.match.Match; |
| 29 | +import in.twizmwaz.cardinal.module.region.AbstractRegion; |
| 30 | +import in.twizmwaz.cardinal.module.region.Region; |
| 31 | +import in.twizmwaz.cardinal.module.region.RegionBounds; |
| 32 | +import in.twizmwaz.cardinal.util.Geometry; |
| 33 | +import in.twizmwaz.cardinal.util.ListUtil; |
| 34 | +import in.twizmwaz.cardinal.util.MaterialPattern; |
| 35 | +import org.bukkit.block.Block; |
| 36 | +import org.bukkit.util.Cuboid; |
| 37 | +import org.bukkit.util.Vector; |
| 38 | + |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.List; |
| 42 | +import java.util.stream.Collectors; |
| 43 | + |
| 44 | +public class FiniteBlockRegion extends AbstractRegion { |
| 45 | + |
| 46 | + private FiniteBlockRegion(RegionBounds bounds, List<Block> blocks) { |
| 47 | + super(bounds); |
| 48 | + super.setBlocks(blocks); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Collection<Block> getBlocks() { |
| 53 | + return super.getBlocks(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Vector getRandomPoint() { |
| 58 | + return ListUtil.getRandom((List<Block>) super.getBlocks()).getLocation(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isBounded() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + public boolean contains(Vector evaluating) { |
| 67 | + evaluating = Geometry.floor(evaluating); |
| 68 | + for (Block block : super.getBlocks()) { |
| 69 | + if (block.getLocation().equals(evaluating)) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean isRandomizable() { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates a block finite region that matches a material. |
| 83 | + * @param match The match this region belongs to. |
| 84 | + * @param region The region to build the finite block region on. MUST be bounded. |
| 85 | + * @param pattern The material pattern to filter blocks. |
| 86 | + * @return A FiniteBlockRegion containing all regions that matched the pattern. |
| 87 | + */ |
| 88 | + public FiniteBlockRegion getFromMaterialPattern(Match match, Region region, MaterialPattern pattern) { |
| 89 | + List<Block> blocks = region.getBlocks().stream().filter(block -> pattern.contains(block.getType(), block.getData())) |
| 90 | + .collect(Collectors.toList()); |
| 91 | + return new FiniteBlockRegion(new RegionBounds(match, getBounds(blocks)), blocks); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + private Cuboid getBounds(List<Block> blocks) { |
| 96 | + List<Vector> vectors = new ArrayList<>(); |
| 97 | + vectors.addAll(blocks.stream().map(Block::getLocation).collect(Collectors.toList())); |
| 98 | + vectors.addAll(blocks.stream().map(block -> block.getLocation().plus(1, 1, 1)).collect(Collectors.toList())); |
| 99 | + return Cuboid.enclosing(vectors.toArray(new Vector[vectors.size()])); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments