My function not working D:

Discussion in 'Plugin Development' started by BadBoy6767, Sep 30, 2013.

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

    BadBoy6767

    Hey everyone. i got a function for a working minigame im making called BuildPVP.
    I need help cause i have a bug i cant squash. D:.

    onCommand Method:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] args) {
    3.  
    4. if (label.equalsIgnoreCase("setpos")) {
    5.  
    6. if (sender.hasPermission("buildpvp.setpos")) {
    7. Player p = (Player) sender;
    8.  
    9. this.getConfig().set("spawnX", p.getLocation().getX());
    10. this.getConfig().set("spawnY", p.getLocation().getY());
    11. this.getConfig().set("spawnZ", p.getLocation().getZ());
    12. this.getConfig().set("spawnWorld", p.getLocation().getWorld().getName());
    13. this.saveConfig();
    14. }
    15.  
    16. } else if (label.equalsIgnoreCase("bpjoin")) {
    17.  
    18. players.add(sender.getName());
    19. addPlayer(sender.getName());
    20.  
    21. } else if (label.equalsIgnoreCase("bpleave")) {
    22.  
    23. if (players.contains(sender.getName())) {
    24.  
    25. players.remove(sender.getName());
    26. removePlayer(sender.getName());
    27.  
    28. } else {
    29.  
    30. sender.sendMessage("Not contained.");
    31.  
    32. }
    33.  
    34. } else if (label.equalsIgnoreCase("setleavepos")) {
    35.  
    36. if (sender.hasPermission("buildpvp.setleavepos")) {
    37. Player p = (Player) sender;
    38.  
    39. this.getConfig().set("leaveSpawnX", p.getLocation().getX());
    40. this.getConfig().set("leaveSpawnY", p.getLocation().getY());
    41. this.getConfig().set("leaveSpawnZ", p.getLocation().getZ());
    42. this.getConfig().set("leaveSpawnWorld", p.getLocation().getWorld().getName());
    43. this.saveConfig();
    44. }
    45.  
    46. }
    47.  
    48. return true;
    49.  
    50. }


    addPlayer Method:
    Code:java
    1. public void addPlayer(String s) {
    2.  
    3. Player p = Bukkit.getServer().getPlayer(s);
    4.  
    5. p.teleport(new Location(Bukkit.getWorld("world"), getConfig().getInt("spawnX"), getConfig().getInt("spawnY"), getConfig().getInt("spawnZ")));
    6.  
    7. p.getInventory().clear();
    8. ItemStack swordIS = new ItemStack(Material.IRON_SWORD);
    9. ItemMeta swordMeta = swordIS.getItemMeta();
    10. swordMeta.setDisplayName(ChatColor.RED + "Sword");
    11. swordIS.setItemMeta(swordMeta);
    12. swordIS.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    13.  
    14. ItemStack gaIS = new ItemStack(Material.GOLDEN_APPLE);
    15. ItemMeta gaMeta = gaIS.getItemMeta();
    16. gaMeta.setDisplayName(ChatColor.BOLD + "" + ChatColor.LIGHT_PURPLE
    17. + "God Apple");
    18. gaIS.setItemMeta(gaMeta);
    19.  
    20. PlayerInventory pi = p.getInventory();
    21. if (new java.util.Random().nextInt(10) != 3) {
    22. pi.setHelmet(new ItemStack(Material.IRON_HELMET));
    23. pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    24. pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    25. pi.setBoots(new ItemStack(Material.IRON_BOOTS));
    26. } else {
    27. pi.setHelmet(new ItemStack(Material.GOLD_HELMET));
    28. pi.setChestplate(new ItemStack(Material.GOLD_CHESTPLATE));
    29. pi.setLeggings(new ItemStack(Material.GOLD_LEGGINGS));
    30. pi.setBoots(new ItemStack(Material.GOLD_BOOTS));
    31. }
    32.  
    33. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    34. 20 * 10, 8));
    35.  
    36. }


    And the removePlayer Method:
    Code:java
    1. public void removePlayer(String s) {
    2.  
    3. Player p = Bukkit.getServer().getPlayer(s);
    4.  
    5. p.teleport(new Location(Bukkit.getWorld("world"), getConfig().getInt("leaveSpawnX"), getConfig().getInt("leaveSpawnY"), getConfig().getInt("leaveSpawnZ")));
    6.  
    7. }


    So what am i doing wrong? :(.

    Note that i DO have experience with java and bukkit and have made some awesome games with it.
     
  2. Offline

    MrSparkzz

  3. Offline

    BadBoy6767

    MrSparkzz
    Nothing pops up except:
    (my user) issued server command: /bpjoin,
    (my user) issued server command: /bpleave,

    anyone? :(

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

    xTrollxDudex

  5. Offline

    SkillSam

    Perhaps for your methods, instead of using a string, why not use a Player instead?

    Code:
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23.  
    24. public List<String> players = new ArrayList<String>();
    25.  
    26. public boolean onCommand (CommandSender sender, Command cmd, String lbl, String[] args) {
    27.  
    28. if (!(sender instanceof Player)) {
    29. sender.sendMessage(ChatColor.RED + "You must be a player to perform this command.");
    30. return true;
    31. }
    32.  
    33. Player p = (Player) sender;
    34.  
    35. if (cmd.getName().equalsIgnoreCase("setpos")) {
    36. if (!(p.hasPermission("buildpvp.setpos"))) {
    37. p.sendMessage(ChatColor.RED + "You do not have permission");
    38. return true;
    39. }
    40. getConfig().set("spawnX", p.getLocation().getX());
    41. getConfig().set("spawnY", p.getLocation().getY());
    42. getConfig().set("spawnZ", p.getLocation().getZ());
    43. getConfig().set("spawnWorld", p.getLocation().getWorld().getName());
    44. saveConfig();
    45. return true;
    46. }
    47. if (cmd.getName().equalsIgnoreCase("setleavepos")) {
    48. if (!(p.hasPermission("buildpvp.setleavepos"))) {
    49. p.sendMessage(ChatColor.RED + "You do not have permission");
    50. return true;
    51. }
    52. getConfig().set("leaveSpawnX", p.getLocation().getX());
    53. getConfig().set("leaveSpawnY", p.getLocation().getY());
    54. getConfig().set("leaveSpawnZ", p.getLocation().getZ());
    55. getConfig().set("leaveSpawnWorld", p.getLocation().getWorld().getName());
    56. saveConfig();
    57. return true;
    58. }
    59. if (cmd.getName().equalsIgnoreCase("bpjoin")) {
    60. players.add(p.getName());
    61. addPlayer(p);
    62. return true;
    63. }
    64. if (cmd.getName().equalsIgnoreCase("bpleave")) {
    65. if (!(players.contains(p.getName()))) {
    66. p.sendMessage(ChatColor.RED + "Not Contained.");
    67. return true;
    68. }
    69. players.remove(p.getName());
    70. removePlayer(p);
    71.  
    72. }
    73. return true;
    74. }
    75.  
    76. public void addPlayer (Player p) {
    77. Location loc = new Location(
    78. Bukkit.getServer().getWorld(this.getConfig().getString("spawnWorld")),
    79. this.getConfig().getDouble("spawnX"),
    80. this.getConfig().getDouble("spawnY"),
    81. this.getConfig().getDouble("spawnZ")
    82. );
    83. p.teleport(loc);
    84.  
    85. p.getInventory().clear();
    86.  
    87. ItemStack Sword = new ItemStack(Material.IRON_SWORD);
    88. ItemMeta SwordMeta = Sword.getItemMeta();
    89. SwordMeta.setDisplayName(ChatColor.RED + "Sword");
    90. SwordMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
    91. Sword.setItemMeta(SwordMeta);
    92. p.getInventory().addItem(Sword);
    93.  
    94. ItemStack GoldApple = new ItemStack(Material.GOLDEN_APPLE);
    95. ItemMeta GoldMeta = GoldApple.getItemMeta();
    96. GoldMeta.setDisplayName(ChatColor.BOLD + "" + ChatColor.LIGHT_PURPLE + "God Apple");
    97. GoldApple.setItemMeta(GoldMeta);
    98. p.getInventory().addItem(GoldApple);
    99.  
    100. PlayerInventory inv = p.getInventory();
    101. if (new Random().nextInt(10) != 3) {
    102. inv.setArmorContents(new ItemStack[] {
    103. new ItemStack(Material.IRON_BOOTS),
    104. new ItemStack(Material.IRON_LEGGINGS),
    105. new ItemStack(Material.IRON_CHESTPLATE),
    106. new ItemStack(Material.IRON_HELMET)
    107. });
    108. }
    109. else {
    110. inv.setArmorContents(new ItemStack[] {
    111. new ItemStack(Material.GOLD_BOOTS),
    112. new ItemStack(Material.GOLD_LEGGINGS),
    113. new ItemStack(Material.GOLD_CHESTPLATE),
    114. new ItemStack(Material.GOLD_HELMET)
    115. });
    116. }
    117.  
    118. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20 * 10, 8));
    119. }
    120.  
    121. public void removePlayer(Player p) {
    122. Location loc = new Location(
    123. Bukkit.getServer().getWorld(this.getConfig().getString("leaveSpawnWorld")),
    124. this.getConfig().getDouble("leaveSpawnX"),
    125. this.getConfig().getDouble("leaveSpawnY"),
    126. this.getConfig().getDouble("leaveSpawnZ")
    127. );
    128. p.teleport(loc);
    129. }
    130.  
    131. }
    132.  
     
  6. Offline

    BadBoy6767

    xTrollxDudex
    SkillSam
    I already did that and it still didnt work.
    Everything is working fine. Except it dosent teleport me.
     
  7. Offline

    Hutchmaster99

    Can you post your plugin.yml? Make sure to have all your commands in the plugin.yml
     
  8. Offline

    BadBoy6767

    Hutchmaster99
    If i didnt put the commands in. it would say
    Code:Java
    1. Unknown command. Please type /help for help.

    But it dosent say it. EVERYTHING works except teleportation.
     
  9. Offline

    kiwhen

    I've just tested the code myself, and if you really do not get anything, you must be doing something wrong. I haven't changed anything important, and everything works fine. Here's the code I used (it's stripped down, but it is the same as yours):
    Show Spoiler
    Code:java
    1. List<String> players = new ArrayList<String>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if (label.equalsIgnoreCase("setpos")) {
    5. Player p = (Player) sender;
    6. this.getConfig().set("spawnX", p.getLocation().getX());
    7. this.getConfig().set("spawnY", p.getLocation().getY());
    8. this.getConfig().set("spawnZ", p.getLocation().getZ());
    9. this.getConfig().set("spawnWorld", p.getLocation().getWorld().getName());
    10. this.saveConfig();
    11. } else if (label.equalsIgnoreCase("bpjoin")) {
    12. players.add(sender.getName());
    13. addPlayer(sender.getName());
    14. } else if (label.equalsIgnoreCase("bpleave")) {
    15. if (players.contains(sender.getName())) {
    16. players.remove(sender.getName());
    17. removePlayer(sender.getName());
    18. } else {
    19. sender.sendMessage("Not contained.");
    20. }
    21. } else if (label.equalsIgnoreCase("setleavepos")) {
    22. Player p = (Player) sender;
    23. getConfig().set("leaveSpawnX", p.getLocation().getX());
    24. getConfig().set("leaveSpawnY", p.getLocation().getY());
    25. getConfig().set("leaveSpawnZ", p.getLocation().getZ());
    26. getConfig().set("leaveSpawnWorld", p.getLocation().getWorld().getName());
    27. saveConfig();
    28. }
    29. return true;
    30. }
    31.  
    32. public void addPlayer(String s) {
    33. Player p = getServer().getPlayer(s);
    34. p.teleport(new Location(getServer().getWorld("Test"), getConfig().getInt("spawnX"), getConfig().getInt("spawnY"), getConfig().getInt("spawnZ")));
    35. }
    36.  
    37. public void removePlayer(String s) {
    38. Player p = getServer().getPlayer(s);
    39. p.teleport(new Location(getServer().getWorld("Test"), getConfig().getInt("leaveSpawnX"), getConfig().getInt("leaveSpawnY"), getConfig().getInt("leaveSpawnZ")));
    40.  
    41. }


    Try to compile this as the main, and only class of a plugin, and run it to check. If it does work, then there is something in your code that you haven't posted here. For example, another plugin could be blocking your commands.

    Also, there is about two million pending NullPointerException-errors in your code that you should take a look at. (If you know Java, you should know what I'm on about.)
     
Thread Status:
Not open for further replies.

Share This Page