Chunk Loading problems

Discussion in 'Plugin Development' started by AlvinB, Oct 2, 2016.

Thread Status:
Not open for further replies.
  1. So, I'm messing around trying to create a plugin which teleports an entity from one location to another when right clicked. It works completely fine at short distances, but when I try to teleport an entity from an unloaded Chunk, nothing seems to happen. The teleport() method simply doesn't do anything, and manually removing and respawning the entity does work, but it doesn't actually remove the original entity. So I draw the logical conclusion that the unloaded Chunks is the problem. Furthermore, if the entity is in the spawn chunks when teleported, everything works fine, confirming my hypothesis.

    I then tried to load the chunk where the entity was when teleporting, but to no success, I also tried loading 5x5 chunks around it to make it an entity-processing chunk (I think that's how it works..), but again, to no success. So my question is, how do I manually load a Chunk so the entity can be teleported away?
     
  2. Have you tried loading the chunk and teleporting the entity a few ticks later?
     
  3. @FisheyLP
    Yep, didn't work either.. Here's my code btw:
    Code:java
    1. package com.bringholm.featherteleport;
    2.  
    3. import org.apache.commons.lang.WordUtils;
    4. import org.bukkit.Chunk;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.entity.Animals;
    9. import org.bukkit.entity.Entity;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.player.PlayerInteractAtEntityEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.inventory.EquipmentSlot;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. import java.util.HashMap;
    19. import java.util.UUID;
    20.  
    21. import static org.bukkit.ChatColor.GOLD;
    22.  
    23. public class FeatherTeleport extends JavaPlugin implements Listener {
    24. private HashMap<UUID, Entity> selectedMobs = new HashMap<>();
    25. private static FeatherTeleport main;
    26.  
    27. public FeatherTeleport() {
    28. main = this;
    29. }
    30.  
    31. public static FeatherTeleport getInstance() {
    32. return main;
    33. }
    34.  
    35. @Override
    36. public void onEnable() {
    37. this.getServer().getPluginManager().registerEvents(this, this);
    38. }
    39.  
    40. @EventHandler
    41. public void onEntityRightClick(PlayerInteractAtEntityEvent e) {
    42. if (e.getHand() == EquipmentSlot.HAND && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.FEATHER) {
    43. if (e.getRightClicked() instanceof Animals) {
    44. selectedMobs.put(e.getPlayer().getUniqueId(), e.getRightClicked());
    45. e.getPlayer().sendMessage(GOLD + "Selected " + WordUtils.capitalizeFully(e.getRightClicked().getType().toString()) + " to teleport!");
    46. }
    47. }
    48. }
    49.  
    50. @EventHandler
    51. public void onRightClick(PlayerInteractEvent e) {
    52. if (e.getItem() == null) {
    53. return;
    54. }
    55. if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getHand() == EquipmentSlot.HAND && e.getItem().getType() == Material.FEATHER) {
    56. if (selectedMobs.containsKey(e.getPlayer().getUniqueId())) {
    57. Location location = e.getClickedBlock().getLocation().add(0.5, 1, 0.5);
    58. Entity entity = selectedMobs.get(e.getPlayer().getUniqueId());
    59. if (!entity.getLocation().getWorld().equals(e.getPlayer().getWorld())) {
    60. e.getPlayer().sendMessage(GOLD + "Cannot teleport entities between worlds!");
    61. return;
    62. }
    63. if (!location.getBlock().getType().isSolid()) {
    64. loadChunks(entity.getLocation().getChunk());
    65. entity.teleport(location);
    66. e.getPlayer().sendMessage(GOLD + "Teleported " + WordUtils.capitalizeFully(entity.getType().toString()) + "!");
    67. selectedMobs.remove(e.getPlayer().getUniqueId());
    68. } else {
    69. e.getPlayer().sendMessage(GOLD + "Unsafe location to teleport mob to!");
    70. }
    71. }
    72. }
    73. }
    74.  
    75. private void loadChunks(Chunk chunk) {
    76. World world = chunk.getWorld();
    77. for (int x = chunk.getX() - 6; x <= chunk.getX() + 6; x++) {
    78. for (int z = chunk.getZ() - 6; z <= chunk.getZ() + 6; z++) {
    79. world.loadChunk(x, z);
    80. }
    81. }
    82. }
    83. }
     
  4. Offline

    Zombie_Striker

    There is no reason to have this. If you have other classes, pass the main instance of this class through the other class's constructor.

    Remember, the item in the player's hand can be null.
    Your main problem:
    What may be happening is that the chunks are unloading right after you load them. Try listening to ChunkUnloadEvent to make sure those chunks are not unloading. Also, try checking if the chunk is actually loaded using (Chunk#isLoaded()).
     
    AlvinB likes this.
  5. @Zombie_Striker
    1. That's just part of my part of my plugin template I forgot to delete :p
    2. I do a nullcheck right before that?
    3. I can't believe I didn't think of that.. That was the problem, thank you very much!
     
  6. @Zombie_Striker
    Actually, it isn't fixed, although I have made progress. Using the ChunkUnloadEvent I can prevent the chunks from unloading, provided that they have already been loaded by the player, but I can't seem to artificially load them. Chunk#load() doesn't work, and loading a 21x21 grid of chunks around the chunks doesn't seem to load it either..
     
Thread Status:
Not open for further replies.

Share This Page