Jump to content

[1.12.2]How to send command when player join a game?


mlgmxyysd

Recommended Posts

I use 

Minecraft.getMinecraft().player.sendChatMessage("/login " + password);

then game throw a NullPointerException.

Context:

package org.meowcat.autocommadner;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Properties;
public class JoinEvent {
	private Properties prop = new Properties();
	JoinEvent() {
		MinecraftForge.EVENT_BUS.register(this);
		FMLCommonHandler.instance().bus().register(this);
	}
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void PlayerJoinGame(PlayerEvent.PlayerLoggedInEvent event) throws IOException {
	    try{
	        Class mc = Class.forName(Minecraft.class.getName());
	        Method[] methods = mc.getMethods();
	        for (Method method : methods){
                Main.logger.info(method.getName());
            }
        } catch (ClassNotFoundException e ) {
            e.printStackTrace();
        }
		File file = new File("config/user.properties");
		if (!file.exists()) {
            Minecraft.getMinecraft().shutdown();
		}
        InputStream in = new BufferedInputStream(new FileInputStream("config/user.properties"));
        prop.load(in);
        String password = prop.getProperty("Password");
        String registered = prop.getProperty("IsRegistered");
        if (!registered.equals("true")) {
           Minecraft.getMinecraft().player.sendChatMessage("/register " + password + " " + password);
        } else {
           Minecraft.getMinecraft().player.sendChatMessage("/login " + password);
        }
	}
}

 

Link to comment
Share on other sites

What exactly are you trying to do? From an end-user persective

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

9 hours ago, diesieben07 said:

It might not be. Still, why? This usually means you are pirating the game.

 

You need to use ClientTickEvent, PlayedLoggedInEvent is server-side only. And that is not how @SideOnly works.

The mod is a client mod,not a server mod.

Link to comment
Share on other sites

send your code and the error

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

13 hours ago, diesieben07 said:

The code above has not changed. No error was posted.

It may cause by network problem.

package org.meowcat.autocommadner;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Properties;
public class JoinEvent {
	private Properties prop = new Properties();
	JoinEvent() {
		MinecraftForge.EVENT_BUS.register(this);
		FMLCommonHandler.instance().bus().register(this);
	}
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void PlayerJoinGame(TickEvent.ClientTickEvent event) throws IOException {
	    try{
	        Class mc = Class.forName(Minecraft.class.getName());
	        Method[] methods = mc.getMethods();
	        for (Method method : methods){
                Main.logger.info(method.getName());
            }
        } catch (ClassNotFoundException e ) {
            e.printStackTrace();
        }
		File file = new File("config/user.properties");
		if (!file.exists()) {
            Minecraft.getMinecraft().shutdown();
		}
        InputStream in = new BufferedInputStream(new FileInputStream("config/user.properties"));
        prop.load(in);
        String password = prop.getProperty("Password");
        String registered = prop.getProperty("IsRegistered");
        if (!registered.equals("true")) {
           Minecraft.getMinecraft().player.sendChatMessage("/register " + password + " " + password);
        } else {
           Minecraft.getMinecraft().player.sendChatMessage("/login " + password);
        }
	}
}
java.lang.NullPointerException: Unexpected error
	at org.meowcat.autocommadner.JoinEvent.PlayerJoinGame(JoinEvent.java:42)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_18_JoinEvent_PlayerJoinGame_ClientTickEvent.invoke(.dynamic)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
	at net.minecraftforge.fml.common.FMLCommonHandler.onPreClientTick(FMLCommonHandler.java:344)
	at net.minecraft.client.Minecraft.func_71407_l(Minecraft.java:1708)
	at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1098)
	at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:398)
	at net.minecraft.client.main.Main.main(SourceFile:123)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

These are codes & errors

Link to comment
Share on other sites

What are you trying to do with that code?

Edited by Cadiboo

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

5 hours ago, diesieben07 said:

Why on earth is this here? And blindly catching ClassNotFoundException and ignoring it is a bad idea. Handle exceptions properly.

 

Why on earth are you doing this every tick?! And registered should not be an option. Do not pirate the game.

 

You can only do this when the game is actually connected to a server. ClientTickEvent fires every client tick (duh), which is also during e.g. the main menu.

Please don't get caught up in the secondary problem.

I only want to solve NullPointerException.

Link to comment
Share on other sites

On 12/27/2018 at 10:51 AM, diesieben07 said:

It might not be. Still, why? This usually means you are pirating the game.

 

You need to use ClientTickEvent, PlayedLoggedInEvent is server-side only. And that is not how @SideOnly works.

Not to go too off topic, but /login can be used on servers as a staff security measure as well as for cracked server auth. So, it's not always malicious. I once had to use it on my online mode server because we had this hacker who somehow figured out that commands could be run by staff before they were authenticated (I have no idea how, but I'm not lying, he'd done it) and so I had to add a login command for staff to counteract it.

Link to comment
Share on other sites

  • 3 weeks later...
On 12/31/2018 at 11:20 PM, diesieben07 said:

I know, and this is the only reason this thread is not locked yet.

 

This explains nothing.

 

As far as the cause of your exception goes, I have already explained that above.

OKAY, I does not use /login command, replace /login to /exp ok?

IS THIS OK?

package org.meowcat.autocommadner;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Properties;
public class JoinEvent {
	private Properties prop = new Properties();
	JoinEvent() {
		MinecraftForge.EVENT_BUS.register(this);
		FMLCommonHandler.instance().bus().register(this);
	}
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void PlayerJoinGame(TickEvent.ClientTickEvent event) throws IOException {
	    try{
	        Class mc = Class.forName(Minecraft.class.getName());
	        Method[] methods = mc.getMethods();
	        for (Method method : methods){
                Main.logger.info(method.getName());
            }
        } catch (ClassNotFoundException e ) {
            e.printStackTrace();
        }
            Minecraft.getMinecraft().player.sendChatMessage("/exp");
	}
}
Link to comment
Share on other sites

On 12/31/2018 at 2:24 PM, hiotewdew said:

Not to go too off topic, but /login can be used on servers as a staff security measure as well as for cracked server auth. So, it's not always malicious. I once had to use it on my online mode server because we had this hacker who somehow figured out that commands could be run by staff before they were authenticated (I have no idea how, but I'm not lying, he'd done it) and so I had to add a login command for staff to counteract it.

Yes, it is necessary to use unofficial login system in offline-mode server.

Link to comment
Share on other sites

  • 2 years later...
On 1/16/2019 at 8:34 AM, diesieben07 said:

Why do you want an offline-mode server?

There is no reason except:

  1. To allow pirating the game, in which case that is not allowed.
  2. For a local network, in which case there i no need for whitelisting (you are already in a local network, noone can join from outside).

 

This is an old discussion, but after reading through it seems you've been unfair to @mlgmxyysd and wrong to assume that there are only two reasons why someone might want an offline mode server. I can give you a couple more valid and legitimate reasons:

1. The local network point is valid, and yes nobody can join from outside, but he could be running his server on a university network in which case custom authentication would be useful. It could be that nobody could join from outside, but 30 thousand people could join from inside.

2. He could be running his server on a network where Minecraft's authentication endpoint is blocked.

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.

Announcements



×
×
  • Create New...

Important Information

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