Help with attributes

Discussion in 'Plugin Development' started by DarkBladee12, Jul 4, 2013.

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

    DarkBladee12

    Hey guys, I'm currently trying to find out how to modify specific attributes of entities and items. I started with a simple method which should modify the attack strength of an entity, but when I use this method the entity instantly dies and I have no idea why...

    That's my code:

    Code:java
    1. public static void changeAttackDamage(LivingEntity le, double damage) {
    2. EntityLiving el = ((CraftLivingEntity) le).getHandle();
    3. NBTTagCompound nbt = new NBTTagCompound();
    4. el.b(nbt);
    5. NBTTagList list = nbt.getList("Attributes");
    6. NBTTagList nlist = new NBTTagList();
    7. for (int i = 0; i < list.size(); i++) {
    8. AttributeModifier am = GenericAttributes.a((NBTTagCompound) list.get(i));
    9. AttributeModifier nam = am;
    10. String name = am.b();
    11. if (name.equals("generic.attackDamage")) {
    12. nam = new AttributeModifier(am.b(), damage, am.c());
    13. }
    14. nlist.add(AttributeChanger.convertToNBT(nam));
    15. }
    16. nbt.set("Attributes", nlist);
    17. el.a(nbt);
    18. }
    19.  
    20. private static NBTTagCompound convertToNBT(AttributeModifier am) {
    21. NBTTagCompound nbtTagCompound = new NBTTagCompound();
    22. nbtTagCompound.setString("Name", am.b());
    23. nbtTagCompound.setDouble("Amount", am.d());
    24. nbtTagCompound.setInt("Operation", am.c());
    25. nbtTagCompound.setLong("UUIDMost", am.a().getMostSignificantBits());
    26. nbtTagCompound.setLong("UUIDLeast", am.a().getLeastSignificantBits());
    27. return nbtTagCompound;
    28. }


    T3h Cr33p3r I know that this is possible, but potion effects multiply the base damage and attributes make it possible to change the base damage and much more :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    metalhedd likes this.
  2. Offline

    Jogy34

  3. Offline

    DarkBladee12

    Jogy34 Does this mean that I have to Make a list where I add the modified list and apply it to the NBT tag or what? Could you post some code so I could see how to do it? I don't understand all this NBT stuff :/

    T3h Cr33p3r Well I'm trying to modify the new attributes that have been implemented in 1.6 not messing around with potion effects... The attack strength is just a test, you can modify even more stuff which is not possible with potion effects!

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

    Cybermaxke

  5. Offline

    DarkBladee12

    Cybermaxke Thanks, I'll see if it helps me to make my methods work ;)

    EDIT: That didn't help me either, the entity still just dies... That's my current code:
    Code:java
    1. public static void changeAttackDamage(LivingEntity le, double damage) {
    2. EntityLiving el = ((CraftLivingEntity) le).getHandle();
    3. NBTTagCompound tag = new NBTTagCompound();
    4. el.b(tag);
    5. NBTTagList list = new NBTTagList();
    6. list.add(getTag(new AttributeModifier("generic.attackDamage", damage, 0)));
    7. for (AttributeModifier modifier : getModifiers(el)) {
    8. if (!modifier.b().equals("generic.attackDamage")) {
    9. list.add(getTag(modifier));
    10. }
    11. }
    12. tag.set("Attributes", list);
    13. el.a(tag);
    14. }
    15.  
    16. public static List<AttributeModifier> getModifiers(EntityLiving el) {
    17. List<AttributeModifier> modifiers = new ArrayList<AttributeModifier>();
    18. NBTTagCompound tag = new NBTTagCompound();
    19. el.b(tag);
    20. NBTTagList taglist = tag.getList("Attributes");
    21. List<NBTBase> list = getList(taglist);
    22. Iterator<NBTBase> iter = list.iterator();
    23. while (iter.hasNext()) {
    24. NBTTagCompound tag2 = (NBTTagCompound) iter.next();
    25. modifiers.add(getModifier(tag2));
    26. }
    27. return modifiers;
    28. }
    29.  
    30. @SuppressWarnings("unchecked")
    31. public static List<NBTBase> getList(NBTTagList list) {
    32. try {
    33. Field f = NBTTagList.class.getDeclaredField("list");
    34. f.setAccessible(true);
    35. return (List<NBTBase>) f.get(list);
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }
    39.  
    40. return null;
    41. }
    42.  
    43. private static NBTTagCompound getTag(AttributeModifier modifier) {
    44. NBTTagCompound tag = new NBTTagCompound();
    45. tag.setString("Name", modifier.b());
    46. tag.setDouble("Amount", modifier.d());
    47. tag.setInt("Operation", modifier.c());
    48. tag.setLong("UUIDMost", modifier.a().getMostSignificantBits());
    49. tag.setLong("UUIDLeast", modifier.a().getLeastSignificantBits());
    50. return tag;
    51. }
    52.  
    53. public static AttributeModifier getModifier(NBTTagCompound tag) {
    54. String name = tag.getString("Name");
    55. double amount = tag.getDouble("Amount");
    56. int operation = tag.getInt("Operation");
    57. UUID id = new UUID(tag.getLong("UUIDMost"), tag.getLong("UUIDLeast"));
    58. return new AttributeModifier(id, name, amount, operation);
    59. }
     
  6. Offline

    Jogy34


    Code:
    NBTTagCompound attributes = new NBTTagCompound();
    attributes.addString("Name", "generic.attackDamage");
    attributes.addDouble("Amount", 10.0);
    NBTTagList modifiers = new NBTTagList();
    NBTTagCompound first = new NBTTagCompound();
    first.addString("Name", "This is a Name");
    first.addDouble("Amount", 10);
    first.addInt("Opperation", 0); //Adds the amount to the base, 1 will multiply the base by the amount, 2 will multiply the current value by the amount
    UUID uuid = UUID.randomUUID();
    first.addLong("UUIDMost", uuid.getMostSignificantBits());
    first.addLong("UUIDLeast", uuid.getLeastSignificantBits());
    modifiers.add(first);
    attributes.setList("Modifiers", modifiers);
    //Give the mob the attributes tag here
    
    I think this should work however it is completely untested so I'm not sure


    Not true, you should be able to do it by using Craftbukkit/NMS code. A lot of people don't like using Craftbukkit or NMS code because it easily gets a lot more complicated and it has to be updated on every bukkit release due to the dynamically changing package names.

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

    LucasEmanuel

  8. Offline

    Jogy34

    net.minecraft.server, its one of the packages in the craftbukkit.jar file. The bukkit.jar just contains the Bukkit API, the Craftbukkit code in the craftbukkit.jar is the actual implementation of the Bukkit API interfaces, and the NMS code in the craftbukkit.jar is the actual server along with a few extra bits that allows the craftbukkit code to interface with it easier and is, well, what the craftbukkit code interfaces with. The craftbukkit.jar also contains the API
     
  9. Offline

    Cybermaxke

    DarkBladee12
    I will look into the attribute system for entities, cause its not using the same way. ;)
     
  10. Offline

    DarkBladee12

    Jogy34 Well your code doesn't work either... Cybermaxke If I just add a new attribute modifier for "generic.attackDamage" the zombie does only 1 damage instead of 2 - 3 no matter which value i choose for "Amount". If I just modify the amount off the current attribute modifier it won't do anything. That's the code I use for modifying the existant modifier:

    Code:java
    1. public static void changeAttackDamage(LivingEntity le, double damage) {
    2. EntityLiving el = ((CraftLivingEntity) le).getHandle();
    3. NBTTagCompound tag = new NBTTagCompound();
    4. el.b(tag);
    5. NBTTagList attributes = tag.getList("Attributes");
    6. NBTTagList list = new NBTTagList(attributes.getName());
    7. for (int i = 0; i < attributes.size(); i++) {
    8. NBTTagCompound tag2 = (NBTTagCompound) attributes.get(i);
    9. String name = tag2.getString("Name");
    10. if (name.equals("generic.attackDamage")) {
    11. tag2.setDouble("Amount", damage);
    12. }
    13. list.add(tag2);
    14. }
    15. tag.set("Attributes", list);
    16. el.a(tag);
    17. }
     
  11. Offline

    Cybermaxke

    Last edited by a moderator: Jun 3, 2016
  12. Offline

    DarkBladee12

    Cybermaxke Thanks for this, I'll look into it tomorrow ;)
     
  13. Offline

    Cybermaxke

    There are currently problems but I am fixing them now, so you will be able to see working code tomorrow. ;)
     
Thread Status:
Not open for further replies.

Share This Page