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,14 +77,17 @@ 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;
// Legitimate to not use entities from the event object since they are final in PlayerLeashEntityEvent scheduler.runAtEntity(villager, leash -> {
scheduler.runAtEntity(villager, leash -> villager.setLeashHolder(player)); // Legitimate to not use entities from the event object since they are final in PlayerLeashEntityEvent
if (villager.setLeashHolder(player)) {
if (log_enabled) { handItem.subtract(1);
final Location location = villager.getLocation(); if (log_enabled) {
VillagerOptimizer.getLog().info(Component.text(player.getName() + " leashed a villager at " + final Location location = villager.getLocation();
"x=" + location.getX() + ", y=" + location.getY() + ", z=" + location.getZ() + VillagerOptimizer.getLog().info(Component.text(player.getName() + " leashed a villager at " +
" in world " + location.getWorld().getName()).style(VillagerOptimizer.plugin_style)); "x=" + location.getX() + ", y=" + location.getY() + ", z=" + location.getZ() +
} " in world " + location.getWorld().getName()).style(VillagerOptimizer.plugin_style));
}
}
});
} }
} }