Solved [1.12.2] Setting Block Data Back One Integer

Discussion in 'Spigot Plugin Development' started by Summoi, Jul 19, 2018.

  1. I know this is probably a basic thing in java but I have no idea how to do, (I'm self taught)
    But lets say I have a block with a data value of 3


    Code (Text):
    if (block.getData() == 3) {
            //do nothing
    }
    And I wanna change that value to 2 but I don't wanna just set it to 2 I wanna subtract the integer by 1
    So for instance


    Code (Text):
    if (block.getData() == 3) {
       event.getClickedBlock().setData((byte)2);
    }
    Instead of setting it to an integer of 2 I wanna subtract so I don't have to make a loop of code...
    Pretty basic but any ideas?


    ---------------------
    So I just made a way around it instead...


    Code (Text):
                        if (block.getData() == 1) {
                            block.setData((byte) 0);
                        }
                        if (block.getData() == 2) {
                            block.setData((byte) 1);
                        }
                        if (block.getData() == 3) {
                            block.setData((byte) 2);
                        }
     
    #1 Summoi, Jul 19, 2018
    Last edited: Jul 19, 2018
    • Funny Funny x 1
  2. MiniDigger

    Supporter

    well, just remove your if statements and just the the data to the current data minus one. might also check that it never goes under 0.
     
  3. Yeah so the code I have so far is


    Code (Text):
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onDye (PlayerInteractEvent event) {
                Player player = event.getPlayer();
                Block block = event.getClickedBlock();
               
                if (block.getType().equals(Material.CAULDRON)) {
                    if (block.getData() == 0) {
                        //do nothing
                    } else {
                    if (player.getItemInHand().containsEnchantment(Enchantment.DAMAGE_ALL)) {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7(&c!&7) &e-> &aYou place your item in the cauldron."));
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            //do nothing
                            e.printStackTrace();
                        }
                        player.getInventory().getItemInHand().removeEnchantment(Enchantment.DAMAGE_ALL);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7(&c!&7) &e-> &aThe item comes back cleaned from all enchants."));
                        //need to set blockdata here
                        }
                    }
                }
        }
    How would I set the data to minus one that's my whole question sorry if i'm wording it wrong.
     
  4. MiniDigger

    Supporter

    first, remove the sleep. you are halting the whole server for 2 seconds.
    use the bukkit scheduler and shedule a delayed task.

    I don't really see where your problem with the decrementing is.
    smth like block.setdata (block.getdata -1) should work just fine.
     
  5. The sleep was for testing I'm not keeping it, and block.getdata doesn't allow any integer you mean setdata?
     
  6. Thread solved see code above
     
  7. @Summoi you could easily do
    Code (Text):
    block.setData(((byte)block.getData()-1)>-1?((byte)block.getData()-1):(0))
    This would let you subtract it, and also, if its less than 0 after subtraction: which means a nullpointer, you could reset it to any value you want. Sorry for spoon feeding.

    Think about it more,
    if you get the data as a Byte.
    Code (Text):
    byte k=block.getData();
    if(k==0)k=block.getData();
    else k--;
    block.setData(k);
    Would work perfectly right?