Solved Avoid take items

Discussion in 'Plugin Development' started by xelatercero, Nov 24, 2016.

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

    xelatercero

    i have a block called collector thatt when is placed collect the items and put into a chest in a 10 block range, but if there are another collector in the range of another collector, when item is droped is duplicated in the chest because, the 2 collector take the item, my question is how i can avoid this?

    PHP:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.instance, new Runnable() {
                    public 
    void run() {
                       
                       
                        if(
    main.getCollectorConfig().contains("CollectorWorld." "collectors")) {
                            for(
    String key main.getCollectorConfig().getConfigurationSection("CollectorWorld." "collectors").getKeys(false)) {
                                if(
    main.getCollectorConfig().get("CollectorWorld." "collectors." key) != null) {
                                   
                                   
                                   
                                   
                                    
    World world Bukkit.getWorld(main.getCollectorConfig().getString("CollectorWorld." "collectors." key ".location.world"));
                                    
    int x main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".location.x");
                                    
    int y main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".location.y");
                                    
    int z main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".location.z");
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                    
    Location loc = new Location(worldxyz);
                                   
                                   
                                   
                                    
    Block bl world.getBlockAt(loc);
                                   
                                    if(
    bl != null) {
                                        if(
    bl.getType() == Material.DIAMOND_BLOCK) {
                                         
    Collection<Entityents world.getNearbyEntities(bl.getLocation(), 101010);
                                        
                                       
                                        
                                         for(
    Entity ent ents) {
                                             if(
    ent instanceof Item) {
                                                 
    ItemStack item = ((Itement).getItemStack();
                                                
                                                 if(
    main.getCollectorConfig().contains("CollectorWorld." "collectors." key ".atribbs.linked-chest.world")) //here i chek if there are a chest location by checking if exist a key called world {
                                                    
                                                    
                                                    
    World world2 Bukkit.getWorld(main.getCollectorConfig().getString("CollectorWorld." "collectors." key ".atribbs.linked-chest.world"));
                                                     
    int x2 main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".atribbs.linked-chest.x");
                                                     
    int y2 main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".atribbs.linked-chest.y");
                                                     
    int z2 main.getCollectorConfig().getInt("CollectorWorld." "collectors." key ".atribbs.linked-chest.z");
                                                     
    Location locChest = new Location(world2x2y2z2);
                                                    
                                                        
                                                         if(
    locChest.getBlock().getType() == Material.CHEST) {
                                                         
    Chest ch = (ChestlocChest.getBlock().getState();
                                                         
    ch.getInventory().addItem(item);
                                                         
    ent.remove();
                                                        
                                                     } else {
                                                         return;
                                                     }
                                                 }
                                                
                                                
                                             }
                                            
                                           
                                            
                                         }
                                        }
                                    }
                                   
                                }
                            }
                        } else return;
                       
                      
                    }
                }, 
    0L20L);
                
     
  2. Offline

    jlin13

    @xelatercero

    You could store the location of the collector chests, listen on the BlockPlaceEvent, and if a player places a collector block within a 10 block radius of a chest, cancel the BlockPlaceEvent and send a message.

    Alternatively, a more complex solution could be mapping players with chests in a HashMap and only allowing players to store their "collected" items in their respective chests, given that they are within a 10 block radius. This would be a simple if statement in your code:
    Code:
    if (map.get(<reference to player object>).getBlockX() == locChest.getBlockX() && ...)
    
    If you don't want to use HashMaps, you can also use Metadata to achieve the same result with the chests
     
    xelatercero likes this.
  3. Offline

    Zombie_Striker

    @xelatercero
    If you remove the item, the entity instance should be considered invalid. Make sure the item is not invalid before storing the item.
     
    xelatercero likes this.
  4. Offline

    xelatercero

    Last edited: Nov 25, 2016
  5. Offline

    Lordloss

    After the instanceof Item, make another check to see if the Item is valid. It has a method for this.
     
  6. Offline

    xelatercero

    @Lordloss and what is the clue to check if is valid, what returns? , no better question what is cheking? works great.
     
  7. Offline

    Lordloss

    The clue is, it wont add the item if it is allready removed by your plugin. That was your problem, i dont really get your question.
     
  8. Offline

    Zombie_Striker

    @xelatercero
    Are you asking for the method to use? The method Item#isValid() returns whether the item has been removed that tick. simply make sure the item is valid before adding the item to the inventory.
     
Thread Status:
Not open for further replies.

Share This Page