UUID not working correctly

Discussion in 'Plugin Development' started by lionkingist, Jul 18, 2014.

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

    lionkingist

    I have been working on this and I just can't get it to work. I want to be to spawn 2 wolfs on the player that has killed the first wolf. On line 44 is what i am having trouble with. It just does not seem to work.

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3. import java.util.UUID;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.entity.Wolf;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.entity.EntityDeathEvent;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public final class SpawnWolf implements CommandExecutor, Listener{
    18.  
    19. ArrayList<UUID> u = new ArrayList<UUID>();
    20.  
    21. public void spawnWolf(Player target, int number ){
    22. for(int x = 1; x <= number;x++){
    23. Wolf wolf= (Wolf) target.getLocation().getWorld().spawn(target.getLocation(), Wolf.class);
    24. wolf.setAngry(true);
    25. wolf.setTarget(target);
    26. wolf.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,600,3));
    27. wolf.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,600, 2));
    28. wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,600,2));
    29. wolf.setFireTicks(599);
    30. wolf.isAdult();
    31. wolf.setCustomName("A Pissed Wolf");
    32. wolf.setCustomNameVisible(true);
    33. UUID wolfID = wolf.getUniqueId();
    34. u.add(wolfID);
    35. }
    36. }
    37.  
    38. @EventHandler
    39. public void onWolfDeath(EntityDeathEvent event){
    40. Player player = (Player) event.getEntity().getKiller();
    41. UUID deadwolf = event.getEntity().getUniqueId();
    42. if(player instanceof Player){
    43. player.sendMessage("You are a player and just killed something");
    44. if(u.contains(deadwolf)){
    45. player.sendMessage("You killed a wolf!");
    46. }else{
    47. player.sendMessage("test");
    48. }
    49.  
    50. }
    51. }
    52.  
    53. public boolean onCommand(CommandSender sender,Command cmd, String label, String[] args){
    54.  
    55. Player player = (Player) sender;
    56. if(cmd.getName().equalsIgnoreCase("bully")){
    57. if(args.length < 1){
    58. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+"Use /bully [player] [type] [amount]");
    59. }
    60.  
    61. if(args[1].equalsIgnoreCase("wolf")){
    62. Player target = player.getServer().getPlayer(args[0]);
    63. int number = Integer.parseInt(args[2]);
    64. spawnWolf(target, number);
    65. }
    66. }
    67. return false;
    68. }
    69. }
    70.  



    Code:java
    1. public class Main extends JavaPlugin{
    2.  
    3. @Override
    4. public void onEnable(){
    5. getServer().getPluginManager().registerEvents(new SpawnWolf(), this);
    6. this.getCommand("bully").setExecutor(new SpawnWolf());
    7. }
    8.  
    9. @Override
    10. public void onDisable(){
    11.  
    12. }
    13.  
    14.  
    15. }
     
  2. Offline

    zombiekiller753

    lionkingist
    Did you register the listener?
    That's the only reason I can think of

    (Btw wolf.isAdult() does nothing)
     
  3. Offline

    lionkingist

    Yes i did, because when I kill and animal it says "You are a player and just killed something"
     
  4. Offline

    izarooni

    lionkingist
    You should make sure the killer isn't null
    And I'm not sure if this changes anything but couldn't you have done
    Code:java
    1. UUID deadwolf = player.getUniqueId();

    instead of
    Code:java
    1. UUID deadwolf = event.getEntity().getUniqueId();
     
  5. Offline

    _LB

    Then he would get the UUID of the player instead of the entity that the player killed.
     
  6. Offline

    lionkingist

    Yeah, I put this up here because i am getting no errors or anything so I have no idea what to do.
     
  7. Offline

    izarooni

    _LB
    Oh whoops. Got confused. Sorry
     
  8. Offline

    lionkingist

    Does anyone have an idea of another way of doing this?
     
  9. Offline

    GlacialCreeper

    Well, does it tell you test? Also, is there any exceptions showing up in the console when you hit a wolf?
     
  10. Offline

    lionkingist

    It does tell me test, and nothing shows up in the console.
     
  11. Offline

    izarooni

    GlacialCreeper
    I just tried his code and got no errors but both messages showed.
     
  12. Offline

    GlacialCreeper

    Hm wait, I just noticed that you never actually added the deadwolf to the array list. I think that's the problem, you only added the wolfID to u, but never deadwolf.
     
  13. Offline

    ImDeJay

    Try storing the UUID in the form of a string see if that changes anything.

    Also much wanna check if the entity that was killed was a wolf you your not executing this code everytime an entity is killed.

    if(event.getentity instanceof wolf){
    ^ psuedo code.
     
    GlacialCreeper likes this.
  14. Offline

    GlacialCreeper

    Yeah, definitely do that at at some point.

    lionkingist
    Code:java
    1. @EventHandler
    2. public void onWolfDeath(EntityDeathEvent event){
    3. Player player = (Player) event.getEntity().getKiller();
    4. Entity entity = event.getEntity();
    5. UUID dead = entity.getUniqueId();
    6. if(player instanceof Player){
    7. player.sendMessage("You are a player and just killed something");
    8. if (entity instanceof Wolf){
    9. u.add(dead);
    10. player.sendMessage("Wolf added!");
    11. }
    12. if(u.contains(entity)){
    13. player.sendMessage("You killed a wolf!");
    14. }else{
    15. player.sendMessage("test");
    16. }
    17.  
    18. }
    19. }

    I changed it up a bit, although you may have to adjust it to your needs. Does this work?

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

    lionkingist

    GlacialCreeper
    That did not work with my command, and even when i spawn a wolf and kill it, it does not say "Wolf added!"
     
  16. Offline

    GlacialCreeper

    Huh, that's strange. Lemme write my own code, and see if it works, and I'll get back to you.
     
  17. Offline

    lionkingist

    Ok, thanks for you help!
     
  18. Offline

    GlacialCreeper

    lionkingist, one question, why did you choose to use an array list to hold wolves? I mean, why hold wolf uuid's? No offense intended, but it seems strange (there doesn't seem a purpose other than testing if it has the dead wolf)
     
  19. Offline

    lionkingist

    GlacialCreeper
    I am not sure why I chose this, I am pretty new to java, and Bukkit, so I was looking ways to do this and i was someone use an array list so..
     
  20. Offline

    GlacialCreeper

    lionkingist its ok, but I took them off, as there seemed no purpose (sorry!). I got the bully command to work, but if you have no arguments, it says an internal error occurred. To fix this (just realized), make sure to check if the args[].length is equal to 1, before using if (args[1].equalsIgnoreCase("wolf"))....
    Anyway, here's my code, I can't test it right now, but here:
    Code:java
    1. package me.camo.plugin.spawnwolves.listeners;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.entity.Wolf;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15.  
    16. public class SpawnWolf implements CommandExecutor, Listener {
    17.  
    18. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    19. {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("bully")){
    22.  
    23. if (args.length == 0){
    24. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+"Use /bully [player] [type] [amount]");
    25. }
    26.  
    27. if (args.length == 1){
    28. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+"Use /bully [player] [type] [amount]");
    29. }
    30.  
    31. if (args.length == 2){
    32. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+"Use /bully [player] [type] [amount]");
    33. }
    34.  
    35. if (args.length == 3){
    36. if (args[1].equalsIgnoreCase("wolf")){
    37. Player target = player.getServer().getPlayer(args[0]);
    38. int number = Integer.parseInt(args[2]);
    39. spawnWolf(target, number);
    40. }
    41. }
    42. }
    43. return false;
    44. }
    45.  
    46.  
    47. public void spawnWolf(Player player, int number)
    48. {
    49. for(int x = 1; x <= number;x++){
    50. Wolf wolf= (Wolf) player.getLocation().getWorld().spawn(player.getLocation(), Wolf.class);
    51. wolf.setAngry(true);
    52. wolf.setTarget(player);
    53. wolf.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,600,3));
    54. wolf.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,600, 2));
    55. wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,600,2));
    56. wolf.setFireTicks(599);
    57. wolf.setAdult();
    58. wolf.setCustomName("A Pissed Wolf");
    59. wolf.setCustomNameVisible(true);
    60. }
    61. }
    62.  
    63. @EventHandler
    64. public void onWolfDeath(EntityDeathEvent event)
    65. {
    66. Player killer = event.getEntity().getKiller();
    67. Entity entity = event.getEntity();
    68.  
    69. killer.sendMessage(ChatColor.RED+"You've killed something!");
    70.  
    71. if (entity instanceof Wolf){
    72. killer.sendMessage(ChatColor.RED+"You've killed a wolf!");
    73. spawnWolf(killer, 2);
    74. }
    75.  
    76. }
    77.  
    78. }
    79.  

    As of right now, killing a wolf won't spawn them; Idk why.
     
  21. Offline

    lionkingist

    GlacialCreeper
    This does work, but the problem is the same because two wolfs will spawn when any wolf is killed and not the one that the command spawns on the player.

    Ok, I have redone the code with comparing the UUIDs. And it makes no sense in why it is not spawning the OGWolfDead because the UUID and UUIDdead are the same because they are print in chat and i can see that they are the same.
    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandExecutor;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.entity.Wolf;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDeathEvent;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. public final class SpawnWolf implements CommandExecutor, Listener{
    15.  
    16. String UUID;
    17.  
    18. public void OGWolf(Player target){
    19. Wolf wolf= (Wolf) target.getLocation().getWorld().spawn(target.getLocation(), Wolf.class);
    20. wolf.setAngry(true);
    21. wolf.setTarget(target);
    22. wolf.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,600,3));
    23. wolf.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,600, 2));
    24. wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,600,2));
    25. wolf.setFireTicks(599);
    26. wolf.setCustomName("A Pissed Wolf");
    27. wolf.setCustomNameVisible(true);
    28. UUID = wolf.getUniqueId().toString();
    29. target.sendMessage(UUID);
    30. }
    31.  
    32. public void OGWolfDead(Player target){
    33. Wolf wolf = (Wolf) target.getLocation().getWorld().spawn(target.getLocation(), Wolf.class);
    34. wolf.setAngry(true);
    35. wolf.setTarget(target);
    36. wolf.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,600,3));
    37. wolf.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,600, 2));
    38. wolf.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,600,2));
    39. wolf.setFireTicks(599);
    40. wolf.setBaby();
    41.  
    42. }
    43.  
    44. @EventHandler
    45. public void onWolfDeath(EntityDeathEvent event){
    46. Player player = (Player) event.getEntity().getKiller();
    47. Entity entity = event.getEntity();
    48. String UUIDdead = entity.getUniqueId().toString();
    49. if(player instanceof Player){
    50. player.sendMessage("You are a player and just killed something");
    51. player.sendMessage(UUIDdead);
    52. if (UUIDdead.equalsIgnoreCase(UUID)){
    53. player.sendMessage("It worked!");
    54. OGWolfDead(player);
    55. }
    56. }
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender,Command cmd, String label, String[] args){
    60. Player player = (Player) sender;
    61. if (cmd.getName().equalsIgnoreCase("bully")){
    62.  
    63. if (args.length == 0){
    64. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+" Use /bully [player] [type]");
    65. }
    66.  
    67. if (args.length == 1){
    68. sender.sendMessage(ChatColor.GOLD+"There is not enought arguments!"+ChatColor.RED+" Use /bully [player] [type]");
    69. }
    70.  
    71. if (args.length == 2){
    72. if (args[1].equalsIgnoreCase("wolf")){
    73. Player target = player.getServer().getPlayer(args[0]);
    74. OGWolf(target);
    75. }
    76. }
    77. }
    78. return false;
    79. }
    80. }


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

    fireblast709

  23. Offline

    lionkingist

  24. Offline

    fireblast709

    lionkingist according to your stacktrace, it was not set yet, which means that the appropiate hasn't been called yet
     
  25. Offline

    GlacialCreeper

    yeah, you see the thing is, methods can only see things initialized (things that were set) when they're in the class's curly braces. If they're set in a method, only that said method would see that it isn't null. NullPointerExceptions are very annoying :mad:
    EDIT: fireblast709 would making the string static work?
     
  26. Offline

    fireblast709

    GlacialCreeper new developer + static = a huge amount of exceptions and badly designed code on the long run. Also, static wouldn't solve it nonetheless
     
  27. Offline

    GlacialCreeper

    fireblast709 oh, my bad then. What would you use with static, as a plugin dev (or just in general), if I may ask?
     
  28. Offline

    lionkingist

    Should I save the uuid in a file?
     
  29. Offline

    mazentheamazin

    GlacialCreeper
    In OOP, static should be used for things that are not considered as object such as utilities; for managers I would recommend using singletons.
     
  30. Offline

    GlacialCreeper

    So, in a sense, like the Bukkit class? (everything is static in it)
     
Thread Status:
Not open for further replies.

Share This Page