RTB Preffs

Author Topic: RTB Preffs  (Read 4264 times)

Do you think this would work or am I doing it completely wrong

Code: [Select]
if(isFile("Add-Ons/System_ReturnToBlockland/RTBR_ServerControl_Hook.cs"))
{
if(!$RTB::RTBR_ServerControl_Hook)
{
exec("Add-Ons/System_ReturnToBlockland/RTBR_ServerControl_Hook.cs");
}
RTB_registerPref("Super Admin","ClearGhostBricks","OAA::superadminonly","bool","Server_Clearghostbricks",1,0,0);
RTB_registerPref("Admin","ClearGhostBricks","OAA:adminonly","bool","Server_Clearghostbricks",1,0,0);
}
else
{
$OAA::superadminonly = 1;
$OAA::adminonly = 0;
}
function serverCmdClearGhostBricks(%client)
{
if(!%client.isSuperAdmin)
return;

%count = clientGroup.getCount();

for(%i = 0; %i < %count; %i++)
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}
function serverCmdClearGhostBricks(%client)
{
if(!%client.isAdmin)
return;

%count = clientGroup.getCount();

for(%i = 0; %i < %count; %i++)
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}
« Last Edit: August 02, 2014, 06:51:55 PM by Ducky duck »

I'm pretty sure the RTB pref just changes the global variable, you still need to check what it's set to in the code so it knows to check if they are admin or super admin. When you execute two functions named the same thing, the second one is the one that works. RTB prefs can't pick which one to use.

The duplicator add-on does this admin only RTB pref check, you should look at that.

if $adminonly and !isadmin
{
    getforgetedm8
}

There are many problems with this - the main one being that you attempt to define the same function twice.

I prolly just made it worse but here it goes

Code: [Select]
if(isFile("Add-Ons/System_ReturnToBlockland/RTBR_ServerControl_Hook.cs"))
{
if(!$RTB::RTBR_ServerControl_Hook)
{
exec("Add-Ons/System_ReturnToBlockland/RTBR_ServerControl_Hook.cs");
}
RTB_registerPref("Super Admin","Clearghostbricks","OAA::superadminonly","bool","Server_clearghostbricks",1,0,0);
RTB_registerPref("Admin","Clearghostbricks","OAA::adminonly","bool","Server_clearghostbricks",1,0,0);
}
else
{
$OAA::superadminonly = 1;
$OAA::adminonly = 0;
}
function serverCmdClearGhostBricks(%client)
{
if(!%client.isAdmin)
return;


%count = clientGroup.getCount();

for(%i = 0; %i < %count; %i++)
if $adminonly and !isadmin
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}

function serverCmdClearGhostBricks(%client)
{
if(!%client.isAdmin)
return;


%count = clientGroup.getCount();

for(%i = 0; %i < %count; %i++)
if $superadminonly and !issuperadmin
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}

Why are you trying to write the same function twice instead of adding a check for $OAA::blahAdmin? And I'm not 100% sure on this but wouldn't RTB_registerPref("AdminCheck","ClearGhostBricks","OAA::adminLevel","list Admin " @ $adminLevel::Admin @ " SuperAdmin " @ $adminLevel::SuperAdmin); work better than two different prefs?

I don't know why you're still writing two functions at this point, I have already told you twice that only the second one is going to work. Take a look at how the duplicator registers the admin only pref.

RTB_registerPref("Admin Only", "Duplorcator", "Pref::Duplorcator::AdminOnly", "bool", "Tool_Duplicator", 0, 0, 0);


if($Pref::Duplorcator::AdminOnly && !%client.isAdmin)
   {
      commandToClient(%client,'centerPrint',"<color:99AAAA>You must be an \c4Admin<color:99AAAA> to use the \c4Duplorcator",3);
      return;
   }

Right above when you use the duplicator command it checks if the pref is equal to 1, which would mean it is admin only, and it checks if the player using the command isn't admin. If both are true then it runs the code, and the code tells the player that they have to be an admin and then returns. Returning basically stops the code from running, so anything you put after that won't run.

