Posted October 29, 201410 yr First my code: @SubscribeEvent public void killIdentitytDiscs(WorldEvent.Unload event){ for(int i = 0; i < event.world.loadedEntityList.size(); i++){ Entity currentEntity = (Entity) event.world.loadedEntityList.get(i); if(currentEntity instanceof IdentityDiscEntity){ EntityPlayer player = (EntityPlayer) ((IdentityDiscEntity)currentEntity).getThrower(); if(!player.capabilities.isCreativeMode){ player.inventory.addItemStackToInventory(((IdentityDiscEntity)currentEntity).drop); } currentEntity.setDead(); System.out.println("I Ran"); } } } The console will print out "I Ran" when exiting the world, but when loading up the world again, the entity hasn't been deleted. Any ideas?
October 29, 201410 yr Author That is not going to work, because setDead relies on the world still being loaded. Why are you using the world unload for this? Basically, the entity has a value stored that becomes null if the player logs out. this causes a null pointer exception. My original solution was to look for player log out events, and delete the entity before the value becomes null (the entity is only relevant when the player it came from is online), but this only works on dedicated servers. To work on single player I figured the next best event would be the world unload event, since it happens when the player logs out on single player.
October 29, 201410 yr Author Not sure I get your problem. Of course you cannot get the thrower anymore when the player leaves, but... you can't circumvent that. Exactly, so when the thrower logs out/the server shuts down, I want to kill the entity so it doesn't try to get the thrower. Does that make sense?
October 30, 201410 yr Author When the thrower throws the entity keep a reference to the entity in the thrower. Then when they log out you can kill the entity. But I still don't get why you need to do that. Minecraft throwables know their owner and they don't do this. I do have a reference to the thrower, but the player log out event only works on dedicated servers, not single player, and I need it to work on both. As for Minecraft throwables, I discovered that the client side entity does not store the thrower, only the server side. In addition, my entity bases its flight path on where the player is, so it's constantly using the reference to the thrower, where Minecraft throwables do not call their thrower. So they don't have null pointer issues while I do.
October 30, 201410 yr Author You can just check if the player is null in the entity... Although: If you have a EntityPlayer field in your entity it will not suddenly become null if the player logs out. It will still point to a player (but if you do this it will cause all sorts of problems, the least of which is that it is a memory leak). For how to get the thrower on the client: use the DataWatcher to send the entityID of the thrower. Right...that would make sense...I guess I was just over complicating the problem. However, if the reference doesnt become null, how can I tell the player when the player logs out on SP if the player log out event wont work on SP? And I had already figured thr client thing out.
October 30, 201410 yr Author Check for .isDead on the player. If it is, remove your reference to it. Right, that makes sense, thanks for the help!
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.