Increase Block Resistance/Strength?

Discussion in 'Plugin Development' started by Satros, Mar 20, 2012.

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

    Satros

    Maybe its all in my head, but I thought there used to be a way to set a blocks health/strength/resistance/durability (one of those) in bukkit. However I've scoured the APIs though and could find no such thing.

    Basically what I am trying to do is make it so given a block at a position on the map, increase or decrease the amount of time it takes for someone to break said block.

    The plan was to make all blocks in an area stronger so that people could not grief as easily, but still could if they were determined enough.

    I did find this thread http://forums.bukkit.org/threads/setting-block-resistance-values.56316/#post-939765
    But I'm having a hard time following what they did and if it was even within bukkit.

    Any help or leads is appreciated.
     
  2. Offline

    bleachisback

    It's possible to do, but only with NMS code (meaning it's not a bukkit API, but in the vanilla minecraft code). If you want to change it, simply do this:
    Code:java
    1. Method method=net.minecraft.server.Block.class.getDeclaredMethod("c", float.class);
    2. method.setAccessible(true);
    3. method.invoke(net.minecraft.server.Block.BED, 50.0F);

    This would make beds be the same hardness as obsidian. Just keep in mind that every time you update, you have to change "c" to whatever the obfuscated name is.

    Alternatively, you could do this:
    Code:java
    1. Field field=net.minecraft.server.Block.class.getDeclaredField("strength");
    2. field.setAccessible(true);
    3. field.setFloat(net.minecraft.server.Block.BED, 50.0F);

    This wouldn't require updating
     
  3. Offline

    fromgate

    Will it work with all beds? Or there is the way to change hardness of single block?
     
  4. Offline

    Sayshal

    Couldn't you set the strength of the tool being you at a set location (aka the block) ? instead of making the block harder, make the pick slower?
     
  5. Offline

    bleachisback

    Yes, I believe you do it the exact some way, but instead of using the static net.minecraft.server.Block.BED, you use the instance of the block.
     
  6. Offline

    ZeusAllMighty11

    He means will he be able to select a bed in-game for that hardness.
     
  7. Offline

    bleachisback

    I am confused by what you mean. I told him how "to change hardness of single block", like he said. Also, I don't know why you would want to go through the trouble of finding a single bed in-game to change the hardness of other beds when you could just use the static variable.
     
  8. Offline

    ZeusAllMighty11

    Perhaps he is trying to create an "anti-bed-grief" plugin? Dunno.

    bleachisback
     
  9. Offline

    fromgate

    I'm interesting to change hardness of block if player trying to break it, is not "owner" of area where block located. I think beds is only example :)
     
  10. Offline

    bleachisback

    yes, you can do that. Simply change
    Code:java
    1. field.setFloat(net.minecraft.server.Block.BED, 50.0F);


    to

    Code:java
    1. field.setFloat(blockYouWantToChange, 50.0F);


    Keep in mind that it's NMS Block, not Bukkit Block. To convert, do

    Code:java
    1. net.minecraft.server.Block nmsBlock=((CraftBlock)bukkitBlock).getHandle()
     
  11. Offline

    Satros

    I seem to be having some trouble with this. It is telling me that .getHandle() doesnt exist for type CraftBlock, and it also isn't recognizing Field (I'm guessing I need to import something but not sure where to import from)

    Code:text
    1. public void TriggerResistance(Block block, String teamName) {
    2. //set block to higher resistance based on location
    3. net.minecraft.server.Block nmsBlock=((CraftBlock)block).getHandle();
    4. Field field=net.minecraft.server.Block.class.getDeclaredField("strength");
    5. field.setAccessible(true);
    6. field.setFloat(nmsBlock, 50.0F);
    7. }
     
  12. Offline

    bleachisback

    you need to import java.lang.Reflect
    Also, I can't find how to get a specific block in NMS code, so you may just have to stick with making all blocks of that type the same strength.
     
  13. Offline

    Satros

    Hmm unfortunately that won't work for what I'm doing, because I need to change block resistance based on who is in what area, and looking through the NMS source it seems like it may not be possible, as it looks like the references to specific block's durability seems to be static for each block type.

    I guess I will have to develop a strange algorithm instead for the time being. Where upon damage the block turns to obsidian, but then on break it turns back into the original block and drops, will look weird but it will have to do.
     
  14. Offline

    bleachisback

    I think this is the best solution I've seen to this problem lol
     
  15. Offline

    nisovin

    Minecraft does not have a Block instance for each block in the game. There's only one Block per block type.
     
Thread Status:
Not open for further replies.

Share This Page