From a33c4d4c51144e88d807c5c562e8e7d14255d050 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Mon, 29 Jul 2013 10:22:39 +0200 Subject: [PATCH] New Inventory-Blocking, that doesn't require horse blocking any longer. Can be bypassed with a dynamic permission (see http://dev.bukkit.org/bukkit-plugins/limited-creative/pages/permissions/#w-additional-dynamic-permissions) --- .../limitedcreative/ModInventories.java | 4 --- .../limitedcreative/limits/LimitConfig.java | 15 ----------- .../limits/NoLimitPermissions.java | 25 +++++++++++++++++-- .../limits/PlayerListener.java | 16 ++++++++++++ src/main/resources/lang/messages.properties | 1 + 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java index 0b77ab0..ab7514b 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java @@ -5,7 +5,6 @@ import java.util.Map; import java.util.WeakHashMap; import org.bukkit.GameMode; -import org.bukkit.entity.Animals; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -86,9 +85,6 @@ public class ModInventories extends CoreModule { if (plugin.getPermManager().hasPermission(player, InventoryPermissions.KEEP_INVENTORY)) return; player.closeInventory(); - if (player.getVehicle() != null && player.getVehicle() instanceof Animals) { - player.leaveVehicle(); - } GameMode cgm = player.getGameMode(); if (gm == GameMode.ADVENTURE && !config.getSeparateAdventure()) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java index 318b202..c6e0dc5 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/LimitConfig.java @@ -179,9 +179,6 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup * - LEVER * - STONE_BUTTON * - WOOD_BUTTON - * - WORKBENCH - * - ANVIL - * - ENCHANTMENT_TABLE */ @IsConfigurationNode(name = "interact", order = 600) public BlackList getBlockInteraction() { @@ -193,9 +190,6 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup interactList.add(new BlackList.Blacklisted(Material.LEVER)); interactList.add(new BlackList.Blacklisted(Material.STONE_BUTTON)); interactList.add(new BlackList.Blacklisted(Material.WOOD_BUTTON)); - interactList.add(new BlackList.Blacklisted(Material.WORKBENCH)); - interactList.add(new BlackList.Blacklisted(Material.ANVIL)); - interactList.add(new BlackList.Blacklisted(Material.ENCHANTMENT_TABLE)); } } return interactList; @@ -210,23 +204,14 @@ public class LimitConfig extends Configuration implements IConfigurationSubGroup * if you're not sure). * * default: - * - MINECART_CHEST - * - MINECART_FURNACE - * - MINECART_HOPPER * - ITEM_FRAME - * - VILLAGER */ @IsConfigurationNode(name = "entityInteract", order = 650) public BlackListEntity getBlockEntityInteraction() { if (interactEntityList == null) { interactEntityList = new BlackListEntity(config.getList("entityInteract")); if (!config.contains("entityInteract")) { - interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.MINECART_CHEST)); - interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.MINECART_FURNACE)); - interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.MINECART_HOPPER)); - interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.ITEM_FRAME)); interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.VILLAGER)); - interactEntityList.add(new BlackListEntity.Blacklisted(EntityType.HORSE)); } } return interactEntityList; diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/NoLimitPermissions.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/NoLimitPermissions.java index 60e4d74..ae64272 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/NoLimitPermissions.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/NoLimitPermissions.java @@ -20,6 +20,8 @@ package de.jaschastarke.minecraft.limitedcreative.limits; import java.util.Collection; import org.bukkit.block.Block; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; import org.bukkit.material.MaterialData; import org.bukkit.permissions.PermissionDefault; @@ -50,7 +52,7 @@ public class NoLimitPermissions extends SimplePermissionContainerNode { public static final IPermission ALL = new ParentPermissionContainerNode(PARENT, "*", PermissionDefault.OP, PARENT); /** - * Allows bypassing the "do not open a chest"-limitation + * Allows bypassing the "do not open a chest" and "do not open inventory"-limitation */ @IsChildPermission public static final IPermission CHEST = new BasicPermission(PARENT, "chest", PermissionDefault.FALSE); @@ -107,7 +109,13 @@ public class NoLimitPermissions extends SimplePermissionContainerNode { */ @IsChildPermission public static final IPermission STATS_POTION = new BasicPermission(PARENT, "potion", PermissionDefault.FALSE); - + + public static IDynamicPermission INVENTORY(Inventory inv) { + return new InventoryPermission(CHEST, inv.getType()); + } + public static IDynamicPermission INVENTORY(InventoryType invtype) { + return new InventoryPermission(CHEST, invtype); + } public static IDynamicPermission INTERACT(Block block) { return new MaterialPermission(BASE_INTERACT, new MaterialData(block.getType(), block.getData())); } @@ -120,7 +128,20 @@ public class NoLimitPermissions extends SimplePermissionContainerNode { public static IDynamicPermission BREAK(Block block) { return new MaterialPermission(BASE_BREAK, new MaterialData(block.getType(), block.getData())); } + + public static class InventoryPermission extends DynamicPermission { + private InventoryType it; + public InventoryPermission(IAbstractPermission parent, InventoryType t) { + super(parent); + it = t; + } + + @Override + protected void buildPermissionsToCheck(Collection perms) { + perms.add(new BasicPermission(parent, it.toString())); + } + } public static class MaterialPermission extends DynamicPermission { private MaterialData md; public MaterialPermission(IAbstractPermission parent, MaterialData m) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java index 45ae779..97c796d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/limits/PlayerListener.java @@ -34,6 +34,7 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerExpChangeEvent; @@ -77,6 +78,18 @@ public class PlayerListener implements Listener { } } + @EventHandler + public void onInventoryOpen(InventoryOpenEvent event) { + if (event.getPlayer() instanceof Player) { + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { + if (checkPermission((Player) event.getPlayer(), NoLimitPermissions.INVENTORY(event.getInventory()))) + return; + event.setCancelled(true); + ((Player) event.getPlayer()).sendMessage(mod.getPlugin().getLocale().trans("blocked.inventory")); + } + } + } + @EventHandler public void onEntityDeath(EntityDeathEvent event) { if (event.getEntity() instanceof Player) { @@ -252,4 +265,7 @@ public class PlayerListener implements Listener { private boolean checkPermission(PlayerEvent event, IDynamicPermission perm) { return mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), perm); } + private boolean checkPermission(Player player, IDynamicPermission perm) { + return mod.getPlugin().getPermManager().hasPermission(player, perm); + } } diff --git a/src/main/resources/lang/messages.properties b/src/main/resources/lang/messages.properties index ade321c..0c5c344 100644 --- a/src/main/resources/lang/messages.properties +++ b/src/main/resources/lang/messages.properties @@ -36,6 +36,7 @@ cmdblock.blocked.not_console: This command can not be used from console exception.config.material_not_found: (Config) Material with name/id "{0}" was not found. exception.region.not_optional: You can not be {0} in that area +blocked.inventory: Opening inventories is not allowed in creative mode blocked.chest: Access to chests is not allowed in creative mode blocked.survival_flying: You should stay on ground, when leaving a creative-area blocked.outside_place: You can not place blocks outside of the gamemode-area