MobHealth Code

Select a file to view contents.

.classpath
.gitignore
.project
.settings/org.eclipse.core.resources.prefs
README.md
bin/me/sablednah/MobHealth/API/MobHealthAPI.class
bin/me/sablednah/MobHealth/HeroesEventListener.class
bin/me/sablednah/MobHealth/MessageScheduler.class
bin/me/sablednah/MobHealth/Metrics$1.class
bin/me/sablednah/MobHealth/Metrics$Graph.class
bin/me/sablednah/MobHealth/Metrics$Plotter.class
bin/me/sablednah/MobHealth/Metrics.class
bin/me/sablednah/MobHealth/MobHealth$1.class
bin/me/sablednah/MobHealth/MobHealth.class
bin/me/sablednah/MobHealth/MobHealthCommandExecutor.class
bin/me/sablednah/MobHealth/NewWidgitActions.class
bin/me/sablednah/MobHealth/SaveToggle.class
bin/me/sablednah/MobHealth/ServerDamageEntityListener$ScheduledshowBar.class
bin/me/sablednah/MobHealth/ServerDamageEntityListener.class
bin/me/sablednah/MobHealth/SetHealth.class
bin/me/sablednah/MobHealth/SkillMessageScheduler.class
bin/me/sablednah/MobHealth/SpoutNotifications.class
bin/me/sablednah/MobHealth/Updater$UpdateResult.class
bin/me/sablednah/MobHealth/Updater$UpdateRunnable.class
bin/me/sablednah/MobHealth/Updater$UpdateType.class
bin/me/sablednah/MobHealth/Updater.class
licence.txt
pom.xml
src/main/java/me/sablednah/MobHealth/API/ActionBar.java
src/main/java/me/sablednah/MobHealth/API/ActionBar_1_8_1.java
src/main/java/me/sablednah/MobHealth/API/ActionBar_1_8_3.java
src/main/java/me/sablednah/MobHealth/API/ActionBar_1_8_4.java
src/main/java/me/sablednah/MobHealth/API/MobHealthAPI.java
src/main/java/me/sablednah/MobHealth/API/PlayerHealthEvent.java
src/main/java/me/sablednah/MobHealth/HeroesEventListener.java
src/main/java/me/sablednah/MobHealth/MessageScheduler.java
src/main/java/me/sablednah/MobHealth/Metrics.java
src/main/java/me/sablednah/MobHealth/MobHealth.java
src/main/java/me/sablednah/MobHealth/MobHealthCommandExecutor.java
src/main/java/me/sablednah/MobHealth/NewWidgitActions.java
src/main/java/me/sablednah/MobHealth/SaveToggle.java
src/main/java/me/sablednah/MobHealth/ServerDamageEntityListener.java
src/main/java/me/sablednah/MobHealth/SetHealth.java
src/main/java/me/sablednah/MobHealth/SkillMessageScheduler.java
src/main/java/me/sablednah/MobHealth/SpoutNotifications.java
src/main/java/me/sablednah/MobHealth/Updater.java
src/main/resources/config.yml
src/main/resources/lang.yml
src/main/resources/players.yml
src/main/resources/plugin.yml


/*
 * This file is part of MobHealth.
 * Copyright (C) 2012-2013 Darren Douglas - [email protected]
 *
 * MobHealth 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.
 *
 * MobHealth 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 MobHealth.  If not, see <http://www.gnu.org/licenses/>.
 */

package main.java.me.sablednah.MobHealth;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
//import java.io.Reader;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;


import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

public class SaveToggle {
	MobHealth mobhealth;
	
	public SaveToggle(MobHealth mh) {
		this.mobhealth = mh;
	}

	public void save(HashMap<UUID, Boolean> pluginEnabled,String path) throws Exception {

		for (Entry<UUID, Boolean> entry : pluginEnabled.entrySet()){
			if (entry.getValue() != null && entry.getKey() != null) {
				getPlayerConfig().set("players." + entry.getKey(), entry.getValue());
			}
		}
		getPlayerConfig().save(MobHealth.PlayerConfigurationFile);
	}

	@SuppressWarnings("deprecation")
    public HashMap<UUID,Boolean> load(String path) throws Exception {
		HashMap<UUID, Boolean> out = new HashMap<UUID, Boolean>();
		if (getPlayerConfig().getConfigurationSection("players") != null) {
			for (String key : getPlayerConfig().getConfigurationSection("players").getKeys(false)) {
			    if (key.length()==32) {
				out.put(UUID.fromString(key), getPlayerConfig().getBoolean("players." + key));
			    } else {
                                out.put(Bukkit.getServer().getOfflinePlayer(key).getUniqueId(), getPlayerConfig().getBoolean("players." + key));   
			    }
			}
			return out;
		} else {
			return new HashMap<UUID,Boolean>();
		}
	}

	public void reloadPlayerConfig() {
		Plugin plugin = Bukkit.getPluginManager().getPlugin("MobHealth");
		if (MobHealth.PlayerConfigurationFile == null) {
			MobHealth.PlayerConfigurationFile = new File(plugin.getDataFolder(), "players.yml");
		}
		MobHealth.PlayerConfig = YamlConfiguration.loadConfiguration(MobHealth.PlayerConfigurationFile);
		MobHealth.PlayerConfig.options().copyDefaults(true);

		// Look for defaults in the jar
		try {
			Reader defConfigStream = mobhealth.getTextResourcePublic("players.yml");
			if (defConfigStream != null) {
				YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
				MobHealth.PlayerConfig.setDefaults(defConfig);
			}
		} catch (NoSuchMethodError e) {
			InputStream defConfigStream = mobhealth.getResourcePublic("players.yml");
			if (defConfigStream != null) {
				@SuppressWarnings("deprecation")
				YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
				MobHealth.PlayerConfig.setDefaults(defConfig);
			}
		}
	}

	public FileConfiguration getPlayerConfig() {
		if (MobHealth.PlayerConfig == null) {
			reloadPlayerConfig();
		}
		return MobHealth.PlayerConfig;
	}

	public void savePlayeronfig() {
		if (MobHealth.PlayerConfig == null || MobHealth.PlayerConfigurationFile == null) {
			return;
		}
		try {
			MobHealth.PlayerConfig.save(MobHealth.PlayerConfigurationFile);
		} catch (IOException ex) {
			MobHealth.logger.severe("Could not save Player config to " + MobHealth.PlayerConfigurationFile + " " + ex);
		}
	}
}

Leave a Reply