isRegionProtected block cache, fixed RPG NOT DETECTED message

This commit is contained in:
nathank33 2015-01-18 21:27:41 -08:00
parent 709b5f76cb
commit 4c4e1f1c65
3 changed files with 36 additions and 3 deletions

View file

@ -77,6 +77,7 @@ public class ConfigManager {
config.addDefault("Properties.RegionProtection.RespectTowny", true); config.addDefault("Properties.RegionProtection.RespectTowny", true);
config.addDefault("Properties.RegionProtection.RespectPreciousStones", true); config.addDefault("Properties.RegionProtection.RespectPreciousStones", true);
config.addDefault("Properties.RegionProtection.RespectLWC", true); config.addDefault("Properties.RegionProtection.RespectLWC", true);
config.addDefault("Properties.RegionProtection.CacheBlockTime", 5000);
config.addDefault("Properties.TagAPI.Enabled", true); config.addDefault("Properties.TagAPI.Enabled", true);

View file

@ -43,6 +43,7 @@ import org.bukkit.World.Environment;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock; import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.FallingSand; import org.bukkit.entity.FallingSand;
@ -55,6 +56,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.griefcraft.lwc.LWC; import com.griefcraft.lwc.LWC;
@ -143,14 +145,18 @@ import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
public class Methods { public class Methods {
static ProjectKorra plugin; static ProjectKorra plugin;
private static FileConfiguration config = ProjectKorra.plugin.getConfig();
public static Random rand = new Random(); public static Random rand = new Random();
public static double CACHE_TIME = config.getDouble("Properties.RegionProtection.CacheBlockTime");
private static final ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE); private static final ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
public static ConcurrentHashMap<Block, Information> movedearth = new ConcurrentHashMap<Block, Information>(); public static ConcurrentHashMap<Block, Information> movedearth = new ConcurrentHashMap<Block, Information>();
public static ConcurrentHashMap<Integer, Information> tempair = new ConcurrentHashMap<Integer, Information>(); public static ConcurrentHashMap<Integer, Information> tempair = new ConcurrentHashMap<Integer, Information>();
public static ConcurrentHashMap<String, Long> cooldowns = new ConcurrentHashMap<String, Long>(); public static ConcurrentHashMap<String, Long> cooldowns = new ConcurrentHashMap<String, Long>();
// Represents PlayerName, previously checked blocks, and whether they were true or false
public static ConcurrentHashMap<String, ConcurrentHashMap<Block, Boolean>> blockProtectionCache = new ConcurrentHashMap<String, ConcurrentHashMap<Block, Boolean>>();
public static ArrayList<Block> tempnophysics = new ArrayList<Block>(); public static ArrayList<Block> tempnophysics = new ArrayList<Block>();
public static HashSet<Block> tempNoEarthbending = new HashSet<Block>(); public static HashSet<Block> tempNoEarthbending = new HashSet<Block>();
private static Integer[] plantIds = { 6, 18, 31, 32, 37, 38, 39, 40, 59, 81, 83, 86, 99, 100, 103, 104, 105, 106, 111, 161, 175}; private static Integer[] plantIds = { 6, 18, 31, 32, 37, 38, 39, 40, 59, 81, 83, 86, 99, 100, 103, 104, 105, 106, 111, 161, 175};
@ -1096,7 +1102,7 @@ public class Methods {
return 1; return 1;
} }
} else { } else {
Bukkit.getServer().broadcastMessage("RPG NOT DETECTED"); //Bukkit.getServer().broadcastMessage("RPG NOT DETECTED");
if (isNight(world) && BendingManager.events.get(world).equalsIgnoreCase("FullMoon")) return plugin.getConfig().getDouble("Properties.Water.FullMoonFactor"); if (isNight(world) && BendingManager.events.get(world).equalsIgnoreCase("FullMoon")) return plugin.getConfig().getDouble("Properties.Water.FullMoonFactor");
if (isNight(world)) return plugin.getConfig().getDouble("Properties.Water.NightFactor"); if (isNight(world)) return plugin.getConfig().getDouble("Properties.Water.NightFactor");
@ -1436,8 +1442,33 @@ public class Methods {
if (Arrays.asList(plantIds).contains(block.getTypeId())) return true; if (Arrays.asList(plantIds).contains(block.getTypeId())) return true;
return false; return false;
} }
/*
* isRegionProtectedFromBuild is one of the most server intensive methods in the
* plugin. It uses a blockCache that keeps track of recent blocks that may have already been checked.
* Abilities like TremorSense call this ability 5 times per tick even though it only needs to check a single block,
* instead of doing all 5 of those checks this method will now look in the map first.
*/
public static boolean isRegionProtectedFromBuild(Player player, String ability, Location loc) { public static boolean isRegionProtectedFromBuild(Player player, String ability, Location loc) {
if(!blockProtectionCache.containsKey(player.getName()))
blockProtectionCache.put(player.getName(), new ConcurrentHashMap<Block, Boolean>());
final ConcurrentHashMap<Block, Boolean> blockMap = blockProtectionCache.get(player.getName());
final Block block = loc.getBlock();
if(blockMap.containsKey(block))
return blockMap.get(block);
boolean value = isRegionProtectedFromBuildPostCache(player, ability, loc);
blockMap.put(block, value);
new BukkitRunnable() {
public void run() {
blockMap.remove(block);
}
}.runTaskLater(ProjectKorra.plugin, (long) (CACHE_TIME / 20));
return value;
}
private static boolean isRegionProtectedFromBuildPostCache(Player player, String ability, Location loc) {
boolean allowharmless = plugin.getConfig().getBoolean("Properties.RegionProtection.AllowHarmlessAbilities"); boolean allowharmless = plugin.getConfig().getBoolean("Properties.RegionProtection.AllowHarmlessAbilities");
boolean respectWorldGuard = plugin.getConfig().getBoolean("Properties.RegionProtection.RespectWorldGuard"); boolean respectWorldGuard = plugin.getConfig().getBoolean("Properties.RegionProtection.RespectWorldGuard");

View file

@ -33,6 +33,7 @@ Properties:
RespectTowny: true RespectTowny: true
RespectPreciousStones: true RespectPreciousStones: true
RespectLWC: true RespectLWC: true
CacheBlockTime: 5000
Air: Air:
CanBendWithWeapons: false CanBendWithWeapons: false
Particles: smoke Particles: smoke