Block not being placed

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, Jun 1, 2016.

  1. Am I doing something wrong, I'm trying to set the block a player is holding in his hand

    Code (Text):

    @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Bukkit.broadcastMessage("3");
            dataManager dm = new dataManager(pl);
            FileConfiguration data = dm.getData();
            Player p = event.getPlayer();
            if(data.getStringList("SneakyAdmins.players").contains(p.getName())){
                Bukkit.broadcastMessage("4");
                event.setCancelled(true);
                Block b = event.getBlock();
                b.getLocation().getBlock().setType(p.getItemInHand().getType());
            }
        }
     
     
  2. I believe the reason is that since the event was cancelled, after the event finishes the server sets the block back to air. The way around this would be to schedule a task for 1 tick later that changes the block to your desired type.
     
  3. I'm trying to cancel it so that it doesn't make a sound when placing. Is there another event that I could use?
     
  4. What I suggested should achieve what you are after.
     
  5. I don't think so because it still plays the block being placed sound.
     
  6. It shouldn't do since you cancel the block place event.
     
  7. Sorry I misunderstood you
    I am now trying
    Code (Text):

    @EventHandler
        public void onBlockPlace(BlockPlaceEvent event){
            Bukkit.broadcastMessage("3");
            dataManager dm = new dataManager(pl);
            FileConfiguration data = dm.getData();
            Player p = event.getPlayer();
            if(data.getStringList("SneakyAdmins.players").contains(p.getName())){
                Bukkit.broadcastMessage("4");
                event.setCancelled(true);
                Block b = event.getBlock();
                Location loc = new Location(b.getWorld(), b.getX(), b.getY(), b.getZ());
                HashMap<String, BukkitRunnable> task = new HashMap<String, BukkitRunnable>();
                task.put(p.getName(), new BukkitRunnable(){
                    int i = 1;
                    public void run() {
                        i--;
                        if(i == 0)
                            loc.getBlock().setType(p.getItemInHand().getType());
                    }
                }); task.get(p.getName()).runTaskTimer(pl, 1, 1);
               
            }
        }
     
    But it still playing the sound
     
  8. It will still play the sound for the person who placed the block, because that is client side. Have you checked to see if other players still hear the sound?

    Edit: Also, I wouldn't use a task timer, the code as you have it will run the task every tick for the rest of time :)
     
  9. @St3venAU Using ProtocolLib can stop those sounds.
     
  10. I'm afraid I don't know how ProtocolLibs work. Know a good tutorial or explaining to me now?