Solved Efficient switch statement?

Discussion in 'Plugin Development' started by Qaez, Dec 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Qaez

    Hello,

    I have a question because I am making a Wand Plugin,
    And I have different spells. But what I want to make is when a player right click, it will go to the next spell.
    I saw some methods for that:
    - Switch statement
    - Set durability, get durability
    - Set lore, get lore

    What is the most efficient way? And can you give an example?
    The wand is btw a Blaze Rod
     
  2. Offline

    Qaez

    BUMP
     
  3. Offline

    NathanWolf

    Sounds familiar! :)

    You'll want to keep a list of spells, and the index of the active spell. When they right-click, increment the index. If the index is greater than or equal to the size of your spell list, set the index to 0 (so it will wrap back around to the beginning).

    How you choose to display the active spell is up to you (lore, displayName, chat messages, etc)- but isn't really related to how you cycle the chosen spell (or switch statements, for that matter)
     
  4. Offline

    Qaez

    Ehmm can you please give an example? NathanWolf

    I saw that you have a magic wand server, what do you use for this?

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

    NathanWolf

    Assuming you want to create a separate class to manage your spells (you should), the basic idea would be something like below. Not tested or compiled, very over-simplified example.

    Friendly advice: You're mostly asking very basic Java questions at this point- this is just list iteration. It's really a good idea to study up on some Java (using external resources for beginning Java programmers) before coming here to ask more Bukkit-specific questions, like about the API or things specific to plugin creation;

    Code:java
    1.  
    2.  
    3. public class Spells {
    4. // Class properties
    5. private List<String> spells = new ArrayList<String>();
    6. private int currentSpell = 0;
    7.  
    8. // Constructor
    9. public Spells() {
    10. // Load this from config or something
    11. spells.add("Boom");
    12. spells.add("Teleport");
    13. spells.add("Arrow");
    14. }
    15.  
    16. public String getCurrentSpell() {
    17. return spells.get(currentSpell);
    18. }
    19.  
    20. public void nextSpell() {
    21. currentSpell = (currentSpell + 1) % spells.size();
    22. }
    23. }
    24.  
    25. // In your plugin, or your listener
    26. private Spells spells;
    27.  
    28. // Do this in onEnable for your plugin, or in your listener's constructor- or somewhere appropriate.
    29. @Override
    30. public void onEnable() {
    31. spells = new Spells();
    32. }
    33.  
    34. // In your listener, or plugin's, event handler for interact:
    35. spells.nextSpell();
    36. player.sendMessage(spells.getCurrentSpell());



    That's the only plugin I still maintain, Magic. It normally shows up in my profile on the left, but since it's not at the moment for some reason I'll spam a little:

    http://dev.bukkit.org/bukkit-plugins/magic/

    It started life as an hMod plugin, and the original mechanic was right-click to cycle spells, left-click to cast.

    Showing the active spell in a nice way that doesn't break immersion has always been a challenging goal for this plugin. I started with chat messages, then moved to using inventory materials as "icons" for spells- and now I use an inventory system entirely, and no longer have a spell cycling option. I rely on changing the active item's name for showing the active spell, which looks and works a lot nicer than chat messages (IMO).

    Feel free to come visit and play around :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    Qaez

    Thanks for all the help! Iterating is the only thing I dont understand hahah so thats why I asked it. NathanWolf
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page