Sorry for the text wall, but I tried to explain this as simply as I could. Please tell me if you didn't understand part of it. (Anyone else: please tell me if I got any of this wrong :P)

On RTB prefs:
As Thorfin25 stated, you can also combine the Super Admin and Admin checks into one variable using a List pref. General format:

RTB_registerPref("[GUIName]", "[GUICategory]", "[PrefName]", "list [Opt0] 0 [Opt1] 1 [Opt2] 2 [Opt3] 3...", "[ModName], 1, 0, 0);

where [Opt0], [Opt1], etc. are the names of your options. It's possible to use more than two options, so let's add a Host-Only setting. (Unfortunately, I don't remember how to check if a client is the host - you'll have to ask someone else.) In this case, the code should be something like:

RTB_registerPref("Restriction Level","ClearGhostBricks","OAA::AdminLevel","list Admin 1 SuperAdmin 2 Host 3","Server_Clearghostbricks",1,0,0);

If this is used, then $OAA:AdminLevel will be 1 for admins and above, 2 for super admins, and 3 for host-only. This is set with a dropdown list of options (of course!) in the RTB prefs menu. It'd probably be a good idea to make sure only the host can change it, but I don't remember how to do that. It's one of those last 3 arguments ('1, 0, 0').


On binary logic:
You don't need to define two functions at all (if you want to, there's more on that topic later), or even use two Ifs. Basically: '&&' means 'And', '||' means 'Or'. The P in PEMDAS applies to these - &&s and ||s in parentheses are used first. Let's combine the check for admin and the check for super admin, and tell the client if they can't clear ghost bricks.

if((!%client.isAdmin && $OAA:AdminLevel == 1) || (!%client.isSuperAdmin && $OAA:AdminLevel == 2)) {
    messageClient(%client, "<color:00ffff>Server<color:ffffff>: You don't have permission to clear ghost bricks.");
    return;
}
//Rest of the function.

Or, a more readable version, with the way it'd be written in english:
if(                          //IF
    (!%client.isAdmin          //the client is NOT admin or super admin
    && $OAA:AdminLevel == 1)   //AND the admin restriction level is '1'...
    ||                       //OR,
    (!%client.isSuperAdmin     //if the client is NOT super admin (but might be a normal admin)
    && $OAA:AdminLevel == 2)   //AND the admin restriction level is '2'...
) {                          //THEN
    messageClient(%client, "<color:00ffff>Server<color:ffffff>: You don't have permission to clear ghost bricks.");
                               //tell the client they can't clear ghost bricks
    return;                    //and ignore the rest of the function.
}                            //Otherwise, if the client met the restrictions, continue to
                             //the rest of the function.



On having two 'function serverCmdClearGhostBricks's:
As everyone's saying, defining a function 'only works once'. Every time you make a function with the same name, all previous times go ignored by Blockland. For example, let's say I want to make a function to add two numbers together.

AddNumbers.cs:
function AddThese(%a, %b) {return %a ++;}

Upon testing, I notice that the function doesn't do what I want it to at all! I forgot what ++ is for - it just adds 1 to a variable. AddThese(%a, %b) completely ignores %b and just adds 1 to %a - AddThese(2, 9) is 3 instead of 11! Instead of changing the function, I type a new one before it:

AddNumbers.cs:
function AddThese(%a, %b) {return %a + %b;}
function AddThese(%a, %b) {return %a++;}


To my surprise, the function still doesn't work! This is because only the second one - the last to appear in the code, in this case - is used. The line 'function AddThese(%a, %b) {return %a + %b;}' does absolutely nothing, in this case.


On Packages and 'parent::':
There is a way to define a function twice and have it do both things. You need to use a package and parent the older function.
Let's say I want a function that makes the console say 'Hi!', aptly named 'SayHi'.

SayHi.cs:
function SayHi() {
    echo("Hi!");
}


Easy enough! But what if I also want to make it say 'How are you doing?' This should be easy - but the catch is, I absolutely need it in a separate function (to keep it organized or something).
THIS WILL NOT WORK:

