Java Code Examples for cn.nukkit.utils.Utils#readFile()

The following examples show how to use cn.nukkit.utils.Utils#readFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaPluginLoader.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public PluginDescription getPluginDescription(File file) {
    try (JarFile jar = new JarFile(file)) {
        JarEntry entry = jar.getJarEntry("nukkit.yml");
        if (entry == null) {
            entry = jar.getJarEntry("plugin.yml");
            if (entry == null) {
                return null;
            }
        }
        try (InputStream stream = jar.getInputStream(entry)) {
            return new PluginDescription(Utils.readFile(stream));
        }
    } catch (IOException e) {
        return null;
    }
}
 
Example 2
Source File: JavaPluginLoader.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public PluginDescription getPluginDescription(File file) {
    try (JarFile jar = new JarFile(file)) {
        JarEntry entry = jar.getJarEntry("nukkit.yml");
        if (entry == null) {
            entry = jar.getJarEntry("plugin.yml");
            if (entry == null) {
                return null;
            }
        }
        try (InputStream stream = jar.getInputStream(entry)) {
            return new PluginDescription(Utils.readFile(stream));
        }
    } catch (IOException e) {
        return null;
    }
}
 
Example 3
Source File: JavaPluginLoader.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public PluginDescription getPluginDescription(File file) {
    try (JarFile jar = new JarFile(file)) {
        JarEntry entry = jar.getJarEntry("nukkit.yml");
        if (entry == null) {
            entry = jar.getJarEntry("plugin.yml");
            if (entry == null) {
                return null;
            }
        }
        try (InputStream stream = jar.getInputStream(entry)) {
            return new PluginDescription(Utils.readFile(stream));
        }
    } catch (IOException e) {
        return null;
    }
}
 
