New save player system

This commit is contained in:
Grafe 2013-01-23 15:11:37 +01:00
parent f0b564a452
commit 1dd71df330
5 changed files with 219 additions and 185 deletions

View File

@ -1,45 +0,0 @@
package com.dre.dungeonsxl;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class DOfflinePlayer {
public static CopyOnWriteArrayList<DOfflinePlayer> players=new CopyOnWriteArrayList<DOfflinePlayer>();
//Variables
public String name;
public Location oldLocation;
public ItemStack[] oldInventory;
public ItemStack[] oldArmor;
public int oldLvl;
public int oldExp;
public int oldHealth;
public int oldFoodLevel;
public GameMode oldGamemode;
public DOfflinePlayer(){
players.add(this);
}
//Static
public static void check(Player player){
for(DOfflinePlayer offplayer:players){
if(offplayer.name.equalsIgnoreCase(player.getName())){
players.remove(offplayer);
player.teleport(offplayer.oldLocation);
player.getInventory().setContents(offplayer.oldInventory);
player.getInventory().setArmorContents(offplayer.oldArmor);
player.setTotalExperience(offplayer.oldExp);
player.setLevel(offplayer.oldLvl);
player.setHealth(offplayer.oldHealth);
player.setFoodLevel(offplayer.oldFoodLevel);
player.setGameMode(offplayer.oldGamemode);
}
}
}
}

View File

