Work with enum and similar things

Discussion in 'Plugin Development' started by Der_Udo, Feb 6, 2013.

Thread Status:
Not open for further replies.
  1. Hi!
    I have started with my new plugin and want to add something, which does different actions...
    It's hard to explain, so here's an example:
    Code:
    int i = blabla;
    if (i==1){
        action 1;
        return;
    }
    if (i==2){
        action 2;
        return;
    }
    if (i==3){
        action 3;
        return;
    }
    if (i==4){
        action 4;
        return;
    }
    if (i==5){
        action 5;
        return;
    }
    if (i==1){
        action 5;
        return;
    }
    A good example are the mobs:
    Every mob has another way it moves. But instead using the bad code I've showed above bukkit uses something better. I think enums.
    I just want to save an action to a player. Then I could just use (Class.executeAction(player, action);
    I just don't know how to do this...

    I want to do it like this:
    Variables store two things of a player: Action a and Object o.
    Sometimes when the action was triggered, it happens like this:
    If action == Action.PotionEffect { p.addPotionEffect(o(PotionEffect));
    if (action == Action.Damage { p.damage(o(int));

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. So what you want to know? How to create an enum type? Imho a lmgtfy question but I'm bored at the moment, so here is your enum:
    Code:
    public enum Action {
       PotionEffect,
       Damage,
       SomethingElse
    }
    You can assign values to the fields as well by appending = value. For more information about enums simply Google for "java enum".
     
    Der_Udo likes this.
  3. That's exactly what I need. Thank you very much!

    Just a last question: What's the fastest way to check the enums?
    I've seen something like :
    case Damage { action}
    case SomethingElse { action }

    Found it out! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    keensta

    Post the code you used (Switch code) so people if they ever need help on this subject again and they find this thread know how to do it. :)
     
    ohtwo likes this.
  5. Offline

    Taien

    I'll do it for him.

    Code:
    //Assuming a is a variable of type Action
    switch (a)
    {
    case Damage:
      //stuff
      break;
    case ThrowPotion:
      //stuff
      break;
    case SomethingElse: case AnotherThing: //for when you want two types or more to do the same thing, you can string them like this
      //stuff
      break;
    case default:
      //stuff
    }
    
    You need a 'break;' after each case, in order to tell it to exit the switch. You could throw one on the default(and you can elect to not have a default as well) but its unnecessary since that's the end of the switch.
     
    keensta likes this.
  6. My code looks pretty similar to this!
     
Thread Status:
Not open for further replies.

Share This Page