<commit_before>package valandur.webapi.integration.huskycrates;

import com.codehusky.huskycrates.HuskyCrates;
import com.codehusky.huskycrates.crate.VirtualCrate;
import org.eclipse.jetty.http.HttpMethod;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.PluginContainer;
import valandur.webapi.WebAPI;
import valandur.webapi.api.annotation.WebAPIEndpoint;
import valandur.webapi.api.annotation.WebAPIServlet;
import valandur.webapi.api.servlet.WebAPIBaseServlet;
import valandur.webapi.json.JsonService;
import valandur.webapi.servlet.ServletData;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@WebAPIServlet(basePath = "husky")
public class HuskyCratesServlet extends WebAPIBaseServlet {

    public static void onRegister() {
        JsonService json = WebAPI.getJsonService();
        json.registerSerializer(CachedCrate.class, CachedCrateSerializer.class);
        json.registerSerializer(CachedCrateReward.class, CachedCrateRewardSerializer.class);
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path = "crate", perm = "husky")
    public void getCrates(ServletData data) {
        Optional<PluginContainer> optContainer = Sponge.getPluginManager().getPlugin("huskycrates");
        if (!optContainer.isPresent()) {
            data.sendError(HttpServletResponse.SC_NOT_FOUND, "HuskyCrates plugin not found");
            return;
        }

        Optional<?> optPlugin = optContainer.get().getInstance();
        if (!optPlugin.isPresent()) {
            data.sendError(HttpServletResponse.SC_NOT_FOUND, "HuskyCrates plugin instance not found");
            return;
        }

        List<CachedCrate> crates = new ArrayList<>();
        HuskyCrates plugin = (HuskyCrates)optPlugin.get();
        for (VirtualCrate crate : plugin.getCrateUtilities().crateTypes.values()) {
            crates.add(new CachedCrate(crate));
        }

        data.addJson("ok", true, false);
        data.addJson("crates", crates, data.getQueryParam("details").isPresent());
    }
}
<commit_msg>fix(integration): Run husky crates plugin methods on the main server thread<commit_after>package valandur.webapi.integration.huskycrates;

import com.codehusky.huskycrates.HuskyCrates;
import com.codehusky.huskycrates.crate.VirtualCrate;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.ConcurrentHashSet;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.PluginContainer;
import valandur.webapi.WebAPI;
import valandur.webapi.api.annotation.WebAPIEndpoint;
import valandur.webapi.api.annotation.WebAPIServlet;
import valandur.webapi.api.servlet.WebAPIBaseServlet;
import valandur.webapi.json.JsonService;
import valandur.webapi.servlet.ServletData;

import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
import java.util.Set;

@WebAPIServlet(basePath = "husky")
public class HuskyCratesServlet extends WebAPIBaseServlet {

    public static void onRegister() {
        JsonService json = WebAPI.getJsonService();
        json.registerSerializer(CachedCrate.class, CachedCrateSerializer.class);
        json.registerSerializer(CachedCrateReward.class, CachedCrateRewardSerializer.class);
    }


    private HuskyCrates getHuskyPlugin(ServletData data) {
        Optional<PluginContainer> optContainer = Sponge.getPluginManager().getPlugin("huskycrates");
        if (!optContainer.isPresent()) {
            data.sendError(HttpServletResponse.SC_NOT_FOUND, "HuskyCrates plugin not found");
            return null;
        }

        Optional<?> optPlugin = optContainer.get().getInstance();
        if (!optPlugin.isPresent()) {
            data.sendError(HttpServletResponse.SC_NOT_FOUND, "HuskyCrates plugin instance not found");
            return null;
        }

        return (HuskyCrates)optPlugin.get();
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path = "crate", perm = "list")
    public void getCrates(ServletData data) {
        HuskyCrates plugin = getHuskyPlugin(data);
        if (plugin == null) return;

        Set<CachedCrate> crates = new ConcurrentHashSet<>();
        WebAPI.runOnMain(() -> {
            for (VirtualCrate crate : plugin.getCrateUtilities().crateTypes.values()) {
                crates.add(new CachedCrate(crate));
            }
        });

        data.addJson("ok", true, false);
        data.addJson("crates", crates, data.getQueryParam("details").isPresent());
    }

    @WebAPIEndpoint(method = HttpMethod.GET, path = "crate/:id", perm = "one")
    public void getCrate(ServletData data, String id) {
        HuskyCrates plugin = getHuskyPlugin(data);
        if (plugin == null) return;

        Optional<CachedCrate> optRes = WebAPI.runOnMain(() -> {
            VirtualCrate crate = plugin.crateUtilities.getVirtualCrate(id);
            if (crate == null) {
                data.sendError(HttpServletResponse.SC_NOT_FOUND, "Crate not found");
                return null;
            }

            return new CachedCrate(crate);
        });

        data.addJson("ok", optRes.isPresent(), false);
        data.addJson("crate", optRes.orElse(null), true);
    }
}

{'fraction_non_alphanumeric': 0.10847903741304757, 'fraction_numerical': 0.0, 'mean_word_length': 3.2458100558659218, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062305', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1595, 'n_words': 270}