com.mewin.util.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.mewin.util.Utils.java

Source

/*
 * Copyright (C) 2012 mewin <mewin001@hotmail.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.mewin.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import org.apache.commons.lang.StringEscapeUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.Yaml;

/**
 *
 * @author mewin<mewin001@hotmail.de>
 */
public final class Utils {
    private static SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    public static Plugin getPlugin(String plugName, Class pluginClass) {
        Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(plugName);

        if (plugin != null || !pluginClass.isInstance(plugin)) {
            return null;
        }

        return plugin;
    }

    public static Map getConfig(Plugin plugin) {
        Yaml yaml = new Yaml();
        File configFile = new File(plugin.getDataFolder(), "config.yml");

        if (!configFile.exists()) {
            createDefaultConfig(plugin);
        }
        try {
            FileReader reader = new FileReader(configFile);

            return (Map) yaml.load(reader);
        } catch (FileNotFoundException ex) {
            plugin.getLogger().log(Level.SEVERE, "Could not load plugin config file: ", ex);
            return (Map) yaml.load(plugin.getResource("/config.yml"));
        }
    }

    public static void createDefaultConfig(Plugin plugin) {
        FileOutputStream out = null;
        InputStream in = null;
        try {
            File configFile = new File(plugin.getDataFolder(), "/config.yml");

            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdirs();
            }

            if (!configFile.exists()) {
                configFile.createNewFile();
            }

            out = new FileOutputStream(configFile);
            in = plugin.getResource("config.yml");

            int next;
            while ((next = in.read()) >= 0) {
                out.write(next);
            }
        } catch (IOException ex) {
            plugin.getLogger().log(Level.SEVERE, "Could not create default configuration file: ", ex);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }

                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
            }
        }
    }

    public static File getLogFile(Plugin plugin) {
        File logFolder = new File(plugin.getDataFolder(), "logs");

        if (!logFolder.exists()) {
            logFolder.mkdirs();
        }

        File logFile = new File(logFolder, sdf.format(new Date()) + ".log");

        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException ex) {
                Bukkit.getLogger().log(Level.SEVERE, "Could not create log file for " + plugin.getName() + ": ",
                        ex);
                return null;
            }
        }

        return logFile;
    }

    public static String unescapeHTML(String html) {
        return StringEscapeUtils.unescapeHtml(html);
    }

    public static String unescapeJava(String java) {
        return StringEscapeUtils.unescapeJava(java);
    }

    public static String unescape(String string) {
        return unescapeJava(unescapeHTML(string)).replace("\r", "");
    }
}