Asynchronous issues

Discussion in 'Spigot Plugin Development' started by Lazertx, Oct 20, 2014.

  1. I am currently developing a SkyBlock plugin that supports all the old SkyBlock plugins and I am having an issue with a schematic selector feature that I am adding. I will paste the relevant methods into the post, but what I don't understand is why anything is being run asynchronous as it should be scheduled synchronously.

    StackTrace
    Code (Text):
    [09:35:02] [Netty IO #1/WARN]: java.lang.IllegalStateException: Asynchronous entity world add!
    [09:35:02] [Netty IO #1/WARN]:  at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.server.v1_7_R4.World.a(World.java:2608)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.server.v1_7_R4.Chunk.addEntities(Chunk.java:793)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:40)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.chunkio.ChunkIOProvider.callStage2(ChunkIOProvider.java:13)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.util.AsynchronousExecutor.skipQueue(AsynchronousExecutor.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.util.AsynchronousExecutor.getSkipQueue(AsynchronousExecutor.java:295)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:16)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.server.v1_7_R4.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:120)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.server.v1_7_R4.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:102)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.CraftWorld.getChunkAt(CraftWorld.java:121)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.CraftWorld.getBlockAt(CraftWorld.java:85)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.craftbukkit.v1_7_R4.CraftWorld.getBlockAt(CraftWorld.java:527)
    [09:35:02] [Netty IO #1/WARN]:  at org.bukkit.Location.getBlock(Location.java:82)
    [09:35:02] [Netty IO #1/WARN]:  at com.skyblock.utils.LocationUtil.getHomeLocation(LocationUtil.java:42)
    [09:35:02] [Netty IO #1/WARN]:  at com.skyblock.tasks.SelectorBuilder$1$2.run(SelectorBuilder.java:84)
    [09:35:02] [Netty IO #1/WARN]:  at com.skyblock.utils.HologramUtil$1.onPacketReceiving(HologramUtil.java:212)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.SortedPacketListenerList.invokeReceivingListener(SortedPacketListenerList.java:114)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketRecieving(SortedPacketListenerList.java:67)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.PacketFilterManager.handlePacket(PacketFilterManager.java:614)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.PacketFilterManager.invokePacketRecieving(PacketFilterManager.java:581)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.netty.NettyProtocolInjector.packetReceived(NettyProtocolInjector.java:295)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.netty.NettyProtocolInjector.onPacketReceiving(NettyProtocolInjector.java:261)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.netty.ChannelInjector.decode(ChannelInjector.java:458)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:232)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:131)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
    [09:35:02] [Netty IO #1/WARN]:  at com.comphenix.protocol.injector.netty.ChannelInjector$4.channelRead(ChannelInjector.java:227)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:173)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.handler.timeout.ReadTimeoutHandler.channelRead(ReadTimeoutHandler.java:149)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:100)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:480)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:447)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:341)
    [09:35:02] [Netty IO #1/WARN]:  at net.minecraft.util.io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
    [09:35:02] [Netty IO #1/WARN]:  at java.lang.Thread.run(Unknown Source)
     

    HologramUtil.protocol()
    This is called on the onEnable method, but the packet listener inside is called when the USE_ENTITY packet is received.
    Code (Text):
        public static void protocol() {
            SkyBlock.p.getProtocolManager().addPacketListener(
                    new PacketAdapter(SkyBlock.p, ListenerPriority.NORMAL,
                            PacketType.Play.Client.USE_ENTITY) {
                        @Override
                        public void onPacketReceiving(PacketEvent event) {
                            if (event.getPacketType() == PacketType.Play.Client.USE_ENTITY) {
                                try {
                                    PacketContainer packet = event.getPacket();

                                    if (playerHolograms.containsKey(event.getPlayer())) {
                                        ArrayList<Hologram> holograms = playerHolograms.get(event.getPlayer());
                                        ArrayList<HologramUseEntityEvent> toRun = new ArrayList<HologramUseEntityEvent>();
                                        for (Hologram hologram : holograms) {
                                            if (hologram.getEntityID().equals(packet.getIntegers().read(0))) {
                                                for (HologramUseEntityEvent hologramUseEntityEvent : hologram.getHologramUseEntityEvents()) {
                                                    toRun.add(hologramUseEntityEvent);
                                                }
                                            }
                                        }
                                        for (HologramUseEntityEvent hologramUseEntityEvent : toRun) {
                                            hologramUseEntityEvent.run(packet.getIntegers().read(0));
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
        }

    HologramUseEntityEvent
    Code (Text):
    package com.skyblock.utils;

    /**
    * Created by Derek on 10/5/2014.
    * Time: 9:51 AM
    */
    public interface HologramUseEntityEvent {

        public abstract void run(int entityID);
    }
     

    SelectorBuilder.run()
    Code (Text):
        @Override
        public void run() {
            final FileConfiguration fileConfiguration = SkyBlock.p.getFileConfiguration();
            final int selectorSize = (2 * (schematics.size() - 2)) + 5;
            final Location pasteLocation = location.clone().add(fileConfiguration.getInt(ConfigPaths.SELECTOR_X_OFFSET), fileConfiguration.getInt(ConfigPaths.SELECTOR_Y_OFFSET), selectorSize / 2 + fileConfiguration.getInt(ConfigPaths.SELECTOR_Z_OFFSET));
            for (int i = 1; i <= selectorSize; i++) {
                new SchematicBuilder(new File(FilePaths.SCHEMATICS, "default.schematic"), pasteLocation.clone().subtract(0, 0, i)).runTaskTimer(SkyBlock.p, 10, 0);
            }
            new SchematicBuilder(new File(FilePaths.SCHEMATICS, "default2.schematic"), pasteLocation.clone()).runTaskTimer(SkyBlock.p, 10, 0);
            new SchematicBuilder(new File(FilePaths.SCHEMATICS, "default2.schematic"), pasteLocation.clone().subtract(0, 0, (2 * (schematics.size() - 2)) + 6)).runTaskTimer(SkyBlock.p, 10, 0);

            final UserData senderData = SkyBlock.p.getConfigManager().getUserData(player.getUniqueId());

            senderData.setIslandData(new IslandData(Arrays.asList(player.getUniqueId()), new ArrayList<UUID>(), new ArrayList<UUID>(), player.getUniqueId(), location.clone(), pasteLocation.clone().add(2, 1, -selectorSize / 2), null));
            senderData.getIslandData().save();
            senderData.save();
            new BukkitRunnable() {
                @Override
                public void run() {
                    player.teleport(senderData.getIslandData().getIslandHomeLocation());
                    for (int i = 1; i <= selectorSize; i++) {
                        if (i % 2 == 0) {
                            final String name = schematics.get(i / 2 - 1);
                            HologramUtil.addHologram(name, pasteLocation.clone().add(3, 3, 0.5).subtract(0, 0, i), player, new HologramUseEntityEvent() {
                                @Override
                                public void run(int entityID) {
                                    new SchematicBuilder(new File(FilePaths.SCHEMATICS, fileConfiguration.getString(ConfigPaths.getSchematicPath(ConfigPaths.SELECTOR_SCHEMATIC_FILE_NAME, name))), location).runTaskTimer(SkyBlock.p, 10, 0);
                                    IslandData islandData = SkyBlock.p.getConfigManager().getUserData(player.getUniqueId()).getIslandData();
                                    islandData.setIslandSchematic(new File(FilePaths.SCHEMATICS, fileConfiguration.getString(ConfigPaths.getSchematicPath(ConfigPaths.SELECTOR_SCHEMATIC_FILE_NAME, name))));
                                    islandData.save();

                                    HologramUtil.selectHologram(player, entityID);
                                }
                            });
                        }
                    }
                    HologramUtil.addHologram(t("selectorInfo"), pasteLocation.clone().add(3, 4, 0.5).subtract(0, 0, selectorSize / 2), player);
                    HologramUtil.addHologram("Select Island", pasteLocation.clone().add(3, 1, 0.5).subtract(0, 0, selectorSize / 2), player, new HologramUseEntityEvent() {
                        @Override
                        public void run(int entityID) {
                            IslandData islandData = SkyBlock.p.getConfigManager().getUserData(player.getUniqueId()).getIslandData();
                            if (islandData.getIslandSchematic() != null) {
                                HologramUtil.clearPlayer(player);
                                islandData.setIslandHomeLocation(LocationUtil.getHomeLocation(islandData.getIslandLocation(), fileConfiguration.getInt(ConfigPaths.ISLAND_DISTANCE) / 2));
                                player.teleport(islandData.getIslandHomeLocation().clone().add(0.5, 0, 0.5), PlayerTeleportEvent.TeleportCause.PLUGIN);
                                new IslandRebuilder(islandData.getIslandSchematic(), islandData.getIslandLocation(), fileConfiguration.getInt(ConfigPaths.ISLAND_DISTANCE) / 2).runTaskTimer(SkyBlock.p, 10, 0);
                            }
                        }
                    });
                }
            }.runTaskLater(SkyBlock.p, 20);
        }[/syntax]
    [/spoiler]

    LocationUtil.getHomeLocation(Location islandLocation, int radius)
    [spoiler]
    [syntax=java]    public static Location getHomeLocation(Location islandLocation, int radius) {
            for (int x = -radius; radius > x; x++) {
                for (int y = 0; 256 > y; y++) {
                    for (int z = -radius; radius > z; z++) {
                        if (islandLocation.clone().add(x, y, z).getBlock().getType() == Material.CHEST) {
                            return islandLocation.clone().add(x, y + 1, z);
                        }
                    }
                }
            }
            return null;
        }
     
  2. @Lazertx there is no synchronization between Netty (which is supposed to run async) and the event listener, so it's safe to assume that all your code in there is being ran asynchronously.
     
    • Like Like x 1
  3. Thanks you helped me figure it out. You need to do
    Code (Text):
    ProtocolLibrary.getProtocolManager().getAsynchronousManager().registerAsyncHandler().syncStart();
    and you will know it's sync