help with setType()

Discussion in 'Plugin Development' started by boysnnoco, Apr 21, 2015.

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

    boysnnoco

    Code:
    @EventHandler
        public void onToggle(PlayerInteractEvent event) {
          
            if(event.getAction() == Action.PHYSICAL) {
              
                if(event.getClickedBlock().getType() == Material.STONE_PLATE) {
                    Block b = event.getClickedBlock();
                   //Nothing happens here
                    b.setType(Material.AIR);
                    event.getPlayer().setHealth(0D);
                    Location loc = event.getClickedBlock().getLocation();
                    loc.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 0.0f, false, false);
                }
              
            }
          
        }
    Ok so when i do b.setType(Material.AIR); it doesn't set the block to air. Everything else in the if statement runs and I don't know why can someone help me.
    Also, I was debugging it with Bukkit.broadcastMessage(b.getType().toString()); before and after I set the block and before it said stone_plate after it said air but it still showed that there was a stone_plate and acted as if it was still a stone_plate.
     
  2. Offline

    Zombie_Striker

    You have to update the players Inventory. This is the same for setting the type of any item. Whats being changed is all server-side, not client side.
    Code:
    player.updateInv();
     
    Last edited: Apr 21, 2015
  3. Offline

    boysnnoco

    Nope that doesn't work I just checked :/
     
  4. Offline

    bcohen9685

    Are you trying to kill the player?
     
  5. Offline

    Zombie_Striker

    Sorry, I thought the Block was an ItemStack. Disregard the last post.

    what you're doing by getting 'Block b' is creating a new instance of that block. Remove 'Block b' and instead use 'event.getBlock().setType(Material.AIR);'
     
  6. Offline

    boysnnoco

  7. Offline

    Konato_K

    @Zombie_Striker What, holding a reference of an object does not create a new instance

    @boysnnoco Have you tried using Block#breakNaturally?
     
  8. Offline

    boysnnoco

    Yeah Ive tried using breakNaturally that didn't work either.

    Edit:
    Also before anyone asks (or tells me to do this) I've tried using BlockStates and updating the block that way which didn't work.
     
  9. Offline

    caderape

    U might destroy the block under the preasure plate and set it again.
     
  10. Not to sure if this is the case, but it's worth a shot: If there are more then one type of stone plates in minecraft, that may be the problem..

    (Just an idea :p)
     
  11. Offline

    boysnnoco

    That would cause the plate to be dropped which is really annoying
     
  12. Offline

    Koobaczech

    Is this like a landmine code?
     
  13. Offline

    boysnnoco

  14. Offline

    Koobaczech

    Awesome. Letme see if i can fix it

    @boysnnoco Ok i got it!! This is what you need to do. Basically, the PLATE changes when pressed versus not pressed. So you need to make the plate not activate for the code to work xD And to not get it to activate you need to cancel the event with this
    Code:
    e.setCancelled(true);
    e.getClickedBlock().setTypeId(0);
    I found that out because i was making the plate break naturally, and then looping through the dropped entitys to remove them right away, but noticed it was spawning a new plate of the type Not Pressed and the actual plate was becoming pressed, so i figured the event would have to be cancelled

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  15. Offline

    boysnnoco

    Wow thanks!
     
Thread Status:
Not open for further replies.

Share This Page