Add friendlier region deny messages.

This commit is contained in:
sk89q 2014-08-22 15:10:57 -07:00
parent 95d1bd8ee0
commit 0d7e3ad2de
2 changed files with 36 additions and 10 deletions

View File

@ -67,14 +67,18 @@ public RegionProtectionListener(WorldGuardPlugin plugin) {
*
* @param cause the cause
* @param location the location
* @param what what was done
*/
private void tellErrorMessage(Cause cause, Location location) {
private void tellErrorMessage(Cause cause, Location location, String what) {
Object rootCause = cause.getRootCause();
if (rootCause instanceof Player) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
Player player = (Player) rootCause;
player.sendMessage(query.queryValue(location, player, DefaultFlag.DENY_MESSAGE));
String message = query.queryValue(location, player, DefaultFlag.DENY_MESSAGE);
if (message != null) {
player.sendMessage(message.replace("%what%", what));
}
}
}
@ -122,18 +126,21 @@ public void onPlaceBlock(final PlaceBlockEvent event) {
@Override
public boolean apply(Location target) {
boolean canPlace;
String what;
/* Flint and steel, fire charge, etc. */
if (type == Material.FIRE) {
canPlace = query.testBuild(target, associable, DefaultFlag.LIGHTER);
what = "place fire";
/* Everything else */
} else {
canPlace = query.testBuild(target, associable);
what = "place that block";
}
if (!canPlace) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
return false;
}
@ -156,18 +163,21 @@ public void onBreakBlock(final BreakBlockEvent event) {
@Override
public boolean apply(Location target) {
boolean canBreak;
String what;
/* TNT */
if (event.getCause().find(EntityType.PRIMED_TNT, EntityType.PRIMED_TNT) != null) {
canBreak = query.testBuild(target, associable, DefaultFlag.TNT);
what = "dynamite blocks";
/* Everything else */
} else {
canBreak = query.testBuild(target, associable);
what = "break that block";
}
if (!canBreak) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
return false;
}
@ -190,26 +200,31 @@ public void onUseBlock(final UseBlockEvent event) {
@Override
public boolean apply(Location target) {
boolean canUse;
String what;
/* Inventory */
if (Materials.isInventoryBlock(type)) {
canUse = query.testBuild(target, associable, DefaultFlag.USE, DefaultFlag.CHEST_ACCESS);
what = "open that";
/* Beds */
} else if (type == Material.BED) {
canUse = query.testBuild(target, associable, DefaultFlag.USE, DefaultFlag.SLEEP);
what = "sleep";
/* TNT */
} else if (type == Material.TNT) {
canUse = query.testBuild(target, associable, DefaultFlag.TNT);
what = "use explosives";
/* Everything else */
} else {
canUse = query.testBuild(target, associable, DefaultFlag.USE);
what = "use that";
}
if (!canUse) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
return false;
}
@ -230,18 +245,21 @@ public void onSpawnEntity(SpawnEntityEvent event) {
RegionAssociable associable = createRegionAssociable(event.getCause());
boolean canSpawn;
String what;
/* Vehicles */
if (Entities.isVehicle(type)) {
canSpawn = query.testBuild(target, associable, DefaultFlag.PLACE_VEHICLE);
what = "place vehicles";
/* Everything else */
} else {
canSpawn = query.testBuild(target, associable);
what = "place things";
}
if (!canSpawn) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
event.setCancelled(true);
}
}
@ -257,18 +275,21 @@ public void onDestroyEntity(DestroyEntityEvent event) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
boolean canDestroy;
String what;
/* Vehicles */
if (Entities.isVehicle(type)) {
canDestroy = query.testBuild(target, associable, DefaultFlag.DESTROY_VEHICLE);
what = "break vehicles";
/* Everything else */
} else {
canDestroy = query.testBuild(target, associable);
what = "break things";
}
if (!canDestroy) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
event.setCancelled(true);
}
}
@ -283,9 +304,10 @@ public void onUseEntity(UseEntityEvent event) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
boolean canUse = query.testBuild(target, associable, DefaultFlag.USE);
String what = "use that";
if (!canUse) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
event.setCancelled(true);
}
}
@ -301,6 +323,7 @@ public void onDamageEntity(DamageEntityEvent event) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
Player attacker;
boolean canDamage;
String what;
/* PVP */
if (event.getEntity() instanceof Player && (attacker = event.getCause().getFirstPlayer()) != null) {
@ -314,13 +337,16 @@ public void onDamageEntity(DamageEntityEvent event) {
canDamage = true;
}
what = "PvP";
/* Everything else */
} else {
canDamage = query.testBuild(target, associable, DefaultFlag.USE);
what = "hit that";
}
if (!canDamage) {
tellErrorMessage(event.getCause(), target);
tellErrorMessage(event.getCause(), target, what);
event.setCancelled(true);
}
}

View File

@ -81,7 +81,7 @@ public final class DefaultFlag {
public static final StateFlag POTION_SPLASH = new StateFlag("potion-splash", true);
public static final StringFlag DENY_MESSAGE = new StringFlag("deny-message",
"" + ChatColor.RED + ChatColor.BOLD + "Hey!" + ChatColor.GRAY + " Sorry, but you can't do that here.");
"" + ChatColor.RED + ChatColor.BOLD + "Hey!" + ChatColor.GRAY + " Sorry, but you can't %what% here.");
public static final StringFlag GREET_MESSAGE = new StringFlag("greeting");
public static final StringFlag FAREWELL_MESSAGE = new StringFlag("farewell");
public static final BooleanFlag NOTIFY_ENTER = new BooleanFlag("notify-enter");