Jump to content

Checking to see if the owner of (x) is logged in?


Cyan

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

..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.

Link to comment
Share on other sites

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

Okay, just curious. Carry on then.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • If you’ve ever experienced the sinking feeling of losing funds online, you’re not alone. Enter recovery services like ADWARE RECOVERY SPECIALIST, the heroes of the digital age who help individuals track down and retrieve lost funds. These services act as modern-day sheriffs in the Wild West of the internet, bringing justice to those who have fallen victim to online scams and fraud. In a world where online transactions are as common as selfies, the need for recovery services is more pressing than ever. Individuals worldwide are vulnerable to various online schemes, from investment fraud to phishing scams. Recovery services provide a glimmer of hope for those who have lost money, offering a chance to recover what's rightfully theirs and restore faith in the digital realm. The beauty of recovery services like ADWARE RECOVERY SPECIALIST lies in their global reach. Email: Adwarerecoveryspecialist@auctioneer. net  Regardless of where individuals are located worldwide, these services extend their virtual arms to help victims of online fraud. From bustling metropolises to remote villages, no corner of the world is beyond the grasp of these modern-day Robin Hoods. Thanks to the wonders of globalization, recovery services have transcended geographical boundaries to serve individuals worldwide. The interconnected nature of the digital world means that help is just a click away, bridging gaps between nations and bringing relief to those in need. In a world where borders are becoming increasingly blurred, recovery services stand as beacons of accessibility and assistance. When a broker stole from me and withheld access from me for five months, I had no idea that I would be able to recover my trading money back. I'm relieved that I was able to get my money returned without any issues. The impact of recovery services like ADWARE RECOVERY SPECIALIST extends far beyond individual wallets. Communities worldwide have experienced a renewed sense of trust and security, knowing that there are dedicated professionals working tirelessly to recover their lost assets. These services not only bring financial relief but also restore faith in the integrity of the digital world. Trust is the cornerstone of any successful recovery service, and building credibility in a global market can pose unique challenges. ADWARE RECOVERY SPECIALIST strives to maintain transparency and reliability in all its interactions, earning the trust of clients worldwide. ADWARE RECOVERY SPECIALIST can be talked to through:  Website: www.adwarerecoveryspecialist.expert  Telegram: @ADWARE_RECOVERY_SPECIALIST
    • The game wont start and keeps crashing everytime I hit play "The game crashed whilst rendering overlay Error: java.lang.IllegalAccessError: failed to access class com.mojang.blaze3d.platform.GlStateManager$TextureState from class net.coderbot.iris.gl.IrisRenderSystem$DSAARB (com.mojang.blaze3d.platform.GlStateManager$TextureState is in module minecraft@1.18.2 of loader 'TRANSFORMER' @724b939e; net.coderbot.iris.gl.IrisRenderSystem$DSAARB is in module oculus@1.6.4 of loader 'TRANSFORMER' @724b939e)" How do I get it to stop? Log error here: https://pastebin.com/byv1dR54
    • In case anyone happens on this with the same issue, I was able to overcome it. After re-reading @Paint_Ninja's last post numerous time, it finally dawned on me that he was referring wholly to the forge installer, which has nothing to do with the packaged modpack installer. As such, I downloaded the new copy of the forge installer and copied it to my linux host, then opened a second ssh session and prepared a cp command to move the new (correct) installer over to the install directory where I was installing the modpack. After the modpack installer had just about finished retrieving files, I overwrote the installer.jar file with the correct one in a seperate ssh session. The modpack (packaged) installer then used that jar to install forge with the included files, and the install finished as I would have expected. Thanks all!
    • Just tried it again removed directory entirely, downloaded a brand new, fresh copy and did a fresh install, even going as far as running as root for the install to make sure I didn't hose something up permissions wise (I know its a terrible idea.), but same thing, failed install. This is making me feel mighty dumb, I must say... any help anyone could provide would be greatly appreciated.
  • Topics

×
×
  • Create New...

Important Information

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