Solved Help with my Start Plugin

Discussion in 'Plugin Development' started by Fr3eaky, Jul 15, 2014.

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

    Fr3eaky

    I just startet to create my own Plugins.
    Now i got the following Problem:

    Code:java
    1. package PluginTest;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9.  
    10. public class PluginTestProgrammierung extends JavaPlugin{
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    14.  
    15. //Umwandlung Sender in Player
    16. Player p = null;
    17. if(sender instanceof Player){
    18. p = (Player)sender;
    19. }
    20.  
    21.  
    22. //Kommando "Start"
    23. if(cmd.getName().equalsIgnoreCase("Start")){
    24. if(p != null){
    25.  
    26. String Version = this.getDescription().getVersion();
    27. String Name = this.getDescription().getName();
    28. String Author = this.getDescription().getAuthors().get(0);
    29.  
    30. p.sendMessage(ChatColor.GREEN + "Willkommen, " + ChatColor.WHITE + p.getDisplayName());
    31. p.sendMessage(ChatColor.GOLD + "[" + Name + "]" + ChatColor.GREEN + " erfolgreich geladen.");
    32. p.sendMessage(ChatColor.GREEN + "Version: " + ChatColor.WHITE + Version);
    33. p.sendMessage(ChatColor.GREEN + "Erstellt von: " + ChatColor.WHITE + Author);
    34.  
    35.  
    36. return true;
    37.  
    38. }
    39.  
    40.  
    41.  
    42. else {
    43.  
    44. return true;
    45. }
    46.  
    47.  
    48.  
    49. }
    50.  
    51. //Kommando "Fliegen"
    52. if(cmd.getName().equalsIgnoreCase("Fly")){
    53. if(p != null){
    54. if(p.hasPermission("Owner")){
    55.  
    56. if(args.length != 1){
    57. return false;
    58.  
    59. }
    60.  
    61. @SuppressWarnings("deprecation")
    62. Player zielSpieler = this.getServer().getPlayer(args[0]);
    63.  
    64. try{
    65.  
    66. zielSpieler.setFlying(true);
    67. zielSpieler.sendMessage(ChatColor.GREEN + "Fliegen ist aktiviert.");
    68. p.sendMessage(ChatColor.GREEN + "Du hast " + zielSpieler.getDisplayName() + " das Fliegen erlaubt.");
    69.  
    70.  
    71.  
    72.  
    73. p.sendMessage(ChatColor.RED + "Spieler nicht online.");
    74. return true;
    75.  
    76. }
    77.  
    78. } else {
    79.  
    80. p.sendMessage(ChatColor.RED + "Du hast keine Berechtigung dafür.");
    81. return true;
    82.  
    83. }
    84.  
    85. }
    86.  
    87.  
    88. else {
    89. return true;
    90.  
    91. }
    92.  
    93.  
    94. }
    95.  
    96. //Nächste
    97.  
    98.  
    99. return false;
    100.  
    101. }
    102. }


    The Commands work fine, but the else doenst work. This else is for the Permission. If i got no Permission, he should give out the sendMessage.

    Code:java
    1. } else {
    2.  
    3.  
    4.  
    5. p.sendMessage(ChatColor.RED + "Du hast keine Berechtigung dafür.");
    6.  
    7. return true;
    8.  
    9.  
    10.  
    11. }


    Ignore the German Words there, cause im from Germany :D I hope you understand my Problem, and can help me.

    Thanks
     
  2. Offline

    teej107

    If your else doesn't work, then that means your if statement is true. Else will only happen if the if statement is false;
     
  3. Offline

    Fr3eaky

    Sorry but you dont understand :D
    I know how to use If and Else, but it dont works for me.
    Code:java
    1. // Command"Fly"
    2. if (cmd.getName().equalsIgnoreCase("Fly")) {
    3. if (p != null) {
    4.  
    5. //Check for op Permissions
    6. if (p.hasPermission("Owner")) {
    7.  
    8. if (args.length != 1) {
    9. return false;
    10.  
    11. }
    12.  
    13. @SuppressWarnings("deprecation")
    14. Player zielSpieler = this.getServer().getPlayer(args[0]);
    15.  
    16. //He Should check if the Player is Online.
    17. if (zielSpieler.isOnline()) {
    18.  
    19. zielSpieler.setFlying(true);
    20. zielSpieler.sendMessage(ChatColor.GREEN + "Fly is allowed.");
    21. p.sendMessage(ChatColor.GREEN + "You allowed " + zielSpieler.getDisplayName() + " to fly.");
    22.  
    23. //Player not online so give this Output in Chat
    24. } else {
    25.  
    26. p.sendMessage(ChatColor.RED + "Player not online.");
    27. return true;
    28.  
    29. }
    30. //No Permissions so give this Output in Chat
    31. } else {
    32.  
    33. p.sendMessage(ChatColor.RED + "You dont have Permissions for that.");
    34. return true;
    35.  
    36. }
    37.  
    38. }
    39.  
    40. else {
    41. return true;
    42.  
    43. }
    44.  
    45. }
    46.  
    47.  
    48. return false;
    49.  
    50. }
    51. }

    Im Not a OP, but i dont get the right Output Like "You dont habe Permissions for that"
    I think my Code should works like this, and i dont understand why not :D
     
  4. Offline

    SkyleTyler1337

    can i see error code thats in the console.

    Code:java
    1. package me.skylertyler.ctw.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class basicCommand implements CommandExecutor{
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender sender, Command cmd, String label,
    15. String[] args){
    16. if(sender instanceof Player){
    17. Player player = (Player) sender;
    18. if(player.hasPermission("ctw.kit") || player.isOp()){
    19. if(label.equalsIgnoreCase("kit")){
    20. player.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    21. }
    22. }else{
    23. sender.sendMessage(ChatColor.RED + "You need to be op to do this command!");
    24. }
    25. }else{
    26. sender.sendMessage(ChatColor.RED + "You need to be a player to do this command!");
    27. }
    28. return false;
    29. }
    30.  
    31. }
    32.  


    basic command

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. I think I know what you're doing wrong. First of all, change the permission from "Owner" to "[plugin-name-here].owner". Second of all, have a look at your plugin.yml and check if you put said permission in there.
     
  6. Offline

    dsouzamatt

    The actual name of the permission doesn't matter; putting the plugin's name first is probably a good practice, but not vital. Also, while you may still choose to do so for finer control over them, you don't have to register permissions in your plugin.yml as you do for commands for them to still work.

    For more information, here's the official BukkitWiki section on permissions.
     
  7. dsouzamatt First of all, I know putting the plugin's name first is not vital, but when it's good practice, it should be done nevertheless, and suggesting it also good practice. Good forum practice.

    Anyway, this code is very hard to understand, but I think I found a misunderstanding of his:
    Code:java
    1. //Check for op Permissions
    2. if (p.hasPermission("Owner")) {


    Maybe he's mistaking Player.hasPermission("Owner") for Player.isOp(), and it will never return true since he probably didn't set that permission.
    So, Fr3eaky, try using Player.isOp() instead of checking for that permission.
     
  8. Offline

    Fr3eaky

    Doesnt work for me lol. Maybe i am just to stupid... Is my Code really so bad?

    Code:java
    1. // Command "Fly"
    2. if (cmd.getName().equalsIgnoreCase("Fly")) {
    3. if (p != null) {
    4. if (p.isOp()) {
    5.  
    6. if (args.length != 1) {
    7. return false;
    8.  
    9. }
    10.  
    11. @SuppressWarnings("deprecation")
    12. Player zielSpieler = this.getServer().getPlayer(args[0]);
    13.  
    14. if (zielSpieler.isOnline()){
    15. zielSpieler.setAllowFlight(true);
    16. zielSpieler.setFlying(true);
    17. zielSpieler.sendMessage(ChatColor.GREEN + "Fly is allowed.");
    18. p.sendMessage(ChatColor.GREEN + "You allowed " + zielSpieler.getDisplayName() + " to fly.");
    19.  
    20. } else {
    21.  
    22. p.sendMessage(ChatColor.RED + "Player not Online.");
    23. return true;
    24.  
    25. }
    26.  
    27. } else {
    28.  
    29. p.sendMessage(ChatColor.RED + "No Permissions.");
    30. return true;
    31.  
    32. }
    33.  
    34. }
    35.  
    36. else {
    37. return true;
    38.  
    39. }
    40.  
    41. }
    42.  
    43. return false;
    44.  
    45. }
    46. }


    And my Error Code is the following:

    [02:13:53 INFO]: Fr3eaky issued server command: /fly sd
    [02:13:53 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'fly' in plugin PluginTest v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:957) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:818) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    Caused by: java.lang.NullPointerException
    at PluginTest.PluginTest.onCommand(PluginTest.java:57) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.7.10-R0.1-20140713.051447-4.jar:git-Bukkit-1.7.9-R0.2-15-g66c314d-b3104jnks]
    ... 13 more


    I really dont understand this, because i check with the Else Statement if the Player is Online / exist. Why the Hell he give me out this Error in the Console, and not my Message Error Ingame?
     
  9. Offline

    xTigerRebornx

    Fr3eaky Check if the Player is not null, rather then online. null will be returned when the Player isn't online, therefor you cannot invoke isOnline() on the Player (as it is null).
     
  10. xTigerRebornx Oh crap, how could I miss that! I thought, how weird he isn't null-checking the Player... yeah.
    I got ninja'd. Again... *crawls back into debug server and goes back to making things explode*
     
  11. Offline

    Fr3eaky

    You Guys helped me as Hell :D
    Here is my working Code now =)

    Code:java
    1. package PluginTest;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class PluginTest extends JavaPlugin {
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label,
    13. String[] args) {
    14.  
    15. // Umwandlung Sender in Player
    16. Player p = null;
    17. if (sender instanceof Player) {
    18. p = (Player) sender;
    19. }
    20.  
    21.  
    22.  
    23. // Kommando "Start"
    24. if (cmd.getName().equalsIgnoreCase("Start")) {
    25. if (p != null) {
    26.  
    27. String Version = this.getDescription().getVersion();
    28. String Name = this.getDescription().getName();
    29. String Author = this.getDescription().getAuthors().get(0);
    30.  
    31. p.sendMessage(ChatColor.GREEN + "Willkommen, " + ChatColor.WHITE + p.getDisplayName());
    32. p.sendMessage(ChatColor.GOLD + "----------------------------");
    33. p.sendMessage(ChatColor.GOLD + "[" + Name + "]" + ChatColor.GREEN + " erfolgreich geladen.");
    34. p.sendMessage(ChatColor.GREEN + "Version: " + ChatColor.WHITE + Version);
    35. p.sendMessage(ChatColor.GREEN + "Erstellt von: " + ChatColor.WHITE + Author);
    36. p.sendMessage(ChatColor.GOLD + "----------------------------");
    37.  
    38. return true;
    39.  
    40. }
    41.  
    42. else {
    43.  
    44. return true;
    45. }
    46.  
    47. }
    48.  
    49. // Kommando "FliegenAn"
    50. if (cmd.getName().equalsIgnoreCase("FlyOn")) {
    51. if (p != null) {
    52. if (p.isOp()) {
    53.  
    54. if (args.length != 1) {
    55. return false;
    56.  
    57. }
    58.  
    59. @SuppressWarnings("deprecation")
    60. Player zielSpieler = this.getServer().getPlayer(args[0]);
    61.  
    62. if (zielSpieler != null) {
    63. if (zielSpieler.isOnline()) {
    64. zielSpieler.setAllowFlight(true);
    65. zielSpieler.setFlying(true);
    66. zielSpieler.sendMessage(ChatColor.GREEN + "Fliegen wurde aktiviert.");
    67. p.sendMessage(ChatColor.GREEN + "Du hast " + zielSpieler.getDisplayName() + " das Fliegen erlaubt.");
    68. return true;
    69.  
    70. } else {
    71.  
    72. p.sendMessage(ChatColor.RED + "Spieler nicht online.");
    73. return true;
    74.  
    75. }
    76.  
    77. } else {
    78.  
    79. p.sendMessage(ChatColor.RED + "Spieler nicht online.");
    80. return true;
    81.  
    82. }
    83.  
    84. } else {
    85.  
    86. p.sendMessage(ChatColor.RED + "Du hast keine Berechtigung dafür.");
    87. return true;
    88.  
    89. }
    90.  
    91. } else {
    92.  
    93. return true;
    94.  
    95. }
    96.  
    97. }
    98.  
    99. // Kommando "FliegenAus"
    100. if (cmd.getName().equalsIgnoreCase("FlyOff")) {
    101. if (p != null) {
    102. if (p.isOp()) {
    103.  
    104. if (args.length != 1) {
    105. return false;
    106.  
    107. }
    108.  
    109. @SuppressWarnings("deprecation")
    110. Player zielSpieler = this.getServer().getPlayer(args[0]);
    111.  
    112. if (zielSpieler != null) {
    113. if (zielSpieler.isOnline()) {
    114. zielSpieler.setAllowFlight(false);
    115. zielSpieler.setFlying(false);
    116. zielSpieler.sendMessage(ChatColor.GREEN + "Fliegen wurde Deaktiviert.");
    117. p.sendMessage(ChatColor.GREEN + "Du hast " + zielSpieler.getDisplayName() + " das Fliegen verboten.");
    118. return true;
    119.  
    120. } else {
    121.  
    122. p.sendMessage(ChatColor.RED + "Spieler nicht online.");
    123. return true;
    124.  
    125. }
    126.  
    127. } else {
    128.  
    129. p.sendMessage(ChatColor.RED + "Spieler nicht online.");
    130. return true;
    131.  
    132. }
    133.  
    134. } else {
    135.  
    136. p.sendMessage(ChatColor.RED + "Du hast keine Berechtigung dafür.");
    137. return true;
    138.  
    139. }
    140.  
    141. } else {
    142.  
    143. return true;
    144.  
    145. }
    146.  
    147. }
    148.  
    149. //Ende
    150. return false;
    151.  
    152. }
    153. }


    plugin.yml

    name: PluginTest
    main: PluginTest.PluginTest
    version: 1.0
    author: Fr3eaky
    commands:
    Start:
    usage: /Start
    description: Startet die Plugin Informationen.
    FlyOn:
    usage: /FlyOn [Player]
    description: Aktiviert den Flug Modus.
    FlyOff:
    usage: /FlyOff [Player]
    description: Deaktiviert den Flug Modus.

    Thanks for your help Guys!!! Can i code like this Syntax, or is my Code bad like this? I mean it works fine, but can i optimize this Code anyway?
     
  12. Your code seems fine. Please add the [SOLVED] tag to the post title.
     
Thread Status:
Not open for further replies.

Share This Page