Why isn't this code to add a node and entry working? (YAML)

Any help topics that don't fit in the current categories can go here.
Post Reply
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Why isn't this code to add a node and entry working? (YAML)

Post by offluffy »

I have these two blocks of code. I've looked through everything I can think of, and I figure the error has to be in these two blocks somewhere:

niceIp is basically the IP address with the dots replaced with underscores so it doesn't jack up the YAML file.

[syntax=java]
//Add the IP to users.yml if it isn't there already
if (!ipList.contains(niceIp)){
plugin.users.set("Address." + niceIp, new ArrayList<String>());
try{
plugin.users.save(plugin.usersFile);
}catch(IOException e){
e.printStackTrace();
}
}[/syntax]
This one just adds the node if it doesn't exist.

[syntax=java]//Add the username under an IP node in users.yml
if (plugin.users.getStringList("Address").contains(niceIp) && !(plugin.users.getStringList("Address." + niceIp).contains(niceName))){
List<String> ipUsers = plugin.users.getStringList("Address." + niceIp);
ipUsers.add(niceName);
plugin.users.set("Address." + niceIp, ipUsers);
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}[/syntax]
This one adds an entry to the node the previous one created, or adds another entry to the node if it already exists, if the entry doesn't already exist.

The problem I'm having is that the plugin isn't creating the new node or inserting any new values. I'm not sure why, I've looked over it countless times @.@

For reference, the file it's writing to is structured like this:

[syntax=text]User:
<username>:
ips:
- <IP Address>
<username>:
ips:
- <IP Address>
Address:
<IP Address>:
- <username>
<IP Address>:
- <username>[/syntax]
IP addresses in the Address section have underscores instead of dots, i.e., 127_0_0_1

P.S. - The YAML syntax highlighting seems to have some alignment issues XD
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Why isn't this code to add a node and entry working? (YA

Post by jacek »

Hmm, well if there is no error being printed then it must mean that the block is not running. So are you sure that your if statements are correct ? You could add an else block to each one that just prints something to check this.

Could you post the full file ?
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: Why isn't this code to add a node and entry working? (YA

Post by offluffy »

Sure, this is the whole file. I'll try and else iteration and see if that catches anything usefule XD
[syntax=java]package me.offluffy.blip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;

