Learning Plugin development, Can't figure out this problem.

Discussion in 'Plugin Development' started by bmckalip, Oct 1, 2014.

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

    bmckalip

    I'm coding a plugin that will equip a pumpkin to a player's head upon login. if a helmet is already there, it takes the old helmet and adds it to their inventory, and equips a pumpkin. Otherwise, it equips a pumpkin in place of the air currently there.

    The code will correctly equip a pumpkin and put the old helmet in the player's inventory, and will correctly do nothing if the helmet is already a pumpkin, however fails to replace air with a pumpkin.
    Code:java
    1. package bmckalip.pumpkinHeads;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.PlayerInventory;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class pumpkinHeads extends JavaPlugin implements Listener{
    13.  
    14. @EventHandler
    15. public void equipPumpkinHead(final PlayerJoinEvent event){
    16. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    17. public void run(){
    18.  
    19. PlayerInventory playerInventory = event.getPlayer().getInventory();
    20.  
    21. ItemStack pumpkin = new ItemStack(Material.PUMPKIN);
    22. ItemStack air = new ItemStack(Material.AIR);
    23. ItemStack currentHelm = playerInventory.getHelmet();
    24.  
    25. if(currentHelm.equals(pumpkin)){
    26. //do nothing, already a pumpkin.
    27. }else if(currentHelm.equals(air)){
    28. playerInventory.setHelmet(pumpkin);
    29. }else{
    30. playerInventory.addItem(currentHelm);
    31. playerInventory.setHelmet(pumpkin);
    32.  
    33. }
    34. }
    35. }, 20);
    36. }
    37. public void onEnable(){
    38. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    39. }
    40.  
    41. }
    42.  
     
  2. Offline

    Skionz

    Try this. I am not actually sure if the Material type is null or air so just try them both.
    Code:java
    1. if(!currentHelm.equals(pumpkin)){
    2. playerInventory.addItem(currentHelm);
    3. playerInventory.setHelmet(pumpkin);
    4. }else if(currentHelm.getType() == null){
    5. playerInventory.setHelmet(pumpkin);
    6. }
     
  3. Offline

    fireblast709

    bmckalip empty slots can be both null and AIR. Also, instead of equals, I recommend something like the following:
    Code:java
    1. if(currentHelm == null || currentHelm.getType() == Material.AIR)
    2. {
    3. // They have nothing equipped
    4. }
    5. else if(currentHelm.getType() == Material.PUMPKIN)
    6. {
    7. // They already have a pumpkin equipped
    8. }
    9. else
    10. {
    11. // Other helmet
    12. }
    This both prevents NullPointerExceptions, and it's a bit faster / more precise (since most of the time you only care about the Material)

    Skionz Material cannot (or rather, will not) be null :p
     
  4. Offline

    bmckalip

    Thanks, this solution worked.
     
  5. Offline

    Skionz

Thread Status:
Not open for further replies.

Share This Page