Linking effects with an items given name.

Discussion in 'Plugin Development' started by tommyhoogstra, Sep 13, 2013.

Thread Status:
Not open for further replies.
  1. No, everywhere in your PlayerPickupItem handler :p

    line 6 and 27 of your code, for example

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

    tommyhoogstra

    When you say everywhere, do you actually mean everywhere :s
    Ok, So I put it inside the if statements on line 27 and 6.
    Nothing happens upon dropping and re picking the item
     
  3. Add some debug output after line 5 to see if it actually goes through there
     
  4. Offline

    tommyhoogstra

    PeterK so upon dropping the item, the effect stays (which is normal at the moment) and when I pickup the item, it will disable the effects.
    And yeah, I am only confused because I spent too long on visual basic and c# syntax :s
     
  5. Can I see the full code please? ;P
     
  6. Offline

    tommyhoogstra

    When you say full code, do you mean the whole thing or just the pickup event section.
    Because this will be one full page if its the whole thing :)

    PeterK
    Whole thing: http://pastebin.com/9b1ZbH8k
    PickUp:
    Code:java
    1. @EventHandler
    2. public void onItemPickup(PlayerPickupItemEvent event){
    3. Player player = (Player) event.getPlayer();
    4. ItemStack[] playerArmor = player.getInventory().getArmorContents();
    5. if( player.getInventory().firstEmpty() != player.getInventory().getHeldItemSlot() ) return;
    6.  
    7. event.getItem().getItemStack();
    8. if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Zeroxes Doombringer") )
    9. {
    10. event.getItem().getItemStack();
    11. if( getCustomName(playerArmor[3]) != null && getCustomName(playerArmor[3]).equalsIgnoreCase("Zeroxes Bone Visage")
    12. && getCustomName(playerArmor[2]) != null && getCustomName(playerArmor[2]).equalsIgnoreCase("Zeroxes Shadow Plate")
    13. && getCustomName(playerArmor[1]) != null && getCustomName(playerArmor[1]).equalsIgnoreCase("Zeroxes Platelegs")
    14. && getCustomName(playerArmor[0]) != null && getCustomName(playerArmor[0]).equalsIgnoreCase("Zeroxes Battle Boots") )
    15. {
    16. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE,1));
    17. player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Zerox");
    18. }
    19. else // Not wearing the kit or not holding item
    20. {
    21. player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    22. }
    23.  
    24. //
    25. // End Zerox Set
    26. //
    27. }
    28. else{ player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE); }
    29. // event.getItem().getItemStack();
    30. if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Tathalyns Windshear") ) {
    31. event.getItem().getItemStack();
    32. if( getCustomName(playerArmor[3]) != null &&
    33. getCustomName(playerArmor[3]).equalsIgnoreCase("Tathalyns Hawk eye")
    34. && getCustomName(playerArmor[2]) != null &&
    35. getCustomName(playerArmor[2]).equalsIgnoreCase("Tathalyns Fleece")
    36. && getCustomName(playerArmor[1]) != null &&
    37. getCustomName(playerArmor[1]).equalsIgnoreCase("Tathalyns Spiked Stockings")
    38. && getCustomName(playerArmor[0]) != null &&
    39. getCustomName(playerArmor[0]).equalsIgnoreCase("Tathalyns Boots of Mobility") )
    40. {
    41. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE,0));
    42. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE,0));
    43. player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Tathalyn");
    44. }
    45. else // Not wearing the kit
    46. {
    47. player.removePotionEffect(PotionEffectType.SPEED);
    48. player.removePotionEffect(PotionEffectType.JUMP);
    49. }
    50. }
    51. else{
    52. player.removePotionEffect(PotionEffectType.SPEED);
    53. player.removePotionEffect(PotionEffectType.JUMP);
    54. }
    55. //
    56. // End Mazurel Set
    57. //
    58. if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Mazurels Elegant Blade") ) {
    59. event.getItem().getItemStack();
    60. if( getCustomName(playerArmor[3]) != null &&
    61. getCustomName(playerArmor[3]).equalsIgnoreCase("Mazurels Winged Helmet")
    62. && getCustomName(playerArmor[2]) != null &&
    63. getCustomName(playerArmor[2]).equalsIgnoreCase("Mazurels Embossed Plate")
    64. && getCustomName(playerArmor[1]) != null &&
    65. getCustomName(playerArmor[1]).equalsIgnoreCase("Mazurels Legs of Swiftness")
    66. && getCustomName(playerArmor[0]) != null &&
    67. getCustomName(playerArmor[0]).equalsIgnoreCase("Mazurels Light Plated Boots") )
    68. {
    69. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE,2));
    70. player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Mazurel");
    71. }
    72. else // Not wearing the kit
    73. {
    74. player.removePotionEffect(PotionEffectType.SPEED);
    75. }
    76. }
    77. else{
    78. player.removePotionEffect(PotionEffectType.SPEED);
    79.  
    80. }
    81. //
    82. // End Mazurel Set
    83. //
    84. }
    85.  
    86.  
    87.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. okay, just to solve the drop issue first; you can probably copy the code from the inventoryClosEvent for that one
     
  8. Offline

    tommyhoogstra

    PeterK
    yep. dropping is fixed.
     
  9. okay, let's make some major changes to this, there's too much duplicate code >_<
    Create a void method called "checkKit" that only checks the kits, using the held item and armor.
    Then simply add this to every listener:
    Code:java
    1.  
    2. Bukkit.getScheduler().scheduleSyncDelayedTask(
    3. this,
    4. new Runnable()
    5. {
    6. public void run() {
    7. checkKit();
    8. }
    9. },
    10. 5);
    11.  

    What this will do is 5 ticks (=0.25s) after the event was called, perform a check on the player. It's probably a better solution because at this rate, your plugin code would increase immensely with every event you handle, making a gigantic mess and horrible to deal with when trying to fix things or add new features later on ;)

    Sorry that I only mentioned that solution this late, didn't expect it to become that large of a thing xD

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

    tommyhoogstra

    I told you mine was messy :) Because one of the events is a tad different (the .getitem) or something that was removed from the others, should I only do this for the events that are the same?

    PeterK
    what about the event.getplayer, etc.
    Thats required by each individual event otherwise syntax spam
     
  11. Something like this:
    Code:
    public void checkKit( final Player p )
    {
    if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Zeroxes Doombringer")
    && getCustomName(playerArmor[3]) != null && getCustomName(playerArmor[3]).equalsIgnoreCase("Zeroxes Bone Visage")
    && getCustomName(playerArmor[2]) != null && getCustomName(playerArmor[2]).equalsIgnoreCase("Zeroxes Shadow Plate")
    && getCustomName(playerArmor[1]) != null && getCustomName(playerArmor[1]).equalsIgnoreCase("Zeroxes Platelegs")
    && getCustomName(playerArmor[0]) != null && getCustomName(playerArmor[0]).equalsIgnoreCase("Zeroxes Battle Boots") )
    {
    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE,1));
    player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Zerox");
    }
    else // Not wearing the kit or not holding item
    {
    player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    }
    // End Zerox Set
     
    if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Tathalyns Windshear") 
    && getCustomName(playerArmor[3]) != null && getCustomName(playerArmor[3]).equalsIgnoreCase("Tathalyns Hawk eye")
    && getCustomName(playerArmor[2]) != null && getCustomName(playerArmor[2]).equalsIgnoreCase("Tathalyns Fleece")
    && getCustomName(playerArmor[1]) != null && getCustomName(playerArmor[1]).equalsIgnoreCase("Tathalyns Spiked Stockings")
    && getCustomName(playerArmor[0]) != null && getCustomName(playerArmor[0]).equalsIgnoreCase("Tathalyns Boots of Mobility") )
    {
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE,0));
    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE,0));
    player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Tathalyn");
    }
    else // Not wearing the kit or holding the item
    {
    player.removePotionEffect(PotionEffectType.SPEED);
    player.removePotionEffect(PotionEffectType.JUMP);
    }
    }
    
    Having a method that simply uses the held item and calling it with a slight delay will remove all event-compability problems and remove the need to copy & paste loads of code, so there's no longer a difference between these:
    event.getItem().getItemStack()
    event.getPlayer().getItemInHand()
    event.getPlayer().getInventory.getItem(event.getNewSlot())
    ...

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

    tommyhoogstra

    Id have to add the other else after each one as well right. e.g.
    Code:
        if( getCustomName(event.getPlayer().getItemInHand()) != null && getCustomName(event.getPlayer().getItemInHand()).equalsIgnoreCase("Zeroxes Doombringer") )
                {
     
                    if( getCustomName(playerArmor[3]) != null && getCustomName(playerArmor[3]).equalsIgnoreCase("Zeroxes Bone Visage")
                            && getCustomName(playerArmor[2]) != null && getCustomName(playerArmor[2]).equalsIgnoreCase("Zeroxes Shadow Plate")
                            && getCustomName(playerArmor[1]) != null && getCustomName(playerArmor[1]).equalsIgnoreCase("Zeroxes Platelegs")
                            && getCustomName(playerArmor[0]) != null && getCustomName(playerArmor[0]).equalsIgnoreCase("Zeroxes Battle Boots") )
                    {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Integer.MAX_VALUE,1));
                        player.sendMessage(ChatColor.YELLOW + "You have equipped the god set of " + ChatColor.AQUA + "Zerox");
                       
                   
                       
                 
                    } else // Not wearing the kit or not holding item
                        {
                                player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
                           
                    }
                   
                }
        else
        {
                                player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
                             
        }
    
     
  13. I had actually removed them together with an entire if-statement by merging two of them together, your decision how you want to set it up, though
     
  14. Offline

    tommyhoogstra

    So insteant of invent.getplayer() i can basically remove that?
    or should that be changed to like, p.getplayer since "( final Player p )"
     
  15. have you added the method checkKits?
    If you have, simply add the code I posted earlier into each listener.
     
  16. Offline

    tommyhoogstra


    I have created it, but unsure of this:
    [​IMG]
    So those wont matter, and i should just run it anyway.

    And something just came to my head that i think youve been trying to explain
    Because the listener reads that, it doesnt matter if it errors here as it wont error in them event listeners :S.


    [​IMG]
    Whouldnt that be checkKit(p)
     
  17. oh, yes. Sorry xD
    And in the checkKit method, replace "event.getPlayer()" with "p" and remove the line defining the variable player
     
    tommyhoogstra likes this.
  18. Offline

    tommyhoogstra

    And for the checkKit() it self?
    It requires something in it right?
    [​IMG]
    p doesn't want to go into it.
     
  19. event.getPlayer() as argument should work for almost any player-related event
     
  20. Offline

    tommyhoogstra

    PeterK then this comes up
    [​IMG]
    Tried changing it to P, but because the variable isnt public that wouldn't work,
    Removes the error if i make it final PlayerDropItemEven event)
     
  21. oh, i know why. make the variable event final:
    public void onItemDrop( final PlayerDropItemEvent event) {
     
  22. Offline

    tommyhoogstra

    Aha yeah, I said that before :p
    Doing final tests now.

    PeterK
    [​IMG]'
    The InvClose one was different to the rest, could that be why this one errored

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  23. okay, make it check if event.getPlayer() is an instance of player and then cast event.getPlayer() to Player
     
  24. Offline

    tommyhoogstra

    Using "instance of" ?
     
  25. instanceof, yes :)
    if( event.getPlayer() instanceof Player )
     
  26. Offline

    tommyhoogstra

    Omg i learnt something, aha
    [​IMG]
    Not applicable for human entity
     
  27. As I said, cast to Player:
    checkKit((Player)event.getPlayer());
     
    tommyhoogstra likes this.
  28. Offline

    tommyhoogstra

    PeterK that about sums this plugin up (for now)
    Seems to be functioning as planned :)
    Thanks again!


    Though, this will send a message everytime someone activates an event with the set on.
    Is there a way to make this pop up once only, lets say per 5 minutes?
    Boolean or something maybe, idk. Does't matter too much!
    Edit: is it possible to assign a single variable to every player, I was thinking of doing this every 5 times the event is ran by adding 1 to the variable until x = 5 or something
     
  29. Have a look at HashMaps. Don't use the player object as key, use their name instead ;)
     
  30. Offline

    tommyhoogstra

    PeterK
    Will do :p
     
Thread Status:
Not open for further replies.

Share This Page