Example 4
Source File: BaseLang.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(String path) {
    try {
        String content = Utils.readFile(path);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 5
Source File: BaseLang.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(InputStream stream) {
    try {
        String content = Utils.readFile(new BufferedInputStream(stream));
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 6
Source File: BaseLang.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(String path) {
    try {
        String content = Utils.readFile(path);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 7
Source File: BaseLang.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(InputStream stream) {
    try {
        String content = Utils.readFile(stream);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 8
Source File: EconomyAPI.java    From EconomyAPI with GNU General Public License v3.0 5 votes vote down vote up
private void importLanguages(){
	this.language = new HashMap<>();
	
	for(String lang : langList){
		InputStream is = this.getResource("lang_" + lang + ".json");
		try {
			JSONObject obj = new JSONObject(Utils.readFile(is));
			this.language.put(lang, obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
Example 9
Source File: Language.java    From EssentialsNK with GNU General Public License v3.0 5 votes vote down vote up
private static Map<String, String> loadLang(InputStream stream) {
    try {
        String content = Utils.readFile(stream);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            StringBuilder value = new StringBuilder();
            for (int i = 1; i < t.length - 1; i++) {
                value.append(t[i]).append("=");
            }
            value.append(t[t.length - 1]);
            if (value.toString().equals("")) {
                continue;
            }
            d.put(key, value.toString());
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 10
Source File: BaseLang.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(String path) {
    try {
        String content = Utils.readFile(path);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 11
Source File: BaseLang.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected Map<String, String> loadLang(InputStream stream) {
    try {
        String content = Utils.readFile(stream);
        Map<String, String> d = new HashMap<>();
        for (String line : content.split("\n")) {
            line = line.trim();
            if (line.equals("") || line.charAt(0) == '#') {
                continue;
            }
            String[] t = line.split("=");
            if (t.length < 2) {
                continue;
            }
            String key = t[0];
            String value = "";
            for (int i = 1; i < t.length - 1; i++) {
                value += t[i] + "=";
            }
            value += t[t.length - 1];
            if (value.equals("")) {
                continue;
            }
            d.put(key, value);
        }
        return d;
    } catch (IOException e) {
        Server.getInstance().getLogger().logException(e);
        return null;
    }
}
 
Example 12
Source File: EconomyAPI.java    From EconomyAPI with GNU General Public License v3.0 5 votes vote down vote up
private void importLanguages(){
	this.language = new HashMap<>();
	
	for(String lang : langList){
		InputStream is = this.getResource("lang_" + lang + ".json");
		try {
			JSONObject obj = new JSONObject(Utils.readFile(is));
			this.language.put(lang, obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
Example 13
Source File: BugReportGenerator.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private String generate() throws IOException {
    File reports = new File(Nukkit.DATA_PATH, "logs/bug_reports");
    if (!reports.isDirectory()) {
        reports.mkdirs();
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmSS");
    String date = simpleDateFormat.format(new Date());

    StringBuilder model = new StringBuilder();
    long totalDiskSpace = 0;
    int diskNum = 0;
    for (Path root : FileSystems.getDefault().getRootDirectories()) {
        try {
            FileStore store = Files.getFileStore(root);
            model.append("Disk ").append(diskNum++).append(":(avail=").append(getCount(store.getUsableSpace(), true))
                    .append(", total=").append(getCount(store.getTotalSpace(), true))
                    .append(") ");
            totalDiskSpace += store.getTotalSpace();
        } catch (IOException e) {
            //
        }
    }

    StringWriter stringWriter = new StringWriter();
    throwable.printStackTrace(new PrintWriter(stringWriter));

    StackTraceElement[] stackTrace = throwable.getStackTrace();
    boolean pluginError = false;
    if (stackTrace.length > 0) {
        pluginError = !throwable.getStackTrace()[0].getClassName().startsWith("cn.nukkit");
    }


    File mdReport = new File(reports, date + "_" + throwable.getClass().getSimpleName() + ".md");
    mdReport.createNewFile();
    String content = Utils.readFile(this.getClass().getClassLoader().getResourceAsStream("report_template.md"));

    String cpuType = System.getenv("PROCESSOR_IDENTIFIER");
    OperatingSystemMXBean osMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    content = content.replace("${NUKKIT_VERSION}", Nukkit.VERSION);
    content = content.replace("${JAVA_VERSION}", System.getProperty("java.vm.name") + " (" + System.getProperty("java.runtime.version") + ")");
    content = content.replace("${HOSTOS}", osMXBean.getName() + "-" + osMXBean.getArch() + " [" + osMXBean.getVersion() + "]");
    content = content.replace("${MEMORY}", getCount(osMXBean.getTotalPhysicalMemorySize(), true));
    content = content.replace("${STORAGE_SIZE}", getCount(totalDiskSpace, true));
    content = content.replace("${CPU_TYPE}", cpuType == null ? "UNKNOWN" : cpuType);
    content = content.replace("${AVAILABLE_CORE}", String.valueOf(osMXBean.getAvailableProcessors()));
    content = content.replace("${STACKTRACE}", stringWriter.toString());
    content = content.replace("${PLUGIN_ERROR}", Boolean.toString(pluginError).toUpperCase());
    content = content.replace("${STORAGE_TYPE}", model.toString());

    Utils.writeFile(mdReport, content);

    return mdReport.getAbsolutePath();
}
 
Example 14
Source File: BugReportGenerator.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private String generate() throws IOException {
    File reports = new File(Nukkit.DATA_PATH, "logs/bug_reports");
    if (!reports.isDirectory()) {
        reports.mkdirs();
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmSS");
    String date = simpleDateFormat.format(new Date());

    SystemInfo systemInfo = new SystemInfo();
    long totalDiskSize = 0;
    StringBuilder model = new StringBuilder();
    for (HWDiskStore hwDiskStore : systemInfo.getHardware().getDiskStores()) {
        totalDiskSize += hwDiskStore.getSize();
        if (!model.toString().contains(hwDiskStore.getModel())) {
            model.append(hwDiskStore.getModel()).append(" ");
        }
    }

    StringWriter stringWriter = new StringWriter();
    throwable.printStackTrace(new PrintWriter(stringWriter));


    File mdReport = new File(reports, date + "_" + throwable.getClass().getSimpleName() + ".md");
    mdReport.createNewFile();
    String content = Utils.readFile(this.getClass().getClassLoader().getResourceAsStream("report_template.md"));

    Properties properties = getGitRepositoryState();
    System.out.println(properties.getProperty("git.commit.id.abbrev"));
    String abbrev = properties.getProperty("git.commit.id.abbrev");

    content = content.replace("${NUKKIT_VERSION}", Nukkit.VERSION);
    content = content.replace("${GIT_COMMIT_ABBREV}", abbrev);
    content = content.replace("${JAVA_VERSION}", System.getProperty("java.vm.name") + " (" + System.getProperty("java.runtime.version") + ")");
    content = content.replace("${HOSTOS}", systemInfo.getOperatingSystem().getFamily() + " [" + systemInfo.getOperatingSystem().getVersion().getVersion() + "]");
    content = content.replace("${MEMORY}", getCount(systemInfo.getHardware().getMemory().getTotal(), true));
    content = content.replace("${STORAGE_SIZE}", getCount(totalDiskSize, true));
    content = content.replace("${CPU_TYPE}", systemInfo.getHardware().getProcessor().getName());
    content = content.replace("${PHYSICAL_CORE}", String.valueOf(systemInfo.getHardware().getProcessor().getPhysicalProcessorCount()));
    content = content.replace("${LOGICAL_CORE}", String.valueOf(systemInfo.getHardware().getProcessor().getLogicalProcessorCount()));
    content = content.replace("${STACKTRACE}", stringWriter.toString());
    content = content.replace("${PLUGIN_ERROR}", String.valueOf(!throwable.getStackTrace()[0].getClassName().startsWith("cn.nukkit")).toUpperCase());
    content = content.replace("${STORAGE_TYPE}", model.toString());

    Utils.writeFile(mdReport, content);

    return mdReport.getAbsolutePath();
}