Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.blablubbabc.test2;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.craftbukkit.v1_14_R1.entity.CraftVillager;
- import org.bukkit.entity.Villager;
- import org.bukkit.entity.Villager.Profession;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.EquipmentSlot;
- import org.bukkit.inventory.ItemStack;
- public class NoAIVillagerTest implements Listener {
- @EventHandler
- public void onPlayerInteractEvent(PlayerInteractEvent event) {
- if (event.getHand() != EquipmentSlot.HAND) return;
- if (!event.hasBlock()) return;
- if (!event.hasItem()) return;
- ItemStack item = event.getItem();
- if (item.getType() != Material.CARROT) return;
- Block block = event.getClickedBlock();
- Location spawnLoc = block.getLocation().add(0.5D, 1.0D, 0.5D);
- block.getWorld().spawn(spawnLoc, Villager.class, (e) -> {
- e.setPersistent(false);
- e.setAI(false);
- e.setProfession(Profession.FARMER);
- // onGround is false initially.
- // Spigot's entity activation feature has a bypass for all falling entities (does not deactivate them while
- // they are falling, even if they are outside the activation range).
- // Since this entity has NoAi, it will never update its onGround flag.
- // -> Entity keeps getting ignored by the entity activation feature, and therefore keeps getting ticked even
- // outside the activation range. This results in even worse performance than compared to mob with AI that
- // does not bypass Spigot's entity activation feature.
- // See related ticket: https://hub.spigotmc.org/jira/browse/SPIGOT-3947
- // Workaround idea: Manually set the onGround flag to true
- // Possible alternative for reproducing this issue without relying on CB / NMS: Set the villager's NoAI flag
- // after a short delay (after the villager has set its onGround flag 'naturally' to true).
- ((CraftVillager) e).getHandle().onGround = true;
- // New problem now:
- // Spigot will occasionally skip ticking an supposedly active entity (every 4 ticks, see the entity
- // activation feature's checkIfActive function).
- // So instead of doing the entity's full tick, the entity's inactiveTick is called.
- // For villagers this will call the mob's 'mobTick' function, if Spigot's 'tick-inactive-villagers' setting
- // is enabled (it is by default). (This is supposed to retain some villager behavior: Found this
- // https://hub.spigotmc.org/jira/browse/SPIGOT-3846)
- // The problem with that however:
- // mobTick runs the villager's AI behaviors added in MC 1.14.
- // But in vanilla minecraft this function will not get called for NoAI mobs (see call hierarchy:
- // EntityLiving#tick -> EntityLiving#movementTick (checks NoAI flag) -> EntityInsentient#doTick ->
- // EntityVillager#mobTick).
- // But with Spigot this function will occasionally get run for NoAI mobs as well (with all its AI related
- // effects, such as working places getting searched, validated, professions updated, etc.).
- // Possible solution:
- // Check the mob's NoAI state before calling mobTick during the villager's inactive tick.
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement