Added a better data system

This commit is contained in:
Lennart 2013-03-17 18:09:31 +01:00
parent ade8c0c134
commit 261057b508
10 changed files with 211 additions and 87 deletions

View file

@ -1,5 +1,6 @@
package me.lenis0012.mr;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public interface MPlayer extends Player {
@ -43,4 +44,25 @@ public interface MPlayer extends Player {
* @return player chatting?
*/
public boolean isChatting();
/**
* Set the player's home
*
* @param loc Location
*/
public void setHome(Location loc);
/**
* Get the player's home
*
* @return Location
*/
public Location getHome();
/**
* Get the player's config file
*
* @return Config
*/
public PlayerConfig getConfig();
}

View file

@ -5,6 +5,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -38,13 +39,23 @@ public class Marriage extends JavaPlugin
public Logger log = Logger.getLogger("Minecraft");
public boolean eco = false;
ChildManager manager;
public static String COMPAT_VERSION = "v1_5_R1";
public Map<String, PlayerConfig> configs = new HashMap<String, PlayerConfig>();
@Override
public void onEnable()
{
public void onEnable() {
FileConfiguration config = this.getConfig();
PluginManager pm = this.getServer().getPluginManager();
if(this.validVersion(COMPAT_VERSION)) {
log.info("[Marriage] Running on nms path: " + COMPAT_VERSION);
} else {
log.severe("[Marriage] Marriage is not compatible with the version of minecraft you are using!");
log.severe("Please update Marriage or wait for an update.");
return;
}
//register events/commands
pm.registerEvents(new PlayerListener(this), this);
getCommand("marry").setExecutor(new MarryCMD(this));
@ -60,6 +71,10 @@ public class Marriage extends JavaPlugin
FileConfiguration cfg = this.getCustomConfig();
cfg.addDefault("partners", partners);
cfg.options().copyDefaults(true);
if(cfg.contains("Married"))
cfg.set("Married", null);
if(cfg.contains("home"))
cfg.set("home", null);
this.saveCustomConfig();
//setup metrics
@ -67,8 +82,7 @@ public class Marriage extends JavaPlugin
{
Metrics metrics = new Metrics(this);
metrics.start();
} catch(Exception e)
{
} catch(Exception e) {
this.getLogger().info("[Marriage] Failed sending stats to mcstats.org");
}
@ -91,31 +105,36 @@ public class Marriage extends JavaPlugin
manager.stop();
}
public Player getPlayer(String name)
{
private boolean validVersion(String version) {
try {
Class.forName("net.minecraft.server." + version + ".World");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
public Player getPlayer(String name) {
Player t = Bukkit.getServer().getPlayer(name);
if(t != null)
if(t.isOnline())
return t;
for(Player player : this.getServer().getOnlinePlayers())
{
for(Player player : this.getServer().getOnlinePlayers()) {
if(player.getName().toLowerCase().startsWith(name) || player.getName().startsWith(name))
{
return player;
}
}
for(Player player : this.getServer().getOnlinePlayers())
{
for(Player player : this.getServer().getOnlinePlayers()) {
if(player.getName().toLowerCase().endsWith(name) || player.getName().endsWith(name))
{
return player;
}
}
for(Player player : this.getServer().getOnlinePlayers())
{
for(Player player : this.getServer().getOnlinePlayers()) {
if(player.getName().toLowerCase().contains(name) || player.getName().contains(name))
{
return player;
@ -133,8 +152,32 @@ public class Marriage extends JavaPlugin
return new SimpleMPlayer(server, ep);
}
public void reloadCustomConfig()
{
public PlayerConfig getConfig(String name) {
if(configs.containsKey(name))
return configs.get(name);
PlayerConfig cfg = this.getPlayerConfig(name);
configs.put(name, cfg);
return cfg;
}
public PlayerConfig getPlayerConfig(String name) {
File dir = new File(this.getDataFolder(), "playerdata");
dir.mkdirs();
File file = new File(dir, name+".yml");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
log.log(Level.SEVERE, "[Marriage] Could not create data file for player '" +name + "'", e);
}
}
return new PlayerConfig(file);
}
public void reloadCustomConfig() {
if (customConfigFile == null)
{
customConfigFile = new File(getDataFolder(), "data.yml");
@ -147,8 +190,7 @@ public class Marriage extends JavaPlugin
}
}
public FileConfiguration getCustomConfig()
{
public FileConfiguration getCustomConfig() {
if (customConfig == null)
{
this.reloadCustomConfig();
@ -156,8 +198,7 @@ public class Marriage extends JavaPlugin
return customConfig;
}
public void saveCustomConfig()
{
public void saveCustomConfig() {
if (customConfig == null || customConfigFile == null)
{
return;
@ -168,8 +209,7 @@ public class Marriage extends JavaPlugin
this.getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);}
}
public String fixColors(String message)
{
public String fixColors(String message) {
message = message.replaceAll("&0", ChatColor.BLACK.toString());
message = message.replaceAll("&1", ChatColor.DARK_BLUE.toString());
message = message.replaceAll("&2", ChatColor.DARK_GREEN.toString());

View file

@ -0,0 +1,38 @@
package me.lenis0012.mr;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
public class PlayerConfig extends YamlConfiguration {
private File file;
public PlayerConfig(File file) {
this.file = file;
this.reload();
}
public void save() {
try {
this.save(this.file);
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot save " + file, ex);
}
}
public void reload() {
try {
this.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
}
}

View file

@ -4,15 +4,18 @@ import java.util.List;
import net.minecraft.server.v1_5_R1.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.v1_5_R1.CraftServer;
import org.bukkit.craftbukkit.v1_5_R1.entity.CraftPlayer;
public class SimpleMPlayer extends CraftPlayer implements MPlayer {
private String name;
private FileConfiguration cfg;
private Marriage plugin;
private boolean chatting = false;
private FileConfiguration cfg;
public SimpleMPlayer(CraftServer server, EntityPlayer entity) {
super(server, entity);
@ -22,14 +25,13 @@ public class SimpleMPlayer extends CraftPlayer implements MPlayer {
}
public boolean isMarried() {
String par1Str = cfg.getString("Married." + name);
String par1Str = getConfig().getString("partner");
return par1Str != null && par1Str != "";
}
public String getPartner() {
if(isMarried())
{
String par1Str = cfg.getString("Married." + name);
if(isMarried()) {
String par1Str = getConfig().getString("partner");
return par1Str;
}
return "";
@ -38,10 +40,14 @@ public class SimpleMPlayer extends CraftPlayer implements MPlayer {
public void setPartner(String user) {
List<String> list = cfg.getStringList("partners");
list.add(user);
cfg.set("Married." + name, user);
cfg.set("Married." + user, name);
PlayerConfig cfg = this.getConfig();
PlayerConfig partner_cfg = plugin.getConfig(user);
cfg.set("partner", user);
partner_cfg.set("partner", name);
cfg.set("partners", list);
save();
cfg.save();
partner_cfg.save();
}
public void divorce() {
@ -52,12 +58,17 @@ public class SimpleMPlayer extends CraftPlayer implements MPlayer {
list.remove(name);
if(list.contains(partner))
list.remove(partner);
cfg.set("Married."+name, null);
cfg.set("Married."+partner, null);
cfg.set("home."+name, null);
cfg.set("home."+partner, null);
PlayerConfig cfg = this.getConfig();
PlayerConfig partner_cfg = plugin.getConfig(getPartner());
cfg.set("partner", null);
cfg.set("home", null);
partner_cfg.set("partner", null);
partner_cfg.set("home", null);
cfg.set("partners", list);
save();
cfg.save();
partner_cfg.save();
}
}
@ -74,4 +85,54 @@ public class SimpleMPlayer extends CraftPlayer implements MPlayer {
public boolean isChatting() {
return chatting;
}
}
@Override
public void setHome(Location loc) {
PlayerConfig cfg = this.getConfig();
PlayerConfig partner_cfg = plugin.getConfig(getPartner());
String world = loc.getWorld().getName();
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
float yaw = loc.getYaw();
float pitch = loc.getPitch();
cfg.set("home.world", world);
cfg.set("home.x", x);
cfg.set("home.y", y);
cfg.set("home.z", z);
cfg.set("home.yaw", yaw);
cfg.set("home.pitch", pitch);
partner_cfg.set("home.world", world);
partner_cfg.set("home.x", x);
partner_cfg.set("home.y", y);
partner_cfg.set("home.z", z);
partner_cfg.set("home.yaw", yaw);
partner_cfg.set("home.pitch", pitch);
cfg.save();
partner_cfg.save();
}
@Override
public Location getHome() {
PlayerConfig cfg = this.getConfig();
if(!cfg.contains("home"))
return null;
World world = Bukkit.getWorld(cfg.getString("home.world"));
double x = cfg.getDouble("home.x");
double y = cfg.getDouble("home.y");
double z = cfg.getDouble("home.z");
float yaw = cfg.getInt("home.yaw");
float pitch = cfg.getInt("home.pitch");
if(world != null)
return new Location(world, x, y, z, yaw, pitch);
else
return null;
}
@Override
public PlayerConfig getConfig() {
return plugin.getConfig(name);
}
}

View file

@ -4,10 +4,8 @@ import me.lenis0012.mr.MPlayer;
import me.lenis0012.mr.Marriage;
import me.lenis0012.mr.util.EcoUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
public class HomeCommand
@ -21,13 +19,13 @@ public class HomeCommand
player.sendMessage(ChatColor.RED + "You dont have a partner.");
return;
}
String user = player.getName();
Location home = mp.getHome();
if(!player.hasPermission("marry.home") && !player.hasPermission("marry.*"))
{
player.sendMessage(ChatColor.RED + "No permission.");
return;
}
if(plugin.getCustomConfig().getString("home."+user+".world") == null)
if(home == null)
{
player.sendMessage(ChatColor.RED + "Home not set");
return;
@ -41,13 +39,6 @@ public class HomeCommand
}
}
World world = Bukkit.getServer().getWorld(plugin.getCustomConfig().getString("home."+user+".world"));
int x = plugin.getCustomConfig().getInt("home."+user+".x");
int y = plugin.getCustomConfig().getInt("home."+user+".y");
int z = plugin.getCustomConfig().getInt("home."+user+".z");
float yaw = Float.valueOf(plugin.getCustomConfig().getString("home."+user+".yaw"));
float pitch = Float.valueOf(plugin.getCustomConfig().getString("home."+user+".pitch"));
Location home = new Location(world, x, y, z, yaw, pitch);
player.teleport(home);
player.sendMessage(ChatColor.GREEN+"Teleporing to home...");
}

View file

@ -17,8 +17,8 @@ public class MarryCommand
{
if(op.isOnline())
{
MPlayer mp = plugin.getMPlayer(op);
MPlayer tp = plugin.getMPlayer(player);
MPlayer mp = plugin.getMPlayer(player);
MPlayer tp = plugin.getMPlayer(op);
if(!player.hasPermission("marry.marry") && !player.hasPermission("marry.*"))
{
player.sendMessage(ChatColor.RED + "No permission.");

View file

@ -20,7 +20,6 @@ public class SethomeCommand
player.sendMessage(ChatColor.RED + "You dont have a partner.");
return;
}
String user = player.getName();
String partner = mp.getPartner();
if(!player.hasPermission("marry.sethome") && !player.hasPermission("marry.*"))
{
@ -37,25 +36,7 @@ public class SethomeCommand
}
Location loc = player.getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
String yaw = String.valueOf(loc.getYaw());
String pitch = String.valueOf(loc.getPitch());
String world = loc.getWorld().getName();
plugin.getCustomConfig().set("home."+user+".world", world);
plugin.getCustomConfig().set("home."+user+".x", x);
plugin.getCustomConfig().set("home."+user+".y", y);
plugin.getCustomConfig().set("home."+user+".z", z);
plugin.getCustomConfig().set("home."+user+".yaw", yaw);
plugin.getCustomConfig().set("home."+user+".pitch", pitch);
plugin.getCustomConfig().set("home."+partner+".world", world);
plugin.getCustomConfig().set("home."+partner+".x", x);
plugin.getCustomConfig().set("home."+partner+".y", y);
plugin.getCustomConfig().set("home."+partner+".z", z);
plugin.getCustomConfig().set("home."+partner+".yaw", yaw);
plugin.getCustomConfig().set("home."+partner+".pitch", pitch);
plugin.saveCustomConfig();
mp.setHome(loc);
player.sendMessage(ChatColor.GREEN+"Home set");
Player op = Bukkit.getPlayer(partner);

View file

@ -45,4 +45,4 @@ public class PlayerListener implements Listener {
event.setCancelled(true);
}
}
}
}

View file

@ -4,11 +4,6 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import com.bergerkiller.bukkit.common.reflection.classes.EntityTypesRef;
import net.minecraft.server.v1_5_R1.EntityTypes;
@ -19,23 +14,19 @@ public class ReflectionUtil {
public static void registerEntityType(Class<?> cbClass, String cbType, int cbID) {
if(!addedClasses.contains(cbClass)) {
try {
Plugin bkc = Bukkit.getServer().getPluginManager().getPlugin("BKCommonLib");
if(bkc != null)
EntityTypesRef.register(cbClass, cbType, cbID);
else {
//data format start
Class[] tmp = new Class[3];
tmp[0] = Class.class;
tmp[1] = String.class;
tmp[2] = int.class;
//data format end
//data format start
Class[] tmp = new Class[3];
tmp[0] = Class.class;
tmp[1] = String.class;
tmp[2] = int.class;
//data format end
Method entities = EntityTypes.class.getDeclaredMethod("a", tmp);
entities.setAccessible(true);
Method entities = EntityTypes.class.getDeclaredMethod("a", tmp);
entities.setAccessible(true);
//write custom data to the entity list
entities.invoke(entities, cbClass, cbType, cbID);
}
//write custom data to the entity list
entities.invoke(entities, cbClass, cbType, cbID);
entities.setAccessible(false);
addedClasses.add(cbClass);
} catch(Exception e) {

View file

@ -1,5 +1,5 @@
name: Marriage
version: 1.04
version: 1.05
main: me.lenis0012.mr.Marriage
author: lenis0012
dev-url: http://dev.bukkit.org/server-mods/marriage-reloaded/