Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package de.blablubbabc.test2;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.craftbukkit.v1_14_R1.entity.CraftVillager;
  7. import org.bukkit.entity.Villager;
  8. import org.bukkit.entity.Villager.Profession;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.player.PlayerInteractEvent;
  12. import org.bukkit.inventory.EquipmentSlot;
  13. import org.bukkit.inventory.ItemStack;
  14.  
  15. public class NoAIVillagerTest implements Listener {
  16.  
  17.     @EventHandler
  18.     public void onPlayerInteractEvent(PlayerInteractEvent event) {
  19.         if (event.getHand() != EquipmentSlot.HAND) return;
  20.         if (!event.hasBlock()) return;
  21.         if (!event.hasItem()) return;
  22.         ItemStack item = event.getItem();
  23.         if (item.getType() != Material.CARROT) return;
  24.  
  25.         Block block = event.getClickedBlock();
  26.         Location spawnLoc = block.getLocation().add(0.5D, 1.0D, 0.5D);
  27.         block.getWorld().spawn(spawnLoc, Villager.class, (e) -> {
  28.             e.setPersistent(false);
  29.             e.setAI(false);
  30.             e.setProfession(Profession.FARMER);
  31.  
  32.             // onGround is false initially.
  33.             // Spigot's entity activation feature has a bypass for all falling entities (does not deactivate them while
  34.             // they are falling, even if they are outside the activation range).
  35.             // Since this entity has NoAi, it will never update its onGround flag.
  36.             // -> Entity keeps getting ignored by the entity activation feature, and therefore keeps getting ticked even
  37.             // outside the activation range. This results in even worse performance than compared to mob with AI that
  38.             // does not bypass Spigot's entity activation feature.
  39.             // See related ticket: https://hub.spigotmc.org/jira/browse/SPIGOT-3947
  40.  
  41.             // Workaround idea: Manually set the onGround flag to true
  42.             // Possible alternative for reproducing this issue without relying on CB / NMS: Set the villager's NoAI flag
  43.             // after a short delay (after the villager has set its onGround flag 'naturally' to true).
  44.             ((CraftVillager) e).getHandle().onGround = true;
  45.  
  46.             // New problem now:
  47.             // Spigot will occasionally skip ticking an supposedly active entity (every 4 ticks, see the entity
  48.             // activation feature's checkIfActive function).
  49.             // So instead of doing the entity's full tick, the entity's inactiveTick is called.
  50.             // For villagers this will call the mob's 'mobTick' function, if Spigot's 'tick-inactive-villagers' setting
  51.             // is enabled (it is by default). (This is supposed to retain some villager behavior: Found this
  52.             // https://hub.spigotmc.org/jira/browse/SPIGOT-3846)
  53.             // The problem with that however:
  54.             // mobTick runs the villager's AI behaviors added in MC 1.14.
  55.             // But in vanilla minecraft this function will not get called for NoAI mobs (see call hierarchy:
  56.             // EntityLiving#tick -> EntityLiving#movementTick (checks NoAI flag) -> EntityInsentient#doTick ->
  57.             // EntityVillager#mobTick).
  58.             // But with Spigot this function will occasionally get run for NoAI mobs as well (with all its AI related
  59.             // effects, such as working places getting searched, validated, professions updated, etc.).
  60.  
  61.             // Possible solution:
  62.             // Check the mob's NoAI state before calling mobTick during the villager's inactive tick.
  63.         });
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement