I've been trying to make a nickname plugin.
what i expected my code to do:
attempt to read a csv file line-by-line
check if player who called the command is already on file
if they are: replace their old nickname with new one from arguments by swapping the current line with a new player, nickname line
append line to StringBuilder
after checking all lines if player wasn't on file append new entry
write over file with StringBuilder converted to string
however what happens is:
skips over line reading/writing
appends "new entry"
writes over entire file with just the single "new entry"
it skips the loop (and the code in it) regardless of if the player is or isn't already on file, or if/how many entries i manually insert beforehand.
ive tried to get it to not skip in any capacity by:
using a while loop instead of the for loop ( while ((line = reader.readline()) != null) ),
removing the if from inside the loop,
using a plain text file instead of a csv,
swapping double backslash "\\" with single forward slash "/" in file path string,
and probably some other stuff but i can't remember because ive literally been losing sleep trying to fix this or get some idea of why.
from my searching online, I think my code "should" "theoretically" work,
but ive only been learning java, mod making, and programming in general for the past week and a bit so its half bashing my head against a wall and half thinking I have gained some understanding only for it not to be any solution to this problem.
also please be gentle.
btw the system outs were for troubleshooting and wouldn't be final
the actual code:
I know its generally a good idea to give commands their own seperate classes, however this plugin will only have two commands and they will share alot of variables so I thought it could work fine.
what i expected my code to do:
attempt to read a csv file line-by-line
check if player who called the command is already on file
if they are: replace their old nickname with new one from arguments by swapping the current line with a new player, nickname line
append line to StringBuilder
after checking all lines if player wasn't on file append new entry
write over file with StringBuilder converted to string
however what happens is:
skips over line reading/writing
appends "new entry"
writes over entire file with just the single "new entry"
it skips the loop (and the code in it) regardless of if the player is or isn't already on file, or if/how many entries i manually insert beforehand.
ive tried to get it to not skip in any capacity by:
using a while loop instead of the for loop ( while ((line = reader.readline()) != null) ),
removing the if from inside the loop,
using a plain text file instead of a csv,
swapping double backslash "\\" with single forward slash "/" in file path string,
and probably some other stuff but i can't remember because ive literally been losing sleep trying to fix this or get some idea of why.
from my searching online, I think my code "should" "theoretically" work,
but ive only been learning java, mod making, and programming in general for the past week and a bit so its half bashing my head against a wall and half thinking I have gained some understanding only for it not to be any solution to this problem.
also please be gentle.
btw the system outs were for troubleshooting and wouldn't be final
the actual code:
Java:
onCommand(){
Player player = (Player) sender
if(args[0].contains("set"))
try{
String nickname = args[1]
String playerNickname = player.getName() + nickname
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, false));
BufferedReader reader = new BufferedReader(new FileReader(filePath));
System.out.println("searching for player in nickname registry");
StringBuilder stringBuilder = new StringBuilder();
boolean playerEntryReplaced = false;
for(String line = reader.readLine(); line != null; line = reader.readLine()){ //I'm pretty sure the problem is here
System.out.println("current line: " + line);
if(line.contains(playerName)){
System.out.println("player entry exists; replacing nickname");
stringBuilder.append(playerNickname).append('\n');
playerEntryReplaced = true;
} else{
stringBuilder.append(line);
}
System.out.println(line);
}
if(!playerEntryReplaced){
System.out.println("player entry doesn't exist; creating entry");
stringBuilder.append(playernickname).append('\n');
}
String fileAsString = stringBuilder.toString();
System.out.println("nickname registry contains: " + fileAsString);
reader.close();
writer.write(fileAsString);
writer.close();
player.sendMessage("your nickname has been updated to " + nickname);
}catch(IOException e){
System.out.println("player nickname write error");
e.printStackTrace();
}
}
Last edited: