Jump to content

[1.7.10] Can't pickup spawned item.


Awesome_Spider

Recommended Posts

I have a custom item that gets summoned into the world as an EntityItem with a bunch of lightning when a key is pressed. That works, but when the item is spawned I can't pick it up. Here is my summoning code:

EntityItem item = new EntityItem(player.worldObj, player.posX, player.posY - 1, player.posZ - 4, new ItemStack(Lumina.lumicon));
            
player.worldObj.spawnEntityInWorld(item);

 

Why is this happening?

Link to comment
Share on other sites

your problem right now is that u are spawning the item on the client side. which is wrong because it needs to be on the server side. so if the key is pressed send a packet to the server which tells him to spawn an item at a desired location (you should transmit the location via the packet..)

Link to comment
Share on other sites

This example is not thread safe and might kill a server. Just so you know.

Both that code and this thread are for 1.7.10, in which the network was still on the main thread.

 

@TheRealMcrafter - if you're going to verbatim copy / paste code from a tutorial (you didn't even remove my comments!), it's considerate of you to write a note in the code saying where you got it from, i.e. giving credit where credit is due.

Link to comment
Share on other sites

your problem right now is that u are spawning the item on the client side. which is wrong because it needs to be on the server side. so if the key is pressed send a packet to the server which tells him to spawn an item at a desired location (you should transmit the location via the packet..)

 

That might be why the explosion didn't work either. I had lightning strikes then an explosion then the item spawned. That's how I coded it. The explosion worked, but when I jumped in to collect the item, my player started shaking like it does when the block looks like it's not there but it actually was. So the same thing with server and client side goes for explosions, right?

 

I'm away from my code right now, I'll try to figure out packets in the morning.

Link to comment
Share on other sites

This example is not thread safe and might kill a server. Just so you know.

Both that code and this thread are for 1.7.10, in which the network was still on the main thread.

 

@TheRealMcrafter - if you're going to verbatim copy / paste code from a tutorial (you didn't even remove my comments!), it's considerate of you to write a note in the code saying where you got it from, i.e. giving credit where credit is due.

 

Oh dammit you are right, Im sorry

Link to comment
Share on other sites

Ok, I've been messing with packets and I got this when I clicked the key:

width=800 height=5309WC6x7X.png?1 [/img]

I also got a crash log:

 

 

io.netty.handler.codec.DecoderException: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at java.lang.Class.newInstance0(Class.java:357) ~[?:1.7.0_07]
at java.lang.Class.newInstance(Class.java:325) ~[?:1.7.0_07]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:75) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more
[09:40:56] [server thread/ERROR] [FML]: SimpleChannelHandlerWrapper exception
io.netty.handler.codec.DecoderException: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at java.lang.Class.newInstance0(Class.java:357) ~[?:1.7.0_07]
at java.lang.Class.newInstance(Class.java:325) ~[?:1.7.0_07]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:75) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more
[09:40:56] [server thread/ERROR] [FML]: There was a critical exception handling a packet on channel Lumina|Channel
io.netty.handler.codec.DecoderException: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) ~[EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.InstantiationException: awesomespider.lumina.Packets.LumiconSpawnPacket
at java.lang.Class.newInstance0(Class.java:357) ~[?:1.7.0_07]
at java.lang.Class.newInstance(Class.java:325) ~[?:1.7.0_07]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:75) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more

 

 

 

Here is my code:

 

 

Main class:

public static SimpleNetworkWrapper network;

@EventHandler
    public void preinit(FMLPreInitializationEvent event) {
network = NetworkRegistry.INSTANCE.newSimpleChannel("Lumina|Channel");
}

@EventHandler
    public void init(FMLInitializationEvent event) {
network.registerMessage(LumiconSpawnPacket.Handler.class, LumiconSpawnPacket.class, 0, Side.SERVER);
}

 

The packet class:

 

public class LumiconSpawnPacket implements IMessage {
    double posX;
    double posY;
    double posZ;

