Material.getMaterial() problems

Discussion in 'Plugin Development' started by kitor, Nov 20, 2012.

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

    kitor

    I want to change drop from one block to another.
    Somewhere in my code:
    Code:
    Block block = event.getBlock();   //get block
     
    System.out.println("DropEvent");
     
    System.out.println(block.getTypeId());
    System.out.println(block);
    block.setType(Material.getMaterial("DIRT"));   //change type to DIRT
    System.out.println(block.getTypeId());
    System.out.println(block);
     
    block.breakNaturally();    //should drop dirt
    
    In testing envirioment the block at the beginning is STONE. After executing I got in console:
    BUT when I change block.setType(Material.getMaterial("DIRT")); into e.g block.setType(Material.getMaterial("GLOWSTONE"));
    and everything is working ok. Some kind of bug, or I'm doing something wrong? Tested on api 1.4.5.
     
  2. Offline

    CeramicTitan

    Can you give me an brief explanation od what you are trying to do?
     
  3. Offline

    kitor

    I have two blocks, let them be LAMP_ON and LAMP_OFF
    In single case (that was only part of the code) if i get break event of LAMP_ON, i want it to drop item like it was LAMP_OFF.
    But anyway, why using one ID works, but another doesn't? The same applies when using integer IDs instead of names...
     
  4. Offline

    CeramicTitan

    In your code does it change dirt to glowstone?
     
  5. Offline

    kitor

    My sample code works when I use Material.getMaterial("GLOWSTONE") (i get glowstone dust, whatever the block was), but when I use Material.getMaterial("DIRT") - the block remains unchanged.

    I changed first post to make it more clear.
     
  6. Offline

    CeramicTitan

    Its because when the block breaks you are replacing it with dirt so in actual fact nothing is happenening.
     
  7. Offline

    kitor

    So then, why replacing it with dirt doesn't work, but with glowstone does?
     
  8. Offline

    CeramicTitan

    so you wan't if you break dirt it replaces it with glowstone? instead of replacing the drops with glowstone?
     
  9. Offline

    kitor

    well, not - when i got blockbreakevent, i check is it my block. if it is - i'm replacing it with this other block, after that call block.breaknaturally() and set event to cancelled
    with glowstone hardcoded - it is working - so i'm breaking stone and getting glowstone dust. but with dirt hardcoded, after breaking stone i'm getting cobblestone, but following previous case i should get dirt as a drop, right?

    I know, this may be not the best method to achieve that - but still, it should behave the same way in both cases, and it is actually not.
     
  10. Offline

    CeramicTitan

    Code:JAVA
    1. public void onBlockBreak(BlockBreakEvent event){
    2. Block block = event.getBlock(); //get block
    3. if(block.getType() == Material.GLOWSTONE){
    4. System.out.println("DropEvent");
    5.  
    6. System.out.println(block.getTypeId());
    7. System.out.println(block);
    8. block.setType(Material.DIRT); //change type to DIRT
    9. System.out.println(block.getTypeId());
    10. System.out.println(block);
    11.  
    12. block.breakNaturally(new ItemStack(Material.DIRT, 1)); //should drop dirt
    13. }
    14. }
    15. }
    16.  


    kitor
     
  11. Offline

    kitor

    Yes, this works, but in real code I have two variables: LAMP_ON and LAMP_OFF that holds block IDs:

    Code:JAVA
    1.  
    2. public void doNotStole(BlockBreakEvent event) {
    3. if (event.isCancelled()) {
    4. return;
    5. }
    6. Block block = event.getBlock();
    7. if (block.getTypeId() == LAMP_ON
    8. && block.getData() == LAMP_DATA) {
    9. System.out.println("DropEvent");
    10. block.setType(Material.DIRT);
    11. block.breakNaturally();
    12. event.setCancelled(true);
    13. }
    14. }


    in this case it is working, but instead of Material.DIRT it should use block from LAMP_OFF variable. That's why I wanted to use getMaterial() method.
     
  12. Offline

    CeramicTitan

    Does block.breakNaturally(new ItemStack(Material.DIRT, 1)); work?
     
  13. Offline

    kitor

    This gives me no drop at all.
     
  14. Offline

    CeramicTitan

    what about without the ',1' so its just new ItemStack(Material.DIRT));
     
  15. Offline

    kitor

    No, it's not working, but I found quite different, but working sollution:
    Code:JAVA
    1. public void doNotStole(BlockBreakEvent event) {
    2. if (event.isCancelled()) {
    3. return;
    4. }
    5. Block block = event.getBlock();
    6. if (block.getTypeId() == LAMP_ON
    7. && block.getData() == LAMP_DATA) {
    8. System.out.println("DropEvent");
    9. block.setTypeId(0);
    10. ItemStack drop = new ItemStack(LAMP_OFF, 1);
    11. World world = block.getWorld();
    12. world.dropItemNaturally(block.getLocation(), drop);
    13. event.setCancelled(true);
    14. }
    15. }
     
    CeramicTitan likes this.
Thread Status:
Not open for further replies.

Share This Page