Solved Getting Horse Information

Discussion in 'Plugin Development' started by andstu, Jul 22, 2014.

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

    andstu

    Hi, I am trying to get the store information about horses when you hit them with a snowball. So if you hit a black horse with a snowball, it will rename the snowball with the information about that horse. I am new to writing plugins so I get a lot of errors. This is all I have so far:
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    3. Entity e = event.getEntity();
    4. Entity Damager = event.getDamager();
    5.  
    6. if (e instanceof Horse){
    7. String variant = e.this.getVariant();
    8. String jump = e.this.getJumpStrength();
    9. }
    10. }


    I get inside the if statement that say : "e cannot be resolved to a type"
    I don't know how to target a specific horse. Thanks!
     
  2. Offline

    Zarkopafilis

    e.this => e
     
  3. Offline

    fireblast709

    andstu e.this is not syntactically correct... Cast e to Horse and use that for the variant and jump strength
     
    Garris0n and Zarkopafilis like this.
  4. Offline

    DevilMental

    fireblast709 and@Zarkopafilis provided :
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    3. Entity e = event.getEntity();
    4. Entity Damager = event.getDamager();
    5.  
    6. if (e instanceof Horse){
    7. Horse h = (Horse) e;
    8. String variant = h.getVariant();
    9. String jump = h.getJumpStrength();
    10. }
    11. }


    And I hope that's the solution to your problem,
    ~DevilMental
     
  5. Offline

    andstu

    It says that variant can't be a string and it has to be variable type "Variant". Is there any way that I could change the variable type "Variant" to be a string?
     
  6. Offline

    CynutsBR

    *Horse h = (Horse) e;
     
  7. Offline

    ZodiacTheories

    andstu

    Can a variant be a string?
     
  8. Offline

    daavko

    ZodiacTheories If you mean: "Can I get a variant from string?" then yes.
     
  9. Offline

    andstu

    It gives me an error if I say:
    Code:java
    1. String variant = h.getVariant();

    So I was hoping there was a way to convert it.
     
  10. Offline

    ZodiacTheories

    daavko

    I know, I was trying to get andstu to think but nvm :p
     
  11. Offline

    daavko

    ZodiacTheories When I'm online, you can't ask others :D (but only you)
     
    ZodiacTheories likes this.
  12. Offline

    DevilMental

    andstu
    You just use the .toString() method from Variant ;)

    ~DevilMental
     
  13. Offline

    AoH_Ruthless

    andstu
    The code DevilMental is incorrect. The horse variant is not a string but a variant, which is what your IDE is trying to tell you. Change the String type to Variant. I believe JumpStrength is a double (might be an integer). So you have to change String accordingly there as well.

    Edit: DevilMental (Sorry for double tag) What is the purpose in converting a Variant and integer/double to a string? The starting code OP provided does nothing to suggest he requires a string, and your code only served to confuse him. By the way, it is unwise to try and invocate .toString() on a number of some kind, but instead use String.valueOf()
     
  14. Offline

    DevilMental

    AoH_Ruthless That's just what I said, and the double is automaticly casted to String when initialized by a variable.
    AoH_Ruthless Sorry my mistake so for the new code :
    andstu
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    3. Entity e = event.getEntity();
    4. Entity Damager = event.getDamager();
    5.  
    6. if (e instanceof Horse){
    7. Horse h = (Horse) h;
    8. Variant v = h.getVariant();
    9. double jump = h.getJumpStrength();
    10. }
    11. }

    ~DevilMental
     
  15. Offline

    AoH_Ruthless

    DevilMental
    No, we did not say the same thing, but instead opposite. You are suggesting to OP to invocate .toString() on his variant variable. However, the OP doesn't require the variant in string format... So why go through the hassle and convert to String? Why not just make the variable with the Variant type?
     
  16. Offline

    DevilMental

    AoH_Ruthless Made a mistake, sorry (jet lag from New York to France)
     
  17. Offline

    AoH_Ruthless

    DevilMental
    Apology isn't required, we're all friends here :)
     
    ZodiacTheories likes this.
  18. Offline

    andstu

    AoH_Ruthless I needed a string so that I could store that information onto a snowball. I am trying to make a sort of pokeball plugin. ;)

    EDIT: Is the Variant variable type usable as a string? So I could name an item using a variant variable? DevilMental
     
  19. Offline

    DevilMental

    andstu So you could get the name of the variant by
    Code:java
    1. // Considering Variant v;
    2. v.name();
    3. //And getting from a string
    4. Varient fromString = Variant.valueOf(varientString);


    Tag me if you need me ;)
    ~DevilMental
     
  20. Offline

    andstu

    Sorry for all the questions DevilMental :D. I can't find a way to set or find the speed of a horse. Is that because there isn't a way?
    Code:java
    1. double health = h.getMaxHealth(); //The error says this is ambiguous is that ok?
    Is this error ok? Thanks for all of your help.

    I don't think any of this is working. :mad: I don't know if my logger isn't working (did I do something wrong with that?), or if it just doesn't work... I thought this was the best way to have it drop a snowball but that doesn't appear to be working either. :( Here is my code so far:
    Code:java
    1. import java.util.logging.Logger;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.entity.Horse;
    6. import org.bukkit.entity.Horse.Variant;
    7. import org.bukkit.entity.LivingEntity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.Snowball;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15.  
    16. public class PokeballListener implements Listener{
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18.  
    19. @EventHandler
    20. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    21. Entity e = event.getEntity();
    22. Entity Damager = event.getDamager();
    23. if (Damager instanceof Snowball) {
    24. if (e instanceof Horse){
    25. Horse h = (Horse) e;
    26. Variant variant = h.getVariant();
    27. String v = variant.name();
    28. this.logger.info("Horse type " + v);
    29. double jump = h.getJumpStrength();
    30. this.logger.info("Jump Strength " + jump);
    31. boolean tamed = h.isTamed();
    32. this.logger.info("Is Tamed? " + tamed);
    33. //double health = h.getMaxHealth(); //The error says this is ambiguous is that ok?
    34. //how to find speed of a horse?
    35. String name = h.getCustomName();
    36. if (name == null) {
    37. name = "Horse";
    38. }
    39. this.logger.info("Name: " + name);
    40. ItemStack snow = new ItemStack(Material.SNOW_BALL);
    41. ItemMeta m = snow.getItemMeta();
    42. m.setDisplayName((String) name + "\n" + (String) v +"\n"+ jump +"\n" + tamed/* +"\n"+ health*/);
    43. snow.setItemMeta(m);
    44. h.getInventory().addItem(snow);
    45. }
    46. }
    47. }
    48.  
    49.  
    50. }

    AoH_Ruthless
    DevilMental

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

    AoH_Ruthless

    andstu
    Build against bukkit, not craftbukkit (that will remove the ambiguity). You also need to mess around with attributes to modify or retrieve horse speed.
     
  22. Offline

    andstu

    AoH_Ruthless

    Thanks for the tip about the ambiguity. Could that be why nothing is working or I\s there something wrong with my logger? Because when I hit a horse with a snowball, nothing happens so I don't even know where to start debugging.
     
  23. Offline

    AoH_Ruthless

    andstu
    Some weird things with your display name setter line
    • You are casting String to name .. it's already a string.
    • You are casting String to v, it too is a string.
    • 'tamed' is a boolean value, not a string. You can't concatenate boolean values. Use Boolean.toString(boolean) -Scratch that, I forgot the compiler automatically concatenates it by invoking .toString()
     
  24. Offline

    andstu

    AoH_Ruthless DevilMental Where could I download "Bukkit"? I can only find the craftbukkit builds. Also I don't think that the onEntityDamageByEntity event is being activated because nothing is being logged to the console. It doesn't even log when I shoot it with an arrow... Am I using the wrong event? Thanks. Here is my updated code:
    Code:java
    1. package me.atlightning11.Pokeball;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Horse;
    8. import org.bukkit.entity.Horse.Variant;
    9. import org.bukkit.entity.Snowball;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15.  
    16. public class PokeballListener implements Listener{
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18.  
    19. @EventHandler
    20. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    21. this.logger.info("ENTITY DAMAGED");
    22. Entity e = event.getEntity();
    23.  
    24. Entity Damager = event.getDamager();
    25.  
    26.  
    27. if (Damager instanceof Snowball) {
    28. if (e instanceof Horse){
    29. Horse h = (Horse) e;
    30. Variant variant = h.getVariant();
    31. String v = variant.name();
    32. this.logger.info("Horse type " + v);
    33. double jump = h.getJumpStrength();
    34. this.logger.info("Jump Strength " + jump);
    35. boolean tamed = h.isTamed();
    36. this.logger.info("Is Tamed? " + tamed);
    37. //double health = h.getMaxHealth(); //The error says this is ambiguous is that ok?
    38. //how to find speed of a horse?
    39. String name = h.getCustomName();
    40. if (name == null) {
    41. name = "Horse";
    42. }
    43. this.logger.info("Name: " + name);
    44. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    45. ItemMeta m = snow.getItemMeta();
    46. m.setDisplayName(name + "\n" + v +"\n"+ jump +"\n" + tamed/* +"\n"+ health*/);
    47. snow.setItemMeta(m);
    48. org.bukkit.World world = h.getWorld();
    49. world.dropItemNaturally(h.getLocation(), snow);
    50. }
    51. }
    52. }
    53.  
    54.  
    55. }
    56.  
     
  25. Offline

    fireblast709

  26. Offline

    xTigerRebornx

    andstu Did you register your events? You are using the right event for checking damage
     
  27. Offline

    DevilMental

    andstu The ambiguity can be removed by :
    Code:java
    1. Damageable d = (Damageable) h;
    2. // then you can
    3. d.getHealth();
    4. d.getMaxHealth();
    5. // Etc...

    Also in your code, you set the displayname of the snowball with the information separated by \n but I think the \n are ignored. The bukkit.jar can be found at http://dl.bukkit.org/downloads/bukkit
    The speed of a horse (I think) is constant. And about the logger, instead of taking th Minecraft Logger, take the logger from the principal class (that extends JavaPlugin), by :
    Code:java
    1. MainClass.getLogger(); // Replace MainClass by your
    2. // main class that extends
    3. // JavaPlugin


    Hope that helps, tag me if you need help again ;)
    ~DevilMental
     
  28. Offline

    andstu

    DevilMental You were right about it ignoring the "\n" Do you know if there is a way for me to add linebreaks in the names? That was the only way that I could think of. Again Thanks for all your help.
     
  29. Offline

    DevilMental

    andstu Add the information in a List<String>, and set this string as the item's lore. Then, when triggered, get the information from the lore, to spawn the same kind of horse

    Hope that helps,
    ~DevilMental
     
  30. Offline

    andstu

    AoH_Ruthless Would this work to find the speed of a horse?
    Code:java
    1.  
    2. AttributeInstance s = ((EntityInsentient)((CraftLivingEntity)h).getHandle()).getAttributeInstance(GenericAttributes.d);
    3. double speed = s.getValue();
    4. //h is the horse that got hit with a snowball
     
Thread Status:
Not open for further replies.

Share This Page