EventListeners does not work

Discussion in 'Plugin Development' started by super_surviveur, Sep 25, 2020.

Thread Status:
Not open for further replies.
  1. Hello !
    I am currently creating a miner plugin. I have a task that allows me to retrieve all the miners and mine or put a block around them. But when this task runs(every 10 seconds), Eventslisteners are not working so breaking a block in an area protected by the plugin doesn't work for example. Do you have an idea?
    Thank's you!

    My task code :
    Code:
    Bukkit.getScheduler().runTaskTimer(main, () -> {
                ArrayList<Location> loc = main.sql.getArmorStand(1);
                if(loc!=null){
                    for(Location cLoc: loc){
                        List<Entity> entityList = (List<Entity>) cLoc.getWorld().getNearbyEntities(cLoc,0.01,0.01,0.01);
                        for(Entity armorStand: entityList) {
                            if(armorStand.getType()==EntityType.ARMOR_STAND){
                                Random random = new Random();
                                int aX = random.nextInt(4)-2;
                                int aZ = random.nextInt(4)-2;
                                int aY = random.nextInt(2)-2;
                                Location blockLoc = cLoc;
                                blockLoc.setY(blockLoc.getBlockY()+aY);
                                blockLoc.setZ(blockLoc.getBlockZ()+aZ);
                                blockLoc.setX(blockLoc.getBlockX()+aX);
                                Block b = blockLoc.getBlock();
                                Collection<ItemStack> drops = b.getDrops();
                                if(b.getType().isAir()){
                                    b.setType(Material.COBBLESTONE);
                                } else {
                                    b.setType(Material.AIR);
                                }
                                blockLoc.getBlock();
                            }
                        }
                    }
                }
            }, 0, 100);
     
  2. Offline

    KarimAKL

    Could you elaborate? This doesn't really make any sense to me.
     
  3. Offline

    gochi9

    Yes same i can't really understand
     
  4. This code is launched when the server is launched. It is a runTasktimer that runs every 10 seconds. When it launches (so every 10 seconds), the rest of the plugin does not work any more (logical because I am on the main thread) and so my EventsListeners are not working. Do you know how not to block the rest of the plugin when the runTaskTimer is running?
     
  5. Offline

    timtower Administrator Administrator Moderator

    @super_surviveur The timer does not stop the rest of the plugin.
    What is the rest of the plugin? Does that function without this timer active?
     
  6. Yes.
    For example, with this code I can place an other item that a diamond pickaxe when the timer work.
    Code:
    Public void onInteract(InventoryClickEvent e){
      if(e.getCursor().getType()!=Material.DIAMOND_PICKAXE){
        e.setCanceled(true);
      }
    }
     
  7. Offline

    timtower Administrator Administrator Moderator

    @super_surviveur I don't see anything that can stop the server from running though.
    All events will be handled anyways.
     
  8. I have tried without the sheduler and it's not working also. (I'm stupid...) I think the problem comes from this code :
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            InventoryView inv = event.getView();
            Player p = (Player) event.getWhoClicked();
            if(inv.getTitle().contains("§0Mineur charbon")){
                if(event.getCurrentItem().getType()==Material.DIAMOND_PICKAXE){
                    int uuid = Integer.parseInt(inv.getTitle().substring(17));
    
                    Inventory newInv = Bukkit.createInventory(null, 27, "§0Pioche charbon "+uuid);
                    for(int i=0;i<27;i++){
                        newInv.setItem(i, utils.greyGlass());
                    }
                    //SET THE OLD ITEM IN HAND
                    ItemStack itemtoreturn = new ItemStack(Material.AIR);
                    if(main.sql.getPickaxe(uuid)!=null){
                        try {
                            ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(main.sql.getPickaxe(uuid)));
                            BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
                            itemtoreturn = (ItemStack) dataInput.readObject();
                            dataInput.close();
                        } catch (ClassNotFoundException | IOException e) {
                            new IOException("Unable to decode class type.", e);
                        }
                    }
                    newInv.setItem(13, itemtoreturn);
                    p.openInventory(newInv);
    
                }
            } else if (inv.getTitle().contains("§0Pioche charbon")){
                int uuid = Integer.parseInt(inv.getTitle().substring(17));
                if(event.getClickedInventory()==null) {
                    event.setCancelled(true);
                    return;
                }
                if(event.getCurrentItem()!=null){
                    if(event.getCurrentItem().getType()==Material.GRAY_STAINED_GLASS_PANE){
                        event.setCancelled(true);
                        return;
                    }
                }
                if(event.getClickedInventory().getType()!=InventoryType.PLAYER) {
                    if(event.getCursor().getType()==Material.AIR || event.getCursor().getType()==Material.NETHERITE_PICKAXE || event.getCursor().getType()==Material.DIAMOND_PICKAXE || event.getCursor().getType()==Material.GOLDEN_PICKAXE || event.getCursor().getType()==Material.IRON_PICKAXE || event.getCursor().getType()==Material.STONE_PICKAXE || event.getCursor().getType()==Material.WOODEN_PICKAXE){
                        try {
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
                            dataOutput.writeObject(event.getCursor());
                            dataOutput.close();
                            main.sql.setPickaxe(uuid, Base64Coder.encodeLines(outputStream.toByteArray()));
                        } catch (IOException e) {
                            throw new IllegalStateException(e);
                        }
                    } else {
                        event.setCancelled(true);
                    }
                }
            }
        }
    If I add a Bukkit#broadcastMessage() at the begin of this code, sometimes the message doesn't appear and I can place an other item than a pickaxe.
     
    Last edited: Sep 26, 2020
Thread Status:
Not open for further replies.

Share This Page