Jump to content

Recommended Posts

Posted

Hello,

 

So I have some various timers and such running through OnLivingUpdate() for tamed entities, and I ran into a problem with these timers running even when the owner is not on.

 

So my question is, would there be a way to check to see if the owner is online so that they only run while the player is there? I'm currently using the vanilla taming method, as well as for getting the owner. Just need to figure out how to check to see if the owner is online.

 

Thanks,

Cyan.

Posted

So, let me see if I understand.

a) You want these tamed beasts to despawn when the owner logs out?

b) You want the creatures to become still and inert when the owner logs out?

c) You want the creatures to revert to wild(untamed) vanilla status when the owner logs out?

d) Something else entirely?

Posted

..None of the above.

 

I'm just looking for something I can use as a condition to check if the entities owner is online. That way I can make certain things in OnLivingUpdate() only run when the player is online.

Posted

That way I can make certain things in OnUpdate() only run when the player is online.

Okay, just curious. Carry on then.

 

If you wanted more details, you could have asked for more details. Communication makes the world go round. I'm looking for a condition to check if a player is online, nothing else. The details of the function are not really important to answering the question.

 

But since you asked so nicely..

What I'm aiming for is:

public void onLivingUpdate()
{
super.onLivingUpdate();

if (!this.worldObj.isRemote && isTamed() && CONDITIONTOCHECKIFOWNERISONLILNEHERE)
    {
    	energyticker++;
    
    	if (energyticker >700)
    	{
    		setEnergy(getEnergy() - 1);
    		if(getEnergy() < 0)
        	{
    			setEnergy(0);
        	}
    		
        energyticker = 0;
        System.out.println("Energy went down.");
    	}
    	}


 

Like I mentioned in the first post. Its just a simple timer. But it keeps decrementing on servers when the player is offline, SO I am trying to figure if there is a condition somewhere that I have missed to check to see if the owner of the entity is online. But after reading through your previous two posts, it is becoming a little more obvious that you really only skimmed my original post, and then decided to post a snide quip.

 

But for anyone else who may be able to help me out(or you should you decide knock off the snide comments), that is what I am trying to do.

Posted

I have a lead for you. Something like this could help you out. This is completely untested and only based upon what I've found in the source code... you may have to adapt some changes.

 

function isOwnerOnline( String owner ) {
  // playerEntityList is a generic list containing EntityPlayerMP. This cast is just for convenience...
  List<EntityPlayerMP> players = (List<EntityPlayerMP)MinecraftServer.getServer().getConfigurationManager().playerEntityList;
  
  for( Iterator<EntityPlayerMP> it = players.iterator(); it.hasNext(); ) {
    EntityPlayerMP curr = it.next();
    if( curr.username == owner ) {
      return true;
    }
  }
  return false;
}

Posted

That seems like a pretty good lead to me, thank you.

 

May I ask which file that is under so I can look a little more in-depth into it? I would greatly appreciate it.

Posted

Never use @SideOnly (*). It does not, what you think it does.

At the moment you are thinking of Server vs. Client as Server Thread vs. Client Thread (Server Thread may be on a different machine when using a dedicated server, but is on the same machine when playing Singleplayer).

@SideOnly though differentiates between Minecraft Client vs. Dedicated Server. That means: A method with @SideOnly(Side.SERVER) will not be present at all in the Minecraft Client and also not be present in the Integrated Server (because they are running on the same JVM). It will only exist on a Dedicated server.

To check for client thread vs. server thread use world.isRemote. You almost always got a world instance somewhere, every entity has one, every TileEntity has one, etc.

Also: Never use Minecraft.getMinecraft (or any other client-only stuff) in common code.

 

(*) Except on methods that override a method which already has @SideOnly (e.g. Block#registerIcons), then it is actually quite important to add it.

 

To be fair: I use @SideOnly(Side.CLIENT) on my client-sided classes/methods/fields to make sure they are really client-sided (helps to figure out if you use any client references where they shouldn't be while testing on the dedicated server)

 

B2T: To check if an user is currently logged into the server, use this:

Arrays.asList(MinecraftServer.getServer().getAllUsernames()).contains("USERNAME")

So in my case (sanandreasMC) you replace "USERNAME" with "sanandreasMC"

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Arrays.asList(MinecraftServer.getServer().getAllUsernames()).contains("USERNAME")

That is a horrible idea (especially if you check it every tick, like the OP plans to.

getAllUsernames needs to iterate the whole player list and copy the username into a (newly allocated) array. Then you convert that array to a list again, just to iterate it once again (that's what Arrays.asList().contains) does. If you really need to do this, iterate ServerConfigurationManager#playerEntityList directly and check for the username.

 

oooh, I thought the getAllUsernames uses a pre-defined array already... yeah than this is not at all a good idea, scratch that.

But there is the PlayerLoggedInEvent / PlayerLoggedOutEvent, as GotoLink mentioned, and it actually does fire on the server and client AFAIK

 

edit: Or you could always use the IPlayerTracker interface, It does the same things as the events do. Make a class which implements it and register a new instance of that class with GameRegistry.registerPlayerTracker()

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.