Jump to content

Recommended Posts

Posted (edited)

Hey guys, I have made two classes I want to use to execute a commands. One of the classes (NickScan) is my command class and the other (NickScanRunnable) is my runnable class. I have a HTTP GET request running in my runnable class which is ran in my command class on 'processCommand()'. In my runnable class I edit a public variable to the String I want and then I use the Thread.join() method to connect to the thread and after retrieve my public variable which I then output. The problem is Thread.join() suspends my UI thread too. I was wondering if there was any other way to do this? I will attach code below.

 

NickScan class:

 
 
 
 
Spoiler

 


package com.mine215.nickscanner;

import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;

public class NickScan extends CommandBase {
    @Override
    public String getCommandName() {
        return "nickscan";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "command.searchnickscan.usage";
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {
        EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().thePlayer;
        NickScanRunnable nsRunnable = new NickScanRunnable();
        Thread apiConnectThread = new Thread(nsRunnable);
        apiConnectThread.start();
        try {
            apiConnectThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        player.addChatMessage(new ChatComponentText(nsRunnable.resultToChat));
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 0;
    }

    @Override
    public boolean isUsernameIndex(String[] args, int index)
    {
        return index == 0;
    }
}

 

 

NickScanRunnable class:

 
 
 
 
Spoiler

package com.mine215.nickscanner;

import net.minecraft.client.Minecraft;
import net.minecraft.client.network.NetworkPlayerInfo;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.util.EnumChatFormatting;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class NickScanRunnable implements Runnable {
    private String apiKey = "eef99098-f636-42c7-84cf-deff39981377";
    private List<String> playerNames = new ArrayList<String>();
    public String resultToChat = "Bleh bleh bleh";

    @Override
    public void run() {
        Iterable<NetworkPlayerInfo> players = Minecraft.getMinecraft().getNetHandler().getPlayerInfoMap();
        EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().thePlayer;
        while(players.iterator().hasNext()){
            NetworkPlayerInfo element = players.iterator().next();
            String a = getPlayerName(element);
            playerNames.add(a);
        }

        for(int i = 0; i < playerNames.size(); i++) {
            URL url = null;
            try {
                url = new URL("https://api.hypixel.net/player?key=" + apiKey + "&name=" + playerNames.get(i));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            HttpURLConnection connection = null;
            try {
                connection = (HttpURLConnection) url.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                connection.setRequestMethod("GET");
            } catch (ProtocolException e) {
                e.printStackTrace();
            }

            // To store our response
            StringBuilder content = null;

            // Get the input stream of the connection
            BufferedReader input = null;
            try {
                input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String line;
            content = new StringBuilder();
            try {
                while ((line = input.readLine()) != null) {
                    // Append each line of the response and separate them
                    content.append(line);
                    content.append(System.lineSeparator());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (content.toString().length() < 33) {
                resultToChat = EnumChatFormatting.GOLD + "[NickScanner] " + EnumChatFormatting.RED + playerNames.get(i) + EnumChatFormatting.GOLD + " is a nick";
            } else {
                resultToChat = EnumChatFormatting.GOLD + "[NickScanner] " + EnumChatFormatting.DARK_GREEN + playerNames.get(i) + EnumChatFormatting.GOLD + " is not a nick";
            }
        }
    }
    public String getPlayerName(NetworkPlayerInfo networkPlayerInfoIn)
    {
        return networkPlayerInfoIn.getDisplayName() != null ? networkPlayerInfoIn.getDisplayName().getFormattedText() : ScorePlayerTeam.formatPlayerName(networkPlayerInfoIn.getPlayerTeam(), networkPlayerInfoIn.getGameProfile().getName());
    }
}

 

 

Edited by Mine215
Make it look cleaner
Posted (edited)

It's only normal for the main thread to block. I mean you join the main thread, it will be blocked until the other thread finish.

Edited by Zen3515

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.