Next up is protecting against placing blocks.

This commit is contained in:
graywolf336 2014-01-19 14:49:42 -06:00
parent 95649e7f03
commit da26bc172e

View File

@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import com.graywolf336.jail.JailMain; import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.Util; import com.graywolf336.jail.Util;
@ -23,20 +24,63 @@ public class ProtectionListener implements Listener {
//Before we check if the player is jailed, let's save a //Before we check if the player is jailed, let's save a
//tiny bit of resources and check if this protection is enabled //tiny bit of resources and check if this protection is enabled
if(pl.getConfig().getBoolean(Settings.BLOCKBREAKPROTECTION.getPath())) { 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.
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//As our Util.getTime throws an exception when the time is in an
//incorrect format, we catch the exception and don't add any time
//as a fail safe, don't want us to go crazy adding tons of time.
try { try {
long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath())); long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath()));
pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add);
//Generate the protection message, provide the method with two arguments
//First is the time in minutes and second is the thing we are protecting against
String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE,
new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) }); pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) });
//Send the message and then stop the event from happening
event.getPlayer().sendMessage(msg); event.getPlayer().sendMessage(msg);
event.setCancelled(true);
}catch (Exception e) { }catch (Exception e) {
pl.getLogger().severe("Block break penalty's time is in the wrong format, please fix."); pl.getLogger().severe("Block break penalty's time is in the wrong format, please fix.");
event.setCancelled(true);
}
}
}
} }
@EventHandler(ignoreCancelled=true)
public void protectionBlockPlacing(BlockPlaceEvent event) {
//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.
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//As our Util.getTime throws an exception when the time is in an
//incorrect format, we catch the exception and don't add any time
//as a fail safe, don't want us to go crazy adding tons of time.
try {
long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKPLACEPENALTY.getPath()));
pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add);
//Generate the protection message, provide the method with two arguments
//First is the time in minutes and second is the thing we are protecting against
String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE,
new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
pl.getJailIO().getLanguageString(LangString.BLOCKPLACING) });
//Send the message and then stop the event from happening
event.getPlayer().sendMessage(msg);
event.setCancelled(true);
}catch (Exception e) {
pl.getLogger().severe("Block place penalty's time is in the wrong format, please fix.");
event.setCancelled(true);
}
} }
} }
} }