Multiple use of the same thread...

Discussion in 'Plugin Development' started by JTGaming2012, Jul 14, 2014.

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

    JTGaming2012

    Hey there! I have made this grenade in Bukkit, it has a crafting recipe and everything works fine. However, as soon as I throw two grenades at once, it explodes twice on the second thrown grenade instead of the first.

    I understand the problem, I was expecting it, but now I am here, are there any suggestions you guys can give me? I am a bit of a beginner coder so I may need a few examples...

    Thanks for your help! Here is all the code:
    (As I said before, it's all working fine!)

    This is the class that listens to when a player throws a grenade.
    Code:java
    1. public class throwListener implements Listener {
    2.  
    3. public static Item i;
    4.  
    5. public static Item item() {
    6. return i;
    7. }
    8.  
    9. @EventHandler
    10. public void playerThrow(PlayerDropItemEvent event) {
    11. if(event.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("§8Grenade")) {
    12. i = event.getItemDrop();
    13. Player p = event.getPlayer();
    14. Location l = i.getLocation();
    15.  
    16. i.setVelocity(p.getLocation().getDirection().multiply(1.5));
    17. new Thread(new grenade()).start();
    18. }
    19. }
    20. }


    Here is a thread that, after three seconds, triggers all the explosion stuff!
    Code:java
    1. public class grenade implements Runnable {
    2.  
    3. @Override
    4. public void run() {
    5. try {
    6. Thread.sleep(3000);
    7. } catch (InterruptedException e) {
    8. e.printStackTrace();
    9. }
    10.  
    11. Item i = throwListener.item();
    12. Location l = i.getLocation();
    13. TNTPrimed tnt = l.getWorld().spawn(l, TNTPrimed.class);
    14. tnt.setFuseTicks(1);
    15. }
    16. }
     
  2. Offline

    chasechocolate

    Don't make new threads, just use a scheduler, such as a BukkitRunnable.
     
  3. Offline

    JTGaming2012

    chasechocolate When I use schedulers, they crash my server or "make me time out". And anyway, threads seam more professional and efficient.
     
  4. Offline

    _Filip

    Use OOP.
    Static variables are not appropriate for this.
     
  5. Offline

    JTGaming2012

  6. Offline

    Shevchik

    Spawning entites not from main thread can cause server crashes. Just like almost any BukkitAPI call that can do something with world.
    Also you don't know what OOP is and yet speaking something about 'professional'...
     
  7. Offline

    Traks

    This might be useful for you.
    You shouldn't use separate threads when accessing the Bukkit API (or any other part of minecraft server), since the methods and variables are not thread-safe. Doing so anyway might work 99% of the time, but that 1% will screw up everything. Furthermore, introducing additional threads is less resource efficient than using synchronised Bukkit schedulers. Thus it seems quite the opposite to being "professional" in this situation.

    OOP is Object Orientated Programming by the way. Using statics violates the most important aspect of this kind of programming: objects. (in my opinion)
     
    jthort likes this.
  8. Offline

    JTGaming2012

    Shevchik I didn't say I was professional, I said they seam more professional so I decide to use them. If pros use them then they will be reliable, see what I mean?
     
  9. Offline

    jthort

    In that case, your class name should be capitalized, always. CamalCase (don't know if I spelled that right) shouldn't be used for class names

    Also I didn't even know it was possible to have a static constructor? If so, what's the point? Why create a static object?
     
  10. Offline

    JTGaming2012

    jthort Yea, I know... It just seams like it shouldn't when you see, me.jt.class.Class
    Looks weird :)

    Traks Thanks for your help! But I have another little issue. I am tired and can't think straight at the moment but...

    How would I get the item from the throw event in the main class:
    Code:java
    1. Item i = event.getItemDrop();
    2. if(event.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("§8Grenade")) {
    3. i = event.getItemDrop();
    4. Player p = event.getPlayer();
    5. Location l = i.getLocation();
    6.  
    7. i.setVelocity(p.getLocation().getDirection().multiply(1.5));
    8. BukkitTask task = new grenade(this.plugin).runTaskLater(this.plugin, 20);
    9. }


    To here, in the grenade BukkitScheduler class:
    Code:java
    1. @Override
    2. public void run() {
    3.  
    4. Location l = i.getLocation();
    5. TNTPrimed tnt = l.getWorld().spawn(l, TNTPrimed.class);
    6. tnt.setFuseTicks(1);
    7. plugin.getServer().broadcastMessage("Welcome to Bukkit! Remember to read the documentation!");
    8. }


    Thanks

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

    jthort

    JTGaming2012 As they stated above, you should really take a look into OOP (Object Oriented Programming), take a look at this trial http://docs.oracle.com/javase/tutorial/java/concepts/

    But to answer your question specifically you should pass it through the Grenade class through the constructor, just like you passed the instance of "this.plugin"

    See this if you don't know what a constructor is.. http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    Although you should understand what that is after going through the trail in OOP on Oracle
     
  12. Offline

    JTGaming2012

    jthort , I have a project and I am crashing in to issues everyday because all I am trying to do is achieve what I need. I am following no tutorial, just trial and error really until I find a coding style that works for me. Thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page