Thread help

Discussion in 'Plugin Development' started by PreFiXAUT, Jun 25, 2014.

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

    PreFiXAUT

    Hey Guys, I think I have a preety complicated Problem.
    I'm working on my Plugin to add a Event to like every little feature, so you can cancel it and control the Plugin better.
    The Method that starts the Event HAS to return a boolean.
    How i solved it:
    I call the Event when the Input is valid. In a Extra Class (My Listener Class) I listen to this Event and create a new Thread to work on the Database (Except the Event is canceled). In the begin of the Class I created a Thread that I set to when I start the Thread > So I can use the .join() Method from the Method to wait until it's done.
    When the Thread is done it saves the output into a HashMap (defined in the beginning of the Class) so I can read the Data out in the Method and return it.

    THE PROBLEM: The .join() is kinda like a sleep Method, which will be canceled when the Thread where I'm handeling the thing with the Database is done, so the other Thread can continue. The thing is, the other Thread is the MAIN-Thread, so I always freeze the Serer for about 20ms. Since I wan't to add it to all Methods I have, it would lagg super heavy.
    I know I need to put the .join into another Thread then, but how do I return the Method then?

    SourceCode:
    Method:
    Show Spoiler

    Code:java
    1. public static HashMap<String, Boolean> returnData = new HashMap<String, Boolean>();
    2. /**
    3. * Overrides the Data in the Database with the given {@link LobbyData LobbyData}.
    4. * When the MySQL-Setting is activated in the Configuration, it will call the {@link LobbysMySQL#overrideData(LobbyData, java.sql.Connection) overrideData} function and return it.
    5. * When it's added into the Database it will be used from the Plugin and will effect all Lobbys etc.
    6. * @param lobbydata {@link LobbyData LobbyData} that you want to override in the Database.
    7. * @return true if the adding was successfully.<br />false when no Data was found to override or when an Error occurred.
    8. * @throws IllegalArgumentException Throwed when the entered Data is null.
    9. */
    10. public static boolean overrideData(LobbyData lobbydata) throws IllegalArgumentException {
    11. System.out.println("DEBUG: overrideData (LD) called");
    12. if (lobbydata == null) throw new IllegalArgumentException();
    13. if (Main.cfg.getBoolean("use-mysql")) return LobbysMySQL.overrideData(lobbydata, LobbysMySQL.openConnection(LobbysMySQL.getConfigMySQLData()));
    14. else {
    15. LobbyData old = Lobbys.getLobbyData(lobbydata.getGroupid());
    16. Bukkit.getPluginManager().callEvent(new LobbyDataOverrideEvent(old, lobbydata, LobbysHelper.getSign(lobbydata)));
    17. System.out.println("DEBUG: Event called");
    18. try {
    19. CustomEventListener.dataUpdate.join();
    20. System.out.println("DEBUG: Thread stoped.");
    21. boolean tmp = Lobbys.returnData.get(lobbydata.getGroupid());
    22. System.out.println("DEBUG: overrideData(LD) returns > " + tmp);
    23. return tmp;
    24. } catch (Exception e) {
    25. return false;
    26. }
    27. }
    28. }


    Event-Method:
    Show Spoiler

    Code:java
    1. public static Thread dataUpdate, playersUpdate;
    2. @EventHandler
    3. public void onDataUpdate(final LobbyDataOverrideEvent e) {
    4. System.out.println("DEBUG: LobbyDataOverrideEvent called");
    5. dataUpdate = new Thread(new Runnable() {
    6.  
    7. public void run() {
    8. System.out.println("DEBUG: Running");
    9. LobbyData data = e.getNewData();
    10. if (e.isCancelled()) {
    11. Lobbys.returnData.put(data.getGroupid(), false);
    12. System.out.println("DEBUG: Putting in false");
    13. return;
    14. }
    15. /* OVERRIDE */
    16. ArrayList<String> temp = new ArrayList<String>();
    17. boolean flag = false;
    18. for (String read : Lobbys.ld.getData()) {
    19. String[] split = read.split(",");
    20. if (Double.parseDouble(split[2]) == data.getX() && Double.parseDouble(split[3]) == data.getY() && Double.parseDouble(split[4]) == data.getZ()) {
    21. temp.add(data.toString());
    22. flag = true;
    23. } else temp.add(read);
    24. }
    25. Lobbys.ld.setData(temp);
    26. Lobbys.returnData.put(data.getGroupid(), flag);
    27. System.out.println("DEBUG: Putting in flag (" + flag + ")");
    28. }
    29.  
    30. });
    31. if (e.isCancelled())
    32. return;
    33. dataUpdate.start();
    34. Bukkit.getPluginManager().callEvent(new LobbyUpdateEvent(e.getOldData(), e.getNewData(), e.getSign()));
    35. System.out.println("DEBUG: Returning Event");
    36. }


    I hope someone can help, thanks in advance
     
  2. Offline

    PreFiXAUT

  3. Offline

    1Rogue

    Why are you using Thread? Just use bukkit's scheduler.

    Alternatively just fire the event in the location of your code, you don't need to thread it:

    Code:java
    1. YourEvent e = new YourEvent(/* args */);
    2. /*your plugin*/.getServer().getPluginManager().callEvent(e);
    3. if (!e.isCancelled) {
    4. //your code
    5. }
     
  4. Offline

    PreFiXAUT

    Never thought about that. Will try it out when I got time and thanks allot :)
     
Thread Status:
Not open for further replies.

Share This Page