@ -9,7 +9,6 @@ import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@ -34,15 +33,7 @@ public class DPlayer {
public boolean isinTestMode=false; public boolean isinTestMode=false;
public Location oldLocation; public DSavePlayer savePlayer;
public ItemStack[] oldInventory;
public ItemStack[] oldArmor;
public int oldLvl;
public int oldExp;
public int oldHealth;
public int oldFoodLevel;
public int oldFireTicks;
public GameMode oldGamemode;
public boolean isEditing; public boolean isEditing;
public boolean isInDungeonChat=false; public boolean isInDungeonChat=false;
@ -61,18 +52,22 @@ public class DPlayer {
public DPlayer(Player player, World world, Location teleport, boolean isEditing){ public DPlayer(Player player, World world, Location teleport, boolean isEditing){
players.add(this); players.add(this);
this.player=player; this.player=player;
this.world=world; this.world=world;
this.oldLocation=player.getLocation(); this.savePlayer = new DSavePlayer(
this.oldInventory=player.getInventory().getContents(); player.getName(),
this.oldArmor=player.getInventory().getArmorContents(); player.getLocation(),
this.oldExp=player.getTotalExperience(); player.getInventory().getContents(),
this.oldHealth=player.getHealth(); player.getInventory().getArmorContents(),
this.oldFoodLevel=player.getFoodLevel(); player.getLevel(),
this.oldGamemode=player.getGameMode(); player.getTotalExperience(),
this.oldLvl=player.getLevel(); player.getHealth(),
this.oldFireTicks=player.getFireTicks(); player.getFoodLevel(),
player.getFireTicks(),
player.getGameMode()
);
this.player.teleport(teleport); this.player.teleport(teleport);
this.player.getInventory().clear(); this.player.getInventory().clear();
@ -82,6 +77,7 @@ public class DPlayer {
this.player.setHealth(20); this.player.setHealth(20);
this.player.setFoodLevel(20); this.player.setFoodLevel(20);
this.isEditing = isEditing; this.isEditing = isEditing;
if(isEditing) this.player.setGameMode(GameMode.CREATIVE); else this.player.setGameMode(GameMode.SURVIVAL); if(isEditing) this.player.setGameMode(GameMode.CREATIVE); else this.player.setGameMode(GameMode.SURVIVAL);
if(!isEditing){ if(!isEditing){
@ -112,15 +108,7 @@ public class DPlayer {
public void leave(){ public void leave(){
remove(this); remove(this);
this.player.teleport(this.oldLocation); this.savePlayer.reset();
this.player.getInventory().setContents(this.oldInventory);
this.player.getInventory().setArmorContents(this.oldArmor);
this.player.setTotalExperience(this.oldExp);
this.player.setLevel(this.oldLvl);
this.player.setHealth(oldHealth);
this.player.setFoodLevel(oldFoodLevel);
this.player.setGameMode(oldGamemode);
this.player.setFireTicks(oldFireTicks);
if(this.isEditing){ if(this.isEditing){
EditWorld eworld=EditWorld.get(this.world); EditWorld eworld=EditWorld.get(this.world);
@ -248,7 +236,7 @@ public class DPlayer {
} }
public void finish(){ public void finish(){
P.p.msg(this.player, p.language.get("Player_FinishedDungeon"));//ChatColor.YELLOW+"Du hast den Dungeon erfolgreich beendet!"); p.msg(this.player, p.language.get("Player_FinishedDungeon"));
this.isFinished=true; this.isFinished=true;
DGroup dgroup=DGroup.get(this.player); DGroup dgroup=DGroup.get(this.player);
@ -257,7 +245,7 @@ public class DPlayer {
for(Player player:dgroup.players){ for(Player player:dgroup.players){
DPlayer dplayer=get(player); DPlayer dplayer=get(player);
if(!dplayer.isFinished){ if(!dplayer.isFinished){
P.p.msg(this.player, p.language.get("Player_WaitForOtherPlayers"));//ChatColor.YELLOW+"Noch auf Mitspieler warten..."); p.msg(this.player, p.language.get("Player_WaitForOtherPlayers"));
return; return;
} }
} }
@ -279,7 +267,6 @@ public class DPlayer {
p.msg(player, ChatColor.GREEN+"[Chatspy] "+ChatColor.WHITE+msg); p.msg(player, ChatColor.GREEN+"[Chatspy] "+ChatColor.WHITE+msg);
} }
} }
}else{ }else{
GameWorld gworld = GameWorld.get(this.world); GameWorld gworld = GameWorld.get(this.world);
gworld.msg(msg); gworld.msg(msg);
@ -389,52 +376,24 @@ public class DPlayer {
} }
//Static //Static
public static void saveSession(Configuration cFile){
int id = 0;
for(DPlayer dplayer : players){
cFile.set("DPlayer."+id+".name", dplayer.player.getName());
if(dplayer.checkpoint!=null){
cFile.set("DPlayer."+id+".lastcheckpoint", dplayer.checkpoint.location);
}
cFile.set("DPlayer."+id+".class", dplayer.dclass.name);
cFile.set("DPlayer."+id+".isEditing", dplayer.isEditing);
cFile.set("DPlayer."+id+".isFinished", dplayer.isFinished);
cFile.set("DPlayer."+id+".isinTestMode", dplayer.isinTestMode);
cFile.set("DPlayer."+id+".isInDungeonChat", dplayer.isInDungeonChat);
cFile.set("DPlayer."+id+".isReady", dplayer.isReady);
cFile.set("DPlayer."+id+".offlineTime", dplayer.offlineTime);
cFile.set("DPlayer."+id+".oldArmor", dplayer.oldArmor);
cFile.set("DPlayer."+id+".oldExp", dplayer.oldExp);
cFile.set("DPlayer."+id+".oldFireTicks", dplayer.oldFireTicks);
cFile.set("DPlayer."+id+".oldFoodLevel", dplayer.oldFoodLevel);
cFile.set("DPlayer."+id+".oldGamemode", dplayer.oldGamemode);
cFile.set("DPlayer."+id+".oldHealth", dplayer.oldHealth);
cFile.set("DPlayer."+id+".oldInventory", dplayer.oldInventory);
cFile.set("DPlayer."+id+".oldLocation", dplayer.oldLocation);
cFile.set("DPlayer."+id+".oldLvl", dplayer.oldLvl);
cFile.set("DPlayer."+id+".respawnInventory", dplayer.respawnInventory);
cFile.set("DPlayer."+id+".treasureInv", dplayer.treasureInv);
cFile.set("DPlayer."+id+".wolfRespawnTime", dplayer.wolfRespawnTime);
id++;
}
}
public static void remove(DPlayer player){ public static void remove(DPlayer player){
players.remove(player); players.remove(player);
} }
public static DPlayer get(Player player){ public static DPlayer get(Player player){
EditWorld eworld=EditWorld.get(player.getWorld());
boolean isEditing=false;
if(eworld!=null){
isEditing=true;
}
for(DPlayer dplayer:players){ for(DPlayer dplayer:players){
if(dplayer.player.equals(player)){ if(dplayer.player.equals(player)){
if(dplayer.isEditing==isEditing){
return dplayer; return dplayer;
} }
} }
return null;
}
public static DPlayer get(String name) {
for(DPlayer dplayer:players){
if(dplayer.player.getName().equalsIgnoreCase(name)){
return dplayer;
}
} }
return null; return null;
} }
@ -508,45 +467,7 @@ public class DPlayer {
dplayer.wolfRespawnTime--; dplayer.wolfRespawnTime--;
} }
} }
//Update Offline Players
if(dplayer.offlineTime>0){
dplayer.offlineTime++;
if(dplayer.offlineTime>300){
DOfflinePlayer offplayer=new DOfflinePlayer();
offplayer.name=dplayer.player.getName();
offplayer.oldLocation=dplayer.oldLocation;
offplayer.oldInventory=dplayer.oldInventory;
offplayer.oldArmor=dplayer.oldArmor;
offplayer.oldExp=dplayer.oldExp;
offplayer.oldHealth=dplayer.oldHealth;
offplayer.oldFoodLevel=dplayer.oldFoodLevel;
offplayer.oldGamemode=dplayer.oldGamemode;
offplayer.oldLvl=dplayer.oldLvl;
remove(dplayer);
if(dplayer.isEditing){
EditWorld eworld=EditWorld.get(dplayer.world);
if(eworld!=null){
eworld.save();
}
}else{
GameWorld gworld=GameWorld.get(dplayer.world);
DGroup dgroup=DGroup.get(dplayer.player);
if(dgroup!=null){
dgroup.removePlayer(dplayer.player);
if(dgroup.isEmpty()){
dgroup.remove();
gworld.delete();
}
}
}
}
} }
} }
} }
} }
}