    public LumiconSpawnPacket(double posX, double posY, double posZ){
        this.posX = posX;
        this.posY = posY;
        this.posZ = posZ;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        this.posX = buf.readDouble();
        this.posY = buf.readDouble();
        this.posZ = buf.readDouble();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeDouble(posX);
        buf.writeDouble(posY);
        buf.writeDouble(posZ);
    }

    public static class Handler implements IMessageHandler<LumiconSpawnPacket, IMessage> {
        @Override
        public IMessage onMessage(LumiconSpawnPacket message, MessageContext ctx) {
            EntityPlayer player = ctx.getServerHandler().playerEntity;
            Lumina.log.info(Lumina.LOGPREFIX + " LuminaSpawnPacket recieved, player " + player + " has attempted to summon a lumicon. Spawning...");
            try {
                PlayerUtil.startPlayer(player);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

 

My input handler:

public class InputHandler {
    @SubscribeEvent
    public void onKeyInput(InputEvent.KeyInputEvent event) {
        if(KeyBindings.start.isPressed()){
            EntityPlayer player = Minecraft.getMinecraft().thePlayer;

            Lumina.network.sendToServer(new LumiconSpawnPacket(player.posX, player.posY, player.posZ));
        }
    }
}

 

PlayerUtil:

public static void startPlayer(EntityPlayer player) throws IOException {
        if (!isPlayerAlreadyStarted(player)){
            playersStartedList.add(player.getUniqueID());

            savePlayersStarted();

            playPlayerStartAnimation(player);
        }
    }

    public static boolean isPlayerAlreadyStarted(EntityPlayer player){
        return playersStartedList.contains(player.getUniqueID());
    }

    public static void playPlayerStartAnimation(EntityPlayer player) {
        //TODO Work on the animation of the player starting the journey
        player.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "The air hums with energy."));

        player.playSound("portal.travel", 10, 0);

        player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));
        player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));
        player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));
        player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));

        //Taken from GuiIngame debug screen code (thanks to Ernio for helping me)
        int i4 = MathHelper.floor_double((double) (player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
        String direction = Direction.directions[i4];

        ForgeDirection lookDirection = ForgeDirection.valueOf(direction);

        if (lookDirection == ForgeDirection.NORTH) {
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ - 4));

            EntityItem item = new EntityItem(player.worldObj, player.posX, player.posY - 1, player.posZ - 4, new ItemStack(Lumina.lumicon));
            player.worldObj.spawnEntityInWorld(item);
        } else if (lookDirection == ForgeDirection.EAST) {
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX + 4, player.posY - 1, player.posZ));

            EntityItem item = new EntityItem(player.worldObj, player.posX + 4, player.posY - 1, player.posZ, new ItemStack(Lumina.lumicon));
            player.worldObj.spawnEntityInWorld(item);
        } else if (lookDirection == ForgeDirection.SOUTH) {
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX, player.posY - 1, player.posZ + 4));

            EntityItem item = new EntityItem(player.worldObj, player.posX, player.posY - 1, player.posZ + 4, new ItemStack(Lumina.lumicon));
            player.worldObj.spawnEntityInWorld(item);
        } else if (lookDirection == ForgeDirection.WEST) {
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));
            player.worldObj.addWeatherEffect(new EntityLightningBolt(player.worldObj, player.posX - 4, player.posY - 1, player.posZ));

            EntityItem item = new EntityItem(player.worldObj, player.posX - 4, player.posY - 1, player.posZ, new ItemStack(Lumina.lumicon));
            player.worldObj.spawnEntityInWorld(item);
        }
    }

 

 

 

If you need more code just ask. :)

 

Looking at the crash log it's a DecoderException, I have no idea what that means.

Link to comment
Share on other sites

-snip-

@TheRealMcrafter - if you're going to verbatim copy / paste code from a tutorial (you didn't even remove my comments!), it's considerate of you to write a note in the code saying where you got it from, i.e. giving credit where credit is due.

 

Woops, my bad. Your tutorials are the best, credit given ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.