diff --git a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java index 01a0cb3..1b389e2 100644 --- a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -2394,31 +2394,43 @@ public class GriefPrevention extends JavaPlugin if(player.hasPermission("griefprevention.nopvpimmunity")) return; //check inventory for well, anything - PlayerInventory inventory = player.getInventory(); - ItemStack [] armorStacks = inventory.getArmorContents(); - - //check armor slots, stop if any items are found - for(int i = 0; i < armorStacks.length; i++) + if(GriefPrevention.isInventoryEmpty(player)) { - if(!(armorStacks[i] == null || armorStacks[i].getType() == Material.AIR)) return; + //if empty, apply immunity + PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); + playerData.pvpImmune = true; + + //inform the player after he finishes respawning + GriefPrevention.sendMessage(player, TextMode.Success, Messages.PvPImmunityStart, 5L); + + //start a task to re-check this player's inventory every minute until his immunity is gone + PvPImmunityValidationTask task = new PvPImmunityValidationTask(player); + this.getServer().getScheduler().scheduleSyncDelayedTask(this, task, 1200L); } - - //check other slots, stop if any items are found - ItemStack [] generalStacks = inventory.getContents(); - for(int i = 0; i < generalStacks.length; i++) - { - if(!(generalStacks[i] == null || generalStacks[i].getType() == Material.AIR)) return; - } - - //otherwise, apply immunity - PlayerData playerData = this.dataStore.getPlayerData(player.getUniqueId()); - playerData.pvpImmune = true; - - //inform the player after he finishes respawning - GriefPrevention.sendMessage(player, TextMode.Success, Messages.PvPImmunityStart, 5L); } - //checks whether players siege in a world + static boolean isInventoryEmpty(Player player) + { + PlayerInventory inventory = player.getInventory(); + ItemStack [] armorStacks = inventory.getArmorContents(); + + //check armor slots, stop if any items are found + for(int i = 0; i < armorStacks.length; i++) + { + if(!(armorStacks[i] == null || armorStacks[i].getType() == Material.AIR)) return false; + } + + //check other slots, stop if any items are found + ItemStack [] generalStacks = inventory.getContents(); + for(int i = 0; i < generalStacks.length; i++) + { + if(!(generalStacks[i] == null || generalStacks[i].getType() == Material.AIR)) return false; + } + + return true; + } + + //checks whether players siege in a world public boolean siegeEnabledForWorld(World world) { return this.config_siege_enabledWorlds.contains(world); diff --git a/src/me/ryanhamshire/GriefPrevention/PvPImmunityValidationTask.java b/src/me/ryanhamshire/GriefPrevention/PvPImmunityValidationTask.java new file mode 100644 index 0000000..a03e748 --- /dev/null +++ b/src/me/ryanhamshire/GriefPrevention/PvPImmunityValidationTask.java @@ -0,0 +1,55 @@ +/* + GriefPrevention Server Plugin for Minecraft + Copyright (C) 2012 Ryan Hamshire + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +package me.ryanhamshire.GriefPrevention; + +import org.bukkit.entity.Player; + +//sends a message to a player +//used to send delayed messages, for example help text triggered by a player's chat +class PvPImmunityValidationTask implements Runnable +{ + private Player player; + + public PvPImmunityValidationTask(Player player) + { + this.player = player; + } + + @Override + public void run() + { + if(!player.isOnline()) return; + + PlayerData playerData = GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId()); + if(!playerData.pvpImmune) return; + + //check the player's inventory for anything + if(!GriefPrevention.isInventoryEmpty(player)) + { + //if found, cancel invulnerability and notify + playerData.pvpImmune = false; + GriefPrevention.sendMessage(player, TextMode.Warn, Messages.PvPImmunityEnd); + } + else + { + //otherwise check again in one minute + GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, this, 1200L); + } + } +}