Skip to content

Commit

Permalink
added hopper check
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccoystephenson committed Jul 9, 2020
1 parent 6e07b98 commit 4b7cf41
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import factionsystem.Objects.ClaimedChunk;
import factionsystem.Objects.Faction;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
Expand Down Expand Up @@ -45,19 +46,29 @@ public void handle(BlockPlaceEvent event) {

// if chest
if (main.isChest(event.getBlock())) {
// if next to non-owned locked block
if (isNextToNonOwnedLockedBlock(event.getPlayer(), event.getBlock())) {
// if next to non-owned locked chest
if (isNextToNonOwnedLockedChest(event.getPlayer(), event.getBlock())) {
event.setCancelled(true);
player.sendMessage(ChatColor.RED + "You can't place chests next to locked chests you don't own.");
return;
}
}

// if hopper
if (event.getBlock().getType() == Material.HOPPER) {
// if next to or under/above non-owned locked chest
if (isNextToNonOwnedLockedChest(event.getPlayer(), event.getBlock()) || isUnderOrAboveNonOwnedLockedChest(event.getPlayer(), event.getBlock())) {
event.setCancelled(true);
player.sendMessage(ChatColor.RED + "You can't place hoppers next to, under or above locked chests you don't own.");
return;
}
}
}
}
}
}

private boolean isNextToNonOwnedLockedBlock(Player player, Block block) {
private boolean isNextToNonOwnedLockedChest(Player player, Block block) {

// define blocks
Block neighbor1 = block.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ());
Expand Down Expand Up @@ -91,4 +102,24 @@ private boolean isNextToNonOwnedLockedBlock(Player player, Block block) {

return false;
}

private boolean isUnderOrAboveNonOwnedLockedChest(Player player, Block block) {
// define blocks
Block neighbor1 = block.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ());
Block neighbor2 = block.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ());

if (main.isChest(neighbor1)) {
if (main.isBlockLocked(neighbor1) && !main.getLockedBlock(neighbor1).getOwner().equalsIgnoreCase(player.getName())) {
return true;
}
}

if (main.isChest(neighbor1)) {
if (main.isBlockLocked(neighbor2) && !main.getLockedBlock(neighbor2).getOwner().equalsIgnoreCase(player.getName())) {
return true;
}
}

return false;
}
}

0 comments on commit 4b7cf41

Please sign in to comment.