Issue with new project

Discussion in 'Plugin Development' started by jtc883, Feb 21, 2012.

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

    jtc883

    So i'm trying to write a plugin to do a countdown and then spawn an ender dragon above my head for players to fight. Anyhow, this is the code I have and when I run the command as an op it give an internal error. Not being an OP works fine. Anyone care to look it over and see my problem, my thoughts are the int counter = 10; but i'm a noob so what do i know. Thanks in advance

    Code:java
    1.  
    2.  
    3. public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
    4. Player player = (Player)cs;
    5.  
    6. if (cmd.getName().equalsIgnoreCase("dragon"))
    7. if(cs.isOp()){
    8.  
    9. plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    10. int counter = 10;
    11. public void run() {
    12. Bukkit.getServer().broadcastMessage(ChatColor.DARK_GRAY + "Ender Dragon Fight Begins in " + ChatColor.LIGHT_PURPLE + counter);
    13. counter = counter - 1;
    14. }
    15. }, 20L, 200L);
    16.  
    17. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "SLAY THE ENDER DRAGON!!!");
    18. Location loc = player.getLocation();
    19. player.getWorld().spawnCreature(new Location(player.getWorld(), loc.getX(), loc.getY()+10, loc.getZ()), CreatureType.ENDER_DRAGON);
    20.  
    21. } else {
    22. player.sendMessage(ChatColor.AQUA + "Sorry You Must Be an OP to use that Command");
    23. }
    24. return true;
     
  2. Offline

    Jogy34

    try creating the counter variable outside the runnable and also put a few debug statements in there to figure out where you are getting the error
     
  3. Offline

    jtc883

    when i put it outside it erros on the counter = counter - 1 wanting to change it to final. if i change it to final outside it gives error final invaild type on this statement - final counter = 10;

    here is my error message

    21:19:27 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'drag
    on' in plugin dragonslayer v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:5
    09)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:777)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:737)

    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:725)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:537)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    at org.bukkit.craftbukkit.scheduler.CraftScheduler.scheduleAsyncRepeatin
    gTask(CraftScheduler.java:228)
    at me.jtc883.dragonslayer.dragonslayer.onCommand(dragonslayer.java:38)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more

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

    Jogy34

    whats the stack trace you get from your original code?
     
  5. Offline

    jtc883

    So i removed the counter variable and all calls to it to see if it was what was causing the issue. still getting the same error as above. I'm starting to thing that its my call to plugin is coming back null but I have no idea why at the top of my code in my main class i have:

    public static dragonslayer plugin;
     
  6. Offline

    s1mpl3x

    remove your plugin variable and just put this in there
     
  7. Offline

    jtc883

    So this works, but I guess i dont understand the scheduler, my main point is to use it to do the countdown 10-1 then spawn the dragon but now its executing my end code where it says slay the dragon and spawns in then does the countdown but keeps going after 0 into negatives. I'm thinking I need a do while but have no idea where exactly to put it to delay the spawning. Sorry i''m such a noob.
     
  8. Offline

    s1mpl3x

    Code:
           
    getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
                private Location loc = player.getLocation();
                private World world = player.getWorld();
                public void run() {
                    for (int countdown = 10; countdown > 0; countdown--) {
                        getServer().broadcastMessage(ChatColor.DARK_GRAY + "Ender Dragon Fight Begins in " + ChatColor.LIGHT_PURPLE + countdown);
                        try {
                            Thread.sleep(1000); // wait 1sec
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    loc.add(0, 10, 0);
                    world.spawnCreature(loc), CreatureType.ENDER_DRAGON);
     
                }
            });
    didn't test it, but this should work i think ^^
     
  9. Offline

    jtc883

    so this works but the problem lies with the Thread.sleep command, at least fo me it locks the server and the countdown doesnt show until it is already at 0 and the dragon is spawned, this is what im trying to avoid.

    I thought this might be caused by running the server and running my MC off the same machine but it also happens on my girlfriends computer but on the same network. Is there anyway to avoid this to get the countdown to show for me like I want it to?
     
  10. Offline

    s1mpl3x

    Ah right... sry didn't work with bukkit for some time.. well one possibility would be to do it recursive, another would be to have the ticks value in your main-class... well here is the recursive variant:

    Code:
    //in your onCommand function
                Player player = (Player) sender;
                Location loc = player.getLocation().add(0, 10, 0);
                getServer().getScheduler().scheduleAsyncDelayedTask(this, new DragonWaitTask(this, loc, player.getWorld(), 10), 20);
     
     
     
    // put this class at the end of your file
    class DragonWaitTask implements Runnable{
        private Plugin plugin;
        private Location loc;
        private World world ;
        private int ticks;
     
        public DragonWaitTask(Plugin plugin, Location loc, World world, int ticks) {
            this.loc = loc;
            this.world = world;
            this.ticks = ticks;
            this.plugin = plugin;
        }
     
        @Override
        public void run() {
            if (ticks == 0) {
                world.spawnCreature(loc, CreatureType.ENDER_DRAGON);
            }
            else {
                ticks--;
                plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, this, 20);
            }
        }
    
     
  11. Offline

    jtc883

    Ok i'm back to working on my plugin, here is what I have.
    Code:
    public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
                    final Player player = (Player)cs;
               
                if (cmd.getName().equalsIgnoreCase("dragon"))
                    if(cs.isOp()){
                        //op check
                        Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
                            private Location loc = player.getLocation();
                            private World world = player.getWorld();
                            public void run() {
                                for (int countdown = 10; countdown > 0; countdown--) {
                                    getServer().broadcastMessage(ChatColor.DARK_GRAY + "Ender Dragon Fight Begins in " + ChatColor.LIGHT_PURPLE + countdown);
                                                               
                                }
                                getServer().broadcastMessage(ChatColor.AQUA + "SLAY THE DRAGON!!!");
                                loc.add(0, 10, 0);
                                world.spawnCreature(loc, CreatureType.ENDER_DRAGON);
               
                            }
                        });
                        } else {
                      player.sendMessage(ChatColor.AQUA + "Sorry You Must Be an OP to use that Command");
                    }
                return true;
    my problem is I need to delay each number for 1 second. I dont want to use thread.sleep because it hangs the server. I understand that is what it will do so what is another way to delay this for a second so it runs as a countdown. Everything else works fine. Thanks in advance
     
  12. Code:java
    1. @Override public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args)
    2. {
    3.  
    4.  
    5.  
    6. if (cmd.getName().equalsIgnoreCase("dragon"))
    7. {
    8. if (!(cs instanceof Player))
    9. {
    10. cs.sendMessage("We need an location for this command to work, try another command");
    11. }
    12. final Player player = (Player) cs;
    13. if (cs.isOp())
    14. {
    15. //op check
    16. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    17. {
    18. private final Location loc = player.getLocation();
    19. private int countDown = 10;
    20.  
    21. public void run()
    22. {
    23. if (countDown <= 0)
    24. {
    25. getServer().broadcastMessage(ChatColor.AQUA + "SLAY THE DRAGON!!!");
    26. loc.add(0, 10, 0);
    27. loc.getWorld().spawnCreature(loc, CreatureType.ENDER_DRAGON);
    28. }
    29. else
    30. {
    31. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(<PluginNameHere>.this, this,10);//TODO Fix this
    32. Bukkit.getServer().broadcastMessage(ChatColor.DARK_GRAY + "Ender Dragon Fight Begins in " + ChatColor.LIGHT_PURPLE + countDown++);
    33. }
    34. for (int countdown = 10; countdown > 0; countdown--)
    35. {
    36. getServer().broadcastMessage(ChatColor.DARK_GRAY + "Ender Dragon Fight Begins in " + ChatColor.LIGHT_PURPLE + countdown);
    37.  
    38. }
    39.  
    40.  
    41. }
    42. });
    43. }
    44. else
    45. {
    46. player.sendMessage(ChatColor.AQUA + "Sorry You Must Be an OP to use that Command");
    47. }
    48. }
    49. return true;
    50. }

    Try that

    EDIT: it seems it hates my tabs, grr forums.
     
  13. Offline

    jtc883

    Tried this, just looped forever, dunno where im going wrong.

    OK thanks to everyone here i finally figured it out and I will be releasing my first plugin. Thanks everyone.

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

Share This Page