/** * Detects if a Player can use a protectable Block or not * * @param player the Player involved * @param b the Block to be used * @return true if the Player can use the Block, false otherwise */ public boolean canUse(final Player player, final Block b) { if (Perms.hasProtectionSignBypass(player)) { return true; } final Material blockType = b.getType(); final String userId = PlayerIdsUtil.getId(player.getName()); final List<Sign> signLines; if (blockType == Material.CHEST || blockType == Material.TRAPPED_CHEST) { signLines = SignUtil.getSignsForChest(b); } else if (getProtectedMaterials().contains(blockType)) { signLines = SignUtil.getSignsForBlock(b); } else { return true; } boolean protectedBySign = false; boolean explicitlyAllowed = false; for (final Sign sign : signLines) { if (sign.getLine(0).equals(PROTECTION)) { protectedBySign = true; if (ColorUtil.stripColorCodes(sign.getLine(1)).equals(userId) || ColorUtil.stripColorCodes(sign.getLine(2)).equals(userId) || ColorUtil.stripColorCodes(sign.getLine(3)).equals(userId)) { explicitlyAllowed = true; break; } } } return !protectedBySign || explicitlyAllowed; }
/** * Detects if a Block is protected by a Sign or not. Returns the Sign owner if protected, null * otherwise. * * @param b the block to check * @return the Sign owner name if protected, null otherwise */ public String isProtected(final Block b) { final NLocation loc = new NLocation(b.getLocation()); final ProtectionState state = this.isProtectedCache.get(loc); if (state != null && state.timeout > System.currentTimeMillis()) { return state.protectedBy; } else { final Material blockType = b.getType(); String result = null; List<Sign> signLines = null; if (blockType == Material.CHEST || blockType == Material.TRAPPED_CHEST) { signLines = SignUtil.getSignsForChest(b); } else if (getProtectedMaterials().contains(blockType)) { signLines = SignUtil.getSignsForBlock(b); } if (signLines != null) { for (final Sign sign : signLines) { if (PROTECTION.equals(sign.getLine(0))) { result = ColorUtil.stripColorCodes(sign.getLine(3)); break; } } } this.isProtectedCache.put( loc, new ProtectionState(result, System.currentTimeMillis() + CACHE_TIME)); return result; } }
/** * Detect if a Block can be broken by a Player or by something else (Explosion...) * * @param b the Block to be broken * @param player the Player that want to break the Block, if there is one, null otherwise * @return true if the block can be broken [by the Player], false otherwise */ public boolean canBreak(final Block b, final Player player) { final Material blockType = b.getType(); final String userId = player != null ? PlayerIdsUtil.getId(player.getName()) : null; if (blockType == Material.SIGN_POST || blockType == Material.WALL_SIGN) { final Sign sign = (Sign) b.getState(); return !sign.getLine(0).equals(PROTECTION) || player != null && ColorUtil.stripColorCodes(sign.getLine(3)).equals(userId) || player != null && Perms.hasProtectionSignBreak(player); } else { final List<Sign> signLines; if (blockType == Material.CHEST || blockType == Material.TRAPPED_CHEST) { signLines = SignUtil.getSignsForChest(b); } else if (getProtectedMaterials().contains(blockType)) { signLines = SignUtil.getSignsForBlock(b); } else { return true; } for (final Sign sign : signLines) { if (sign.getLine(0).equals(PROTECTION)) { return false; } } return true; } }