Adding a custom itemstack to drops

Discussion in 'Plugin Development' started by Demyxa, May 18, 2019.

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

    Demyxa

    I've been trying to have Cleanstone drop a custom itemstack of a ruby.
    How do I add the custom itemstack to the drop list because as far as I know, you can only add vanilla items to a drop list, right?

    Code:
    public void onBreak(BlockBreakEvent e) {
            Player p = e.getPlayer(); //should I want to simply add the item to the players inv directly
            Block b = e.getBlock();
     
            if (b.getType()== Material.STONE) {
                Random random = new Random(2);
                int luck = random.nextInt(2);
                 if (luck == 0) {
                    b.getDrops().add(Ruby);
     
    Last edited: May 18, 2019
  2. Online

    timtower Moderator Moderator

    @Demyxa Should be able to add anything
     
  3. Offline

    Demyxa

    Simply adding "Ruby" (the name of the ItemStack) doesn't do the trick.
    I'm missing something here.
     
  4. Offline

    Shqep

    I don’t know, remake the dropping? getBlock().getWorld().dropItem?
     
  5. Online

    timtower Moderator Moderator

    Does it not add the item? Do you get errors?
     
  6. Offline

    Demyxa

    "Ruby cannot be resolved to a variable" is what it gives me.
    Not that I could actually test the functionality because I somehow messed up my plugin.yml

    @timtower
     
  7. Offline

    Shqep

    Have you declared the Ruby variable yet? Typos? Or I’m just dumb.
     
  8. Offline

    Demyxa

    I created the itemstack, I'm currently on my localhost and trying to test it but it doesn't work.
    I went ahead and instead tried to add the item directly to the player inventory.

    Code:
    public class Main extends JavaPlugin implements Listener {
       
        public void onEnable() {
            System.out.println("MoreJewels Plugin activated!");
           
            ItemStack Ruby = new ItemStack(Material.DIAMOND);
            ItemMeta m1 = Ruby.getItemMeta();
            m1.setDisplayName("Roher Rubin");
            List<String> lore = Arrays.asList("Ein roter, glänzender Edelstein.");
            m1.setLore(lore);
            Ruby.setAmount(2);
           
           
        }
       
        public void onDisable() {
            System.out.println("MoreJewels Plugin deactivated!");
        }
        @EventHandler
       
        public void onBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            Block b = e.getBlock();
       
            if (b.getType()== Material.STONE) {
                Random random = new Random(2);
                int luck = random.nextInt(2);
                 if (luck == 0) {
                    p.getInventory().addItem(Ruby);
     
  9. Offline

    Shqep

    Method-local variables can’t be accessed by other methods?

    “Can’t be resolved to a variable”, that means it doesn’t find that specific variable?
     
  10. Offline

    Demyxa

    Even when adding the ItemStack to the onBreak method it doesn't give out the item.
    Unless of course I'm being entirely idiotic.

    PHP:
    public void onBreak(BlockBreakEvent e) {
           
            
    ItemStack Ruby = new ItemStack(Material.DIAMOND);
            
    ItemMeta m1 Ruby.getItemMeta();
            
    m1.setDisplayName("Roher Rubin");
            
    Ruby.setAmount(1);
           
            
    Player p e.getPlayer();
            
    Block b e.getBlock();
       
            if (
    b.getType()== Material.STONE) {
                
    Random random = new Random(2);
                
    int luck random.nextInt(2);
                 if (
    luck == 0) {
                    
    p.getInventory().addItem(Ruby);
                
     
  11. Offline

    Shqep

    Maybe because you forgot to .setItemMeta for the ItemStack? And you got a regular diamond so you didn’t notice?

    EDIT: Yes im guessing stuff and scenarios
     
  12. Offline

    Demyxa

    Oh, you're right I did forget to actually set the meta.
    However, that didn't do it either.
     
  13. Offline

    Shqep

    Full inventory? luck doesn’t equal to 0? Not stone? Forgot to register listeners? Ahhh?

    Probably you forgot to register that listener
     
  14. Offline

    Demyxa

    Code:
        public void registerEvents() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
        
    Like this? (This is indeed my first time working with events)
     
  15. Offline

    Shqep

    Yes? Does it work now xD?
     
  16. Offline

    Demyxa

    Nope!
     
  17. Offline

    Shqep

    Did you call registerEvents onEnable? Xd
     
  18. Offline

    Demyxa

    Also Nope!
    EDIT: Still not working though...
     
    Last edited: May 18, 2019
  19. Online

    timtower Moderator Moderator

    Please post your full class.
     
  20. Offline

    Demyxa

    PHP:
    package me.demyxa.morejewels;

    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;



    public class 
    Main extends JavaPlugin implements Listener {
       
        public 
    void registerEvents() {
            
    Bukkit.getPluginManager().registerEvents(thisthis);
        }
       
        public 
    void onEnable() {
            
    System.out.println("MoreJewels Plugin activated!");
            
    registerEvents();
           
           
        }
       
        public 
    void onDisable() {
            
    System.out.println("MoreJewels Plugin deactivated!");
        }
        @
    EventHandler
       
        
    public void onBreak(BlockBreakEvent e) {
           
            
    ItemStack Ruby = new ItemStack(Material.DIAMOND);
            
    ItemMeta m1 Ruby.getItemMeta();
            
    m1.setDisplayName("Roher Rubin");
            
    Ruby.setAmount(1);
            
    Ruby.setItemMeta(m1);
           
            
    Player p e.getPlayer();
            
    Block b e.getBlock();
       
            if (
    b.getType()== Material.REDSTONE_ORE) {
                
    Random random = new Random(2);
                
    int luck random.nextInt(2);
                
    System.out.println(luck);
                 if (
    luck == 1) {
                    
    p.getInventory().addItem(Ruby);
               
                    
                 }
           
               
            }
        }
           
    }
    @timtower I somehow made it magically work, it now gives me the item but the "luck" value always prints out as "8", even when I reload the plugin!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2019
  21. Online

    timtower Moderator Moderator

    @Demyxa Should never be 8 though.
     
  22. Offline

    Demyxa

    I've pumped up the border to 100, now it constantly spams "8" on every broken block that meets the requirement in the console. Before, it did only give "1"
     
  23. Online

    timtower Moderator Moderator

  24. Offline

    Demyxa

    Code:
    package me.demyxa.morejewels;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Main extends JavaPlugin implements Listener {
       
        public void registerEvents() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        public void onEnable() {
            System.out.println("MoreJewels Plugin activated!");
            registerEvents();
           
           
        }
       
        public void onDisable() {
            System.out.println("MoreJewels Plugin deactivated!");
        }
        @EventHandler
       
        public void onBreak(BlockBreakEvent e) {
           
            ItemStack Ruby = new ItemStack(Material.REDSTONE_ORE);
            ItemMeta m1 = Ruby.getItemMeta();
            m1.setDisplayName("Roher Rubin");
            List<String> l1 = Arrays.asList("Ein roher Edelstein. Muss geschliffen werden.");
            m1.setLore(l1);
            Ruby.setAmount(1);
            Ruby.setItemMeta(m1);
           
            Player p = e.getPlayer();
            Block b = e.getBlock();
       
            if (b.getType()== Material.DIAMOND_ORE) {
                Random random = new Random(100);
                int luck = random.nextInt(100);
                System.out.println(luck);
                 if (luck == 1) {
                    p.getInventory().addItem(Ruby);
                
     
  25. Online

    timtower Moderator Moderator

    @Demyxa Remove the number from the constructor
     
  26. Offline

    Demyxa

    That did it! Everything is working as intended now!
    Apologies for the hassle!
     
Thread Status:
Not open for further replies.

Share This Page