Magic spell Loop

Discussion in 'Plugin Development' started by Norbu10, Mar 24, 2014.

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

    Norbu10

    All the things i try dont work! I just want a list like :
    FireBeam
    IceBeam
    WaterBeam
    and that if a player right clicks a woodensword he scrolls trough it and if it hits the end and the player right clicks again it will start over!
    And it will say what spell selected

    If the player leftclicks (swings with the sword) it will activates the Spell
     
  2. Offline

    Minnymin3

    Some context would be helpful... Some of your current code maybe?
     
  3. Offline

    Norbu10

    BUMP!
    Cant get it because im not on my pc
     
  4. Offline

    Konkz

  5. Offline

    Norbu10

  6. Offline

    Konkz


    Your thread seems like a request, sorry - I misread it.

    I'd do it by making a HashMap with String and Integer, then an array showing how many spells there are.

    Add 1 every time he right clicks and then do checks

    if(selectedint.contains(5)){
    //fire
    }

    etc. When the int gets to your arrays size set it to 1 again.

    My example won't work, I forgot how to get an int from a HashMap and I am on phone :p
     
  7. Offline

    Norbu10

    I mean why wouldt this WORK!!!!
    Code:java
    1. public HashMap<Player, Integer> Test = new HashMap<Player, Integer>();
    2. @EventHandler
    3. public void MagicTest1 (PlayerInteractEvent event){
    4. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    5. Player p = event.getPlayer();
    6. if (p.getItemInHand().getType() == Material.ENCHANTED_BOOK){
    7. Test.put(p, Test.get(p)+1);
    8. p.sendMessage("Test = "+Test.get(p));
    9. }
    10. }
    11. }
     
  8. Norbu10 What doesn't work exactly? Do you get an error? Does nothing happen? Are you sure any of that code is executed?

    Also, it's generally better to store the player's name rather than the Player object.
     
  9. Offline

    Norbu10

    Nothing happens
     
  10. Norbu10 Then check if the method executes with debug code. Have you registered the event?
     
  11. Offline

    Norbu10

    Yes
     
  12. Offline

    Mysticate

    Don't compare material types with ==. use .equals(Material....)
     
  13. Norbu10 Try this, and let me know which numbers are said in the console
    Code:
    public HashMap<Player, Integer> Test = new HashMap<Player, Integer>();
        @EventHandler
        public void MagicTest1 (PlayerInteractEvent event){
            System.out.println("1");
            if (event.getAction() == Action.RIGHT_CLICK_AIR){
                Player p = event.getPlayer();
                System.out.println("2");
                if (p.getItemInHand().getType() == Material.ENCHANTED_BOOK){
                System.out.println("3");
                Test.put(p, Test.get(p)+1);
                p.sendMessage("Test = "+Test.get(p));
                }
            }
        }
    EDIT: Mysticate Why can't you compare with ==?
     
  14. Offline

    Norbu10

    i get 1 2 3
     
  15. Okay, so what doesn't work exactly? Norbu10
     
  16. Offline

    Norbu10

    Idk i dont get a message so it has to have a problem with :
    Code:java
    1. Test.put(p, Test.get(p)+1);
     
  17. Norbu10 Do you happen to get any errors in console? Also, you never check if Test contains the player - you just assume that it does (which will most likely cause problems)
     
  18. Offline

    Konkz

    Print the hashmap out then and see if you got added to it.

    Also
    So instead of doing "Player, Integer" do "String, Integer". Then instead of "p" do "p.getName()".
     
  19. Offline

    d3v1n302418

    Norbu10
    He's correct, before increasing it by 1 make sure they are in the HashMap. Code example:
    Code:java
    1. if(spells.containsKey(p.getName()){
    2. spells.put(p.getName(), spells.get(p.getName())+1);
    3. } else {
    4. spells.put(p.getName(), 1);
    5. }
     
  20. Offline

    Norbu10

    I think i made a stupid mistake (It wont say what spell it is It only says :
    Spell Selected
    ------------------------)
    Code:java
    1. public HashMap<String, Integer> Test = new HashMap<String, Integer>();
    2. @EventHandler
    3. public void MagicTest1 (PlayerInteractEvent event){
    4. System.out.println("1");
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    6. Player p = event.getPlayer();
    7. System.out.println("2");
    8. if (p.getItemInHand().getType() == Material.ENCHANTED_BOOK){
    9. System.out.println("3");
    10. if(Test.containsKey(p.getName())){
    11. if (Test.get(p.getName()) == 2){
    12. Test.put(p.getName(), 1);
    13. } else {
    14. Test.put(p.getName(), Test.get(p.getName())+1);
    15. }
    16. } else {
    17. Test.put(p.getName(), 1);
    18. }
    19. p.sendMessage(ChatColor.GOLD+" Spell Selected:");
    20. p.sendMessage(ChatColor.GRAY+""+ChatColor.STRIKETHROUGH+"---------------");
    21. if(Slot.get(p.getName()) == 1){
    22. p.sendMessage(ChatColor.GREEN+"FireBeam");
    23. } else if(Slot.get(p.getName()) == 2){
    24. p.sendMessage(ChatColor.GREEN+"WaterBeam");
    25. }
    26. }
    27. }
    28. }


    SOLVED!
    Dont know how to change the status :\

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page