Posted 23 hours ago23 hr 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 itCommand.Find("award").Use(Player.Console, "give "+p.name+" "+myAward);}
22 hours ago22 hr You're missing a lot of code that's required for a custom command, for instance the command class itself. You can use /cmdcreate to generate a working template that you can change.
20 hours ago20 hr 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 detailsThe logs show this longer version:Type: NullReferenceExceptionSource: MCGalaxy_Message: Object reference not set to an instance of an objectTrace: at MCGalaxy.Extensions.CaselessContains (System.Collections.Generic.List`1[T] items, System.String value) [0x00000] in <f309205a56554c158910ca5ef34fa632>:0at CmdGiveinsane.Use (MCGalaxy.Player p, System.String message) [0x0000c] in <5bd5b930cfd5436dbb6e2f13c1219d68>:0at MCGalaxy.UI.UIHelpers+<>c__DisplayClass3_0.<HandleCommand>b__0 () [0x00000] in <f309205a56554c158910ca5ef34fa632>:0Please help!BTW, source code for reference attached CmdGiveInsane.cs Edited 20 hours ago20 hr by RainZhang Added file for reference
19 hours ago19 hr 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 beforenull, if the player has never gotten an award beforeYou 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
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);
}