mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2024-12-22 17:17:57 +01:00
Added support for the item attribute of the Context and routed PlayerInteract into item-use,.
This commit is contained in:
parent
2d9a28b0e6
commit
fc5c59ec85
@ -392,7 +392,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
plugin.forgetPlayer(player);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
|
||||
@ -412,13 +412,31 @@ public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
}
|
||||
|
||||
// RuleLists
|
||||
RuleSet rules = wcfg.getRuleList().get(KnownAttachment.BLOCK_INTERACT);
|
||||
BukkitContext context = new BukkitContext(event);
|
||||
context.setSourceEntity(player);
|
||||
context.setTargetBlock(event.getClickedBlock().getState());
|
||||
if (rules.process(context)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
RuleSet rules;
|
||||
BukkitContext context;
|
||||
|
||||
// Send one for the block
|
||||
Block block = event.getClickedBlock();
|
||||
if (block != null) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,10 +18,16 @@
|
||||
|
||||
package com.sk89q.worldguard.bukkit.definitions;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
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.rulelists.Criteria;
|
||||
@ -34,6 +40,7 @@ public class ItemCriteria implements Criteria<BukkitContext> {
|
||||
private final EntityResolver entityResolver;
|
||||
private final ItemStackSlotResolver itemResolver;
|
||||
private MaterialPattern[] patterns = new MaterialPattern[0];
|
||||
private Set<PotionEffectType> potionEffects = new HashSet<PotionEffectType>();
|
||||
private Boolean hasData = false;
|
||||
|
||||
public ItemCriteria(EntityResolver entityResolver, ItemStackSlotResolver itemResolver) {
|
||||
@ -63,9 +70,18 @@ public void setPatterns(List<MaterialPattern> patterns) {
|
||||
this.patterns = arr;
|
||||
}
|
||||
|
||||
public Set<PotionEffectType> getPotionEffects() {
|
||||
return potionEffects;
|
||||
}
|
||||
|
||||
public void setPotionEffects(Set<PotionEffectType> effects) {
|
||||
this.potionEffects = effects;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(BukkitContext context) {
|
||||
ItemStack item;
|
||||
boolean found = false;
|
||||
|
||||
if (entityResolver == null) {
|
||||
item = context.getItem();
|
||||
@ -83,17 +99,34 @@ public boolean matches(BukkitContext context) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasData != null && hasData == (item.getDurability() != 0)) {
|
||||
return true;
|
||||
if (hasData != null) {
|
||||
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) {
|
||||
if (pattern.matches(item.getTypeId(), item.getDurability())) {
|
||||
return true;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return found;
|
||||
}
|
||||
|
||||
}
|
@ -21,11 +21,15 @@
|
||||
import static com.sk89q.rulelists.RuleEntryLoader.INLINE;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import com.sk89q.rebar.config.AbstractNodeLoader;
|
||||
import com.sk89q.rebar.config.ConfigurationNode;
|
||||
import com.sk89q.rebar.config.LoaderBuilderException;
|
||||
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.MaterialPattern;
|
||||
import com.sk89q.rulelists.DefinitionException;
|
||||
@ -38,6 +42,8 @@ public class ItemCriteriaLoader extends AbstractNodeLoader<ItemCriteria> {
|
||||
|
||||
private final RuleListsManager manager;
|
||||
private MaterialPatternLoaderBuilder materialLoader = new MaterialPatternLoaderBuilder();
|
||||
private StaticFieldLoaderBuilder<PotionEffectType> effectLoader =
|
||||
new StaticFieldLoaderBuilder<PotionEffectType>(PotionEffectType.class);
|
||||
|
||||
public ItemCriteriaLoader(RuleListsManager manager) {
|
||||
this.manager = manager;
|
||||
@ -69,12 +75,16 @@ public ItemCriteria read(ConfigurationNode node) throws DefinitionException {
|
||||
throw new LoaderBuilderException("No block materials specified");
|
||||
}
|
||||
|
||||
// Potion effects
|
||||
Set<PotionEffectType> effects = node.setOf("potion-effect", effectLoader);
|
||||
|
||||
ItemCriteria criteria = new ItemCriteria(entityResolver, resolver);
|
||||
criteria.setPatterns(patterns);
|
||||
criteria.setDataCheck(hasData);
|
||||
criteria.setPotionEffects(effects);
|
||||
|
||||
RuleListUtils.warnUnknown(node, LoggerUtils.getLogger(getClass()),
|
||||
"item", "entity", "has-data", "material");
|
||||
"item", "entity", "has-data", "material", "potion-effect");
|
||||
|
||||
return criteria;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user