Added support for the item attribute of the Context and routed PlayerInteract into item-use,.

This commit is contained in:
sk89q 2012-11-06 00:51:40 -08:00
parent 2d9a28b0e6
commit fc5c59ec85
3 changed files with 74 additions and 13 deletions

View File

@ -392,7 +392,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
plugin.forgetPlayer(player); plugin.forgetPlayer(player);
} }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -412,13 +412,31 @@ public void onPlayerInteract(PlayerInteractEvent event) {
} }
// RuleLists // RuleLists
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT); RuleSet rules;
BukkitContext context = new BukkitContext(event); BukkitContext context;
context.setSourceEntity(player);
context.setTargetBlock(event.getClickedBlock().getState()); // Send one for the block
if (rules.process(context)) { Block block = event.getClickedBlock();
event.setCancelled(true); if (block != null) {
return; rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
context = new BukkitContext(event);
context.setSourceEntity(player);
context.setTargetBlock(event.getClickedBlock().getState());
if (rules.process(context)) {
event.setUseInteractedBlock(Result.DENY);
}
}
// Send one for the item in the end
ItemStack heldItem = event.getPlayer().getItemInHand();
if (heldItem != null) {
rules = wcfg.getRuleList().get(KnownAttachment.ITEM_USE);
context = new BukkitContext(event);
context.setSourceEntity(event.getPlayer());
context.setItem(heldItem);
if (rules.process(context)) {
event.setUseItemInHand(Result.DENY);
}
} }
} }

View File

@ -18,10 +18,16 @@
package com.sk89q.worldguard.bukkit.definitions; package com.sk89q.worldguard.bukkit.definitions;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import com.sk89q.rebar.util.MaterialPattern; import com.sk89q.rebar.util.MaterialPattern;
import com.sk89q.rulelists.Criteria; import com.sk89q.rulelists.Criteria;
@ -34,6 +40,7 @@ public class ItemCriteria implements Criteria<BukkitContext> {
private final EntityResolver entityResolver; private final EntityResolver entityResolver;
private final ItemStackSlotResolver itemResolver; private final ItemStackSlotResolver itemResolver;
private MaterialPattern[] patterns = new MaterialPattern[0]; private MaterialPattern[] patterns = new MaterialPattern[0];
private Set<PotionEffectType> potionEffects = new HashSet<PotionEffectType>();
private Boolean hasData = false; private Boolean hasData = false;
public ItemCriteria(EntityResolver entityResolver, ItemStackSlotResolver itemResolver) { public ItemCriteria(EntityResolver entityResolver, ItemStackSlotResolver itemResolver) {
@ -63,9 +70,18 @@ public void setPatterns(List<MaterialPattern> patterns) {
this.patterns = arr; this.patterns = arr;
} }
public Set<PotionEffectType> getPotionEffects() {
return potionEffects;
}
public void setPotionEffects(Set<PotionEffectType> effects) {
this.potionEffects = effects;
}
@Override @Override
public boolean matches(BukkitContext context) { public boolean matches(BukkitContext context) {
ItemStack item; ItemStack item;
boolean found = false;
if (entityResolver == null) { if (entityResolver == null) {
item = context.getItem(); item = context.getItem();
@ -83,17 +99,34 @@ public boolean matches(BukkitContext context) {
return false; return false;
} }
if (hasData != null && hasData == (item.getDurability() != 0)) { if (hasData != null) {
return true; found = hasData == (item.getDurability() != 0);
if (!found) {
return false;
}
}
if (item.getType() == Material.POTION && potionEffects.size() > 0) {
for (PotionEffect effect : Potion.fromItemStack(item).getEffects()) {
if (potionEffects.contains(effect.getType())) {
found = true;
break;
}
}
if (!found) {
return false;
}
} }
for (MaterialPattern pattern : patterns) { for (MaterialPattern pattern : patterns) {
if (pattern.matches(item.getTypeId(), item.getDurability())) { if (pattern.matches(item.getTypeId(), item.getDurability())) {
return true; found = true;
break;
} }
} }
return false; return found;
} }
} }

View File

@ -21,11 +21,15 @@
import static com.sk89q.rulelists.RuleEntryLoader.INLINE; import static com.sk89q.rulelists.RuleEntryLoader.INLINE;
import java.util.List; import java.util.List;
import java.util.Set;
import org.bukkit.potion.PotionEffectType;
import com.sk89q.rebar.config.AbstractNodeLoader; import com.sk89q.rebar.config.AbstractNodeLoader;
import com.sk89q.rebar.config.ConfigurationNode; import com.sk89q.rebar.config.ConfigurationNode;
import com.sk89q.rebar.config.LoaderBuilderException; import com.sk89q.rebar.config.LoaderBuilderException;
import com.sk89q.rebar.config.types.MaterialPatternLoaderBuilder; import com.sk89q.rebar.config.types.MaterialPatternLoaderBuilder;
import com.sk89q.rebar.config.types.StaticFieldLoaderBuilder;
import com.sk89q.rebar.util.LoggerUtils; import com.sk89q.rebar.util.LoggerUtils;
import com.sk89q.rebar.util.MaterialPattern; import com.sk89q.rebar.util.MaterialPattern;
import com.sk89q.rulelists.DefinitionException; import com.sk89q.rulelists.DefinitionException;
@ -38,6 +42,8 @@ public class ItemCriteriaLoader extends AbstractNodeLoader<ItemCriteria> {
private final RuleListsManager manager; private final RuleListsManager manager;
private MaterialPatternLoaderBuilder materialLoader = new MaterialPatternLoaderBuilder(); private MaterialPatternLoaderBuilder materialLoader = new MaterialPatternLoaderBuilder();
private StaticFieldLoaderBuilder<PotionEffectType> effectLoader =
new StaticFieldLoaderBuilder<PotionEffectType>(PotionEffectType.class);
public ItemCriteriaLoader(RuleListsManager manager) { public ItemCriteriaLoader(RuleListsManager manager) {
this.manager = manager; this.manager = manager;
@ -69,12 +75,16 @@ public ItemCriteria read(ConfigurationNode node) throws DefinitionException {
throw new LoaderBuilderException("No block materials specified"); throw new LoaderBuilderException("No block materials specified");
} }
// Potion effects
Set<PotionEffectType> effects = node.setOf("potion-effect", effectLoader);
ItemCriteria criteria = new ItemCriteria(entityResolver, resolver); ItemCriteria criteria = new ItemCriteria(entityResolver, resolver);
criteria.setPatterns(patterns); criteria.setPatterns(patterns);
criteria.setDataCheck(hasData); criteria.setDataCheck(hasData);
criteria.setPotionEffects(effects);
RuleListUtils.warnUnknown(node, LoggerUtils.getLogger(getClass()), RuleListUtils.warnUnknown(node, LoggerUtils.getLogger(getClass()),
"item", "entity", "has-data", "material"); "item", "entity", "has-data", "material", "potion-effect");
return criteria; return criteria;
} }