From 9f34304fdb4c9b40c0618aa69a90fc83f8f1f928 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Mon, 20 Jan 2014 17:24:22 -0600 Subject: [PATCH] Enable protecting chests and people being able to open them and not. --- .../com/graywolf336/jail/enums/Settings.java | 1 + .../jail/listeners/ProtectionListener.java | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 2cc7abb..46c2f1f 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -27,6 +27,7 @@ public enum Settings { JAILEDSTOREINVENTORY("jailing.jail.storeInventory"), LOGJAILING("jailing.jail.logToConsole"), MAXAFKTIME("jailing.during.maxAFKTime"), + OPENCHEST("jailing.during.openChest"), RECIEVEMESSAGES("jailing.during.recieveMessages"), RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"), RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"), diff --git a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java index 6fb9a68..614705b 100644 --- a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java @@ -2,11 +2,14 @@ package com.graywolf336.jail.listeners; import java.util.concurrent.TimeUnit; +import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerInteractEvent; import com.graywolf336.jail.JailMain; import com.graywolf336.jail.Util; @@ -136,4 +139,49 @@ public class ProtectionListener implements Listener { } } } + + @EventHandler(ignoreCancelled=true) + public void chestProtection(PlayerInteractEvent event) { + //First thing is first, let's be sure the player we're dealing with is in jail + if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { + //Next, let's check if it is a chest and if they're in a cell + //If they are in a cell and are opening a chest, then we check + //the config to see if they can open the chests + if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.CHEST) { + //Let's get the cell the player is in, then check if it is null or not. + if(pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getName()).isJailedInACell(event.getPlayer().getName())) { + if(pl.getConfig().getBoolean(Settings.OPENCHEST.getPath())) { + //The prisoner is in a cell, so let's check if it is a couple chest. + Material bpos1 = event.getClickedBlock().getLocation().add(-1, 0, 0).getBlock().getType(); + Material bpos2 = event.getClickedBlock().getLocation().add(+1, 0, 0).getBlock().getType(); + Material bpos3 = event.getClickedBlock().getLocation().add(0, 0, -1).getBlock().getType(); + Material bpos4 = event.getClickedBlock().getLocation().add(0, 0, +1).getBlock().getType(); + + boolean pos1 = bpos1 == Material.CHEST || bpos1 == Material.TRAPPED_CHEST; + boolean pos2 = bpos2 == Material.CHEST || bpos2 == Material.TRAPPED_CHEST; + boolean pos3 = bpos3 == Material.CHEST || bpos3 == Material.TRAPPED_CHEST; + boolean pos4 = bpos4 == Material.CHEST || bpos4 == Material.TRAPPED_CHEST; + + if(pos1 || pos2 || pos3 || pos4) { + //it is a double chest, so they're free to go! + if(pl.inDebug()) event.getPlayer().sendMessage("[Jail Debug]: You're opening up a double chest."); + }else { + //it is not a double chest, so we won't be allowing it. + event.setCancelled(true); + return; + } + }else { + //the config has opening chests disabled + event.setCancelled(true); + return; + } + }else { + //The prisoner is not in a cell, so let's not allow it. IF we get feedback from people who + //use the plugin, this might get removed or permission node might be added. + event.setCancelled(true); + return; + } + } + } + } }