Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

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


Cyan
 Share

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.

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.

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • update Forge to the latest version and do not put your server on OneDrive
    • It occurred to me. Can I get the data I want by getting the parameters of the event instantiation.
    • I just checked the description of "LivingHurtEvent" and he tells me that the event does not return results and if I instantiate the event/class and then call his method, I can't get the return value (amount,source). Do I have to get the return value I want in some other way. Here is the code for the "LivingHurtEvent" event /* * Copyright (c) Forge Development LLC and contributors * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.event.entity.living; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.LivingEntity; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.Cancelable; /** * LivingHurtEvent is fired when an Entity is set to be hurt. <br> * This event is fired whenever an Entity is hurt in * {@code LivingEntity#actuallyHurt(DamageSource, float)} and * {@code Player#actuallyHurt(DamageSource, float)}.<br> * <br> * This event is fired via the {@link ForgeHooks#onLivingHurt(LivingEntity, DamageSource, float)}.<br> * <br> * {@link #source} contains the DamageSource that caused this Entity to be hurt. <br> * {@link #amount} contains the amount of damage dealt to the Entity that was hurt. <br> * <br> * This event is {@link Cancelable}.<br> * If this event is canceled, the Entity is not hurt.<br> * <br> * This event does not have a result. {@link HasResult}<br> * <br> * This event is fired on the {@link MinecraftForge#EVENT_BUS}. * @see LivingDamageEvent **/ @net.minecraftforge.eventbus.api.Cancelable public class LivingHurtEvent extends LivingEvent { private final DamageSource source; private float amount; public LivingHurtEvent(LivingEntity entity, DamageSource source, float amount) { super(entity); this.source = source; this.amount = amount; } public DamageSource getSource() { return source; } public float getAmount() { return amount; } public void setAmount(float amount) { this.amount = amount; } } I'm wondering if the 'forge' event is only used as a 'forge' listener and not for other purposes. That is, after these events are instantiated, the method is called and cannot provide any return value, or after instantiation, the method is called and cannot change the data, because the events/classes are only used to listen to minecraft/forge.
    • Posted 21 hours ago C:\Users\trodd\OneDrive\Desktop\server for 35>java -Xmx5G -Xms5G -jar forge-1.16.5-36.2.2.jar nogui 2022-05-27 09:49:55,394 main WARN Advanced terminal features are not available in this environment [09:49:55] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.2, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, nogui] [09:49:55] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 1.8.0_333 by Oracle Corporation [09:49:56] [main/INFO] [ne.mi.fm.lo.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust [09:49:56] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.2 Source=file:/C:/Users/trodd/OneDrive/Desktop/server%20for%2035/libraries/org/spongepowered/mixin/0.8.2/mixin-0.8.2.jar Service=ModLauncher Env=SERVER [09:49:57] [main/INFO] [STDERR/]: [org.antlr.v4.runtime.ConsoleErrorListener:syntaxError:38]: line 1:0 token recognition error at: '~' Exception in thread "main" [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]: java.lang.NoSuchMethodError: sun.security.util.ManifestEntryVerifier.<init>(Ljava/util/jar/Manifest;)V [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.SecureJarHandler.createCodeSource(SecureJarHandler.java:66) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.TransformingClassLoader$DelegatedClassLoader.findClass(TransformingClassLoader.java:275) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:136) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:98) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at java.lang.ClassLoader.loadClass(Unknown Source) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at java.lang.Class.forName0(Native Method) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at java.lang.Class.forName(Unknown Source) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.service.modlauncher.ModLauncherClassProvider.findClass(ModLauncherClassProvider.java:57) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.launch.platform.MixinConnectorManager.loadConnectors(MixinConnectorManager.java:71) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.launch.platform.MixinConnectorManager.inject(MixinConnectorManager.java:60) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.launch.platform.MixinPlatformManager.inject(MixinPlatformManager.java:197) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.launch.MixinBootstrap.inject(MixinBootstrap.java:190) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at org.spongepowered.asm.launch.MixinLaunchPlugin.initializeLaunch(MixinLaunchPlugin.java:196) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.LaunchPluginHandler.lambda$announceLaunch$9(LaunchPluginHandler.java:97) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at java.util.HashMap.forEach(Unknown Source) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.LaunchPluginHandler.announceLaunch(LaunchPluginHandler.java:97) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:52) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) [09:49:57] [main/INFO] [STDERR/]: [java.lang.ThreadGroup:uncaughtException:-1]:         at net.minecraftforge.server.ServerMain.main(ServerMain.java:57) C:\Users\trodd\OneDrive\Desktop\server for 35>PAUSE Press any key to continue . . .
    • You can, but it won't do anything useful. The event instance is already given to you as a parameter.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.