Solved I'm making a plugin, I need some help.

Discussion in 'Plugin Development' started by thomasb454, Jun 18, 2013.

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

    thomasb454

    Hello. My friend gave me some code to compile for a plugin (stop players from spawning God apples from creative inv) But it don't seem to work.
    Code:java
    1. package com.dcsoft;
    2. import org.bukkit.Material;
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.entity.ItemSpawnEvent;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. public class GodApple extends JavaPlugin implements Listener{
    8. public void onEnable() {
    9. getServer().getPluginManager().registerEvents(this, this);
    10. }
    11. public void onDisable() {
    12. }
    13. @EventHandler
    14. public void onSpawnItem(ItemSpawnEvent event) {
    15. if(event.getEntity().getItemStack().getType()== Material.GOLDEN_APPLE) {
    16. event.setCancelled(true);
    17. }}
    18.  

    Is it something to do with that code?
    Thanks.
     
  2. Offline

    Jatoc

  3. Offline

    Ivan

    I think that's the wrong event... I'm not too sure about that tho...
    EDIT: Is it just me...?
    EDIT: Removed before people get mad
     
    Jatoc likes this.
  4. Offline

    thomasb454

    Last edited by a moderator: Jun 2, 2016
  5. Offline

    Jatoc

    thomasb454

    You can't just replace events like that. I already linked you to the javadocs describing/explaining all the different methods you can call for the event.
     
  6. Offline

    thomasb454

    Jatoc

    I read through them and tried some I thought would work. But they don't.
    EDIT: lol Ivan
    Maybe you could help me?
    Code:java
    1. public void onSpawnItem(org.bukkit.event.inventory.InventoryCreativeEvent event) {
    2. if(event.getCursor().getType()== Material.GOLDEN_APPLE) {
    3. event.setCancelled(true);
     
  7. Offline

    Ivan

    You're checking for the current item on the cursor. I think you'll have to use .getSlot() to get the ID of the slot, and you'll have to use event.getInventory().getItem(slotid) to find the right itemstack
     
  8. Offline

    thomasb454

    Ivan
    Ok, I'll go test this.

    Hay Ivan

    Since I'm a noob, I don't know what to do :/

    So, could you write that code for me?
    Sorry If I seem lazy, but I don't understand Java, yet.

    package com.dcsoft;

    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ItemSpawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class GodApple extends JavaPlugin implements Listener{

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    }

    public void onDisable() {

    }

    @EventHandler
    public void onSpawnItem(org.bukkit.event.inventory.InventoryCreativeEvent event) {
    if(event.getSlot().getItem()== Material.GOLDEN_APPLE) {
    event.setCancelled(true);
    }
    }

    }

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

    Ivan

    I think its something around these lines:
    if(event.getInventory().getItem(event.getSlot()).getType == Material.GOLDEN_APPLE)
    //do ur shit but you'll have to add null checks etc.
     
  10. Offline

    thomasb454

    Ivan
    I'll give this a go. Thanks

    Ivan
    Nope, still don't work.

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

    Ivan

    Weird... very weird...
     
  12. Offline

    thomasb454

    :/

    Can anyone else think of a solution?

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

    Ad237

    thomasb454 I hate saying this to people as only a few months ago was I also very new to Java but before attempting plugins this really is worth a look: http://wiki.bukkit.org/Plugin_Tutorial

    It will help you a lot.

    As for the actual question I thought Ivan's solution would work. Obviously not. Mind posting the whole code after adding those changes? Also you dont need

    org.bukkit.event.inventory.InventoryCreativeEvent

    you can just put

    InventoryCreativeEvent

    and then import it.
     
    Eats_Rainbows likes this.
  14. Offline

    thomasb454

    Hello Ad237
    Code:java
    1. package com.dcsoft;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.inventory.InventoryCreativeEvent;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.ItemSpawnEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class GodApple extends JavaPlugin implements Listener{
    11.  
    12. public void onEnable() {
    13. getServer().getPluginManager().registerEvents(this, this);
    14. }
    15.  
    16. public void onDisable() {
    17.  
    18. }
    19.  
    20. @EventHandler
    21. public void onSpawnItem(InventoryCreativeEvent event) {
    22. if(event.getInventory().getItem(event.getSlot()).getType() == Material.GOLDEN_APPLE)
    23. event.setCancelled(true);
    24. }
    25. }
    26.  
    27.  
    28.  
    29.  
    30.  
     
  15. Offline

    pt1448

    thomasb454
    Don't you just import it?

    EDIT: nevermind
     
  16. Offline

    thomasb454

    Anyone have any ideas?
     
  17. Offline

    wizzinangel

    thomasb454 maybe try checking if the inventory contains it. Not just the slots.
     
  18. Offline

    thomasb454

    wizzinangel
    Hi, I want players to obtain GoldenApple via the in-game shop, not by spawning them in. So, this wouldn't work
    I don't know if you can scan per item name? Because the Apples (from the shop) are renamed.

    I really need this to work guys, please help!

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

    wizzinangel

    thomasb454 This stops them from adding the apple to their inventory:

    Code:java
    1.  
    2. @EventHandler
    3. public void onSpawnItem(InventoryCreativeEvent event) {
    4. ItemStack EGApple = new ItemStack(322,1, (byte)1);
    5. if (event.getInventory().contains(Material.getMaterial(322))) {
    6. event.setCancelled(true);
    7. }
    8. if (event.getInventory().contains(EGApple)) {
    9. event.setCancelled(true);
    10. }
    11. }
    12.  


    Hope that helps
     
  20. Offline

    thomasb454

    wizzinangel
    Thanks for this! But I get one error.
    http://prntscr.com/1b0fmk

    Dammit he went offline. :/

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

    historio

    I'll try and help you :D give 1 min tho
     
  22. Offline

    thomasb454

  23. Offline

    historio

    Try
    Code:java
    1. @EventHandler
    2. public void onSpawnItem(InventoryCreativeEvent event) {
    3. Player player = event.getPlayer();
    4. ItemStack EGApple = new ItemStack(322,1, (byte)1);
    5. if (player.getInventory().contains(Material.getMaterial(322))) {
    6. event.setCancelled(true);
    7. }
    8. if (player.getInventory().contains(EGApple)) {
    9. event.setCancelled(true);
    10. }
     
  24. Offline

    thomasb454

  25. Offline

    pt1448

  26. Offline

    historio

    I'm looking in to it :D

    Code:java
    1. @EventHandler
    2. public void onSpawnItem(InventoryCreativeEvent event) {


    Won't work for me?

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

    thomasb454

  28. Offline

    historio

  29. Offline

    thomasb454

  30. Offline

    historio

    I have to go now i'll try and help tomorrow :D

    thomasb454
    Sorry for leaving i will try and help tomorrow if this thing is not fixed :D
     
Thread Status:
Not open for further replies.

Share This Page