<commit_before>package me.exz.omniocular.handler;

import me.exz.omniocular.util.LogHelper;
import me.exz.omniocular.util.NBTHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({"CanBeFinal", "UnusedDeclaration"})
public class JSHandler {
    private static final ScriptEngineManager manager = new ScriptEngineManager(null);
    private static ScriptEngine engine = manager.getEngineByName("javascript");
    public static HashSet<String> scriptSet = new HashSet<String>();
    private static List<String> lastTips = new ArrayList<String>();
    private static int lastHash;
    private static Map<String, String>fluidList = new HashMap<String, String>();
    private static Map<String, String>displayNameList = new HashMap<String, String>();

    public static List<String> getBody(Map<Pattern, Node> patternMap, NBTTagCompound n, String id) {
        if (n.hashCode() != lastHash) {
            lastHash = n.hashCode();
            lastTips.clear();
            //LogHelper.info(NBTHelper.NBT2json(n));
            try {
                String json = "var nbt=" + NBTHelper.NBT2json(n) + ";";
                JSHandler.engine.eval(json);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
            for (Map.Entry<Pattern, Node> entry : patternMap.entrySet()) {
                Matcher matcher = entry.getKey().matcher(id);
                if (matcher.matches()) {
                    Element item = (Element) entry.getValue();
//                    if (item.getElementsByTagName("head").getLength() > 0) {
//                        Node head = item.getElementsByTagName("head").item(0);
//                    }
                    if (item.getElementsByTagName("line").getLength() > 0) {
                        String tip;
                        NodeList lines = item.getElementsByTagName("line");
                        for (int i = 0; i < lines.getLength(); i++) {
                            Node line = lines.item(i);
                            tip = "";
                            if (line.getAttributes().getNamedItem("displayname") != null && !line.getAttributes().getNamedItem("displayname").getTextContent().trim().isEmpty()) {
                                String displayname=StatCollector.translateToLocal(line.getAttributes().getNamedItem("displayname").getTextContent());
                                if (patternMap == ConfigHandler.tooltipPattern) {
                                    tip += "\u00A77" + displayname + ": \u00A7f";
                                } else {
                                    tip += displayname + "\u00A4\u00A4a\u00A4\u00A4b\u00A7f";
                                }
                            }
                            String functionContent = line.getTextContent();
                            String hash = "S" + NBTHelper.MD5(functionContent);
                            if (!JSHandler.scriptSet.contains(hash)) {
                                JSHandler.scriptSet.add(hash);
                                //TODO: automatic add "return" if no return found to simplify config file
                                String script = "function " + hash + "()" + "{" + functionContent + "}";
                                try {
                                    JSHandler.engine.eval(script);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            Invocable invoke = (Invocable) JSHandler.engine;
                            try {
                                String r = String.valueOf(invoke.invokeFunction(hash, ""));
                                if (r.equals("__ERROR__")||r.equals("null")||r.equals("undefined")||r.equals("NaN")) {
                                    continue;
                                }
                                tip += r;
                            } catch (Exception e) {
                                continue;
                                //e.printStackTrace();
                            }
                            if (tip.equals("__ERROR__")) {<commit_msg>automatic add "return" if no "return" found to simplify config file<commit_after>package me.exz.omniocular.handler;

import me.exz.omniocular.util.LogHelper;
import me.exz.omniocular.util.NBTHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({"CanBeFinal", "UnusedDeclaration"})
public class JSHandler {
    private static final ScriptEngineManager manager = new ScriptEngineManager(null);
    private static ScriptEngine engine = manager.getEngineByName("javascript");
    public static HashSet<String> scriptSet = new HashSet<String>();
    private static List<String> lastTips = new ArrayList<String>();
    private static int lastHash;
    private static Map<String, String>fluidList = new HashMap<String, String>();
    private static Map<String, String>displayNameList = new HashMap<String, String>();

    public static List<String> getBody(Map<Pattern, Node> patternMap, NBTTagCompound n, String id) {
        if (n.hashCode() != lastHash) {
            lastHash = n.hashCode();
            lastTips.clear();
            //LogHelper.info(NBTHelper.NBT2json(n));
            try {
                String json = "var nbt=" + NBTHelper.NBT2json(n) + ";";
                JSHandler.engine.eval(json);
            } catch (ScriptException e) {
                e.printStackTrace();
            }
            for (Map.Entry<Pattern, Node> entry : patternMap.entrySet()) {
                Matcher matcher = entry.getKey().matcher(id);
                if (matcher.matches()) {
                    Element item = (Element) entry.getValue();
//                    if (item.getElementsByTagName("head").getLength() > 0) {
//                        Node head = item.getElementsByTagName("head").item(0);
//                    }
                    if (item.getElementsByTagName("line").getLength() > 0) {
                        String tip;
                        NodeList lines = item.getElementsByTagName("line");
                        for (int i = 0; i < lines.getLength(); i++) {
                            Node line = lines.item(i);
                            tip = "";
                            if (line.getAttributes().getNamedItem("displayname") != null && !line.getAttributes().getNamedItem("displayname").getTextContent().trim().isEmpty()) {
                                String displayname=StatCollector.translateToLocal(line.getAttributes().getNamedItem("displayname").getTextContent());
                                if (patternMap == ConfigHandler.tooltipPattern) {
                                    tip += "\u00A77" + displayname + ": \u00A7f";
                                } else {
                                    tip += displayname + "\u00A4\u00A4a\u00A4\u00A4b\u00A7f";
                                }
                            }
                            String functionContent = line.getTextContent();
                            String hash = "S" + NBTHelper.MD5(functionContent);
                            if (!JSHandler.scriptSet.contains(hash)) {
                                JSHandler.scriptSet.add(hash);
                                if (!functionContent.contains("return")){
                                    functionContent="return "+functionContent.trim();
                                }
                                String script = "function " + hash + "()" + "{" + functionContent + "}";
                                try {
                                    JSHandler.engine.eval(script);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            Invocable invoke = (Invocable) JSHandler.engine;
                            try {
                                String r = String.valueOf(invoke.invokeFunction(hash, ""));
                                if (r.equals("__ERROR__")||r.equals("null")||r.equals("undefined")||r.equals("NaN")) {
                                    continue;
                                }
                                tip += r;
                            } catch (Exception e) {
                                continue;
                                //e.printStackTrace();
                            }
                            if (tip.equals("__ERROR__")) {
{'fraction_non_alphanumeric': 0.11091177712114816, 'fraction_numerical': 0.006753904601097509, 'mean_word_length': 1.4913249211356467, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '673158', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2324, 'n_words': 442}