How does a plugin work?

Discussion in 'Spigot Plugin Help' started by CyberDrain, May 28, 2016.

  1. The title may sounds a bit dumb, but I need to know the basics of the basics.

    In my plugin I got a local boolean "isGodMode". Whenever someone initiate the command /god, the boolean will change to true and the gamemode will be switched. Whenever this person types /god again, the plugin sets the boolean back to false and puts the player back into survival mode.

    The problem:
    If I enable god mode with the /god command, the boolean changes to true. When my friend also wants to enter god mode with /god, it will see that the boolean is already on true, so it will put ME back into survival, instead of putting him into god-mode...

    I always thought plugins did not work with shared variables and it would be seperate per player who uses the plugin.
    Anyhow, I need to know what is shared between players and what not. The main java class should be shared, but what about the other classes I got within my plugin?

    -CyberDrain
     
  2. I can offer you a solution to your plugin? I'm not quite sure how to explain this to you.
    Code (Text):
    List<Player> playersInGod = new ArrayList<Player>();

    // player does /god
    if(playersInGod.contains(player)) {
        // disable god mode
        playersInGod.remove(player);
    } else {
        // enable god mode
        playersInGod.add(player);
    }

    // entitydamageevent
    if(event.getEntityType == EntityType.PLAYER) {
        Player player = (Player)event.getEntity();
        if(playersInGod.contains(player)) {
            // cancel event, player is in god.
        }
    }

     
    Essentially, you can create new classes for each of your players, that is what the Player instance is. Every single Player of the server is stored within a Player class. You could do the same with your own plugin too. I'm guessing you didn't learn Java the normal way. Go read through the Oracle Java documents and you'll begin to understand.
     
  3. Actually my first programming experience was with plain java :p
    I just got into minecraft plugins, but you guys were indeed correct. A few new classes did the trick!
    Thanks