Solved Getting Thrown Snowball Lore

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

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

    andstu

    I am writing a pokeball plugin where when a mob is hit by a snowball, the mob drops a snowball with all of its data stored in its lore. I am trying to get the Lore from the thrown snowball and I can't figure out how. Is this possible or should I take a different approach?


    DevilMental
     
  2. Offline

    Wingas

  3. Offline

    DevilMental

    andstu You listen to PlayerInteractEvent, you get the itemstack, you get the ItemMeta, you get the Lore and throw a snowball (and set the player as the shooter). You put put it in a HashMap<UUID, List<String>>, where you put the UUID of the player, and the lore. Then you listen to the ProjectileHitEvent, you check if it's a snowball, you get the shooter, you get the lore from the HashMap, then you spawn the horse with the lore at the Location (Y + 2).
    Hope that helps,
    ~DevilMental

    https://forums.bukkit.org/posts/2687527/

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

    Dragonphase

    andstu

    Projectile extends Metadatable, which means you can get and set Metadata on it. When you throw the snowball, set its Metadata to match the lore of it's ItemStack counterpart.
     
  5. Offline

    DevilMental

    That's also a way around it :p
    andstu You can add Metadata to the pokeball, so you add the List<String> to the pokeball's metadata but it will be resetted every time you shutdown your server/reload your server...
     
  6. Offline

    Dragonphase

    DevilMental
    That doesn't seem like a big deal, since the projectile's lifetime is pretty short.
     
  7. Offline

    DevilMental

    I meant it for the Item, the Projectile Metadata was a pretty good idea ;)
     
  8. Offline

    andstu

    Are HashMaps usable in between Methods? So I could create the HashMap in one method and then use it in another? DevilMental

    Wingas Here is My latest code (before I have tried DevilMental 's idea):
    Code:java
    1. package me.atlightning11.Pokeball;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6.  
    7. import net.minecraft.server.v1_7_R3.AttributeInstance;
    8. import net.minecraft.server.v1_7_R3.EntityInsentient;
    9. import net.minecraft.server.v1_7_R3.GenericAttributes;
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    26. //import org.bukkit.Color;
    27. import org.bukkit.Location;
    28. import org.bukkit.Material;
    29. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity;
    30. import org.bukkit.entity.AnimalTamer;
    31. import org.bukkit.entity.Animals;
    32. import org.bukkit.entity.Bat;
    33. import org.bukkit.entity.Blaze;
    34. import org.bukkit.entity.CaveSpider;
    35. import org.bukkit.entity.Chicken;
    36. import org.bukkit.entity.Cow;
    37. import org.bukkit.entity.Creeper;
    38. import org.bukkit.entity.Damageable;
    39. import org.bukkit.entity.Enderman;
    40. import org.bukkit.entity.Entity;
    41. import org.bukkit.entity.Ghast;
    42. import org.bukkit.entity.Horse;
    43. import org.bukkit.entity.LivingEntity;
    44. import org.bukkit.entity.Horse.Style;
    45. import org.bukkit.entity.MagmaCube;
    46. import org.bukkit.entity.MushroomCow;
    47. import org.bukkit.entity.Ocelot;
    48. import org.bukkit.entity.Ocelot.Type;
    49. import org.bukkit.entity.Pig;
    50. import org.bukkit.entity.PigZombie;
    51. import org.bukkit.entity.Projectile;
    52. import org.bukkit.entity.Sheep;
    53. import org.bukkit.entity.Silverfish;
    54. import org.bukkit.entity.Skeleton;
    55. import org.bukkit.entity.Slime;
    56. import org.bukkit.entity.Snowball;
    57. import org.bukkit.entity.Horse.Variant;
    58. import org.bukkit.entity.Spider;
    59. import org.bukkit.entity.Squid;
    60. import org.bukkit.entity.Witch;
    61. import org.bukkit.entity.Wolf;
    62. import org.bukkit.entity.Zombie;
    63. import org.bukkit.event.EventHandler;
    64. import org.bukkit.event.Listener;
    65. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    66. import org.bukkit.event.entity.ProjectileHitEvent;
    67. import org.bukkit.inventory.ItemStack;
    68. import org.bukkit.inventory.meta.ItemMeta;
    69. import org.bukkit.plugin.PluginDescriptionFile;
    70. import org.bukkit.plugin.java.JavaPlugin;
    71.  
    72. public class Pokeball extends JavaPlugin implements Listener{
    73. public final Logger logger = Logger.getLogger("Minecraft");
    74. public static Pokeball plugin;
    75.  
    76. @Override
    77. public void onDisable () {
    78. PluginDescriptionFile pdfFile = this.getDescription();
    79. this.logger.info(pdfFile.getName() + "Has Been Disabled!");
    80. }
    81.  
    82. @Override
    83. public void onEnable() {
    84. PluginDescriptionFile pdfFile = this.getDescription();
    85. this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has Been Enabled!");
    86. getServer().getPluginManager().registerEvents(this, this);
    87. }
    88.  
    89. public ItemStack horse(Horse h) {
    90. Style style = h.getStyle();
    91. String st = style.name();
    92. org.bukkit.entity.Horse.Color color = h.getColor();
    93. String c = color.name();
    94. Variant variant = h.getVariant();
    95. String v = variant.name();
    96. //this.logger.info("Horse type " + v);
    97. double jump = h.getJumpStrength();
    98. //this.logger.info("Jump Strength " + jump);
    99. boolean tamed = h.isTamed();
    100. //this.logger.info("Is Tamed? " + tamed);
    101. Damageable d = (Damageable) h;
    102. double health = d.getMaxHealth();
    103. AttributeInstance s = ((EntityInsentient)((CraftLivingEntity)h).getHandle()).getAttributeInstance(GenericAttributes.d);
    104. double speed = s.getValue();
    105. //Creds to ToastHelmi for getting the speed
    106. //this.logger.info("Speed: " + speed);
    107. String name = h.getCustomName();
    108. //this.logger.info("Name: " + name);
    109. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    110. ItemMeta m = snow.getItemMeta();
    111. List<String> HD = new ArrayList<String>();
    112. HD.add("Horse");
    113. HD.add(name);
    114. HD.add(v);
    115. HD.add(st);
    116. HD.add(c);
    117. String j = Double.toString(jump);
    118. HD.add(j);
    119. String sp = Double.toString(speed);
    120. HD.add(sp);
    121. String t = Boolean.toString(tamed);
    122. HD.add(t);
    123. String mh = Double.toString(health);
    124. HD.add(mh);
    125. m.setDisplayName("Pokeball");
    126. m.setLore(HD);
    127. snow.setItemMeta(m);
    128. return snow;
    129. }
    130.  
    131. public ItemStack ocelot(Ocelot o){
    132. Type type = null;
    133. boolean tamed = o.isTamed();
    134. boolean adult = o.isAdult();
    135. String name = o.getCustomName();
    136. List<String> OD = new ArrayList<String>();
    137. OD.add("Ocelot");
    138. OD.add(name);
    139. OD.add(Boolean.toString(adult));
    140. OD.add(Boolean.toString(tamed));
    141. if (tamed) {
    142. type = o.getCatType();
    143. String t = type.name();
    144. OD.add(t);
    145. AnimalTamer owner = o.getOwner();
    146. String ow = owner.getName();
    147. OD.add(ow);
    148. }
    149. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    150. ItemMeta m = snow.getItemMeta();
    151. m.setDisplayName("Pokeball");
    152. m.setLore(OD);
    153. snow.setItemMeta(m);
    154.  
    155.  
    156. return snow;
    157. }
    158.  
    159. public ItemStack wolf(Wolf w) {
    160. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    161. ItemMeta m = snow.getItemMeta();
    162. m.setDisplayName("Pokeball");
    163. boolean tamed = w.isTamed();
    164. boolean angry = w.isAngry();
    165. boolean adult = w.isAdult();
    166. String name = w.getCustomName();
    167. List<String> WD = new ArrayList<String>();
    168. if (!tamed) {
    169. WD.add("Wolf");
    170. }
    171. if (tamed) {
    172. WD.add("Dog");
    173. }
    174. WD.add(name);
    175. WD.add(Boolean.toString(angry));
    176. WD.add(Boolean.toString(adult));
    177. if (tamed) {
    178. AnimalTamer owner = w.getOwner();
    179. String o = owner.getName();
    180. WD.add(o);
    181. }
    182. m.setLore(WD);
    183. snow.setItemMeta(m);
    184.  
    185. return snow;
    186. }
    187. public ItemStack zombie(Zombie z) {
    188. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    189. ItemMeta m = snow.getItemMeta();
    190. m.setDisplayName("Pokeball");
    191. boolean villager = z.isVillager();
    192. boolean baby = z.isBaby();
    193. String name = z.getCustomName();
    194. List<String> ZD = new ArrayList<String>();
    195. ZD.add("Zombie");
    196. ZD.add(name);
    197. ZD.add(Boolean.toString(villager));
    198. ZD.add(Boolean.toString(baby));
    199. m.setLore(ZD);
    200. snow.setItemMeta(m);
    201. return snow;
    202. }
    203. public ItemStack zombiepig(PigZombie p) {
    204. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    205. ItemMeta m = snow.getItemMeta();
    206. m.setDisplayName("Pokeball");
    207. boolean baby = p.isBaby();
    208. boolean angry = p.isAngry();
    209. String name = p.getCustomName();
    210. List<String> PD = new ArrayList<String>();
    211. PD.add("Zombie Pigman");
    212. PD.add(name);
    213. PD.add(Boolean.toString(baby));
    214. PD.add(Boolean.toString(angry));
    215. m.setLore(PD);
    216. snow.setItemMeta(m);
    217. return snow;
    218. }
    219. public ItemStack other(LivingEntity e) {
    220. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    221. ItemMeta m = snow.getItemMeta();
    222. m.setDisplayName("Pokeball");
    223. String name = e.getCustomName();
    224. List<String> OD = new ArrayList<String>();
    225. if (e instanceof Creeper) OD.add("Creeper");
    226. if (e instanceof Skeleton) OD.add("Skeleton");
    227. if (e instanceof Ghast) OD.add("Ghast");
    228. if (e instanceof Slime) OD.add("Slime");
    229. if (e instanceof MagmaCube) OD.add("Magma Cube");
    230. if (e instanceof Blaze) OD.add("Blaze");
    231. if (e instanceof Bat) OD.add("Bat");
    232. if (e instanceof MushroomCow) OD.add("Mooshroom");
    233. if (e instanceof Silverfish) OD.add("Silverfish");
    234. if (e instanceof Squid) OD.add("Squid");
    235. if (e instanceof Spider) OD.add("Spider");
    236. if (e instanceof CaveSpider) OD.add("Cave Spider");
    237. if (e instanceof Enderman) OD.add("Enderman");
    238. if (e instanceof Witch) OD.add("Witch");
    239. OD.add(name);
    240. m.setLore(OD);
    241. snow.setItemMeta(m);
    242. return snow;
    243. }
    244. public ItemStack animal(Animals a) {
    245. ItemStack snow = new ItemStack(Material.SNOW_BALL, 1);
    246. ItemMeta m = snow.getItemMeta();
    247. m.setDisplayName("Pokeball");
    248. boolean adult = a.isAdult();
    249. String name = a.getCustomName();
    250. List<String> AD = new ArrayList<String>();
    251. if (a instanceof Chicken) AD.add("Chicken");
    252. if (a instanceof Pig) AD.add("Pig");
    253. if (a instanceof Sheep) AD.add("Sheep");
    254. if (a instanceof Cow) AD.add("Cow");
    255. AD.add(name);
    256. AD.add(Boolean.toString(adult));
    257. m.setLore(AD);
    258. snow.setItemMeta(m);
    259. return snow;
    260. }
    261.  
    262.  
    263.  
    264. @EventHandler
    265. public void onEntityDamageByEntity(EntityDamageByEntityEvent event){
    266. //this.logger.info("ENTITY DAMAGED");
    267.  
    268. ItemStack snow = null;
    269. Location loc = null;
    270.  
    271. Entity e = event.getEntity();
    272. org.bukkit.World world = e.getWorld();
    273. Location location = e.getLocation();
    274.  
    275. Entity Damager = event.getDamager();
    276.  
    277.  
    278. if (Damager instanceof Snowball) {
    279. if (e instanceof Horse){
    280. Horse h = (Horse) e;
    281. snow = horse(h);
    282. //world = h.getWorld();
    283. //location = h.getLocation();
    284. }
    285. if (e instanceof Ocelot) {
    286. this.logger.info("Ocelot");
    287. Ocelot o = (Ocelot) e;
    288. snow = ocelot(o);
    289. //world = o.getWorld();
    290. //location = o.getLocation();
    291. }
    292. if (e instanceof Wolf) {
    293. Wolf w = (Wolf) e;
    294. snow = wolf(w);
    295. //world = w.getWorld();
    296. //location = w.getLocation();
    297. }
    298. if (e instanceof Zombie) {
    299. Zombie z = (Zombie) e;
    300. snow = zombie(z);
    301. }
    302. if (e instanceof PigZombie) {
    303. PigZombie p = (PigZombie) e;
    304. snow = zombiepig(p);
    305. }
    306. if (e instanceof Pig || e instanceof Cow || e instanceof Chicken || e instanceof Sheep) {
    307. Animals a = (Animals) e;
    308. snow = animal(a);
    309. //world = a.getWorld();
    310. //location = a.getLocation();
    311. }
    312. if (e instanceof Creeper || e instanceof Skeleton || e instanceof Ghast || e instanceof Slime || e instanceof MagmaCube || e instanceof Blaze || e instanceof Bat || e instanceof MushroomCow || e instanceof Silverfish || e instanceof Squid || e instanceof Spider || e instanceof CaveSpider || e instanceof Enderman || e instanceof Witch) {
    313. LivingEntity c = (LivingEntity) e;
    314. snow = other(c);
    315. //world
    316. }
    317. loc = new Location(world, 0, 0, 0);
    318. e.teleport(loc);
    319. world.dropItemNaturally(location, snow);
    320. }
    321. }


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

    Wingas

    maybe this work, this one detects throwing snowball to pig (not tested)

    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
    3. if (e.getDamager().getType() == EntityType.SNOWBALL) {
    4. if(!(e.getEntity() instanceof Player)){
    5. if(e.equals(EntityType.PIG)){
    6. ItemStack itemStack = new ItemStack(Material.SNOW_BALL);
    7. ((HumanEntity) e.getDamager()).getInventory().addItem(itemStack);
    8. }
    9. }
    10. }
    11. }
     
  10. Offline

    andstu

    Wingas I already detected when and what the snowball hits. It saves the data of the mob that it hits into the item's lore. I just need a way to get the information from the lore in order to respawn the mob.
     
  11. Offline

    DevilMental

    andstu If you put the HashMap into the class and you declare it public (static or not) then yes you can access the hashmap everywhere.
     
  12. Offline

    andstu

    DevilMental I have never used a hashmap before :D. Is this a proper declaration?
    Code:java
    1. public HashMap<String, List<String>> lore = new HashMap<String, List<String>>();


    Also, how do I get the UUID of a player? DevilMental Dragonphase Wingas

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

    Dragonphase

    andstu

    Code:java
    1. Player.getUniqueId();
     
  14. Offline

    andstu

    DevilMental Dragonphase How would I spawn a custom entity Horse h? Here is my code:
    Code:java
    1. public void spawnhorse(org.bukkit.World world, Location loc, List<String> l){
    2. Horse h = null;
    3. h.setCustomName(l.get(1));
    4. Variant v = Variant.valueOf(l.get(2));
    5. h.setVariant(v);
    6. Style style = Style.valueOf(l.get(3));
    7. h.setStyle(style);
    8. org.bukkit.entity.Horse.Color color = org.bukkit.entity.Horse.Color.valueOf(l.get(4));
    9. h.setColor(color);
    10. double jump = Double.parseDouble(l.get(5));
    11. h.setJumpStrength(jump);
    12. double speed = Double.parseDouble(l.get(6));
    13. AttributeInstance s = ((EntityInsentient)((CraftLivingEntity)h).getHandle()).getAttributeInstance(GenericAttributes.d);
    14. s.setValue(speed);
    15. //Creds to ToastHelimi for Speed setting
    16. h.setTamed(Boolean.valueOf(l.get(7)));
    17. double health = Double.parseDouble(l.get(8));
    18. h.setMaxHealth(health);
    19. spawnEntity(loc, h);
     
  15. Offline

    DevilMental

    andstu Actually I am not sure about the lore.get(1) but use lore[0] because an array starts at 0, not at one. Also, to spawn an horse, don't set it to null, but spawn with the world.spawnEntity and cast it to Horse.
     
  16. Offline

    andstu

    DevilMental hmm. Here it says to use lore.get(x). I need to get the second element in the array because the first one just says what type of mob it is. Am I doing the spawnEntity wrong? I looked up the syntax and I think this should be correct but I just get errors under EntityType HORSE...
    Code:java
    1. Horse h = (Horse) world.spawnEntity(loc, EntityType HORSE);
     
  17. Offline

    DevilMental

    andstu It's EntityType.HORSE
     
  18. Offline

    L33m4n123


    It's because DevilMental obviously didn't read your Code ;)

    to acces an Array, yes you need to do array[index] however as you pass on a List<String> and not an Array it has to be done via .get(index)
     
  19. Offline

    andstu

    DevilMental I get an error under HORSE that says "HORSE cannot be resolved or is not a field"
     
  20. Offline

    Lazertx

    andstu Just replace the space in between EntityType Horse with a .
     
  21. Offline

    DevilMental

  22. Offline

    andstu

    DevilMental I know when I write EntityType.HORSE I get an error. do I have to declare it or something?

    Code:
    Code:java
    1. Horse h = (Horse) world.spawnEntity(loc, EntityType.HORSE);
    Under "HORSE" I get the error "HORSE cannot be resolved or is not a field"

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

    DevilMental

    andstu Did you import org.bukkit.entity.EntityType ?
     
  24. Offline

    andstu

    DevilMental WOW. I feel stupid. Thanks. I imported the wrong one. :)
     
  25. Offline

    DevilMental

  26. Offline

    andstu

    DevilMental Now I just gotta do the same thing for every other mob in the game and I am done! :eek:
     
  27. Offline

    DevilMental

    andstu What about adding the EntityType into the lore ? So that you don't need to repeat the same code for every entity
     
  28. Offline

    andstu

    DevilMental Good Idea! I will try that.

    DevilMental Dragonphase I have a string that has the name of the owner of a cat/dog, is there a way to turn that back into an AnimalTamer variable?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page