Random drop

Discussion in 'Plugin Development' started by xelatercero, Oct 5, 2016.

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

    xelatercero

    I have a plugin that drops items from a sponge. You can craft thta sponge. The problem is that i want that the sponge drop items only if his display name is Magic Sponge , but i dont know how to check the dosplayname of a placed block, i only check the type. Sorrry for my english.

    PHP:
    package me.xela;

    import java.util.ArrayList;
    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 net.md_5.bungee.api.ChatColor;

    public class 
    bloque implements Listener {
      
      
        @
    EventHandler
      
        
    public void onBlock(BlockBreakEvent e) {
            
    Block bl e.getBlock();
            
    Player p e.getPlayer();
            if (
    bl.getType() == Material.SPONGE) { //her i check the type but i want to check thje display name
                
    Random r = new Random();
                
    int index r.nextInt(Main.item.size());
                
    ItemStack obj Main.item.get(index);
                
    String name obj.getType().name();
                
    bl.getWorld().dropItemNaturally(bl.getLocation(), obj);
                
    p.sendMessage("Te ha tocado " ChatColor.RED name);
            }
          
        }
    }
     
  2. Offline

    PumpMelon

    Try using the isSimilar(ItemStack) method
     
  3. Offline

    xelatercero

    @PumpMelon But where i cant use this method with my bl block
     
  4. Offline

    Zombie_Striker

    @PumpMelon
    That will not help.

    @xelatercero
    1. Don't create new instances of random
    2. Don't abuse static. "item" should not be static. If you want to access the list from another class, pass the instance the instance of the main class to this class.
    To solve your problem: get the item's itemmeta and check if the name is equal to "Magic sponge"
     
  5. Offline

    xelatercero

    @Zombie_Striker sorry my ignorance but you can do bl.getItemMet().getDsiplayName();

    And why i cant abuse static?
     
  6. Offline

    Zombie_Striker

    @xelatercero
    Yes, you can't get the display name for a block because blocks do not have display names. That is why I thought you meant the Itemstack's display name.

    If you want the block's displayname, you would have to create your own system for storing display names.
    1. Get a Map, where the keys are locations and the Values are Strings (*the display names)
    2. Create a BlockPlaceEvent listener
    3. Whenever a spongeblock is placed, store the displayname to the Map.
    4. Inside the blockBreakEvent.
    5. If the block is a sponge, and if the block is inside the map, check if the display name is equal to "magic sponge", if so, do what you want.
     
  7. Offline

    PumpMelon

    Oh, thought it was an Interact event.
     
  8. Offline

    xelatercero

    @Zombie_Striker Sorry but i am very new at this i mean how i can get the display name to a String if i cant get the deisplay name of a block..?? if you can iluminate me , thank you for your time
     
  9. @xelatercero I'm not from the Illuminati sorry.

    When an item has a display name for example "&aOver &c9k &adirt block" and it is placed this name will be lost therefor you cannot re-get the name. To do that you would need to log the block placement and break, in your first post you are getting the type name. For example that code would return "DIRT" not "&aOver &c9k &adirt block"
     
  10. Offline

    xelatercero

    @bwfcwalshy thanks, but this dont help me so much i'm lost :(
     
  11. Offline

    mehboss

    @xelatercero

    @Zombie_Striker already told you how to solve your problem.. :(
    Code:
            if (bl.getItemMeta().equals("Magic Sponge")) {
     
    Last edited: Oct 5, 2016
  12. Offline

    xelatercero

    @mehboss hmmm, ley me say it another time, you cant do bl.getItemMeta(); because bl is a block.:confused:
     
  13. @xelatercero Amd as I said in my post, blocks don't have names, they lose them as soon as they are placed. You need to handle them yourself, check when a block is placed, see if that is your custom block, add it to a list or map and handle it that way
     
  14. Offline

    xelatercero

    @bwfcwalshy ok i know i can do an event to check when a block ios placed , but how i can get if ios my block if i cant get the display name ?
     
  15. Offline

    xelatercero

    @AlvinB @Zombie_Striker @bwfcwalshy The can i do this:

    @Lordloss Yes is a question
    -save location in arraylist,do a for loop to check if the block is i a location of the list, drop item and delete the location of tyhe bloc from the lsit

    Sorry for my english.
     
    Last edited: Oct 6, 2016
  16. Offline

    Lordloss

    Was this a question?
     
  17. @xelatercero Well, you need the name. You still can't get the name from location, save in a Map<Location, String> then just get(Location) on break and whala you have the name.
     
  18. Offline

    xelatercero

    @bwfcwalshy ok i understand what you are sayingh but i i no have idea the method to use i a block place event to get the display neme of the block , because i try e.getBlock().getType() but i cant get the displayName
     
  19. Offline

    Zombie_Striker

    @xelatercero
    When you try to place a block get the Items display name, since the item in the player's hand is the block that will be placed down.
     
  20. Offline

    xelatercero

    @Zombie_Striker if my code is correct, then how i get the location and the name and how i check them:

    PHP:
    public void Onbreak(BlockPlaceEvent e ) {
            
    Material m e.getBlock().getType();
            
    String itemMeta e.getItemInHand().getItemMeta().getDisplayName();
            
    Location loc e.getBlock().getLocation();
            if(
    == Material.SPONGE && itemMeta == "Esponja Magica") {
                
    locations.put(itemMetaloc);
            }
          
        }
     
    Last edited: Oct 7, 2016
  21. Offline

    mehboss

    No need to get rude here, my mistake :)
     
  22. Online

    timtower Administrator Administrator Moderator

    @xelatercero Stings are compared with equals, not with ==
     
  23. Offline

    xelatercero

    @timtower ok but now how i get the location and the name and how i check them
     
  24. Offline

    Lordloss

    @xelatercero Blocks dont have a display name. And you allready got the Location in the code you showed.

    As you just need the display name for checking if a player placed the right type of sponge, you dont need to save the name. You only need to save the Location. after this you can check in a blockBreakEvent if the Location is in your List, and drop the loot.
     
    Last edited: Oct 7, 2016
  25. Offline

    xelatercero

    @Lordloss @timtower @Zombie_Striker ok what i have done wrong i mean i put a sponge and when i brak it nothing happens but if i put magic sponge nothing happens

    PHP:
    public void onBlock(BlockBreakEvent e) {
       
            
    Block bl e.getBlock();
            
    Player p e.getPlayer();
            
    Location loc locations.get(e.getBlock().getLocation()); //i am getting well the location from the Map and i am comparing them correctly?
            
    if(e.getBlock().getLocation() == loc) {
            if (
    bl.getType() == Material.SPONGE) {
                
    Random r = new Random();
                
    int index r.nextInt(Main.item.size());
                
    ItemStack obj Main.item.get(index);
                
    String name obj.getType().name();
                
    bl.getWorld().dropItemNaturally(bl.getLocation(), obj);
                
    bl.setType(Material.AIR);
                
    p.sendMessage("Te ha tocado " ChatColor.GREEN name);
            }
            }
        }
     
    Last edited: Oct 7, 2016
  26. Offline

    Zombie_Striker

    This will almost never be true. Use .equals to compare objects.

    Don't create new instance of random. Use ThreadLocalRandom.current to get the random instance.

    Don't abuse static. Remove the static modifier from item and pass the "main" instance through this class's constructor.

    We are going around in circles. Blocks don't have names. You can't get the block's name using the block instance. The only way to get the name of the block would be if you stored the displayname of the item which placed down the block.

    Think of it this way:
    1. You have an block with a display name in your inventory.
    2. You place the block item down, and a block appears where you clicked. The name is not saved when you place down the block. The Block does not have the name, but the item in your inventory does.
    What you need to do is store the name of the item that placed down the block. Listen to BlockPlaceEvent and store the name of the item which placed down the block. Then, use that data to get the name of the broken block.
     
    AlvinB and bwfcwalshy like this.
Thread Status:
Not open for further replies.

Share This Page