Jump to content

How to Disconnect Player When is SinglePlayer


jredfox
 Share

Recommended Posts

Why?: I need EntityUtil.dissconnectPlayer(player) to always  work:

I wrote up a command to disconnect the player and it just freezes the game and doesn't go to the main menu.

package com.EvilNotch.lib.minecraft.content.commands;

import com.EvilNotch.lib.minecraft.EntityUtil;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

public class CMDKick extends CommandBase {
	
	 /**
     * Gets the name of the command
     */
	@Override
    public String getName()
    {
        return "bootPlayer";
    }
    /**
    * Gets the usage string for the command.
    */
    @Override
    public String getUsage(ICommandSender sender)
    {
       return "commands.evilnotchlib.boot.usage";
    }
	@Override
	public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException 
	{
		Entity e = getEntity(server, sender, args[0]);	
		if(e instanceof EntityPlayerMP)
		{
			EntityPlayerMP player = (EntityPlayerMP)e;
			player.connection.disconnect(new TextComponentString("booted"));
		}
	}

    /**
     * Get a list of options for when the user presses the TAB key
     */
    @Override
    public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos)
    {
        return args.length != 1 ? Collections.emptyList() : getListOfStringsMatchingLastWord(args, server.getOnlinePlayerNames());
    }

}

 

Edited by jredfox
Link to comment
Share on other sites

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

It mostly looks like it should work, although I'm not exactly sure why you're using getEntity(). I think there is a getPlayer() method, and also if you look at the ban command they do an different method where they look up a player name in the server's player's list.

 

I guess it is possible that in single-player the disconnect isn't really functional although I couldn't find any obvious code reason why not. Note that the disconnect() method does kick off a scheduled runnable, so your freeze is probably somehow related to that. Is there any error in the log?

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

20 minutes ago, jabelar said:

It mostly looks like it should work, although I'm not exactly sure why you're using getEntity(). I think there is a getPlayer() method, and also if you look at the ban command they do an different method where they look up a player name in the server's player's list.

 

I guess it is possible that in single-player the disconnect isn't really functional although I couldn't find any obvious code reason why not. Note that the disconnect() method does kick off a scheduled runnable, so your freeze is probably somehow related to that. Is there any error in the log?

yeah log says player logged out disconnected for reason doesn't display says saving worlds and then just stops working. Could there be a more proper way to disconnect aka stop the world and go back to the main menu if player is owner?

To clarify the screen of displaying freezes in game showing the world still but, the log shows otherwise

I have verified even adding a tick delay it still occurs occasionally. What does forge use to kick the player when it says hey don't go in this world but, then again your player doesn't even try to render it yet. Should I try and open a gui before starting the process and if so what gui? 

Edited by jredfox
Link to comment
Share on other sites

Oh yeah it looks like you need to bring up the main menu gui.

 

If you look at the code for the GuiIngameMenu class, you'll see that button 1 is the quit game (return to main menu) and the code for the action performed for that button is as follows:

 

                boolean flag = this.mc.isIntegratedServerRunning();
                boolean flag1 = this.mc.isConnectedToRealms();
                button.enabled = false;
                this.mc.world.sendQuittingDisconnectingPacket();
                this.mc.loadWorld((WorldClient)null);

                if (flag)
                {
                    this.mc.displayGuiScreen(new GuiMainMenu());
                }
                else if (flag1)
                {
                    RealmsBridge realmsbridge = new RealmsBridge();
                    realmsbridge.switchToRealms(new GuiMainMenu());
                }
                else
                {
                    this.mc.displayGuiScreen(new GuiMultiplayer(new GuiMainMenu()));
                }

 

I guess technically you should probably copy all of that code (except for the part of the button enable going to false) for the single player case. Note that since this contains reference to the Minecraft and gui classes which are client-side only, you need to put the code in your client proxy (with a dummy version in your common/server proxy) and then call the proxy method from the command class.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

9 hours ago, jabelar said:

Oh yeah it looks like you need to bring up the main menu gui.

 

If you look at the code for the GuiIngameMenu class, you'll see that button 1 is the quit game (return to main menu) and the code for the action performed for that button is as follows:

 


                boolean flag = this.mc.isIntegratedServerRunning();
                boolean flag1 = this.mc.isConnectedToRealms();
                button.enabled = false;
                this.mc.world.sendQuittingDisconnectingPacket();
                this.mc.loadWorld((WorldClient)null);

                if (flag)
                {
                    this.mc.displayGuiScreen(new GuiMainMenu());
                }
                else if (flag1)
                {
                    RealmsBridge realmsbridge = new RealmsBridge();
                    realmsbridge.switchToRealms(new GuiMainMenu());
                }
                else
                {
                    this.mc.displayGuiScreen(new GuiMultiplayer(new GuiMainMenu()));
                }

 

I guess technically you should probably copy all of that code (except for the part of the button enable going to false) for the single player case. Note that since this contains reference to the Minecraft and gui classes which are client-side only, you need to put the code in your client proxy (with a dummy version in your common/server proxy) and then call the proxy method from the command class.

