I made a little proof-of-concept just to test out the idea, and it appears to work perfectly well. Java is a tad less helpful when dealing with strings, arrays, etc. than PHP is, but I think this could provide an improvement worth the extra work.
Oh, and Java also holds the platform independent title like PHP, and can (if you reallllllllly wanted, though I'd hope not) release scripts in closed source.
Here's my little test. It just spams someone with console messages. You type "/spam Kira 5 1 What's up?" and it will spam "Kira" 5 times, every 1 second with the message "What's up?". Of course, this is something you would probably never actually make for a server, but it shows the point, as you can have multiple people spamming eachother.
Off the top of my head, I can think of it being useful in servers like Happy Fun Time, where there's a delay between things happening. Rather than holding up the script for (not very long), you could throw it in another thread. It also helps with the delayed commands and whatnot. And you could use it for infinite loops until something happens, then myThread.stop(). Tons of things it could be used for. Kind'a a whole new section of ideas opened up for armagetron scripts .. at least for me.
So, here it is ...
MultiThreadSpam.java
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class MultiThreadSpam
{
public static String line;
public static String[] split;
public static Map<String, Thread> spamUsers = new HashMap<String, Thread>();
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
while (getLine())
{
if (split[0].equals("INVALID_COMMAND"))
{
String sender = split[2];
String command = split[1].substring(1);
if (command.equalsIgnoreCase("spam") && split.length > 8)
{
if (!spamUsers.containsKey(sender) && isNumeric(split[6]) && isNumeric(split[7]))
{
int amount, delay;
String user = split[5];
amount = Integer.parseInt(split[6]);
delay = Integer.parseInt(split[7]);
String message = join(Arrays.copyOfRange(split, 8, split.length), " ");
Spammer sendSpam = new Spammer(sender, user, message, amount, delay);
Thread thread = new Thread(sendSpam);
thread.start();
spamUsers.put(sender, thread);
}
}
}
}
}
public static boolean getLine()
{
try
{
line = br.readLine();
}
catch (IOException io)
{
return false;
}
split = line.split("\\s+");
return true;
}
public static String join(String[] words, String glue)
{
String text = "";
for (String word : words)
{
text += text.length() > 0 ? " " + word : word;
}
return text;
}
public static boolean isNumeric(String input)
{
try
{
Integer.parseInt(input);
}
catch (Exception e)
{
return false;
}
return true;
}
}
Code: Select all
public class Spammer implements Runnable
{
private static String sender;
private static String user;
private static String message;
private static int times;
private static int delay;
public Spammer(String from, String to, String msg, int amount, int wait)
{
sender = from;
user = to;
message = msg;
times = amount;
delay = wait * 1000;
}
public void run()
{
for (int i = 0; i < times; i++)
{
System.out.println("PLAYER_MESSAGE " + user + " \"" + message + "\"");
sleep(delay);
}
MultiThreadSpam.spamUsers.remove(sender);
}
public void sleep(int time)
{
try
{
Thread.sleep(time);
}
catch (InterruptedException e) {}
}
}
So, now I got a question. Do you guys see any downside to this? I mean, all I can see is benefits, minus the little bit of extra work. Java is a pretty easy language for people to learn as well that don't know how to script yet.
Curious of your thoughts.