use of com.palmergames.bukkit.towny.object.PlayerCache in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerInteractEntity.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
if (event.getRightClicked() != null) {
TownyWorld World = null;
try {
World = TownyUniverse.getDataSource().getWorld(event.getPlayer().getWorld().getName());
if (!World.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// World not registered with Towny.
e.printStackTrace();
return;
}
Player player = event.getPlayer();
boolean bBuild = true;
int blockID = 0;
/*
* Protect specific entity interactions.
*/
switch(event.getRightClicked().getType()) {
case ITEM_FRAME:
TownyMessaging.sendDebugMsg("ItemFrame Right Clicked");
blockID = 389;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
break;
case PAINTING:
blockID = 321;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
break;
case MINECART:
if (event.getRightClicked() instanceof org.bukkit.entity.minecart.StorageMinecart) {
blockID = 342;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.RideableMinecart) {
blockID = 328;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.PoweredMinecart) {
blockID = 343;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.HopperMinecart) {
blockID = 408;
} else {
blockID = 321;
}
if ((blockID != 0) && (!TownySettings.isSwitchMaterial(BukkitTools.getMaterial(blockID).name())))
return;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.SWITCH);
break;
default:
break;
}
if (blockID != 0) {
// Allow the removal if we are permitted
if (bBuild)
return;
event.setCancelled(true);
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
if (cache.hasBlockErrMsg())
TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
return;
}
/*
* Item_use protection.
*/
if (event.getPlayer().getItemInHand() != null) {
/*
* Info Tool
*/
if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
Entity entity = event.getRightClicked();
TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Entity Info"), ChatTools.formatCommand("", "Entity Class", "", entity.getType().getEntityClass().getSimpleName())));
event.setCancelled(true);
}
if (TownySettings.isItemUseMaterial(event.getPlayer().getItemInHand().getType().name())) {
event.setCancelled(onPlayerInteract(event.getPlayer(), null, event.getPlayer().getItemInHand()));
return;
}
}
}
}
use of com.palmergames.bukkit.towny.object.PlayerCache in project Towny by ElgarL.
the class PlayerCacheUtil method cacheBuild.
/**
* Update the player cache for Build rights at this WorldCoord.
*
* @param player
* @param worldCoord
* @param id
* @param data
* @param buildRight
*/
private static void cacheBuild(Player player, WorldCoord worldCoord, Integer id, byte data, Boolean buildRight) {
PlayerCache cache = plugin.getCache(player);
cache.updateCoord(worldCoord);
cache.setBuildPermission(id, data, buildRight);
TownyMessaging.sendDebugMsg(player.getName() + " (" + worldCoord.toString() + ") Cached Build: " + buildRight);
}
use of com.palmergames.bukkit.towny.object.PlayerCache in project Towny by ElgarL.
the class PlayerCacheUtil method cacheItemUse.
/**
* Update the player cache for Item_use rights at this WorldCoord.
*
* @param player
* @param worldCoord
* @param id
* @param data
* @param itemUseRight
*/
private static void cacheItemUse(Player player, WorldCoord worldCoord, Integer id, byte data, Boolean itemUseRight) {
PlayerCache cache = plugin.getCache(player);
cache.updateCoord(worldCoord);
cache.setItemUsePermission(id, data, itemUseRight);
TownyMessaging.sendDebugMsg(player.getName() + " (" + worldCoord.toString() + ") Cached Item Use: " + itemUseRight);
}
use of com.palmergames.bukkit.towny.object.PlayerCache in project Towny by ElgarL.
the class PlayerCacheUtil method getCachePermission.
/**
* Returns player cached permission for BUILD, DESTROY, SWITCH or ITEM_USE
* at this location for the specified item id.
*
* Generates the cache if it doesn't exist.
*
* @param player
* @param location
* @param blockId
* @param data
* @param action
* @return true if the player has permission.
*/
public static boolean getCachePermission(Player player, Location location, Integer blockId, byte data, ActionType action) {
WorldCoord worldCoord;
try {
worldCoord = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(location));
PlayerCache cache = plugin.getCache(player);
cache.updateCoord(worldCoord);
TownyMessaging.sendDebugMsg("Cache permissions for " + action.toString() + " : " + cache.getCachePermission(blockId, data, action));
// Throws NullPointerException if the cache is empty
return cache.getCachePermission(blockId, data, action);
} catch (NullPointerException e) {
// New or old cache permission was null, update it
worldCoord = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(location));
TownBlockStatus status = cacheStatus(player, worldCoord, getTownBlockStatus(player, worldCoord));
triggerCacheCreate(player, location, worldCoord, status, blockId, data, action);
PlayerCache cache = plugin.getCache(player);
cache.updateCoord(worldCoord);
TownyMessaging.sendDebugMsg("New Cache Created and updated!");
TownyMessaging.sendDebugMsg("New Cache permissions for " + blockId + ":" + action.toString() + ":" + status.name() + " = " + cache.getCachePermission(blockId, data, action));
return cache.getCachePermission(blockId, data, action);
}
}
Aggregations