Jump to content

MCGalaxy Custom Command Compile Error

Posted

I'm trying to use /compile on a custom command I made in MCGalaxy, and when I try to compile the below code(C#), it shows this error:

Error #CS1525 on line 3 - Unexpected symbol 'void', expecting 'class', 'delegate', 'enum', interface', 'partial', 'ref', or 'struct'

Code:

using MCGalaxy.Modules.Awards;

public override void Use(Player p, string message)

{

const string myAward = "Insane";

bool hasAwardAlready = PlayerAwards.Get(p.name).CaselessContains(myAward);

if (hasAwardAlready) {

p.Message("You have already claimed this award!");

return;

// When we write return, that will quit running the command so that the code below doesn't run

}

//Give the player the award if they haven't already gotten it

Command.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);

}

Solved by Goodly

Go to solution

Featured Replies

  • Author

Now there's this error when using /giveinsane [user]: !!!Error (Object reference not set to an instance of an object) - See Logs tab for more details

The logs show this longer version:

Type: NullReferenceException

Source: MCGalaxy_

Message: Object reference not set to an instance of an object

Trace: at MCGalaxy.Extensions.CaselessContains (System.Collections.Generic.List`1[T] items, System.String value) [0x00000] in <f309205a56554c158910ca5ef34fa632>:0

at CmdGiveinsane.Use (MCGalaxy.Player p, System.String message) [0x0000c] in <5bd5b930cfd5436dbb6e2f13c1219d68>:0

at MCGalaxy.UI.UIHelpers+<>c__DisplayClass3_0.<HandleCommand>b__0 () [0x00000] in <f309205a56554c158910ca5ef34fa632>:0

Please help!

BTW, source code for reference attached

CmdGiveInsane.cs

Edited by RainZhang
Added file for reference

  • Solution

When you see "Object reference not set to an instance of an object", that means it's trying to get the value of a variable in your code, but because that variable is null, it fails to find anything.

The issue is that PlayerAwards.Get(p.name) can give you either:

  • List<string>, a list of 0 or more awards that the player has got before

  • null, if the player has never gotten an award before

You cannot call .CaselessContains on a null variable because that function requires a valid string to run. The solution is not to call PlayerAwards.Get and CaselessContains in the same line, so that we can check if the list is null first.
By the way, please make sure you indent the code (tab or four spaces) every time there is an open bracket {, otherwise the code will become extremely difficult to understand.
An example of fixes is attached.

CmdGiveInsane.cs

READ THE RULES BEFORE YOU POST

Create an account or sign in to comment