Multi-Threading

Discussion in 'Plugin Development' started by xXCryptoFreakXx, Nov 16, 2012.

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

    xXCryptoFreakXx

    Well, I'm making my first bukkit plugin, and like many other noobs, I'm stumbling on Multi-Threading. Below are the samples of my plugin that involve multi-threading, as well as the error in the console. The command involved, '/pigbomb', spawns a pig, waits, and then blows it up. The second thread is to - well - wait. The command works fine as long as you don't try to blow up two pigs at once, which is when crashes the server.





    Code:
    else if (cmd.getName().equalsIgnoreCase("pigbomb")){
                    Player player = (Player) sender;
                    Location loc = player.getLocation();
                    World w = loc.getWorld();   
                    bomb = w.spawnEntity(loc, EntityType.PIG);     
                    ((Pig) bomb).setBaby();
                    sender.sendMessage("§9Pig armed, you have 10 seconds.");
                    Thread t = new Thread(new PigGoBoom());
                    t.start();
    }
    
    Code:
    private static class PigGoBoom extends Thread
        {
     
            synchronized public void run()
            {
                try
                {
                    Thread.currentThread();
                    Thread.sleep(4000);
                }
                catch (InterruptedException e)
                {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                    senda.sendMessage("§4DEBUG MESSAGE: Well, that's an InterruptedException!");
                   
                }
                  float explosionPower = 8F;   
                  Location exploc = null;
                  int random = 0;
                  for (random=1;random<4;random++)           
                  {
                      exploc = (bomb.getLocation());
                      switch (random)
                      {
                          case 1: 
                                  exploc.setX(exploc.getX()+(Math.random()*4));
                                  exploc.setY(exploc.getY()+(Math.random()*4));
                                  exploc.setZ(exploc.getZ()+(Math.random()*4));
                                  break;
                          case 2:
                                  exploc.setX(exploc.getX()+(Math.random()*4));
                                  exploc.setY(exploc.getY()-(Math.random()*4));
                                  exploc.setZ(exploc.getZ()-(Math.random()*4));
                              break;
                          case 3:
                                  exploc.setX(exploc.getX()-(Math.random()*4));
                                  exploc.setY(exploc.getY()+(Math.random()*4));
                                  exploc.setZ(exploc.getZ()+(Math.random()*4));
                              break;
                          case 4:
                                  exploc.setX(exploc.getX()-(Math.random()*4));
                                  exploc.setY(exploc.getY()-(Math.random()*4));
                                  exploc.setZ(exploc.getZ()-(Math.random()*4));
                              break;
                         
                      }
                      exploc.getWorld().createExplosion(exploc, explosionPower);
     
                  }
                this.interrupt();
                 
            }
        }
    
    And heres the console when it crashes:
    [​IMG]

    Anyone?

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

    psycowithespn

Thread Status:
Not open for further replies.

Share This Page