Reflection assistance

Discussion in 'Spigot Plugin Development' started by Realm, Apr 28, 2017.

  1. I'm attempting to change the speed at which players can mine...
    I believe I need to use NMS to do that. Currently, this is the code I have:


    Code (Text):
            for (Material material : hardness.keySet()) {

                Field field = CraftMagicNumbers.getBlock(material).getClass().getDeclaredField("strength");
                CraftMagicNumbers.getBlock(material).c(1f);
            }
    However, the whole field line is red underlined, stating
    "Unhandled exception: java.lang.NoSuchFieldException"

    I think it's because I have the wrong imports, I have these imports:

    Code (Text):
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.craftbukkit.v1_8_R3.util.CraftMagicNumbers;

    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Set;
    Also, if it helps.. Im using maven. Any ideas ? ;/
     
  2. Add a try catch block? That's what an exception is.
     
    • Agree Agree x 1
  3. Update:

    I've now tried this (with the exact same error mind you...)

    Code (Text):
                Method method=net.minecraft.server.v1_8_R3.Block.class.getDeclaredMethod("c", float.class);
                Field field = net.minecraft.server.v1_8_R3.Block.class.getDeclaredField("strength");
    FYI, I can't import these because I already have a 'Block' import (Spigot's)
     
  4. Holy shit... *facepalm*, shoulda known that... lol its late. Thanks for the help buddy
     
    • Optimistic Optimistic x 1
  5. Did you try reading this ^
     
    • Optimistic Optimistic x 1
  6. He was correct... it was an unhandled exception. Thats about it lol
     
  7. On another note though... along the same line of thought... is there a way to loop through all the blocks listed by minecraft without using spigot, and have a similar functionality to Material#matchMaterial ?
     
  8. Material.values() as it's an enum.
     
  9. Looping through every material? do
    Code (Text):
    for(Material m : Material.values()) {
        // Your code...
    }
     
  10. Is there a list of valid materials? I just want to know so that I can provide a list of valid entries in the config, so owners can set the hardness of a block manually:p


    Sent from my iPhone using Tapatalk
     
  11. Material#isBlock ?
     
  12. Like an online link I can provide to the owners, so they know all valid entries


    Sent from my iPhone using Tapatalk
     
  13. Why don't you give the player haste?
     
  14. Not as authentic, plus I just simply don't want them to have a potion effect.


    Sent from my iPhone using Tapatalk
     
  15. Mas

    Mas

  16. Either the JavaDoc or just loop the following and make a <ul> list to provide:
    Code (Java):
    List<Material> materials = Arrays.stream(Material.values()).filter(Material::isBlock).collect(Collectors.toList());
     
  17. Thankyou for this


    Sent from my iPhone using Tapatalk