-
Java 2 NPC
So... I made this program two years ago. I posted it in a help thread once, but I'd figure someone else might find it useful.
Code:
package scripting.npc;
import client.MapleClient;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
//IMPORTS
import server.*;
//END
/**
* @author DeathStar/Zychronix
*/
class Java2npc {
static String author = "DeathStar/Zychronix";
static String date = "Feburary XIV, MMXVI";
static int scriptID = 9901002;
static String mainLocation = "E:\\Blossoms\\";
static String thisFile = mainLocation + "src\\scripting\\npc\\Java2npc.java";
static String outputFile = "E:\\Blossoms\\scripts\\npc\\";
MapleClient c;
NPCConversationManager cm = new NPCConversationManager(c, 0);
//VARIABLES
String talk = "";
int status = 0;
int selection = 0;
//END
public void action(int m, int t, int s) {
if (status == 1) {
cm.sendSimple(
"#L0#1#l\r\n" +
"#L1#2#l\r\n" +
"#L2#3#l\r\n" +
"#L3#4#l\r\n" +
"#L4#5#l\r\n" +
"#L5#6#l"
);
} else if (status == 2) {
cm.sendOk(s + " selection");
cm.dispose();
}
//END
}
public static void main(String[] args) {
File file = new File(thisFile);
FileInputStream data;
DataInputStream in;
BufferedReader read;
BufferedWriter write = null;
File output = new File(outputFile + scriptID +".js");
try {
write = new BufferedWriter(new FileWriter(output));
} catch (IOException ex) {
System.out.println("Derp.");
}
try {
data = new FileInputStream(file);
in = new DataInputStream(data);
read = new BufferedReader(new InputStreamReader(in));
String currentLine;
try {
boolean toPrint = false;
boolean toSkip = false;
for (int i = 0; i < 5; i++) {
if (i == 0) { // print out credits
write.write(
"/*\n"
+ " Author: " + author + "\n"
+ " Date: " + date + "\n"
+ " Script ID: " + scriptID + "\n"
+ "*/\n");
} else if (i == 1) { // Get and print out imports
while ((currentLine = read.readLine()) != null) {
if (currentLine.equals("//IMPORTS")) {
toPrint = true;
continue;
}
if (toPrint) {
if (!currentLine.equals("//END")) {
String[] j2JS = currentLine.split(" ");
write.write("importPackage(Packages." + j2JS[1].substring(0, j2JS[1].length() - 3) + ");");
} else {
write.write("\n\n");
data = new FileInputStream(file);
in = new DataInputStream(data);
read = new BufferedReader(new InputStreamReader(in));
break;
}
}
}
} else if (i == 2) { // Get and print out action
write.write(
"function action(m, t, s) {\n"
+ " status++;\n"
+ " if (m <= 0) {\n"
+ " cm.dispose();\n"
+ " return;\n"
+ " }");
toPrint = false;
while ((currentLine = read.readLine()) != null) {
if (currentLine.equals("public void action(int m, int t, int s) {")) {
toPrint = true;
write.write("\n");
continue;
}
if (toPrint) {
if (!currentLine.equals(" //END")) {
write.write("\n");
write.write(currentLine);
} else {
write.write("\n");
data = new FileInputStream(file);
in = new DataInputStream(data);
read = new BufferedReader(new InputStreamReader(in));
break;
}
}
}
write.write("}\n\n");
} else if (i == 3) { // Get and print out declareDefaults
write.write("function declareDefaults() {");
toPrint = false;
while ((currentLine = read.readLine()) != null) {
if (currentLine.equals(" //VARIABLES")) {
toPrint = true;
write.write("\n");
continue;
}
if (toPrint) {
if (!currentLine.equals(" //END")) {
String[] j2JS = currentLine.split(" ");
write.write("\t");
for (int x = 5; x < j2JS.length; x++) {
write.write(j2JS[x]);
if (x == 7) {
write.write("\n");
} else {
write.write(" ");
}
}
} else {
break;
}
}
}
write.write("}\n\n");
} else if (i == 4) { // Print out start
write.write(
"function start() {\n"
+ " declareDefaults();\n"
+ " action(1, 0, 0);\n"
+ "}");
}
}
data.close();
in.close();
read.close();
write.close();
System.out.println("Done.");
} catch (IOException ex) {
System.out.println("Derp.");
}
} catch (FileNotFoundException ex) {
System.out.println("Java2npc.java not found.");
}
}
}
You need to put this in your source, you can put it anywhere really.
Code:
static String author = "DeathStar/Zychronix";
static String date = "Feburary XIV, MMXVI";
static int scriptID = 9901002;
static String mainLocation = "E:\\Blossoms\\";
static String thisFile = mainLocation + "src\\scripting\\npc\\Java2npc.java";
static String outputFile = "E:\\Blossoms\\scripts\\npc\\";
This bit of code is how you set it up.
Change the author, date, scriptID to whatever you want. The mainLocation and thisFile is combined together for the complete location of the Java2npc.java file. The output file is wherever you want to put it. It'll use the scriptID you gave it for the file name.
Code:
//IMPORTS
import server.*;
//END
If you need to import a package do it in between here.
Code:
//VARIABLES
String talk = "";
int status = 0;
int selection = 0;
//END
If you need other variables you can put them in here.
Code:
public void action(int m, int t, int s) {
if (status == 1) {
cm.sendSimple(
"#L0#1#l\r\n" +
"#L1#2#l\r\n" +
"#L2#3#l\r\n" +
"#L3#4#l\r\n" +
"#L4#5#l\r\n" +
"#L5#6#l"
);
} else if (status == 2) {
cm.sendOk(s + " selection");
cm.dispose();
}
//END
}
This is where all the code for your NPC will go. I only use the action method here so your NPC will use the status variable for separating the NPC.
The program uses the comments to search for the imports, variables, and codes so I wouldn't delete them.
Here is end result after running this program.
Code:
/*
Author: DeathStar/Zychronix
Date: Feburary XIV, MMXVI
Script ID: 9901002
*/
importPackage(Packages.server);
function action(m, t, s) {
status++;
if (m <= 0) {
cm.dispose();
return;
}
if (status == 1) {
cm.sendSimple(
"#L0#1#l\r\n" +
"#L1#2#l\r\n" +
"#L2#3#l\r\n" +
"#L3#4#l\r\n" +
"#L4#5#l\r\n" +
"#L5#6#l"
);
} else if (status == 2) {
cm.sendOk(s + " selection");
cm.dispose();
}
}
function declareDefaults() {
talk = "";
status = 0;
selection = 0;
}
function start() {
declareDefaults();
action(1, 0, 0);
}