does that get called on client or server side and should I also be disconnecting the server side and before or after? Commands run on server so what wait next tick then disconnect player?

Edited by jredfox
Link to comment
Share on other sites

Game Crash when player is owner:
https://pastebin.com/WvngUH2E

 

public static void disconnectPlayer(EntityPlayerMP player,TextComponentString msg) 
{	
	if(isPlayerOwner(player))
	{
			Minecraft mc = Minecraft.getMinecraft();
            boolean flag = mc.isIntegratedServerRunning();
            boolean flag1 = mc.isConnectedToRealms();
            mc.world.sendQuittingDisconnectingPacket();
            mc.loadWorld((WorldClient)null);

            if (flag)
            {
                mc.displayGuiScreen(new GuiMainMenu());
            }
            else if (flag1)
            {
                RealmsBridge realmsbridge = new RealmsBridge();
                realmsbridge.switchToRealms(new GuiMainMenu());
            }
            else
            {
                mc.displayGuiScreen(new GuiMultiplayer(new GuiMainMenu()));
            }
	}
	else
	{
		player.connection.disconnect(msg);
	}
}

 

Edited by jredfox
Link to comment
Share on other sites

16 minutes ago, jredfox said:

does that get called on client or server side and should I also be disconnecting the server side and before or after? Commands run on server so what wait next tick then disconnect player?

Right so what you command needs to do is see if it is on a dedicated server or integrated server. If on integrated server, it will need to send a dedicated custom packet to client. The client message receiver would run the code mentioned above.

 

i think the error message you posted is because it looks like this is running inside of a server tick handler, and a server wouldn't have the ability to create a GUI. You need custom packet to client to let it know what is going on.

 

There may be easier way to disconnect a single player, but I'm just thinking that the code above is the actual way the Minecraft already disconnects a single player so should be safe and accurate if run from the client side.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

5 minutes ago, jabelar said:

Right so what you command needs to do is see if it is on a dedicated server or integrated server. If on integrated server, it will need to send a dedicated custom packet to client. The client message receiver would run the code mentioned above.

 

i think the error message you posted is because it looks like this is running inside of a server tick handler, and a server wouldn't have the ability to create a GUI. You need custom packet to client to let it know what is going on.

 

There may be easier way to disconnect a single player, but I'm just thinking that the code above is the actual way the Minecraft already disconnects a single player so should be safe and accurate if run from the client side.

I shouldn't need packets since I am the server and client. let my try that code from the client side

Link to comment
Share on other sites

23 minutes ago, jabelar said:

 

I tried running the thing that's always suppose to work but, sometimes it freezes the game. player.connection.disconnect(msg); on server side. And it always froze from the command.

Edited by jredfox
Link to comment
Share on other sites

29 minutes ago, jredfox said:

I shouldn't need packets since I am the server and client. 

Wrong. You really, really need to understand the client and server stuff. When there is integrated server ("single player") there is still a separate client and server thread and they still communicate using packets. The packets just use the link local address (127.0.0.1 I think) but they are still being used!

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

7 minutes ago, jabelar said:

Wrong. You really, really need to understand the client and server stuff. When there is integrated server ("single player") there is still a separate client and server thread and they still communicate using packets. The packets just use the link local address (127.0.0.1 I think) but they are still being used!

ok

Edited by jredfox
Link to comment
Share on other sites

4 minutes ago, jabelar said:

Wrong. You really, really need to understand the client and server stuff. When there is integrated server ("single player") there is still a separate client and server thread and they still communicate using packets. The packets just use the link local address (127.0.0.1 I think) but they are still being used!

wait just realize my idea can't work since I want it to be a server utility how to disconnect player from server side only?

Link to comment
Share on other sites

7 minutes ago, jredfox said:

wait just realize my idea can't work since I want it to be a server utility how to disconnect player from server side only?

Okay. In that case the closest thing I can think of is the ban command. You can look at that code. However, your original code seemed to be close to that so I'm not totally sure why it didn't work. It is possible that you were calling the server side disconnect method from the client and so that might have caused your original problem.

 

In the ban player command, they use this code:

                EntityPlayerMP entityplayermp = server.getPlayerList().getPlayerByUsername(args[0]);

                if (entityplayermp != null)
                {
                    entityplayermp.connection.disconnect(new TextComponentTranslation("multiplayer.disconnect.banned", new Object[0]));
                }

 

Where obviously your command usage would need the player name as args[0].

 

But this is very close to your original code, so not quite sure what the difference is except it uses different way to get the player instance.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

Look at the kick command.

it's not usable on client side till 1.13 and I can't view the code. maybe they have a custom new packet or fixed something?

how do I view the source for that command?

Edited by jredfox
Link to comment
Share on other sites

3 minutes ago, jabelar said:

Okay. In that case the closest thing I can think of is the ban command. You can look at that code. However, your original code seemed to be close to that so I'm not totally sure why it didn't work. It is possible that you were calling the server side disconnect method from the client and so that might have caused your original problem.

 

