Data Storage - Banning Plugin (Minecraft)
Posted: Thu Jan 17, 2013 11:47 am
Hey,
I have an issue with the Banning Plugin Tutorial for Minecraft you posted here:
http://www.youtube.com/watch?v=2dXNasDd ... B7&index=2
In your Tutorial you use an Array of Strings.
I need to use a String, Integer Hashmap.
My plan is to store a username(string) and the time they used the command via a timestamp so that it would look like this:
On this command:
on a server reboot and running the command again it gives out this:
This is my current code of the List class which just differs slightly from the one you explained in your tutorial:
I have an issue with the Banning Plugin Tutorial for Minecraft you posted here:
http://www.youtube.com/watch?v=2dXNasDd ... B7&index=2
In your Tutorial you use an Array of Strings.
I need to use a String, Integer Hashmap.
My plan is to store a username(string) and the time they used the command via a timestamp so that it would look like this:
player1:71962877 player2:13280947 etc.My problem lies within the Load function. Adding things to the file via the hashmap works fine and gives out just as wanted. On a reboot of the MC Server it messes everything up as I don't really know how to fix it I came here to ask for help.
On this command:
if (commandName.equalsIgnoreCase("herp")){
if (args.length == 0){
erfolg = true;
long timestamp = System.currentTimeMillis()/1000;
int timestamp2 = (int) timestamp;
plugin.listPlayers.add(player.getName(), timestamp2);
player.sendMessage("Added");
plugin.listPlayers.save();
}
it gives out this:
illfated:1358422872which is perfect
on a server reboot and running the command again it gives out this:
illfated:1358422910 illfated:1358422872:1358422896as you can see the first line shouldnt be there at all and he added another timestamp to the second line. I know that his is because how I handle the load() but I have no other idea how to do that as I'm a pretty major noob.
This is my current code of the List class which just differs slightly from the one you explained in your tutorial:
package com.shadoom.shadplug;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.plugin.Plugin;
public class ShadList {
private File storageFile;
public HashMap<String, Integer> values = new HashMap<String, Integer>();
public ShadList(File file){
this.storageFile = file;
this.values = new HashMap<String, Integer>();
if(this.storageFile.exists() == false){
try {
this.storageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void load(){
try {
DataInputStream inputStream = new DataInputStream(new FileInputStream(this.storageFile));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
long timestamp = System.currentTimeMillis()/1000;
int timestamp2 = (int) timestamp;
String line;
while ((line = reader.readLine()) != null){
if(this.values.containsKey(line) == false){
this.values.put(line, timestamp2);
}
}
reader.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void save(){
try {
FileWriter stream = new FileWriter(this.storageFile);
BufferedWriter out = new BufferedWriter(stream);
for (Map.Entry<String, Integer> entry : values.entrySet()){
out.write(entry.getKey() + ":" + entry.getValue());
out.newLine();
}
out.close();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean contains(String value){
return this.values.containsKey(value);
}
public void add(String value, Integer timestamp2){
if(this.values.put(value, timestamp2) != null){
this.values.put(value, timestamp2);
}
}
public void remove(String value){
this.values.remove(value);
}
public HashMap<String, Integer> getValues(){
return this.values;
}
}