Why is it not working ?

Discussion in 'Plugin Development' started by Adriani6, Oct 9, 2013.

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

    Adriani6

    Right, I wrote a plugin, which listens for command, and on command, it runs a class file, which has normal, nothing to do with craftbukkit, code. The code is to put a folder into .zip and upload it to FTP server.

    I have tested the .zip and upload code in eclipse and it worked, I got my zipped file on desktop and it was uploaded on my FTP server.

    Now, When I tried to make it a bukkit plugin, it starts-up but when I execute a command to begin this process it throws me back an error. Any idea why ?
    Question is, is it possible to run not-related craftbukkit code, as a plugin.

    I will upload code snippets and error log when I get home, if needed.
     
  2. Offline

    metalhedd

    You're going to need to provide a lot more information before anyone can help you. we'll need to see the actual error messages at the very least. and seeing the code would help too. I don't understand what the relevance of the .zip file is. bukkit can't run code in a zip file.
     
  3. Offline

    Adriani6

    You either didn't read what I wrote, or you just misunderstood everything. It's all written in Java in one package (.jar) just different classes. There's only one file which contains "extends JavaPlugin{" and that's working, but the other classes don't. Just as if they were not able to be ran by console as plugin. The other class is responsible for zipping folders and yet the other, last class is responsible for uploading the zip files to FTP servers, which doesn't work when it's converted into a plugin, but works as a standalone plugin.
     
  4. Offline

    metalhedd

    I did read what you wrote, it just wasn't very clear, I still don't understand what you mean by: "doesn't work when it's converted into a plugin, but works as a standalone plugin" but that's really beside the point. The real issue is that you're saying it:

    If you would show us the error, maybe. what's in the server logs? there's probably a stack trace that tells you exactly why it's happening. You've literally given *no* useable information for anyone to help you, just: "I wrote some code that works, but doesn't work as a bukkit plugin, it just throws an error."
     
  5. Offline

    xigsag

    He is simply asking for some code/error. We know that you will upload them when you reach home, and we don't know what time or how long that will take, but we need you to supply us with the error code or log message, so we can help.

    I really don't see what you would be expecting when you decided to post a thread about an error, and not providing the error. In addition, your explanation was not really the best either.

    P.S
    Do you actually know the basics of coding a bukkit plugin, e.g registering commandexecutors, getCommand().setExecutor(); , and actually putting the command into the plugin.yml under the
    Code:
    commands:
        testcommand:
            description: description of test command.
            permissions: permission.of.test.command
    ?

    Just giving a few examples of things you would normally need to take note of when adding commands to your plugin. Maybe watch a few youtube videos about it? Bukkit has been around for quite awhile now, so maybe search for an already existing thread on this exact same forum that you posted a thread on?

    TheBCBroz on youtube does some great plugin development videos, thenewboston does some great java tutorial videos, maybe those would be of some help to you.
     
  6. Offline

    kevinspl2000

    Okay so this is the basics:
    Code:java
    1. public class classname extends JavaPlugin {
    2. public void onEnable() {
    3. PluginManager pm = getServer().getPluginManager();
    4. //use this if the class file doesn't have a command, type this. If you are using this methord, then you have to let it implement Listener.
    5. pm.registerEvents(new classname(), this);
    6. //use this is the class file has a command. if it does type this. also you have to let the class implement CommandExecutor.
    7. getCommand("example").setExecutor(new classname());
    8.  
    9.  
    10.  
    11. System.out.println("pluginname enabled!");
    12. }
    13. }
     
  7. Offline

    xigsag

    kevinspl2000
    Code:java
    1. //another way of putting it:
    2.  
    3. public ListenerClass lc;
    4. public CommandClass cc;
    5.  
    6. public void onEnable(){
    7. //ignore this. this is just how i code.
    8. lc = new ListenerClass();
    9. cc = new CommandClass();
    10. //this is for registering events. e.g PlayerMoveEvent, BlockBreakEvent, etc.
    11. getServer().getPluginManager().registerEvents(lc);
    12. //this is for registering commands. don't forget to add it in the plugin.yml!
    13. getCommand("testcommand").setExecutor(cc);
    14. }
    15.  
     
  8. Offline

    kevinspl2000

    What is the point of adding the variable unless you are only using it more than once?
     
    The_Doctor_123 likes this.
  9. Offline

    Goblom

    Posting the errors you are getting would help a lot. Just saying that there is an error wont help you at all
     
  10. Offline

    Adriani6

    Ok. I'm home now, here's the codes and errors.

    This is class called "Plugin" it's the main class of the plugin.

    Code:java
    1. package adriani6.zip1;
    2.  
    3. import org.bukkit.command.CommandExecutor;
    4. import org.bukkit.plugin.PluginManager;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Plugin extends JavaPlugin {
    8.  
    9. public void onEnable()
    10. {
    11. PluginManager pm = getServer().getPluginManager();
    12.  
    13. getCommand("backup").setExecutor((CommandExecutor) new Main());
    14.  
    15. System.out.println("BackUp enabled!");
    16. }
    17. public void onDisable(){
    18. System.out.println("BackUp Disabled!");
    19. }
    20. }


    This is class which I want to be ran at the start of the plugin, it's called "Main", I know how to add commands, no worry here.

    Code:java
    1. package adriani6.zip1;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.util.zip.ZipEntry;
    6. import java.util.zip.ZipOutputStream;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9.  
    10. public class Main extends JavaPlugin {
    11. public static void main(String[] a) throws Exception {
    12. zipFolder("C:/Users/Adriani6/Desktop/1", "c:\\Users/Adriani6/Desktop/a.zip");
    13. }
    14.  
    15. static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    16. ZipOutputStream zip = null;
    17. FileOutputStream fileWriter = null;
    18.  
    19. fileWriter = new FileOutputStream(destZipFile);
    20. zip = new ZipOutputStream(fileWriter);
    21.  
    22. addFolderToZip("", srcFolder, zip);
    23. zip.flush();
    24. zip.close();
    25. }
    26.  
    27. static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
    28. throws Exception {
    29.  
    30. File folder = new File(srcFile);
    31. if (folder.isDirectory()) {
    32. addFolderToZip(path, srcFile, zip);
    33. } else {
    34. byte[] buf = new byte[1024];
    35. int len;
    36. FileInputStream in = new FileInputStream(srcFile);
    37. zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
    38. while ((len = in.read(buf)) > 0) {
    39. zip.write(buf, 0, len);
    40. }
    41. }
    42. }
    43.  
    44. static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
    45. throws Exception {
    46. File folder = new File(srcFolder);
    47.  
    48. for (String fileName : folder.list()) {
    49. if (path.equals("")) {
    50. addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
    51. } else {
    52. addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
    53. }
    54. }
    55. }
    56. }


    Now the Error....

    Code:
    2013-10-09 00:42:53 [INFO] [AI6BACKUP] Enabling AI6BACKUP v0.1
    2013-10-09 00:42:53 [SEVERE] Error occurred while enabling AI6BACKUP v0.1 (Is it up to date?)
    java.lang.ClassCastException: adriani6.zip1.Main cannot be cast to org.bukkit.command.CommandExecutor
        at adriani6.zip1.Plugin.onEnable(Plugin.java:14)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
        at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
        at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
        at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
        at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Goblom kevinspl2000 xigsag metalhedd

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

    metalhedd

    You can't have 2 class that extend JavaPlugin, only 1. Main should implement the CommandExecutor or TabExecutor interface instead.
     
  12. Offline

    xigsag

    Normally, I use it more than once in my plugins. I also use them in the main class itself, so it would be easier for me to create variables. It is just my way of doing things. I just included it there, together with the comment which tells you to ignore it unless you understand what it means.

    Adriani6
    Code:java
    1. public class Main() extends JavaPlugin implements CommandExecutor, Listener{
    2.  
    3. public void onEnable(){
    4. getServer().getPluginManager().registerEvents(this, this);
    5. getCommand("backup").setExecutor(this);
    6. // although the .setExecutor() part is not required if your main class is the commandexecutor.
    7. }
    8.  
    9. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    10. if(sender instanceof Player){
    11. Player p = (Player) sender;
    12.  
    13. if(label.equalsIgnoreCase("backup")){
    14. //insert backup code here
    15. }
    16.  
    17. }
    18. }
    19.  
    20. @EventHandler
    21. public void onBlockBreak(BlockBreakEvent e){
    22. //example event
    23. }
    24.  
    25. }


    I just crammed the commandexecutor and the events listener into one class. If you want it to be tidy, split them up into different classes.

    EDIT:

    By the way, I just noticed. You casted CommandExecutor on the getCommand() method. You have to implement it, not cast it. Implement it in the class like this:

    Code:java
    1. public class CommandClass() implements CommandExecutor{
    2. Plugin javaplugin_extended_class;
    3. public CommandClass(Plugin javaplugin_extended_class){
    4. this.javaplugin_extended_class = javaplugin_extended_class;
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    8. //yada yada yada.
    9. }
    10. }


    That should be the problem. Although you might have already fixed it by now.

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

    Goblom

    Your main class is written like a Java Application not a bukkit plugin. Here are a few things missing

    Code:java
    1. public class BackupCommand implements CommandExecutor
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)


    With the current "Main" class you would want to run the "zipFolder(String, String);"
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (args.length == 3) {
    3. zipFolder(args[1].toString(), args[2].toString());
    4. return true;
    5. }
    6. return false;
    7. }
     
  14. Offline

    xigsag

    Goblom
    Well damn, that explained everything I typed in that single post.
     
  15. Offline

    Adriani6

    Ok. I feel like an absolute ass asking over and over again, but please understand that this is my first bukkit plugin, on this scale :p

    Code:java
    1. package adriani6.Main;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import java.io.File;
    11. import java.io.FileInputStream;
    12. import java.io.FileNotFoundException;
    13. import java.io.FileOutputStream;
    14. import java.io.IOException;
    15. import java.util.ArrayList;
    16. import java.util.List;
    17. import java.util.zip.ZipEntry;
    18. import java.util.zip.ZipOutputStream;
    19.  
    20. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    21.  
    22. public void onEnable(){
    23. getServer().getPluginManager().registerEvents(this, this);
    24. getCommand("backup").setExecutor(this);
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    28. if(sender instanceof Player){
    29. Player p = (Player) sender;
    30.  
    31. if(label.equalsIgnoreCase("backup")){
    32.  
    33. return true;
    34. public static void main(String[] args) throws IOException {
    35. File directoryToZip = new File("C:\\projects\\workspace\\testing\\stuff");
    36.  
    37. List<File> fileList = new ArrayList<File>();
    38. System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
    39. getAllFiles(directoryToZip, fileList);
    40. System.out.println("---Creating zip file");
    41. writeZipFile(directoryToZip, fileList);
    42. System.out.println("---Done");
    43. }
    44.  
    45. public static void getAllFiles(File dir, List<File> fileList) {
    46. try {
    47. File[] files = dir.listFiles();
    48. for (File file : files) {
    49. fileList.add(file);
    50. if (file.isDirectory()) {
    51. System.out.println("directory:" + file.getCanonicalPath());
    52. getAllFiles(file, fileList);
    53. } else {
    54. System.out.println(" file:" + file.getCanonicalPath());
    55. }
    56. }
    57. } catch (IOException e) {
    58. e.printStackTrace();
    59. }
    60. }
    61.  
    62. public static void writeZipFile(File directoryToZip, List<File> fileList) {
    63.  
    64. try {
    65. FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip");
    66.  
    67. for (File file : fileList) {
    68. if (!file.isDirectory()) { // we only zip files, not directories
    69. addToZip(directoryToZip, file, zos);
    70. }
    71. }
    72.  
    73. zos.close();
    74. fos.close();
    75. } catch (FileNotFoundException e) {
    76. e.printStackTrace();
    77. } catch (IOException e) {
    78. e.printStackTrace();
    79. }
    80. }
    81.  
    82. public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
    83.  
    84.  
    85. // we want the zipEntry's path to be a relative path that is relative
    86. // to the directory being zipped, so chop off the rest of the path
    87. String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
    88. file.getCanonicalPath().length());
    89. System.out.println("Writing '" + zipFilePath + "' to zip file");
    90. ZipEntry zipEntry = new ZipEntry(zipFilePath);
    91. zos.putNextEntry(zipEntry);
    92.  
    93. byte[] bytes = new byte[1024];
    94. int length;
    95. while ((length = fis.read(bytes)) >= 0) {
    96. zos.write(bytes, 0, length);
    97. }
    98.  
    99. zos.closeEntry();
    100. fis.close();
    101. }
    102.  
    103.  
    104. }
    105. return false;
    106.  
    107.  
    108.  
    109. }
    110. }
    111. }
    112. }
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
    119.  


    I made it all into one class, to save hussle. But new error came up. I have literally no idea how to fix it, but probably it's simple as hell.


    Code:
    2013-10-10 23:29:01 [SEVERE] Could not load 'plugins\AI6BACKUP.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.Error: Unresolved compilation problems:
        Syntax error, insert "}" to complete Statement
        Syntax error, insert "else Statement" to complete IfStatement
        Syntax error, insert "}" to complete Statement
        Syntax error, insert "else Statement" to complete BlockStatements
        Syntax error, insert "}" to complete MethodBody
     
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.<init>(CraftServer.java:217)
        at net.minecraft.server.v1_6_R2.PlayerList.<init>(PlayerList.java:56)
        at net.minecraft.server.v1_6_R2.DedicatedPlayerList.<init>(SourceFile:11)
        at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:106)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.Error: Unresolved compilation problems:
        Syntax error, insert "}" to complete Statement
        Syntax error, insert "else Statement" to complete IfStatement
        Syntax error, insert "}" to complete Statement
        Syntax error, insert "else Statement" to complete BlockStatements
        Syntax error, insert "}" to complete MethodBody
     
        at adriani6.Main.Main.<init>(Main.java:30)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 9 more
    Help again pleaseeeeees.
     
  16. Offline

    Scyntrus

    What the hell. You can't just drop the entire contents of your original CLASS into a METHOD body.
     
  17. Offline

    xigsag

    No offense, Adriani6, but do you know java to begin with? Doesn't seem so.. You should probably go learn some java first, 'cos these are pretty beginner level java mistakes.

    And you cant drop a method into a method.

    Code:java
    1. public boolean onCommand(blah bla){
    2. //this is a method.
    3.  
    4. public static void blahblah(){
    5. //this is a method in a method.
    6. //this is what you are doing.
    7. //this is wrong.
    8. }
    9.  
    10. }


    Imagine a container being the method. You don't normally put a glass jar inside a glass jar, now do you? Unless you do, then I have nothing to say.. but in a normal situation, you don't.
     
  18. Offline

    Adriani6

    I know basics. Just a beginner with java. Thanks for explanation, i got it now :)
     
  19. Offline

    lycano

    What is the purpose of his plugin?
     
  20. Offline

    xigsag

    lycano
    Apparently, to back up stuff.
     
  21. Offline

    lycano

    Oh i see, thank you.
     
Thread Status:
Not open for further replies.

Share This Page