public class LoginListener implements Listener {

private Blip plugin;

public LoginListener(Blip plugin){
this.plugin = plugin;
}

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerLogin(PlayerLoginEvent event){

//Get the player's name
Player playerName = event.getPlayer();
String niceName = playerName.getName().toLowerCase().replace(".", "_");

String ip = "";

//Get the player's IP address
if (event.getResult().equals(Result.ALLOWED)){
ip = event.getKickMessage();
}

String niceIp = ip.replace(".", "_");

String eventPlayer = event.getPlayer().getName();

//Check if it's a standard name if enforce-naming-standards is enabled
if (plugin.config.getBoolean("blip.enforce-naming-standards")){
Pattern pattern = Pattern.compile("[A-Za-z0-9_]{1,16}");
boolean matcher = pattern.matcher(eventPlayer).matches();

if(!matcher){
event.setKickMessage("Please come back when you have a valid username!");
event.setResult(Result.KICK_OTHER);
plugin.log.warn(playerName.getName() + " was kicked: Invalid Name");
return;
}
}

//Check if the user has already been banned by another plugin or by the server (does not pass in an IP if so)
if (ip.length() < 2){
plugin.log.info(event.getPlayer().getName() + " is already banned by the server or another plugin.");
return;
}

//Logs that a user logged in (irregardless whether they are banned)
//TODO Alter this log entry, add a new one after succesfull login.
plugin.log.info(playerName.getName() + " logged in from " + ip);

//Get list of Users and IPs in users.yml
Set<String> userList = plugin.users.getConfigurationSection("User").getKeys(false);
Set<String> ipList = plugin.users.getConfigurationSection("Address").getKeys(false);

//Add the user to users.yml if it isn't there already
if (!userList.contains(niceName)){
plugin.users.set("User." + niceName, new ArrayList<String>());
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}

//Add the IP to users.yml if it isn't there already
if (!ipList.contains(niceIp)){
plugin.users.set("Address." + niceIp, new ArrayList<String>());
try{
plugin.users.save(plugin.usersFile);
}catch(IOException e){
e.printStackTrace();
}
}

//Add the user's IP under their node in users.yml
if(!(plugin.users.getStringList("User." + niceName + ".ips").contains(ip))){
List<String> userIps = plugin.users.getStringList("User." + niceName + ".ips");
userIps.add(ip);
plugin.users.set("User." + niceName + ".ips", userIps);
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}

//Add the username under an IP node in users.yml
if (plugin.users.getStringList("Address").contains(niceIp) && !(plugin.users.getStringList("Address." + niceIp).contains(niceName))){
List<String> ipUsers = plugin.users.getStringList("Address." + niceIp);
ipUsers.add(niceName);
plugin.users.set("Address." + niceIp, ipUsers);
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}

//Get list of banned users ans IPs
List<String> bannedUsers = plugin.banned.getStringList("Users");
List<String> bannedIps = plugin.banned.getStringList("Addresses");

//Check if the username is banned
if (bannedUsers.contains(niceName)){
event.setKickMessage(plugin.config.getString("blip.banmsg"));
event.setResult(Result.KICK_OTHER);
if (plugin.config.getBoolean("blip.autoban-enabled")){
if (!bannedIps.contains(ip) && !plugin.config.getStringList("blip.exemptips").contains(ip)){
bannedIps.add(ip);

plugin.banned.set("Addresses", bannedIps);

try{
plugin.banned.save(plugin.bannedFile);
}catch (IOException e){
e.printStackTrace();
}
}
}
}

//Check if the IP is banned
if (bannedIps.contains(ip)){
event.setKickMessage(plugin.config.getString("blip.banmsg"));
event.setResult(Result.KICK_OTHER);
if (plugin.config.getBoolean("blip.autoban-enabled")){
if (!bannedUsers.contains(niceName) && !plugin.config.getStringList("blip.exemptusers").contains(niceName)){
bannedUsers.add(niceName);

plugin.banned.set("Users", bannedUsers);

try{
plugin.banned.save(plugin.bannedFile);
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
}
[/syntax]

It's commented up pretty well at least to the point what I expected each block to do XD
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: Why isn't this code to add a node and entry working? (YA

Post by offluffy »

Well, I finally found the error! \o/

A friend unintentionally helped me when he logged in when I was looking through the else iterations. XD
But as it so happens, in the if loop that was checking if the IP node was already there, previously this:
[syntax=java]//Add the username under an IP node in users.yml
if (plugin.users.getStringList("Address").contains(niceIp) && !(plugin.users.getStringList("Address." + niceIp).contains(niceName))){
List<String> ipUsers = plugin.users.getStringList("Address." + niceIp);
ipUsers.add(niceName);
plugin.users.set("Address." + niceIp, ipUsers);
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}
[/syntax]

It was trying to use the .getStringList("Address"), when it should have been .getConfigurationSection("Address"), since it's nodes immediately under that rather than entries. So now I have this:
[syntax=java]if (plugin.users.getConfigurationSection("Address").contains(niceIp)){
if (!(plugin.users.getStringList("Address." + niceIp).contains(niceName))){
List<String> ipUsers = plugin.users.getStringList("Address." + niceIp);
ipUsers.add(niceName);
plugin.users.set("Address." + niceIp, ipUsers);
try{
plugin.users.save(plugin.usersFile);
}catch (IOException e){
e.printStackTrace();
}
}
else{
plugin.log.severe("Did not pass - Line 135");
}
}[/syntax]
On a latter note, I guess I could add .getKeys(false) so there isn't a lotta unnecessary information in the Set. If that doesn't utterly break everything again XD
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Why isn't this code to add a node and entry working? (YA

Post by jacek »

Ah, well I would probably have never spotted that !

And I think you could use

[syntax=java]plugin.users.getConfigurationSection("Address").getKeys(false).contains(niceIp)[/syntax]
as you said, not sure if it's really going to make a difference in terms of performance.

anyway, good that you got it working :D
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: Why isn't this code to add a node and entry working? (YA

Post by offluffy »

Thanks again XD
Post Reply