-1

I'm writing a simple Bukkit/Spigot plugin that replaces endermen inside of End City structures with shulkers. Here's the code:

package ru.cardboardbox.shulkerspawner;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BlockVector;

import java.util.logging.Logger;

public final class main extends JavaPlugin implements Listener {
    World world_the_end = Bukkit.getWorld("world_the_end");
    private Logger log = Logger.getLogger("Minecraft");
    @Override
    public void onEnable() {
        log.info("[ShulkerSpawner] Good morning sunshine!");
        getServer().getPluginManager().registerEvents(this, this);
    }
    @EventHandler(ignoreCancelled = true)
    public void onSpawn(CreatureSpawnEvent e) {

        Location spawneeStandingOnBlockLocation = e.getLocation().add(new BlockVector(0,-1,0));
        Location spawneeRealLocation = e.getLocation();
        Block checkedBlock = spawneeStandingOnBlockLocation.getBlock();
        Material checkedBlockMaterial = checkedBlock.getType();
        Entity spawnedEntity = e.getEntity();
        log.info("[ShulkerSpawner] We're checking at " + spawneeRealLocation + ", the entity being an instance of " + e.getEntityType());
        log.info("[ShulkerSpawner] The entity is standing on the block " + spawneeStandingOnBlockLocation.toString() + ", which is " + checkedBlockMaterial.toString());

        if (e.getEntityType() == EntityType.ENDERMAN) {



            /////// NULL POINTER NEXT LINE
            Location endCityOrNull = world_the_end.locateNearestStructure(spawneeRealLocation, StructureType.END_CITY,3,true);



            if (endCityOrNull != null) {
                if (    checkedBlockMaterial == Material.PURPUR_BLOCK  ||
                        checkedBlockMaterial == Material.PURPUR_SLAB   ||
                        checkedBlockMaterial == Material.PURPUR_PILLAR ||
                        checkedBlockMaterial == Material.PURPUR_STAIRS )
                {
                    log.info("[ShulkerSpawner] Successfully(?) spawned a shulker at " + spawneeRealLocation.toString());
                    //It's cruft, but it's EXACTLY what I need. Don't ask why. I really need to replace endermen with shulkers.
                    world_the_end.spawnEntity(spawneeRealLocation,EntityType.SHULKER);
                    spawnedEntity.remove();
                } else {
                    log.info("[ShulkerSpawner] Entity is not on city blocks");
                }
            } else {
                log.info("[ShulkerSpawner] Entity is not near a city");
            }
        } else {
            log.info("[ShulkerSpawner] Entity is not an Enderman");
        }
    }


}

Problem: The marked line, against all odds, tends to throw up with NullPointerException. The method locateNearestStructure is nullable and returns null when a structure was no found.

The kicker: The NullPointerException is thrown upon assignment. It's not thrown for the world where the structure we're looking for doesn't spawn (so it would always return null).

The question: Why does this happen, and how can this be fixed?

    [08:56:54] [Server thread/INFO]: [ShulkerSpawner] We're checking at Location{world=CraftWorld{name=world_the_end},x=1667.5,y=102.0,z=35.5,pitch=0.0,yaw=1.818178}, the entity being an instance of ENDERMAN
[08:56:54] [Server thread/INFO]: [ShulkerSpawner] The entity is standing on the block Location{world=CraftWorld{name=world_the_end},x=1667.5,y=101.0,z=35.5,pitch=0.0,yaw=1.818178}, which is PURPUR_BLOCK
[08:56:54] [Server thread/ERROR]: Could not pass event CreatureSpawnEvent to shulkerspawner v1.0-SNAPSHOT
java.lang.NullPointerException: null
    at ru.cardboardbox.shulkerspawner.main.onSpawn(main.java:39) ~[?:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor7.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.15.2.jar:git-Paper-129]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.15.2.jar:git-Paper-129]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.15.2.jar:git-Paper-129]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:559) ~[patched_1.15.2.jar:git-Paper-129]
    at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:631) ~[patched_1.15.2.jar:git-Paper-129]
    at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.doEntityAddEventCalling(CraftEventFactory.java:552) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.WorldServer.addEntity0(WorldServer.java:1214) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.WorldServer.addEntity(WorldServer.java:1121) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.SpawnerCreature.spawnMobs(SpawnerCreature.java:123) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.ChunkProviderServer.lambda$tickChunks$7(ChunkProviderServer.java:723) ~[patched_1.15.2.jar:git-Paper-129]
    at it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap$1.forEach(Long2ObjectLinkedOpenHashMap.java:1661) ~[patched_1.15.2.jar:git-Paper-129]
    at com.google.common.collect.Iterables$UnmodifiableIterable.forEach(Iterables.java:105) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.ChunkProviderServer.tickChunks(ChunkProviderServer.java:659) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.ChunkProviderServer.tick(ChunkProviderServer.java:602) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.WorldServer.doTick(WorldServer.java:406) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.MinecraftServer.b(MinecraftServer.java:1245) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:430) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:1112) ~[patched_1.15.2.jar:git-Paper-129]
    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:934) ~[patched_1.15.2.jar:git-Paper-129]
    at java.lang.Thread.run(Thread.java:835) [?:?]

To suggestions about world_the_end containing a null reference: This strangely covers all dimensions of the world, and works right in the overworld. Only in The End it throws up like this. Folder for The End is called "world_the_end" Did it default to the default world, and am I completely misunderstanding how bukkit worlds work?

3 Answers3

0

You could try to get all worlds in the server and identify the end one as follows:

public World getTheEnd() {
    for(World w: Bukkit.getServer().getWorlds()) {
        if(w.getEnvironment().equals(World.Environment.THE_END)) {
            return w;
        }
    }
    return null;
}

In Spigot/Bukkit server World objects have a parameter of type World.Environment which can be:

  • World.Environment.NORMAL
  • World.Environment.NETHER
  • World.Environment.THE_END

Note that this solution will only work if your server has only 1 end world. Otherwise the function getTheEnd() will return an (probably) unpredictable the end world.

Please let me know if this solved your issue.

PauMAVA
  • 1,163
  • 14
  • 23
0

The answer is a combination of the other answer, and the comments, since world_the_end is null, you need a better way to get the end dimension.

World world = spawnedEntity.getWorld();
if(!world.getEnvironment().equals(World.Environment.THE_END)){
    // Since the world is not the end stop there.
    return;
}

Now you can replace world_the_end with world, since we know it's the end, and at that point you also know it's an Enderman.

0

You can not directly set the world_the_end object, because worlds are loaded after your plugin is being loaded. So there exists no world with the name world_the_end at this time.

You can set the attribute in the onEnable() method, like this:

public final class main extends JavaPlugin implements Listener {   

    World world_the_end;

    private Logger log = Logger.getLogger("Minecraft");

    @Override
    public void onEnable() {
        world_the_end = Bukkit.getWorld("world_the_end");

        log.info("[ShulkerSpawner] Good morning sunshine!");
        getServer().getPluginManager().registerEvents(this, this);
    }
Funzter
  • 86
  • 5