Andrew Eggers
Andrew Eggers

Reputation: 31

The left-hand side of an assignment must be a variable

I have an issue I believe it may be a simple fix but I am currently stuck.

I have tried looking around but couldn't find anything helpful.

This is my error: The left-hand side of an assignment must be a variable

Code error picture here aswell

This is my code:

} else if (token.equals("character-rights")) {
        player.getRights().ordinal() = Integer.parseInt(token2);


    public enum PlayerRights {

    PLAYER(true, true, true, true, false, 60),
    MODERATOR(true, true, true, true, false, 0),
    ADMINISTRATOR(false, false, false, false, false, 0),
    DEVELOPER(true, true, true, true, true, 0),
    DONATOR(true, true, true, true, true, 10);

    private boolean canTrade, canDuel, canPk, canSell, debugMode;
    private int yellTimer;

    private PlayerRights(boolean canTrade, boolean canDuel, boolean canPk, boolean canSell, boolean debugMode, int yellTimer) {
        this.canTrade = canTrade;
        this.canDuel = canDuel;
        this.canPk = canPk;
        this.canSell = canSell;
        this.debugMode = debugMode;
        this.yellTimer = yellTimer;
    }

    private static final ImmutableSet<PlayerRights> STAFF = Sets.immutableEnumSet(ADMINISTRATOR, DEVELOPER);
    private static final ImmutableSet<PlayerRights> MEMBERS = Sets.immutableEnumSet(MODERATOR, ADMINISTRATOR, DEVELOPER, DONATOR);
    private static final ImmutableSet<PlayerRights> NORMAL = Sets.immutableEnumSet(PLAYER, DONATOR);

    public boolean isStaff() {
        return STAFF.contains(this);
    }

    public boolean isMember() {
        return MEMBERS.contains(this);
    }

    public boolean isPlayer() {
        return NORMAL.contains(this);
    }

    public boolean canTrade() {
        return canTrade;
    }

    public boolean canDebug() {
        return debugMode;
    }

    public boolean canSell() {
        return canSell;
    }

    public boolean canDuel() {
        return canDuel;
    }

    public boolean canPk() {
        return canPk;
    }

    public int getYellTimer() {
        return yellTimer;
    }

}

and

public PlayerRights playerRights;

public PlayerRights getRights() {
    return playerRights;
}

The top code is where the error takes place. I would really appreciated some help, would make my day.

Upvotes: 1

Views: 1368

Answers (2)

Ossin Java guy
Ossin Java guy

Reputation: 365

You need to add the following method to the class (Player?) that contains public PlayerRights getRights() :

public void setRights(PlayerRights rights) {
  this.playerRights = rights;
}

Now, call this setter method in the line you have in the screen shot:

player.setRights(PlayerRights.values()[Integer.parseInt(token2)];

Be ready to catch an array-out-of-bounds exception for wrong input.

Upvotes: 0

Sweeper
Sweeper

Reputation: 273805

Enums are immutable. You need to reassign a new enum value instead of writing things like

myEnum.ordinal() = 0;

You have to write a switch statement to check what Integer.parseInt(token2) is, and use the setRights method.

switch (Integer.parseInt(token2)) {
    case 0:
        player.setRights(PlayerRights.PLAYER);
        break;
    case 1:
        player.setRights(PlayerRights.MODERATOR);
        break;
    case 2:
        player.setRights(PlayerRights.ADMINISTRATOR);
        break;
    case 3:
        player.setRights(PlayerRights.DEVELOPER);
        break;
    case 4:
        player.setRights(PlayerRights.DONATOR);
        break;
}

If your player class does not have a setRights method, you should probably add one.

Upvotes: 2

Related Questions