MinecraftForge modding Send a command from the client to the server - java

So I wanted to create a GUI for a server to make stuff easier for ppl. I created a Gui with buttons for easy use so you don't have to enter commands all the time. The button works but I didn't find any function that executes a command as the player itself. I googled around but didn't find a solution for this problem.
(btw. this obviously didn't work: player.sendChatToPlayer("/{any command}"); (player is a EntityPlayer))
Regards Jens

Do a Minecraft.getMinecraft().thePlayer.sendChatMessage("/Command here")

In case you want to execute a client-side only command, you can use
ClientCommandHandler.instance.executeCommand(Minecraft.getMinecraft().thePlayer, "/your command");

Related

Java UI on top of Ruby script

I've been searching for answer without any luck for the below problem:
I have a quite lengthy ruby script which if you run in terminal(MAC) requires multiple inputs from user throughout execution. I would like to put on top of that script a Java UI - Swing or JavaFX so that the input is done via the Java UI throughout execution.
Is there any way to achieve this, how do you track the state of the script execution and how do you pass a input from Java UI to ruby script once required.
Now this is might be broad question so here is the example below how it would look like more or less:
I have a test.rb file with the below:
puts "enter your name:
#name = gets
puts #name
...
So Java would look something like:
String command = "ruby test.rb";
Process run = Runtime.getRuntime().exec(command);
run.waitFor();
...
So I could assign results of 'run' to the String but thats not much use.
I have looked at the JRuby but without any luck also (Maybe just dont understand it).
Many Thanks!

Selenium: sendKeys()

I tried using Java + Selenium to automatically login to my Gmail account however each time I am blocked by Google. When I log in manually (even on the very same server) I have no such issue. Further, if I use "sendKeys" type lib (https://pypi.python.org/pypi/SendKeys/0.3) I have no such problem.
My question is two fold:
What are the tech differences between how Selenium "sends keys" and how a normal user would? Or how the Python library does?
Is there a way to modify selenium so it mimics a real user? Or is there another library to use instead of Selenium that mimicks a real user?
Thanks
Actually its the difference of speed. Human being types slowly and script write it very fastly. So, you have to decrease your script speed. So on the whole you have to built better bot.
Better bots are not going to be detected by any server.
You should include some delay in your code to cheat google that your code is real human - that's it :) Thread.sleep could be the easiest solution here.
Open gmail & login & open latest received mail
DriverObj.get("http://www.gmail.com");
DriverObj.findElement(By.id("gmail-sign-in")).click();
DriverObj.findElement(By.id("Email")).sendKeys("youremailID");
DriverObj.findElement(By.id("next")).click();
DriverObj.findElement(By.id("Passwd")).sendKeys("yourpassword");
DriverObj.findElement(By.id("signIn")).click();
Thread.sleep(2000);
WebElement tableelement=DriverObj.findElement(By.xpath("//div[#class='Cp']/div/table//tbody"));
java.util.List<WebElement> tableelement1=tableelement.findElements(By.xpath("//div[#class='Cp']/div/table//tbody/tr"));
int tableelementsize=tableelement1.size();
int i;
if(tableelementsize>0){
//DriverObj.navigate().refresh();
for(i=0;i<tableelementsize;i++){
DriverObj.findElement(By.xpath("//div[#class='Cp']/div/table//tbody/tr/td[6]")).click();
}
}else {
if(tableelementsize==0){
DriverObj.navigate().refresh();
for(i=0;i<tableelementsize;i++){
DriverObj.findElement(By.xpath("//div[#class='Cp']/div/table//tbody/tr/td[6]")).click();
}
}
path of mail box might be change (you have need to change according to your work)
Answer to 1st question :
Sendkeys() method refers to user input to a particular text box on the web page where as we can also use javascriptExecuter for the same operation.
Answer to 2nd question :
As per my knowledge NO. but I am not so sure about it.
In Java use the following code to log in to Gmail.
However I can say after clicking on sign in button the Inbox page takes time to load (at around 10 seconds max)
So in time being if you are searching for another element without any wait (implicit or explicit) then it will throw error.
driver.get("http://www.gmail.com/");
driver.findElement(By.id("Email")).sendKeys("userName");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
Then you can use Thread.sleep(10000) or if you have written any wait method to make the driver wait for the next object to be visible , You can use that one.
Absolutely, Selenium just provides library to write automated test scripts. If you wanted to act as a human. Then you should add some wait statement in between your test steps into entire script. Until you didn't add any wait in script, it will behave like a robotic test only. If you take any examples of test automation tool, tool doesn't add any extra information/action towards automation testing.Thats what automated testing is... It's not automatic testing. It's just computer aided testing done by machine. I hope now you'll be more clear with above example.
Another solution is send a wrong e-mail as first attempt, after the error message, you could apply the right e-mail, this is an human action.

Java command line exit command?

I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println();. so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener, but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance
To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:
1.- Create a JLabel(and a scrollbar).
2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");.
3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...
As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.
4.-Add a JTextFiel to send input to the server.
5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.
Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!
If you don't want to have a visible gui, you could create an "always-focused invisible grafical interface" with the listener.

CreateProcess for Running JAR File Starts with Window Minimized

I'm using a CreateProcess call within a C++ program to execute a JAR file that runs a Java Swing GUI application. All works fine with the exception that the Java app starts off minimized and I want it to start with the window displayed. Here's the relevant code snippet:
// Construct the command string to be used for the CreateProcess call,
//including a parameter string
sprintf(cmdStr, "javaw -jar \"AppDir\\App.jar\" %s", parmStr);
// Create and initialized startup-info structure for use with CreateProcess call
STARTUPINFO startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.wShowWindow = SW_NORMAL;
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.cb = sizeof(startInfo);
PROCESS_INFORMATION procInfo;
ZeroMemory(&procInfo, sizeof(procInfo));
if (!CreateProcess(NULL, cmdStr, NULL, NULL, FALSE, 0, NULL, NULL, &startInfo, &procInfo))
{
MessageBox( dialogOwner, "Create Process Error", "Application not instantiated", MB_OK);
}
According to the MSDN literature, setting the wShowWindow flag to SW_NORMAL and dwFlags to STARTF_USESHOWWINDOW ought to do the trick but some of the comments I've read in this and other forums imply that sometimes those flags are ignored (e.g. for console apps) so I was wondering if that was the case here. For the record, I've had this problem before then it went away on its own and now it's back after I made some code changes. But I wasn't setting any flags in the startupinfo structure before, so I was hoping to achieve some consistency in behaviour by doing so. Any tips or pointers would be appreciated...
Sheldon R.
Okay, I've been working on a solution to my problem and I'm finally ready to talk about it, since it appears to be working :) I call my Java app one of two ways: either by popping up a dialog box first to collect login credentials, or by calling the app directly using saved credentials. For the login-dialog case, I call CreateProcess with the parameter "javaw...", whereas the saved-credential case appears to need "java..." to avoid the app starting up minimized. Aside from the difference in the parameter string, everything else about the CreateProcess call is the same. I don't know why I would use "java" in one case and "javaw" in the other, but since it's working, I won't question it :) But, of course, if someone wants to enlighten on the subject, I'd be happy to learn more. Thanks to #Jim Garrison for the suggestion, even if it wasn't ultimately the solution to my issue...
Sheldon R.
This is an update to my previous answer: The reason I had to call my java applet two different ways (i.e. "java" or "javaw") depending on the context, had to do with a bug in the C++ application from which I was calling my applet. The reason I know this is because a few months after fixing my issue, a newer version of this application was released, and this version didn't have the underlying bug, which essentially caused a new bug in my applet due to the "java" command doing what you'd expect i.e. instantiating a console window in addition to the applet window, much to the surprise of my business users :). So for the new bug-free version of the C++ application, I call my applet using the "javaw" command regardless of whether or not a dialog box is instantiated first to enable the user to enter login credentials...
Sheldon

rJava startMainLoop() function kills Java operations

I've developed an interface that allows a user to load and manipulate data. The GUI is developed in Java and all the computational stuff is done in the background by R, linking the two with jri. The idea is that the user doesn't have to have any knowledge of R to use it, it's all options and buttons. However, i'd like to give the user the option to write some code if necessary. So here is my problem:
If I use the following code to start the Rengine and not let the user interact via console, everything works fine:
Rengine re=new Rengine(null, false, new TextConsole());
But if I use this:
Rengine re=new Rengine(null, true, new TextConsole());
The functionality of the gui doesnt work. I tried using the
re.startMainLoop();
function after the data was loaded. I was able to manipulate the data from the comand line in R, for example I could make a new variable from a column of the data loaded:
newVariable<-data$column1
But yet again, I couldn't use the gui anymore. Has anyone got any ideas or explainations as to why this is?
Thanks in advance,
Aran
Fundamentally, if REPL is not running, R is simply used via eval calls from your code. You have control at all times, except during the actual evaluation. That is the most common use, because you can do pretty much anything that way.
The moment you enable the event loop (REPL), you have to implement the callback methods that are used by the loop. By design R surrenders the control only by calling the rReadConsole callback which you have to implement. The example TextConsole works only as a demo, it uses blocking call (readLine()) to wait so you definitely don't want to use that in your GUI. You'll have to implement all callbacks correspondingly to react to your GUI's elements (wait in ReadConsole for your GUI to wake it up from a separate thread, dispatch WriteConsole to your elements etc.). You can have a look at JGR how it's done properly. Unless you are really building a general purpose R GUI, I wouldn't go into that trouble ...
(PS: please use stats-rosuda-devel mailing list for rJava/JRI questions - you get answers much faster)

Categories