Jump to content

[Forge 1.8.9] Alternative to thread.sleep?


CoalOres

Recommended Posts

I've been using this code to execute a bulk operation:

 

 while((line = buffered.readLine()) != null) {
            	Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line );
            	Thread.sleep(100);

 

It reads 100 lines, which works. However it freezes my Minecraft and because of this the time is not consistent, and so sometimes I get the message "Sending Commands too fast!", making it inconsistent.

 

 

Link to comment
Share on other sites

depends on where this class of yours is being called. You should probably post the whole class not just part of a method.

 

but you would most likely need to create your own tick variable and make it count up

 

int ticks;

ticks++;

 

Whole class:

package com.simpleafk;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

public class wparty extends CommandBase {

@Override
public String getCommandName() {
	// TODO Auto-generated method stub
	return "wparty";
}

@Override
public String getCommandUsage(ICommandSender sender) {
	// TODO Auto-generated method stub
	return "wparty";
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender icommandsender) {

	return true;
}
@Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
	String path = "config/whitelist.txt";
        String line = null;
	try { 
	FileReader file = new FileReader(path);
        BufferedReader buffered = 
                new BufferedReader(file);
    	    sender.addChatMessage(new ChatComponentText(EnumChatFormatting.DARK_AQUA + "People on your invite list: "));
            while((line = buffered.readLine()) != null) {
            	Minecraft.getMinecraft().thePlayer.sendChatMessage("/party invite " + line );
            	Thread.sleep(100);
            	
            }
            buffered.close();
	} catch (IOException | InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}

The file in question is totally fine, just need a better thing for the delay.

Link to comment
Share on other sites

So, you want to not spam commands without freezing your Minecraft (if I understand correctly)? AFAIK, you could put your loop in a new thread, but Minecraft might not allow it. Worth a try anyways.

But this stuff about counting ticks? Sounds like it would be better.

Link to comment
Share on other sites

Welp, the command is only going to run once. You need something that can tick if you need to put a pause in your commands. So you are going to need to make something that can tick and then use something to the likes of:

if (ticks % 20 == (20 - 1){
//do stuff
}

 

This will fire every second. So adjust the numbers as needed.

 

If it were me, when the command is fired, I would get all the names out the file, put them in a global array list.

 

Have your ticking entity watching said array list, if it != null, then do stuff.

 

Once it iterates through all the names, clear the list.

But surely that won't work because of the loop? And that still doesn't give me an actual delay in time.

Link to comment
Share on other sites

No you would re-write your command to instead loop to ge tthe names out of the file and add them to an array list. After the loop you would create your entity and pass the list to the entity.

 

The entity could then use the onUpdate() method to iterate through the list at a slower pace using ticks.

That seems really complicated, is there any simpler way? I've seen stuff about scheduled executor delays?

Link to comment
Share on other sites

No you would re-write your command to instead loop to ge tthe names out of the file and add them to an array list. After the loop you would create your entity and pass the list to the entity.

 

The entity could then use the onUpdate() method to iterate through the list at a slower pace using ticks.

That seems really complicated, is there any simpler way? I've seen stuff about scheduled executor delays?

 

There is no "simpler" way.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.