Well, I saw a thing about "codes" stored in a text file, where users are given codes and then they can use them to redeem things. So, I wrote some stuff up using a text file and an array list.
I give you the basic methods for this.
Code:
public static boolean useCode(String toRemove) {
if (Server.codes.contains(toRemove)) {
if (Server.codes.remove(toRemove)) {
arrayToFile(Server.codeLocation, Server.codes);
return true;
}
}
return false;
}
public static void fileToArray(String file, ArrayList array, boolean debug) {
try {
File theFile = new File(file);
if (file == null) {
if (debug) {
System.out.println("[fileToArray] File is null");
}
return;
}
array.clear();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
array.add(line);
if (debug) {
System.out.println("Added : " + line);
}
}
if (debug) {
System.out.println("List contains " + array.size() + " entries");
}
br.close();
if (debug) {
System.out.println("[fileToArray] Loaded " + array.size() + " entries");
}
} catch (IOException ex) {
//Logger.getLogger(manager.class.getName()).log(Level.SEVERE, null, ex);
if (debug) {
System.out.println("[fileToArray] Error loading file : " + file);
ex.printStackTrace();
}
}
}
public static void arrayToFile(String file, ArrayList array) {
try {
FileWriter outFile = outFile = new FileWriter(file);
PrintWriter out = out = new PrintWriter(outFile);
for (int i = 0; i < array.size(); i++) {
out.println(array.get(i));
}
out.close();
outFile.close();
} catch (IOException ex) {
//Logger.getLogger(manager.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("[arrayToFile] Error writing to file : " + file);
ex.printStackTrace();
}
}
Basically you setup your file location and array list, when your server starts, load your array list from a file using the methods, then in commands or whatever, use useCode as a boolean, so if the boolean is true, you give the player a reward, if its false, the code does not exist. If it is true, the item is removed from the array and the array is saved.
An example command for this is
Code:
if (cmd.startsWith("code")) {
String entry = cmd.substring(5);
if (Misc.useCode(entry)) {
c.Send("The code you redeemed is correct!");
c.addItem(995, 100000);
} else {
c.Send("Sorry, the code you have entered is invalid");
}
}
So if the array (loaded from the text file) contains the code, the user is given the whatever, and the item is removed from the array, and then it is re-saved to the file.
Expanded uses :
Have it read from a web site, and on the website, use a donation system? or perhaps some type of forum integration?
Comments? Criticism?