Posted June 2, 201510 yr 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?
June 2, 201510 yr Now that I think of it, I think it's on client side, I'm using a key binding to spawn it and if I'm correct key bindings are on client side.
June 2, 201510 yr Yea, if you spawn it on the client side it will just be a ghost item. You need to send a packet to the server and spawn it there.
June 2, 201510 yr diesieben will be your friend http://www.minecraftforge.net/forum/index.php/topic,20135.0.html Dont forget about thread safety http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html
June 2, 201510 yr 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..)
June 2, 201510 yr Example: I use this to send a string to the server from the client: The message class: https://github.com/TheRealMcrafter/SirenMod/blob/master/src/main/java/TheRealMcrafter/SirenMod/packet/SirenModUpdateSirenColorMessage.java Feel free to browse around my source to figure it out.
June 2, 201510 yr The message class: https://github.com/TheRealMcrafter/SirenMod/blob/master/src/main/java/TheRealMcrafter/SirenMod/packet/SirenModUpdateSirenColorMessage.java Feel free to browse around my source to figure it out. 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. http://i.imgur.com/NdrFdld.png[/img]
June 2, 201510 yr 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.
June 2, 201510 yr The message class: https://github.com/TheRealMcrafter/SirenMod/blob/master/src/main/java/TheRealMcrafter/SirenMod/packet/SirenModUpdateSirenColorMessage.java Feel free to browse around my source to figure it out. 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
June 2, 201510 yr Ok, I've been messing with packets and I got this when I clicked the key: [/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.
June 2, 201510 yr Thanks, it works now, but the lightning I'm summoning (for effect) is burning the item , but I suppose that's my problem.
June 3, 201510 yr -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
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.