Я пишу простой плагин Bukkit / Spigot, который заменяет эндерменов внутри структур End City на шалкеры. Вот код:
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");
}
}
}
Проблема: отмеченная линия, несмотря ни на что, имеет тенденцию вызывать исключение NullPointerException. Метод locateNearestStructure имеет значение null и возвращает ноль, если структура не найдена.
Кикер: исключение NullPointerException вызывается при назначении. Он не создан для мира, где искомая структура не появляется (поэтому она всегда будет возвращать ноль).
Вопрос: почему это происходит и как это можно исправить?
[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) [?:?]
К предложениям о world_the_end
, содержащим нулевую ссылку: это странным образом охватывает все измерения мира и работает прямо в верхнем мире. Только в конце это вырвет, как это. Папка для Конца называется "world_the_end". По умолчанию это мир по умолчанию, и я совершенно не понимаю, как работают миры Bukkit?