Is there a similar approach to the following code from Pawn in Java?

123 views Asked by At

I used to code in Pawn years ago but now I am working with other languages. This is a piece of code that allowed me to create 3d arrays with enum.

enum healthHack
{
    Float:acHealth,
    bool:hImmune,
    bool:aImmune,
    Float:acArmour,
    hcTimer,
    pTick,
    bool:afkImmune,
    bool:hasSpawned
};

new hcInfo[MAX_PLAYERS][healthHack];

Assuming the player_id is 5, MAX_PLAYERS is 500 and while accessing it, I would be able to do it like,

hcInfo[player_id][hasSpawned] = false;

hcInfo[player_id][acHealth] = 100;

I was wondering if Java has a similar approach to 3d arrays like this?

2

There are 2 answers

0
marstran On BEST ANSWER

It's better practice in Java to create a class representing a Player and then updating it's values. You could use a Map<PlayerId, Player> to get the player with a specific playerId.

public class Player {

    private String playerId;
    private double acHealth;
    private boolean hImmune;
    private boolean aImmune;
    private double acArmour;
    private int hcTimer; // What type? I'm just guessing int.
    private int pTick;
    private boolean afkImmune;
    private boolean hasSpawned;

    // Constructor
    // Getters and setters
    // toString, hashCode, equals
}

Map<String, Player> playerMap = new HashMap<>();
playerMap.add("1", player1);

Player player = playerMap.get("1");
if (player != null) {
    player.setHasSpawned(false);
    player.setAcHealth(100.0);
}
0
Narish On

C# implementation using classes and Dictionary to allow for lookups by the player id

public class Player 
{
    public int Id {get; set;} //you seem to use int here
    public double AcHealth {get; set;}
    public boolean HImmune {get; set;}
    public boolean AImmune {get; set;}
    public double AcArmour {get; set;}
    public Timer HcTimer {get; set;} //Maybe should look into System.Timers.Timer
    public int PTick {get; set;}
    public boolean AfkImmune {get; set;}
    public boolean HasSpawned {get; set;}
}

Dictionary<int, Player> players = new Dictionary<int, Player>
{
    [5] = new Player 
    {
        //init fields here
    },
    //... more players
};

Player? p1 = players[5];
if (p1 is not null)
{
    p1.HasSpawned = false;
    p1.AcHealth = 100.0;
}