Asynch Tasks

Discussion in 'Spigot Plugin Development' started by ramidzk, Apr 14, 2017.

Thread Status:
Not open for further replies.
  1. I am trying to get a plugin to query stuff to server b but there is one problem.

    When a request is sent, the server is like on halt for the response from the server b. When I move too much, the server says I moved too fast when it unfreezes.

    I tried to use bukkit async tasks but it still freezes for a response from server b.
     
    #1 ramidzk, Apr 14, 2017
    Last edited: Apr 14, 2017
  2. LOL you mean async, can you please give some code and stacktraces?
     
  3. I'll make up a fake stack trace:
    → WolframSearch.onCommand(CommandSender, Command, String, String[])
    → Now as a new Async Task
    → WolframSearch.query(String[], CommandSender)
    → WolframApp.query(String, String, WolframPluginCore CommandSender)

    now the foggy stuff

    → new WAEngine();
    → WAQuery query = engine.createQuery();
    → query.setInput(args);
    → TryCatchBlock Start
    → WAQueryResult queryResult = engine.performQuery(query); The HTTPS request sender
    → Error checking/Printing stuff
    → TryCatchBlick End

    If you already haven't figured out, WA is Wolfram|Alpha
     
  4. "I'm sorry, but you're spelling this word incorrectly. It's rather 'async'/'asynchronous'. Don't worry, though, just a small typo :p"
    Doesn't that seem like a much more fitting response to it?
    What have you tried?
    And no, I don't mean write it like you have, that much I've gotten, but what is your actual code?
     
  5. It's the Wolfram|Alpha API

    The actual WolframApp Class:

    Code (Text):
    package io.github.ramidzkh.dire;

    import java.util.logging.Level;

    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;

    import com.wolfram.alpha.WAEngine;
    import com.wolfram.alpha.WAException;
    import com.wolfram.alpha.WAPlainText;
    import com.wolfram.alpha.WAPod;
    import com.wolfram.alpha.WAQuery;
    import com.wolfram.alpha.WAQueryResult;
    import com.wolfram.alpha.WASubpod;

    public class WolframApp {

        public static void query(String args,
                String appid,
                WolframPluginCore core,
                CommandSender sender) {
           
            // The WAEngine is a factory for creating WAQuery objects,
            // and it also used to perform those queries. You can set properties of
            // the WAEngine (such as the desired API output format types) that will
            // be inherited by all WAQuery objects created from it. Most applications
            // will only need to crete one WAEngine object, which is used throughout
            // the life of the application.
            WAEngine engine = new WAEngine();
           
            // These properties will be set in all the WAQuery objects created from this WAEngine.
            engine.setAppID(appid);
            engine.addFormat("plaintext");

            // Create the query.
            WAQuery query = engine.createQuery();
           
            // Set properties of the query.
            query.setInput(args);
           
            try {
                // For educational purposes, print out the URL we are about to send:
                core.getLogger().log(Level.INFO, sender.getName() +  ": Query URL:");
                core.getLogger().log(Level.INFO, sender.getName() + ": " +  engine.toURL(query) + "\n");
               
                // This sends the URL to the Wolfram|Alpha server, gets the XML result
                // and parses it into an object hierarchy held by the WAQueryResult object.
                WAQueryResult queryResult = engine.performQuery(query);
               
                if (queryResult.isError()) {
                    sender.sendMessage("Query error");
                    sender.sendMessage("  error code: " + queryResult.getErrorCode());
                    sender.sendMessage("  error message: " + queryResult.getErrorMessage());
                } else if (!queryResult.isSuccess()) {
                    sender.sendMessage(ChatColor.RED + "Query was not understood; no results available.");
                } else {
                    sender.sendMessage(ChatColor.GREEN + "Successful query. Pods follow:\n");
                    for (WAPod pod : queryResult.getPods()) {
                        if (!pod.isError()) {
                            sender.sendMessage(ChatColor.AQUA + pod.getTitle());
                            sender.sendMessage(ChatColor.AQUA + "------------");
                            for (WASubpod subpod : pod.getSubpods()) {
                                for (Object element : subpod.getContents()) {
                                    if (element instanceof WAPlainText) {
                                        sender.sendMessage(ChatColor.BLUE + ((WAPlainText) element).getText());
                                        sender.sendMessage("");
                                    }
                                }
                            }
                            sender.sendMessage("");
                        }
                    }
                }
            } catch (WAException e) {
                sender.sendMessage("Error! Message as is follows: " + e.getMessage());
                core.getLogger().log(Level.WARNING, "ERROR QUERY BY " + sender.getName() + " QUERY: " + args, e);
            }
        }
    }
     

    Go see the Wolfram|Alpha Java API for the source but it just gets delayed by the HTTPS request.

    Code (Text):
    package io.github.ramidzkh.dire;

    import org.bukkit.command.BlockCommandSender;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitTask;

    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.HoverEvent.Action;
    import net.md_5.bungee.api.chat.TextComponent;

    public class WolframSearch implements CommandExecutor {

        final WolframPluginCore core;

        public WolframSearch(WolframPluginCore core) {
            this.core = core;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
            if(args.length < 1) {
                sender.sendMessage("No query!");
                return false;
            }
           
            if(sender instanceof ConsoleCommandSender) {
                query(args, sender);
                return true;
            } else if(sender instanceof BlockCommandSender) {
                sender.sendMessage("Not enough space for a query");
                return true;
            } else if(sender instanceof Player) {
                Player player = (Player) sender;
               
                if(player.isOp() || player.hasPermission("query")) {
                    BukkitTask task = core.getServer().getScheduler().runTask(core, new Runnable() {
                        @Override
                        public void run() {
                            query(args, player);
                        }
                    });
                   // Some TextComponent Stuff        
                    return true;
                } else {
                    TextComponent tc = new TextComponent();
                    tc.setColor(ChatColor.RED);
                    tc.addExtra("You dont have enough permissions ");
                    TextComponent ptc = new TextComponent();
                    ptc.setColor(ChatColor.YELLOW);
                    if(player.getCustomName() != null) ptc.addExtra(player.getCustomName()); else ptc.addExtra(player.getName());
                    tc.addExtra(ptc);
                    tc.addExtra("!");
                    tc.setHoverEvent(new HoverEvent(Action.SHOW_TEXT, new ComponentBuilder("You don't have enough permissions to query that!").create()));
                    tc.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/help"));
                    player.spigot().sendMessage(tc);
                }
            }
           
            return true;
        }

        private void query(String[] args, CommandSender sender) {
            if(args.length > 1) {
                String s = "";
                for(String sss : args) {
                    s+= sss + " ";
                }
               
                WolframApp.query(s, core.getConfig().getString("app-id"), core, sender);
            } else WolframApp.query(args[0], core.getConfig().getString("app-id"), core, sender);
        }

    }
     
     
  6. you know that is running a sync task, ye?
    #runAsynchronousTask or something like that is what you want.
     
  7. LOL I did that while coding but I might of got it clogged up when I did git reset --hard
     
Thread Status:
Not open for further replies.