diff --git a/README.md b/README.md
index fb6bed9..2910387 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,9 @@ Tools for managing villagers on Spigot servers.
* Protect villagers from grief by only allowing WorldGuard region members to harm them.
+* Prevent villagers from being renamed by players who don't have build permission
+ at the villager's location.
+
* Protect villagers from being hurt by blacklisted mob types. e.g. `protect_from_mobs: [Evoker, Evoker_Fangs, Vex, Vindicator]`.
Note that novice villagers select their profession to suit a nearby workstation.
diff --git a/pom.xml b/pom.xml
index 4100be2..0e2cefa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
io.github.redwallhp
VillagerUtils
- 2.1.0
+ 2.2.0
UTF-8
diff --git a/src/main/java/io/github/redwallhp/villagerutils/listeners/VillagerProtector.java b/src/main/java/io/github/redwallhp/villagerutils/listeners/VillagerProtector.java
index 1beb14a..43c5352 100644
--- a/src/main/java/io/github/redwallhp/villagerutils/listeners/VillagerProtector.java
+++ b/src/main/java/io/github/redwallhp/villagerutils/listeners/VillagerProtector.java
@@ -5,6 +5,7 @@
import java.util.List;
import org.bukkit.ChatColor;
+import org.bukkit.Material;
import org.bukkit.entity.AbstractVillager;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
@@ -14,6 +15,8 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
+import org.bukkit.event.player.PlayerInteractEntityEvent;
+import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.potion.PotionType;
import io.github.redwallhp.villagerutils.VillagerUtils;
@@ -65,6 +68,30 @@ public void onEntityDamageByPlayer(EntityDamageByEntityEvent event) {
}
}
+ /**
+ * Prevent players from applying name tags to villagers when they can't
+ * build at the villager location.
+ */
+ @EventHandler(ignoreCancelled = true)
+ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
+ Player player = event.getPlayer();
+
+ // There's no trivial way to map EquipmentSlot enum to Inventory index.
+ if (event.getHand() == EquipmentSlot.HAND &&
+ player.getEquipment().getItemInMainHand().getType() != Material.NAME_TAG) {
+ return;
+ }
+ if (event.getHand() == EquipmentSlot.OFF_HAND &&
+ player.getEquipment().getItemInOffHand().getType() != Material.NAME_TAG) {
+ return;
+ }
+
+ if (!WorldGuardHelper.canBuild(player, event.getRightClicked().getLocation())) {
+ event.setCancelled(true);
+ player.sendMessage(ChatColor.RED + "You can't rename that villager in this region.");
+ }
+ }
+
/**
* Handle WorldGuard based protection from players' potions
*/