SayHi.cs:
function SayHi() {
    echo("Hi!");
}
function SayHi() {
    echo("How are you doing?");
}


As above, Blockland entirely ignores the first function, using the second one instead. To fix this, we first need to put the second function in a package so it doesn't override the first...

SayHi.cs:
function SayHi() {
    echo("Hi!");
}
package AlsoAskHow { //everything past this line is in a "package" named "AlsoAskHow"
    function SayHi() {
        echo("How are you doing?");
    }
}; //the package "AlsoAskHow" stops here
echo("SayHi.cs was executed!"); //this line is not part of it and will always run


Note that a package differs from a function: it always has a {, but not (), after its name, and always ends with a };. Once the package is created, it can be used to switch between both SayHi()s.
-Using ActivatePackage(AlsoAskHow); will make SayHi(); result in "How are you doing?".
-Using DeactivatePackage(AlsoAskHow); will make SayHi(); result in "Hi!".
Basically - a package is used to 'enable' or 'disable' pieces of your code whenever you want to do so. (All new packages are 'disabled' by default.) The problem with this is that the console still only says either one line or the other. To use both of them, we need to tell the function in the package to use the function outside the package.

Blockland considers the function outside of the package to be the "original" function. We can use this to refer to it from the one inside the package, like so:

function SayHi() {
    echo("Hi!");
}
package AlsoAskHow {
    function SayHi() {
        parent::SayHi(); //this basically means "use the original SayHi here"
        echo("How are you doing?");
    }
};


Please note that packages are very useful in certain situations, but SHOULD NOT be used to tack pieces onto one function like this. You can add that code to the function itself just as easily.
Edit: Whoops, apparently there's an explanation of why packages are useful here and I didn't even notice it.
« Last Edit: August 03, 2014, 01:44:18 PM by ThinkInvisible »

-snip-
That was better explained out of all of them, gj and thankyou.

Thankyou everyone else too/

I forgeted it up again :c

Code: [Select]
RTB_registerPref("Restriction Level","ClearGhostBricks","OAA::AdminLevel","list Admin 1 SuperAdmin 2","Server_Clearghostbricks",1,0,0);
function serverCmdClearGhostBricks(%client)
{
if(
    (!%client.isAdmin
    && $OAA:AdminLevel == 1)
    ||
    (!%client.isSuperAdmin
    && $OAA:AdminLevel == 2)
) {
    messageClient(%client, "<color:00ffff>Server<color:ffffff>: You don't have permission to clear ghost bricks.");

    return;
}
for(%i = 0; %i < %count; %i++)
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}

I forgeted it up again :c

Code: [Select]
RTB_registerPref("Restriction Level","ClearGhostBricks","OAA::AdminLevel","list Admin 1 SuperAdmin 2","Server_Clearghostbricks",1,0,0);
function serverCmdClearGhostBricks(%client)
{
if(
    (!%client.isAdmin
    && $OAA:AdminLevel == 1)
    ||
    (!%client.isSuperAdmin
    && $OAA:AdminLevel == 2)
) {
    messageClient(%client, "<color:00ffff>Server<color:ffffff>: You don't have permission to clear ghost bricks.");

    return;
}
for(%i = 0; %i < %count; %i++)
{
serverCmdCancelBrick(clientGroup.getObject(%i));
}
}
The global variables need two :: not one.
The second argument of message client should be '' (notice, not a quotation ).

The second argument of message client should be '' (notice, not a quotation ).
Why should it? I thought you were supposed to use "" for most strings. The only functions that need '' that I can think of are player.playThread, commandToServer, and commandToClient.

The function for messaging a client is messageClient(Target,Sound,Message); So you need it to be messageClient(%client,'',"Blah blah message");

Why should it? I thought you were supposed to use "" for most strings. The only functions that need '' that I can think of are player.playThread, commandToServer, and commandToClient.
You are correct on the using " " for strings - however, in this case, you use ' ' because it's a tagged string. (Pretty sure)