Revise WorldGuard event hooks again

Hide all permission messages from WorldGuard on shop interaction,
except when chest-access is denied on shop creation.
This commit is contained in:
Eric 2018-11-05 16:15:10 +01:00
parent e38fe9fe14
commit 59828df99d

View File

@ -5,8 +5,11 @@ import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.nms.Hologram;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
@ -16,12 +19,12 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.codemc.worldguardwrapper.WorldGuardWrapper;
import org.codemc.worldguardwrapper.event.AbstractWrappedEvent;
import org.codemc.worldguardwrapper.event.DamageEntityEvent;
import org.codemc.worldguardwrapper.event.UseBlockEvent;
import org.codemc.worldguardwrapper.event.UseEntityEvent;
@ -35,59 +38,52 @@ public class WorldGuardListener implements Listener {
}
private boolean isAllowed(Player player, Location location, Action action) {
private boolean isAllowed(Player player, Location location) {
ClickType clickType = ClickType.getPlayerClickType(player);
if (clickType != null && clickType.getClickType() == EnumClickType.CREATE) {
// If the player is about to create a shop, but does not have
// access to the chest, show the 'permission denied' message
// (if not previously set to allowed by another plugin).
// If the player can open the chest, that message should be hidden.
return WorldGuardWrapper.getInstance().queryStateFlag(player, location, "chest-access").orElse(false);
}
Shop shop = plugin.getShopUtils().getShop(location);
if (shop != null) {
if (shop.getVendor().getUniqueId().equals(player.getUniqueId()) && shop.getShopType() != Shop.ShopType.ADMIN) {
return true;
}
}
WorldGuardWrapper wgWrapper = WorldGuardWrapper.getInstance();
if (ClickType.getPlayerClickType(player) != null) {
switch (ClickType.getPlayerClickType(player).getClickType()) {
case CREATE:
return wgWrapper.queryStateFlag(player, location, "create-shop").orElse(false)
&& wgWrapper.queryStateFlag(player, location, "chest-access").orElse(false);
case REMOVE:
case INFO:
case OPEN:
return true;
}
} else {
if (shop != null) {
String flagName = (shop.getShopType() == Shop.ShopType.NORMAL ? "use-shop" : "use-admin-shop");
return wgWrapper.queryStateFlag(player, location, flagName).orElse(false);
}
// Don't show 'permission denied' messages for any kind of
// shop interaction even if entity/block interaction is not
// allowed in the region.
return true;
}
return false;
}
private void handleEntityInteraction(Player player, Entity entity, AbstractWrappedEvent event) {
if (entity.getType() == EntityType.ARMOR_STAND) {
if (!Hologram.isPartOfHologram((ArmorStand) entity))
return;
for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.getHologram() != null && shop.getHologram().contains((ArmorStand) entity)) {
if (isAllowed(player, shop.getLocation())) {
event.setResult(Result.ALLOW);
}
return;
}
}
}
}
@EventHandler(priority = EventPriority.LOW)
public void onUseEntity(UseEntityEvent event) {
if (Config.enableWorldGuardIntegration) {
Player player = event.getPlayer();
if (event.getOriginalEvent() instanceof PlayerInteractAtEntityEvent) {
Entity e = event.getEntity();
if (e.getType() == EntityType.ARMOR_STAND) {
if (!Hologram.isPartOfHologram((ArmorStand) e))
return;
for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.getHologram() != null && shop.getHologram().contains((ArmorStand) e)) {
if (isAllowed(player, shop.getLocation(), Action.RIGHT_CLICK_BLOCK)) {
event.setResult(Result.ALLOW);
}
return;
}
}
}
handleEntityInteraction(player, event.getEntity(), event);
}
}
}
@ -98,22 +94,7 @@ public class WorldGuardListener implements Listener {
Player player = event.getPlayer();
if (event.getOriginalEvent() instanceof EntityDamageByEntityEvent) {
Entity e = event.getEntity();
if (e.getType() == EntityType.ARMOR_STAND) {
if (!Hologram.isPartOfHologram((ArmorStand) e))
return;
for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.getHologram() != null && shop.getHologram().contains((ArmorStand) e)) {
if (isAllowed(player, shop.getLocation(), Action.LEFT_CLICK_BLOCK)) {
event.setResult(Result.ALLOW);
}
return;
}
}
}
handleEntityInteraction(player, event.getEntity(), event);
}
}
}
@ -124,21 +105,19 @@ public class WorldGuardListener implements Listener {
Player player = event.getPlayer();
if (event.getOriginalEvent() instanceof PlayerInteractEvent) {
PlayerInteractEvent orig = (PlayerInteractEvent) event.getOriginalEvent();
Block block = event.getBlocks().get(0);
Material type = block.getType();
if (orig.hasBlock()) {
Material type = orig.getClickedBlock().getType();
if (type == Material.CHEST || type == Material.TRAPPED_CHEST) {
if (isAllowed(player, orig.getClickedBlock().getLocation(), orig.getAction())) {
event.setResult(Result.ALLOW);
}
if (type == Material.CHEST || type == Material.TRAPPED_CHEST) {
if (isAllowed(player, block.getLocation())) {
event.setResult(Result.ALLOW);
}
}
} else if (event.getOriginalEvent() instanceof InventoryOpenEvent) {
InventoryOpenEvent orig = (InventoryOpenEvent) event.getOriginalEvent();
if (orig.getInventory().getHolder() instanceof Chest) {
if (isAllowed(player, ((Chest) orig.getInventory().getHolder()).getLocation(), Action.RIGHT_CLICK_BLOCK)) {
if (isAllowed(player, ((Chest) orig.getInventory().getHolder()).getLocation())) {
event.setResult(Result.ALLOW);
}
}