How to read from a list from a text file?
#1

I understand kinda how file reading works, but I can't get my script to work.

If someone joins the server with a listed IP, they will be opped, simple as that. I dont know how to read for each IP, etc.

pawn Код:
public OnPlayerConnect(playerid)
{
    GameTextForPlayer(playerid,"~r~Destruction Derby",5000,5);
    RemoveBuildingForPlayer(playerid, 11394, -2048.1641, 168.3125, 31.7344, 0.25); //Class Select
    IsPlayerJoined[playerid] = false;
   
    new File:oplist = fopen("oplist.txt");
    if(oplist)
    {
        new str[128];
        while(fread(oplist, str))
        {
            new playerIP[16];
            GetPlayerIp(playerid, playerIP, 16);
            if(strcmp(playerIP, str) == 0)
            {
                IsOp[playerid] = true;
                SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
            }
        }
        fclose(oplist);
    }
    else
    {
        print("The file \"oplist.txt\" does not exists, or can't be opened.");
    }
    return 1;
}
Reply
#2

Write this fix:

Код:
new str[128],playerIP[16];
while(fread(oplist, str))
{
    GetPlayerIp(playerid, playerIP, 16);
    switch(str[strlen(str)-2]) {
        case 13: str[strlen(str)-2] = EOS;
    }
    if(strcmp(playerIP, str) == 0) {
        IsOp[playerid] = true;
	SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
    }
}
Greekz
Reply
#3

If in every line an IP is stored without anything else such as tags etc, the code about reading and comparing is correct though I'd still not recommend file functions for such things. SQL is far better.

Also few tips: get the player's IP only once (outside of the loop) and if the IPs do match, stop the loop as there isn't any reason to keep reading from the file.
Reply
#4

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Write this fix:

Код:
new str[128],playerIP[16];
while(fread(oplist, str))
{
    GetPlayerIp(playerid, playerIP, 16);
    switch(str[strlen(str)-2]) {
        case 13: str[strlen(str)-2] = EOS;
    }
    if(strcmp(playerIP, str) == 0) {
        IsOp[playerid] = true;
	SendClientMessage(playerid, -1, "Use /ophelp to see the op guide.");
    }
}
Greekz
Doesn't seem to work.

And @Konstantinos I planned on closing the loop once I got this part working.
Reply
#5

Quote:
Originally Posted by Aerotactics
Посмотреть сообщение
Doesn't seem to work.
It should...

Well, make a print-Debug, show me what is printed...

check the ips, for me it's working fine...
Reply
#6

Can you show an example of how it's saved in oplist.txt?
Reply
#7

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
It should...

Well, make a print-Debug, show me what is printed...

check the ips, for me it's working fine...
Yeah, the debug shows it is reading correctly, but it's not familiarizing with my ip address.

list Example:

123.456.789.999
123.45.67.890
1.2.34.56

EDIT: Would it recognize my IP even if I'm connected via "localhost" or would it not be able to read my IP (it's own IP)? XD
Reply
#8

Quote:
Originally Posted by Aerotactics
Посмотреть сообщение
EDIT: Would it recognize my IP even if I'm connected via "localhost" or would it not be able to read my IP (it's own IP)? XD
If you connect through localhost it reads: 127.0.0.1
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)