Fix the race condition when protecting blocks, fixes #34

This commit is contained in:
graywolf336 2015-01-26 02:00:49 -06:00
parent 2c85300e3b
commit 7fd0a8ddc7
3 changed files with 24 additions and 62 deletions

View File

@ -14,7 +14,6 @@ import com.graywolf336.jail.command.JailHandler;
import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.legacy.LegacyManager;
import com.graywolf336.jail.listeners.BlockListener;
import com.graywolf336.jail.listeners.CacheListener;
import com.graywolf336.jail.listeners.CellSignListener;
import com.graywolf336.jail.listeners.EntityListener;
@ -87,7 +86,6 @@ public class JailMain extends JavaPlugin {
pm = new PrisonerManager(this);
PluginManager plm = this.getServer().getPluginManager();
plm.registerEvents(new BlockListener(this), this);
plm.registerEvents(new CacheListener(this), this);
plm.registerEvents(new CellSignListener(this), this);
plm.registerEvents(new EntityListener(this), this);

View File

@ -1,54 +0,0 @@
package com.graywolf336.jail.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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, priority = EventPriority.HIGHEST)
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, priority = EventPriority.HIGHEST)
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

@ -29,9 +29,7 @@ public class ProtectionListener implements Listener {
//Before we check if the player is jailed, let's save a
//tiny bit of resources and check if this protection is enabled
if(pl.getConfig().getBoolean(Settings.BLOCKBREAKPROTECTION.getPath())) {
//Let's check if the player is jailed, otherwise the other listener
//in the BlockListener class will take care of protecting inside
//of the jails.
//Let's check if the player is jailed
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) {
//Get the breaking whitelist, check if the current item is in there
if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKBREAKWHITELIST.getPath()),
@ -63,6 +61,17 @@ public class ProtectionListener implements Listener {
//Stop the event from happening, as the block wasn't in the whitelist
event.setCancelled(true);
}
}else {
//The player is not jailed but they're trying to break blocks inside of the Jail
//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);
}
}
}
}
@ -72,9 +81,7 @@ public class ProtectionListener implements Listener {
//Before we check if the player is jailed, let's save a
//tiny bit of resources and check if this protection is enabled
if(pl.getConfig().getBoolean(Settings.BLOCKPLACEPROTECTION.getPath())) {
//Let's check if the player is jailed, otherwise the other listener
//in the BlockListener class will take care of protecting inside
//of the jails.
//Let's check if the player is jailed
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) {
//Get the placing whitelist, check if the current item is in there
if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKPLACEWHITELIST.getPath()),
@ -106,6 +113,17 @@ public class ProtectionListener implements Listener {
//Stop the event from happening, as the block wasn't in the whitelist
event.setCancelled(true);
}
}else {
//The player is not jailed but they're trying to place blocks inside of the Jail
//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);
}
}
}
}