Skip to content

Commit

Permalink
Version 2.2.0
Browse files Browse the repository at this point in the history
Prevent villagers from being named when the player does not
have build permission at the villager's location.
  • Loading branch information
totemo committed Apr 13, 2020
1 parent a3b030b commit c70d22e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>io.github.redwallhp</groupId>
<artifactId>VillagerUtils</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit c70d22e

Please sign in to comment.