make leashing logic more natural

This commit is contained in:
xGinko 2024-02-09 00:33:41 +01:00
parent aa0eedab87
commit 003240df00

View File

@ -63,25 +63,7 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene
event.setCancelled(true); // Cancel the event, so we don't interact with the villager event.setCancelled(true); // Cancel the event, so we don't interact with the villager
Villager villager = (Villager) event.getRightClicked(); Villager villager = (Villager) event.getRightClicked();
if (villager.isLeashed()) return;
if (villager.isLeashed()) {
// If leash holder clicked leashed villager, unleash.
try {
if (
villager.getLeashHolder().getUniqueId().equals(player.getUniqueId())
&& villager.setLeashHolder(null)
&& log_enabled
) {
final Location location = villager.getLocation();
VillagerOptimizer.getLog().info(Component.text(player.getName() + " un-leashed a villager at " +
"x=" + location.getX() + ", y=" + location.getY() + ", z=" + location.getZ() +
" in world " + location.getWorld().getName()).style(VillagerOptimizer.plugin_style));
}
} catch (IllegalStateException ignored) {} // Shouldn't throw because we checked LivingEntity#isLeashed()
// Otherwise do nothing. There should only ever be one leash holder
return;
}
if (only_optimized && !villagerCache.getOrAdd(villager).isOptimized()) return; if (only_optimized && !villagerCache.getOrAdd(villager).isOptimized()) return;
// Call event for compatibility with other plugins, constructing non deprecated if available // Call event for compatibility with other plugins, constructing non deprecated if available
@ -95,9 +77,10 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene
// If canceled by any plugin, do nothing // If canceled by any plugin, do nothing
if (!leashEvent.callEvent()) return; if (!leashEvent.callEvent()) return;
scheduler.runAtEntity(villager, leash -> {
// Legitimate to not use entities from the event object since they are final in PlayerLeashEntityEvent // Legitimate to not use entities from the event object since they are final in PlayerLeashEntityEvent
scheduler.runAtEntity(villager, leash -> villager.setLeashHolder(player)); if (villager.setLeashHolder(player)) {
handItem.subtract(1);
if (log_enabled) { if (log_enabled) {
final Location location = villager.getLocation(); final Location location = villager.getLocation();
VillagerOptimizer.getLog().info(Component.text(player.getName() + " leashed a villager at " + VillagerOptimizer.getLog().info(Component.text(player.getName() + " leashed a villager at " +
@ -105,4 +88,6 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene
" in world " + location.getWorld().getName()).style(VillagerOptimizer.plugin_style)); " in world " + location.getWorld().getName()).style(VillagerOptimizer.plugin_style));
} }
} }
});
}
} }