Solved Inventory Manipulation Not Working

Discussion in 'Plugin Development' started by Notorious-Riddler, Jul 2, 2013.

Thread Status:
Not open for further replies.
  1. So im still learning and i went onto the bukkit plugin tutorial page to see how to do inventory manipulation i copy and pasted the code but its not working ? Heres the beginning of my code

    Code:java
    1. public class Signs extends JavaPlugin{
    2.  
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public static Signs plugin;
    5.  
    6. @Override
    7. public void onDisable()
    8. {
    9. PluginDescriptionFile pdffile = this.getDescription();
    10. this.logger.info(pdffile.getName() + " Has Been Disabled ");
    11. }
    12.  
    13.  
    14. @Override
    15. public void onEnable()
    16. {
    17. PluginDescriptionFile pdffile = this.getDescription();
    18. this.logger.info(pdffile.getName() + " Version : " + pdffile.getVersion() + " Has Been Enabled ");
    19. getConfig().options().copyDefaults(true);
    20. saveConfig();
    21. }
    22.  
    23. public void onPlayerJoin(PlayerJoinEvent evt) {
    24. Player player = evt.getPlayer(); // The player who joined
    25. PlayerInventory inventory = player.getInventory(); // The player's inventory
    26. ItemStack itemstack = new ItemStack(Material.DIAMOND, 64); // A stack of diamonds
    27.  
    28. if (inventory.contains(itemstack)) {
    29. inventory.addItem(itemstack); // Adds a stack of diamonds to the player's inventory
    30. player.sendMessage("Welcome! You seem to be reeeally rich, so we gave you some more diamonds!");
    31. }
    32. }


    someone please help me as im still a beginner and want to learn so much more.
     
  2. Offline

    MP5K

    Hello Notorious-Riddler,
    Im pretty sure you have an title mistake in your code: :3
    Code:java
    1. if (inventory.contains(itemstack)) {
    2. // if the player has an stack of diamonds
    3.  
    4. inventory.addItem(itemstack);
    5. // give him another stack diamonds
    6.  
    7. player.sendMessage("Welcome! You seem to be reeeally rich, so we gave you some more diamonds!");
    8. // + send him a message
    9. }

    try using:
    Code:java
    1. if (inventory.contains(itemstack) == false) { // or: if(!inventory.contains(itemstack){ ...
    2. // if the player hasn't an stack of diamonds
    3.  
    4. inventory.addItem(itemstack);
    5. // give him another stack diamonds
    6.  
    7. player.sendMessage("Welcome! You seem to be reeeally rich, so we gave you some more diamonds!");
    8. // + send him a message
    9. }


    //edit:
    and add the @EventHandler annotation to your
    Code:java
    1. public void onPlayerJoin(PlayerJoinEvent evt) {
    2. //so it lookes like that:
    3.  
    4. @EventHandler()
    5. public void onPlayerJoin(PlayerJoinEvent evt) {

    and last but not least :3 don't forget to register your listener in your onEnable method
     
  3. Hello MP5K !

    thanks for the code but it still doesn't work here is my code now :




    Code:java
    1. public class Signs extends JavaPlugin{
    2.  
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public static Signs plugin;
    5.  
    6. @Override
    7. public void onDisable()
    8. {
    9. PluginDescriptionFile pdffile = this.getDescription();
    10. this.logger.info(pdffile.getName() + " Has Been Disabled ");
    11. }
    12.  
    13.  
    14. @Override
    15. public void onEnable()
    16. {
    17. PluginDescriptionFile pdffile = this.getDescription();
    18. this.logger.info(pdffile.getName() + " Version : " + pdffile.getVersion() + " Has Been Enabled ");
    19. getConfig().options().copyDefaults(true);
    20. saveConfig();
    21. }
    22.  
    23. public void onPlayerJoin(PlayerJoinEvent evt) {
    24. Player player = evt.getPlayer(); // The player who joined
    25. PlayerInventory inventory = player.getInventory(); // The player's inventory
    26. ItemStack itemstack = new ItemStack(Material.DIAMOND, 64); // A stack of diamonds
    27.  
    28. if (inventory.contains(itemstack) == false) { // or: if(!inventory.contains(itemstack){ ...
    29. // if the player hasn't an stack of diamonds
    30.  
    31. inventory.addItem(itemstack);
    32. // give him another stack diamonds
    33.  
    34. player.sendMessage("Welcome! You seem to be reeeally rich, so we gave you some more diamonds!");
    35. // + send him a message
    36. }
    37. }
     
  4. Offline

    MrSnare

    Add @EventHandler above the inventory method
     
  5. MrSnare It still doesn't work !
     
  6. Offline

    callum.thepro

    Code:java
    1. public class Signs extends JavaPlugin{
    2.  
    3. public final Logger logger = Logger.getLogger("Minecraft");
    4. public static Signs plugin;
    5.  
    6. @Override
    7. public void onDisable()
    8. {
    9. PluginDescriptionFile pdffile = this.getDescription();
    10. this.logger.info(pdffile.getName() + " Has Been Disabled ");
    11. }
    12.  
    13.  
    14. @Override
    15. public void onEnable()
    16. {
    17. PluginDescriptionFile pdffile = this.getDescription();
    18. this.logger.info(pdffile.getName() + " Version : " + pdffile.getVersion() + " Has Been Enabled ");
    19. getConfig().options().copyDefaults(true);
    20. saveConfig();
    21. getServer().getPluginManager().registerEvents(this, this);
    22. }
    23.  
    24. @EventHandler
    25. public void onPlayerJoin(PlayerJoinEvent evt) {
    26. Player player = evt.getPlayer(); // The player who joined
    27. PlayerInventory inventory = player.getInventory(); // The player's inventory
    28. ItemStack itemstack = new ItemStack(Material.DIAMOND, 64); // A stack of diamonds
    29. }
    30. }


    That should work but it needs extra imports
     
    Notorious-Riddler and MrSnare like this.
  7. Offline

    Rprrr

    MP5K
    That first suggestion is just bullshit and totally not relevant. Lol.
     
  8. Offline

    callum.thepro

    Your welcome, good to see another plugin working :)
     
  9. Offline

    KingPsychopath

    Wow Harsh..
     
  10. Offline

    ddeckys

    It was true though...
     
Thread Status:
Not open for further replies.

Share This Page