Merge pull request #109 from YellowZaki/develop

Count PISTON_HEAD and MOVING_PISTON
This commit is contained in:
tastybento 2020-10-28 10:52:16 -07:00 committed by GitHub
commit b9870a4fef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 43 deletions

View File

@ -13,6 +13,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot; import org.bukkit.ChunkSnapshot;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -105,9 +106,9 @@ public class LimitsCalc {
continue; continue;
} }
for (int y = 0; y < Objects.requireNonNull(island.getCenter().getWorld()).getMaxHeight(); y++) { for (int y = 0; y < Objects.requireNonNull(island.getCenter().getWorld()).getMaxHeight(); y++) {
Material blockData = chunk.getBlockType(x, y, z); BlockData blockData = chunk.getBlockData(x, y, z);
// Air is free // Air is free
if (!blockData.equals(Material.AIR)) { if (!blockData.getMaterial().equals(Material.AIR)) {
checkBlock(blockData); checkBlock(blockData);
} }
} }
@ -115,8 +116,8 @@ public class LimitsCalc {
} }
} }
private void checkBlock(Material md) { private void checkBlock(BlockData b) {
md = bll.fixMaterial(md); Material md = bll.fixMaterial(b);
// md is limited // md is limited
if (bll.getMaterialLimits(world, island.getUniqueId()).containsKey(md)) { if (bll.getMaterialLimits(world, island.getUniqueId()).containsKey(md)) {
if (!blockCount.containsKey(md)) { if (!blockCount.containsKey(md)) {

View File

@ -15,6 +15,8 @@ import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.TechnicalPiston;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -169,22 +171,16 @@ public class BlockLimitsListener implements Listener {
private void handleBreak(Event e, Block b) { private void handleBreak(Event e, Block b) {
Material mat = b.getType(); Material mat = b.getType();
// Special handling for crops that can break in different ways
if (mat.equals(Material.WHEAT_SEEDS)) {
mat = Material.WHEAT;
} else if (mat.equals(Material.BEETROOT_SEEDS)) {
mat = Material.BEETROOT;
}
// Check for stackable plants // Check for stackable plants
if (STACKABLE.contains(b.getType())) { if (STACKABLE.contains(b.getType())) {
// Check for blocks above // Check for blocks above
Block block = b; Block block = b;
while(block.getRelative(BlockFace.UP).getType().equals(mat) && block.getY() < b.getWorld().getMaxHeight()) { while(block.getRelative(BlockFace.UP).getType().equals(mat) && block.getY() < b.getWorld().getMaxHeight()) {
block = block.getRelative(BlockFace.UP); block = block.getRelative(BlockFace.UP);
process(block, false, mat); process(block, false);
} }
} }
process(b, false, mat); process(b, false);
// Player breaks a block and there was a redstone dust/repeater/... above // Player breaks a block and there was a redstone dust/repeater/... above
if (b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_WIRE || b.getRelative(BlockFace.UP).getType() == Material.REPEATER || b.getRelative(BlockFace.UP).getType() == Material.COMPARATOR || b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_TORCH) { if (b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_WIRE || b.getRelative(BlockFace.UP).getType() == Material.REPEATER || b.getRelative(BlockFace.UP).getType() == Material.COMPARATOR || b.getRelative(BlockFace.UP).getType() == Material.REDSTONE_TORCH) {
process(b.getRelative(BlockFace.UP), false); process(b.getRelative(BlockFace.UP), false);
@ -278,30 +274,38 @@ public class BlockLimitsListener implements Listener {
} }
} }
private int process(Block b, boolean add) {
return process(b, add, b.getType());
}
// Return equivalents.
public Material fixMaterial(Material b) {
if (b == Material.REDSTONE_WALL_TORCH) { // Return equivalents. b can be Block/Material
public Material fixMaterial(BlockData b) {
Material mat = b.getMaterial();
if (mat == Material.REDSTONE_WALL_TORCH) {
return Material.REDSTONE_TORCH; return Material.REDSTONE_TORCH;
} else if (b == Material.WALL_TORCH) { } else if (mat == Material.WALL_TORCH) {
return Material.TORCH; return Material.TORCH;
} else if (b == Material.ZOMBIE_WALL_HEAD) { } else if (mat == Material.ZOMBIE_WALL_HEAD) {
return Material.ZOMBIE_HEAD; return Material.ZOMBIE_HEAD;
} else if (b == Material.CREEPER_WALL_HEAD) { } else if (mat == Material.CREEPER_WALL_HEAD) {
return Material.CREEPER_HEAD; return Material.CREEPER_HEAD;
} else if (b == Material.PLAYER_WALL_HEAD) { } else if (mat == Material.PLAYER_WALL_HEAD) {
return Material.PLAYER_HEAD; return Material.PLAYER_HEAD;
} else if (b == Material.DRAGON_WALL_HEAD) { } else if (mat == Material.DRAGON_WALL_HEAD) {
return Material.DRAGON_HEAD; return Material.DRAGON_HEAD;
} else if (b != null && b == Material.getMaterial("BAMBOO_SAPLING")) { } else if (mat != null && mat == Material.getMaterial("BAMBOO_SAPLING")) {
return Material.getMaterial("BAMBOO"); return Material.getMaterial("BAMBOO");
} else if (mat == Material.PISTON_HEAD || mat == Material.MOVING_PISTON) {
TechnicalPiston tp = (TechnicalPiston) b;
if (tp.getType() == TechnicalPiston.Type.NORMAL) {
return Material.PISTON;
} else {
return Material.STICKY_PISTON;
}
} }
return b; return mat;
} }
/** /**
* Check if a block can be * Check if a block can be
* *
@ -310,8 +314,8 @@ public class BlockLimitsListener implements Listener {
* @param changeTo - material this block will become * @param changeTo - material this block will become
* @return limit amount if over limit, or -1 if no limitation * @return limit amount if over limit, or -1 if no limitation
*/ */
private int process(Block b, boolean add, Material changeTo) { private int process(Block b, boolean add) {
if (DO_NOT_COUNT.contains(fixMaterial(b.getType())) || !addon.inGameModeWorld(b.getWorld())) { if (DO_NOT_COUNT.contains(fixMaterial(b.getBlockData())) || !addon.inGameModeWorld(b.getWorld())) {
return -1; return -1;
} }
// Check if on island // Check if on island
@ -325,24 +329,14 @@ public class BlockLimitsListener implements Listener {
islandCountMap.putIfAbsent(id, new IslandBlockCount(id, gameMode)); islandCountMap.putIfAbsent(id, new IslandBlockCount(id, gameMode));
if (add) { if (add) {
// Check limit // Check limit
int limit = checkLimit(b.getWorld(), fixMaterial(b.getType()), id); int limit = checkLimit(b.getWorld(), fixMaterial(b.getBlockData()), id);
if (limit > -1) { if (limit > -1) {
return limit; return limit;
} }
islandCountMap.get(id).add(fixMaterial(b.getType())); islandCountMap.get(id).add(fixMaterial(b.getBlockData()));
} else { } else {
if (islandCountMap.containsKey(id)) { if (islandCountMap.containsKey(id)) {
// Check for changes islandCountMap.get(id).remove(fixMaterial(b.getBlockData()));
Material fixed = fixMaterial(changeTo);
if (!fixed.equals(fixMaterial(b.getType())) && fixed.isBlock() && !DO_NOT_COUNT.contains(fixed)) {
// Check limit
int limit = checkLimit(b.getWorld(), fixed, id);
if (limit > -1) {
return limit;
}
islandCountMap.get(id).add(fixed);
}
islandCountMap.get(id).remove(fixMaterial(b.getType()));
} }
} }
updateSaveMap(id); updateSaveMap(id);
@ -363,7 +357,7 @@ public class BlockLimitsListener implements Listener {
// Invalid world // Invalid world
return; return;
} }
islandCountMap.computeIfAbsent(id, k -> new IslandBlockCount(id, gameMode)).remove(fixMaterial(b.getType())); islandCountMap.computeIfAbsent(id, k -> new IslandBlockCount(id, gameMode)).remove(fixMaterial(b.getBlockData()));
updateSaveMap(id); updateSaveMap(id);
}); });
} }