Plugin: loading and saving gates
This commit is contained in:
parent
3c5f37d170
commit
f7a86b3c6f
@ -1,5 +1,7 @@
|
|||||||
package org.mcteam.ancientgates;
|
package org.mcteam.ancientgates;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -9,6 +11,9 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.mcteam.ancientgates.commands.*;
|
import org.mcteam.ancientgates.commands.*;
|
||||||
@ -26,19 +31,36 @@ public class Plugin extends JavaPlugin
|
|||||||
|
|
||||||
private String baseCommand;
|
private String baseCommand;
|
||||||
|
|
||||||
|
private String gatesPath = "gates";
|
||||||
|
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
public List<BaseCommand> commands = new ArrayList<BaseCommand>();
|
public List<BaseCommand> commands = new ArrayList<BaseCommand>();
|
||||||
|
|
||||||
|
|
||||||
public Plugin()
|
public Plugin()
|
||||||
{
|
{
|
||||||
instance = this;
|
instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onLoad()
|
||||||
|
{
|
||||||
|
ConfigurationSerialization.registerClass(Gate.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable()
|
||||||
|
{
|
||||||
|
// Save gates
|
||||||
|
saveGates();
|
||||||
|
|
||||||
log("Disabled");
|
log("Disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable()
|
public void onEnable()
|
||||||
{
|
{
|
||||||
@ -63,6 +85,9 @@ public class Plugin extends JavaPlugin
|
|||||||
pm.registerEvents(this.playerListener, this);
|
pm.registerEvents(this.playerListener, this);
|
||||||
pm.registerEvents(this.blockListener, this);
|
pm.registerEvents(this.blockListener, this);
|
||||||
|
|
||||||
|
// Load gates
|
||||||
|
loadGates();
|
||||||
|
|
||||||
log("Enabled");
|
log("Enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,13 +108,16 @@ public class Plugin extends JavaPlugin
|
|||||||
return this.baseCommand;
|
return this.baseCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
|
||||||
|
{
|
||||||
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
|
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
|
||||||
this.handleCommand(sender, parameters);
|
this.handleCommand(sender, parameters);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void handleCommand(CommandSender sender, List<String> parameters)
|
public void handleCommand(CommandSender sender, List<String> parameters)
|
||||||
{
|
{
|
||||||
if (parameters.size() == 0)
|
if (parameters.size() == 0)
|
||||||
@ -113,15 +141,45 @@ public class Plugin extends JavaPlugin
|
|||||||
sender.sendMessage("Unknown gate-command \"" + commandName + "\". Try " + "/" + getBaseCommand() + " help");
|
sender.sendMessage("Unknown gate-command \"" + commandName + "\". Try " + "/" + getBaseCommand() + " help");
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
|
||||||
// Logging
|
/*
|
||||||
// -------------------------------------------- //
|
* Logging
|
||||||
public static void log(String msg) {
|
*/
|
||||||
|
public static void log(String msg)
|
||||||
|
{
|
||||||
log(Level.INFO, msg);
|
log(Level.INFO, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void log(Level level, String msg) {
|
public static void log(Level level, String msg) {
|
||||||
Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg);
|
Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Saving and Loading Gates
|
||||||
|
*/
|
||||||
|
public void loadGates()
|
||||||
|
{
|
||||||
|
File gatesFile = new File(getDataFolder(), "gates.yml");
|
||||||
|
FileConfiguration gatesConfig = YamlConfiguration.loadConfiguration(gatesFile);
|
||||||
|
|
||||||
|
gatesConfig.getList(gatesPath); // this will create all the gates
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void saveGates()
|
||||||
|
{
|
||||||
|
File gatesFile = new File(getDataFolder(), "gates.yml");
|
||||||
|
FileConfiguration gatesConfig = YamlConfiguration.loadConfiguration(gatesFile);
|
||||||
|
|
||||||
|
gatesConfig.set(gatesPath, new ArrayList<Object>(Gate.getAll()));
|
||||||
|
|
||||||
|
try {
|
||||||
|
gatesConfig.save(gatesFile);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
log("ERROR: Could not save gates to disk.");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user