View File

@ -0,0 +1,154 @@
package com.dre.dungeonsxl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class DSavePlayer {
private static P p = P.p;
private static CopyOnWriteArrayList<DSavePlayer> savePlayers=new CopyOnWriteArrayList<DSavePlayer>();
//Variables
private String playerName;
private Location oldLocation;
private ItemStack[] oldInventory;
private ItemStack[] oldArmor;
private int oldLvl;
private int oldExp;
private int oldHealth;
private int oldFoodLevel;
private int oldFireTicks;
private GameMode oldGamemode;
public DSavePlayer(String playerName, Location oldLocation, ItemStack[] oldInventory, ItemStack[] oldArmor, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, GameMode oldGamemode){
savePlayers.add(this);
this.playerName = playerName;
this.oldLocation = oldLocation;
this.oldInventory = oldInventory;
this.oldArmor = oldArmor;
this.oldExp = oldExp;
this.oldHealth = oldHealth;
this.oldFoodLevel = oldFoodLevel;
this.oldGamemode = oldGamemode;
this.oldLvl = oldLvl;
this.oldFireTicks = oldFireTicks;
save();
}
public void reset(){
Player onlinePlayer = p.getServer().getPlayer(this.playerName);
if(onlinePlayer!=null){
/* Player is online */
onlinePlayer.teleport(this.oldLocation);
onlinePlayer.getInventory().setContents(this.oldInventory);
onlinePlayer.getInventory().setArmorContents(this.oldArmor);
onlinePlayer.setTotalExperience(this.oldExp);
onlinePlayer.setLevel(this.oldLvl);
onlinePlayer.setHealth(this.oldHealth);
onlinePlayer.setFoodLevel(this.oldFoodLevel);
onlinePlayer.setGameMode(this.oldGamemode);
onlinePlayer.setFireTicks(this.oldFireTicks);
} else {
/* Player is offline */
Player offlinePlayer = p.getOfflinePlayer(this.playerName, this.oldLocation);
if(offlinePlayer!=null){
offlinePlayer.getInventory().setContents(this.oldInventory);
offlinePlayer.getInventory().setArmorContents(this.oldArmor);
offlinePlayer.setTotalExperience(this.oldExp);
offlinePlayer.setLevel(this.oldLvl);
offlinePlayer.setHealth(this.oldHealth);
offlinePlayer.setFoodLevel(this.oldFoodLevel);
offlinePlayer.setGameMode(this.oldGamemode);
offlinePlayer.setFireTicks(this.oldFireTicks);
offlinePlayer.saveData();
}
}
savePlayers.remove(this);
save();
}
//Static
public static void save(){
FileConfiguration configFile = new YamlConfiguration();
for(DSavePlayer savePlayer: savePlayers){
configFile.set(savePlayer.playerName+".oldGamemode", savePlayer.oldGamemode.getValue());
configFile.set(savePlayer.playerName+".oldFireTicks", savePlayer.oldFireTicks);
configFile.set(savePlayer.playerName+".oldFoodLevel", savePlayer.oldFoodLevel);
configFile.set(savePlayer.playerName+".oldHealth", savePlayer.oldHealth);
configFile.set(savePlayer.playerName+".oldExp", savePlayer.oldExp);
configFile.set(savePlayer.playerName+".oldLvl", savePlayer.oldLvl);
configFile.set(savePlayer.playerName+".oldArmor", savePlayer.oldArmor);
configFile.set(savePlayer.playerName+".oldInventory", savePlayer.oldInventory);
configFile.set(savePlayer.playerName+".oldLocation.x", savePlayer.oldLocation.getX());
configFile.set(savePlayer.playerName+".oldLocation.y", savePlayer.oldLocation.getY());
configFile.set(savePlayer.playerName+".oldLocation.z", savePlayer.oldLocation.getZ());
configFile.set(savePlayer.playerName+".oldLocation.yaw", savePlayer.oldLocation.getYaw());
configFile.set(savePlayer.playerName+".oldLocation.pitch", savePlayer.oldLocation.getPitch());
configFile.set(savePlayer.playerName+".oldLocation.world", savePlayer.oldLocation.getWorld().getName());
}
try {
configFile.save(new File(p.getDataFolder(), "savePlayers.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void load(){
FileConfiguration configFile = YamlConfiguration.loadConfiguration(new File(p.getDataFolder(), "savePlayers.yml"));
for(String playerName : configFile.getKeys(false)){
//Load inventory data
ArrayList<ItemStack> oldInventoryList = (ArrayList<ItemStack>) configFile.get(playerName+".oldInventory");
ArrayList<ItemStack> oldArmorList = (ArrayList<ItemStack>) configFile.get(playerName+".oldArmor");
ItemStack[] oldInventory = oldInventoryList.toArray(new ItemStack[oldInventoryList.size()]);
ItemStack[] oldArmor = oldArmorList.toArray(new ItemStack[oldArmorList.size()]);
//Load other data
int oldLvl = configFile.getInt(playerName+".oldLvl");
int oldExp = configFile.getInt(playerName+".oldExp");
int oldHealth = configFile.getInt(playerName+".oldHealth");
int oldFoodLevel = configFile.getInt(playerName+".oldFoodLevel");
int oldFireTicks = configFile.getInt(playerName+".oldFireTicks");
GameMode oldGamemode = GameMode.getByValue(configFile.getInt(playerName+".oldGamemode"));
//Location
World world = p.getServer().getWorld(configFile.getString(playerName+".oldLocation.world"));
if(world==null){
world = p.getServer().getWorlds().get(0);
}
Location oldLocation = new Location(
world,
configFile.getDouble(playerName+".oldLocation.x"),
configFile.getDouble(playerName+".oldLocation.y"),
configFile.getDouble(playerName+".oldLocation.z"),
configFile.getInt(playerName+".oldLocation.yaw"),
configFile.getInt(playerName+".oldLocation.pitch"));
//Create Player
DSavePlayer savePlayer = new DSavePlayer(playerName, oldLocation, oldInventory, oldArmor, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGamemode);
savePlayer.reset();
}
}
}

View File

@ -97,9 +97,6 @@ public class P extends JavaPlugin{
//Load All //Load All
this.loadAll(); this.loadAll();
//Load MobTypes
DMobType.load(new File(p.getDataFolder(), "mobs.yml"));
//Spout //Spout
if(mainConfig.enableSpout){ if(mainConfig.enableSpout){
if(P.p.getServer().getPluginManager().getPlugin("Spout")!=null){ if(P.p.getServer().getPluginManager().getPlugin("Spout")!=null){
@ -278,12 +275,20 @@ public class P extends JavaPlugin{
public void loadAll(){ public void loadAll(){
//Load world data
File file = new File(this.getDataFolder(), "data.yml"); File file = new File(this.getDataFolder(), "data.yml");
FileConfiguration configFile = YamlConfiguration.loadConfiguration(file); FileConfiguration configFile = YamlConfiguration.loadConfiguration(file);
DPortal.load(configFile); DPortal.load(configFile);
DGSign.load(configFile); DGSign.load(configFile);
LeaveSign.load(configFile); LeaveSign.load(configFile);
//Load mob types
DMobType.load(new File(p.getDataFolder(), "mobs.yml"));
//Load saved players
DSavePlayer.load();
} }
@ -484,7 +489,8 @@ public class P extends JavaPlugin{
return pplayer; return pplayer;
} }
public boolean setOfflinePlayerPosition(String player, Location location){ public Player getOfflinePlayer(String player, Location location){
Player pplayer = null;
try { try {
//See if the player has data files //See if the player has data files
@ -503,15 +509,17 @@ public class P extends JavaPlugin{
entity.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); entity.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
entity.world = ((CraftWorld) location.getWorld()).getHandle(); entity.world = ((CraftWorld) location.getWorld()).getHandle();
Player target = (entity == null) ? null : (Player) entity.getBukkitEntity(); Player target = (entity == null) ? null : (Player) entity.getBukkitEntity();
target.saveData(); if(target != null) {
return true; //target.loadData();
return target;
}
} }
} }
} }
catch(Exception e) { catch(Exception e) {
return false; return null;
} }
return false; return pplayer;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -17,9 +17,9 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -205,10 +205,11 @@ public class PlayerListener implements Listener{
} }
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerRespawn(PlayerRespawnEvent event){ public void onPlayerRespawn(PlayerRespawnEvent event){
Player player=event.getPlayer(); Player player=event.getPlayer();
DPlayer dplayer=DPlayer.get(player); DPlayer dplayer=DPlayer.get(player);
if(dplayer!=null){ if(dplayer!=null){
if(dplayer.isEditing){ if(dplayer.isEditing){
EditWorld eworld=EditWorld.get(dplayer.world); EditWorld eworld=EditWorld.get(dplayer.world);
@ -221,8 +222,10 @@ public class PlayerListener implements Listener{
} }
}else{ }else{
GameWorld gworld=GameWorld.get(dplayer.world); GameWorld gworld=GameWorld.get(dplayer.world);
if(gworld!=null){ if(gworld!=null){
DGroup dgroup=DGroup.get(dplayer.player); DGroup dgroup=DGroup.get(dplayer.player);
if(dplayer.checkpoint==null){ if(dplayer.checkpoint==null){
event.setRespawnLocation(dgroup.gworld.locStart); event.setRespawnLocation(dgroup.gworld.locStart);
@ -242,10 +245,6 @@ public class PlayerListener implements Listener{
dplayer.wolf.teleport(dplayer.checkpoint.location); dplayer.wolf.teleport(dplayer.checkpoint.location);
} }
} }
} }
} }
} }
@ -288,12 +287,10 @@ public class PlayerListener implements Listener{
} }
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onPlayerQuit(PlayerQuitEvent event){ public void onPlayerJoin(PlayerJoinEvent event){
DPlayer dplayer=DPlayer.get(event.getPlayer()); DPlayer dplayer=DPlayer.get(event.getPlayer().getName());
if(dplayer!=null){ if(dplayer!=null){
//dplayer.goOffline(); dplayer.player = event.getPlayer();
dplayer.leave();
dplayer.player.kickPlayer("");
} }
} }
@ -329,7 +326,6 @@ public class PlayerListener implements Listener{
} }
} }
//Inventory Events //Inventory Events
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onInventoryOpen(InventoryOpenEvent event){ public void onInventoryOpen(InventoryOpenEvent event){