Protect against block breaking and block placing in jails

Unless the player has admin permission. This does not include the
penalties as those will be done in a future commit. This also has a
little better performance on the block placing and breaking events, if
these checks are disabled.
This commit is contained in:
graywolf336 2014-01-08 15:02:24 -06:00
parent ab74a158b8
commit d734d99e88
7 changed files with 89 additions and 4 deletions

View File

@ -4,9 +4,20 @@ This plugins adds Jail to your Minecraft server. Admins can define several jails
[![Build Status](http://ci.graywolf336.com/job/Jail/badge/icon)](http://ci.graywolf336.com/job/Jail/)
Changes
===
With this being version 3 of this plugin, a couple things have changed but the core concept is still the exact same. Here are some drastic changes:
* Time can now be entered different formats, 3hours or 15mins or 4days
* New language system
* New config system (per jail configs are going to make a come back)
* Only prisoners will be able to place/break blocks on the whitelist
* Items in the config use item names now, not the ids
ToDo
===
* About everything
* Enable a whitelist of blocks which can be broke
* Enabled a whitelist of blocks which can be placed
Done
===

View File

@ -43,7 +43,7 @@ public class JailMain extends JavaPlugin {
pm = new PrisonerManager(this);
PluginManager plm = this.getServer().getPluginManager();
plm.registerEvents(new BlockListener(), this);
plm.registerEvents(new BlockListener(this), this);
plm.registerEvents(new EntityListener(), this);
plm.registerEvents(new HandCuffListener(this), this);
plm.registerEvents(new PlayerListener(this), this);
@ -54,7 +54,7 @@ public class JailMain extends JavaPlugin {
if(debug) getLogger().info("Debugging enabled.");
getLogger().info("Successfully loaded, is our impression.");
getLogger().info("Completed enablement.");
}
public void onDisable() {

View File

@ -111,6 +111,22 @@ public class JailManager {
}
}
/**
* Gets the jail which this location is in, will return null if none exist.
*
* @param loc to get the jail from
* @return The jail this block is in, null if no jail found.
*/
public Jail getJailFromLocation(Location loc) {
for(Jail j : jails.values()) {
if(Util.isInsideAB(loc.toVector(), j.getMinPoint().toVector(), j.getMaxPoint().toVector())) {
return j;
}
}
return null;
}
/**
* Checks to see if the given name for a {@link Jail} is valid, returns true if it is a valid jail.
*

View File

@ -33,7 +33,14 @@ import com.graywolf336.jail.beans.Prisoner;
public class Util {
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(m(?:inute)?s?|h(?:ours?)?|d(?:ays?)?|s(?:econd)?s?)?$", Pattern.CASE_INSENSITIVE);
/** Checks if the first {@link Vector} is inside the other two. */
/**
* Checks if the first {@link Vector} is inside this region.
*
* @param point The point to check
* @param first point of the region
* @param second second point of the region
* @return True if all the coords of the first vector are in the entire region.
*/
public static boolean isInsideAB(Vector point, Vector first, Vector second) {
boolean x = isInside(point.getBlockX(), first.getBlockX(), second.getBlockX());
boolean y = isInside(point.getBlockY(), first.getBlockY(), second.getBlockY());

View File

@ -2,6 +2,8 @@ package com.graywolf336.jail.enums;
public enum Settings {
BROADCASTJAILING("jailing.jail.broadcastJailing"),
BLOCKBREAKPROTECTION("jailing.during.blockBreakProtection"),
BLOCKPLACEPROTECTION("jailing.during.blockPlaceProtection"),
COMMANDSONJAIL("jailing.jail.commands"),
COMMANDSONRELEASE("jailing.release.commands"),
COUNTDOWNTIMEOFFLINE("jailing.during.countDownTimeWhileOffline"),

View File

@ -1,7 +1,54 @@
package com.graywolf336.jail.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.enums.Settings;
public class BlockListener implements Listener {
private JailMain pl;
public BlockListener(JailMain plugin) {
this.pl = plugin;
}
@EventHandler(ignoreCancelled=true)
public void blockBreak(BlockBreakEvent event) {
//If we are protecting against block breaking, then let's do the action.
//If we're not, let's not use any processing power to get the jail
//where this block was broke at
if(pl.getConfig().getBoolean(Settings.BLOCKBREAKPROTECTION.getPath())) {
//If there is no jail let's skedaddle
if(pl.getJailManager().getJailFromLocation(event.getBlock().getLocation()) == null) return;
//If the player doesn't have permission to modify the jail,
//then we stop it here. We won't be doing any of the additions to
//a prisoner's sentence here as that's for the protections listener
if(!event.getPlayer().hasPermission("jail.modifyjail")) {
event.setCancelled(true);
}
}
}
@EventHandler(ignoreCancelled=true)
public void blockPlace(BlockPlaceEvent event) {
//If we are protecting against block placing, then let's do the action.
//If we're not, let's not use any processing power to get the jail
//where this block was placed at
if(pl.getConfig().getBoolean(Settings.BLOCKPLACEPROTECTION.getPath())) {
//If there is no jail let's skedaddle
if(pl.getJailManager().getJailFromLocation(event.getBlock().getLocation()) == null) return;
//If the player doesn't have permission to modify the jail,
//then we stop it here. We won't be doing any of the additions to
//a prisoner's sentence here as that's for the protections listener
if(!event.getPlayer().hasPermission("jail.modifyjail")) {
event.setCancelled(true);
}
}
}
}

View File

@ -13,6 +13,8 @@ storage:
password: 'password'
jailing:
during:
blockBreakProtection: true
blockPlaceProtection: true
countDownTimeWhileOffline: false
ignoreSleeping: true
maxAFKTime: 10m #in minutes