In the ban player command, they use this code:


                EntityPlayerMP entityplayermp = server.getPlayerList().getPlayerByUsername(args[0]);

                if (entityplayermp != null)
                {
                    entityplayermp.connection.disconnect(new TextComponentTranslation("multiplayer.disconnect.banned", new Object[0]));
                }

 

Where obviously your command usage would need the player name as args[0].

player connection disconnect is what I was using I could try make a new object that is null at first index like yours but, that is the only difference besides you forcing full player names difficult to type when in mdk.

Link to comment
Share on other sites

14 minutes ago, jabelar said:

Okay. In that case the closest thing I can think of is the ban command. You can look at that code. However, your original code seemed to be close to that so I'm not totally sure why it didn't work. It is possible that you were calling the server side disconnect method from the client and so that might have caused your original problem.

 

In the ban player command, they use this code:


                EntityPlayerMP entityplayermp = server.getPlayerList().getPlayerByUsername(args[0]);

                if (entityplayermp != null)
                {
                    entityplayermp.connection.disconnect(new TextComponentTranslation("multiplayer.disconnect.banned", new Object[0]));
                }

 

Where obviously your command usage would need the player name as args[0].

 

But this is very close to your original code, so not quite sure what the difference is except it uses different way to get the player instance.

woah that seemed to fix it changing it from TextComponentString > TextComponentTranslation made all the difference thank you so much here is my server only 100% kick command 1.13 equivalent in 1.12.2
 

	public static void disconnectPlayer(EntityPlayerMP player,TextComponentString msg) 
	{	
		player.connection.disconnect(new TextComponentTranslation(msg.getText(),new Object[0]) );
	}

 

Link to comment
Share on other sites

14 minutes ago, diesieben07 said:

What are you even saying...

	public static void disconnectPlayer(EntityPlayerMP player,TextComponentString msg) 
	{	
		player.connection.disconnect(new TextComponentTranslation(msg.getText(),new Object[0]) );
	}

Command seems to freeze the game if something else is loading why is it occurring?

Link to comment
Share on other sites

35 minutes ago, jabelar said:

Okay. In that case the closest thing I can think of is the ban command. You can look at that code. However, your original code seemed to be close to that so I'm not totally sure why it didn't work. It is possible that you were calling the server side disconnect method from the client and so that might have caused your original problem.

 

In the ban player command, they use this code:


                EntityPlayerMP entityplayermp = server.getPlayerList().getPlayerByUsername(args[0]);

                if (entityplayermp != null)
                {
                    entityplayermp.connection.disconnect(new TextComponentTranslation("multiplayer.disconnect.banned", new Object[0]));
                }

 

Where obviously your command usage would need the player name as args[0].

 

But this is very close to your original code, so not quite sure what the difference is except it uses different way to get the player instance.

so it's not always working if the world is loading not giving it absolute non loading the game will freeze with booting the player. Should I be pausing the game if player is owner?

Edited by jredfox
Link to comment
Share on other sites

Just now, jredfox said:

if the world is loading not giving it absolute non loading

Can you please spend more than 5 seconds on your posts? This is completely non-understandable.

Also stop posting multiple posts every time. Spend some effort on what you are trying to say. This is a forum, not a chat room.

Link to comment
Share on other sites

13 minutes ago, diesieben07 said:

What do you mean by "something else is loading"? Why are you putting the text of a TextComponentString into a TextComponentTranslation? Where are you calling this method?

I am calling the method disconnectPlayer() from the command class.

Since the client has to have the mod anyways to use the command and have the issue having client only code is acceptable and if is on server only I know it's always going to work with player disconnect

Edited by jredfox
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

    • I figured out what was wrong ... you can spot it pretty quick most likely: { "type": "minecraft:smelting", "ingredient": { "item": "minecraft:gold_nugget_ore" }, "result": "minecraft:gold_nugget", "experience": 0.25, "cookingtime": 150 }   ... so, after changing `minecraft` to `foundations` for all the custom items and all the recipes work. Well that's what copy/paste will get you. One last question about recipes, though. What does the following json key/value do in the game? "group": "copper_ingot"  
    • # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # #      102.54.94.97     rhino.acme.com          # source server #       38.25.63.10     x.acme.com              # x client host # localhost name resolution is handled within DNS itself. #    127.0.0.1       localhost #    ::1             localhost ###0.0.0.0 apps.corel.com ###0.0.0.0 mc.corel.com 0.0.0.0 origin-mc.corel.com ###0.0.0.0 iws.corel.com
    • Is there a way to create a custom map marker for a custom structure/biome (same as a vanilla mansion)?
    • I’ve scrounged the Internet for solutions and found absolutely nothing that has worked for me I have posted my code in a GitHub Repository. I bet the culprit is somewhere in the Container or TileEntity classes (Furnace packages are in blocks/coke_furnace or blocks/foundry) PS I know I’m on 1.12.2 and that it’s not supported I don’t want to update my game or my code. Lol It’s my favorite version and the most widely modded. I don’t plan to release the mod, I’m just customizing my game to my will. I’ll send it to friends but that’s it.
    • 1.19.2 With forge 43.2.4   https://mclo.gs/zCAx1MG      
  • Topics

×
×
  • Create New...

Important Information

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