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.Material;
import org.bukkit.World;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
@ -33,16 +32,8 @@ public class DPlayer {
public World world;
public boolean isinTestMode=false;
public Location oldLocation;
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 DSavePlayer savePlayer;
public boolean isEditing;
public boolean isInDungeonChat=false;
@ -61,19 +52,23 @@ public class DPlayer {
public DPlayer(Player player, World world, Location teleport, boolean isEditing){
players.add(this);
this.player=player;
this.world=world;
this.oldLocation=player.getLocation();
this.oldInventory=player.getInventory().getContents();
this.oldArmor=player.getInventory().getArmorContents();
this.oldExp=player.getTotalExperience();
this.oldHealth=player.getHealth();
this.oldFoodLevel=player.getFoodLevel();
this.oldGamemode=player.getGameMode();
this.oldLvl=player.getLevel();
this.oldFireTicks=player.getFireTicks();
this.savePlayer = new DSavePlayer(
player.getName(),
player.getLocation(),
player.getInventory().getContents(),
player.getInventory().getArmorContents(),
player.getLevel(),
player.getTotalExperience(),
player.getHealth(),
player.getFoodLevel(),
player.getFireTicks(),
player.getGameMode()
);
this.player.teleport(teleport);
this.player.getInventory().clear();
this.player.getInventory().setArmorContents(null);
@ -82,6 +77,7 @@ public class DPlayer {
this.player.setHealth(20);
this.player.setFoodLevel(20);
this.isEditing = isEditing;
if(isEditing) this.player.setGameMode(GameMode.CREATIVE); else this.player.setGameMode(GameMode.SURVIVAL);
if(!isEditing){
@ -112,15 +108,7 @@ public class DPlayer {
public void leave(){
remove(this);
this.player.teleport(this.oldLocation);
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);
this.savePlayer.reset();
if(this.isEditing){
EditWorld eworld=EditWorld.get(this.world);
@ -248,7 +236,7 @@ public class DPlayer {
}
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;
DGroup dgroup=DGroup.get(this.player);
@ -257,7 +245,7 @@ public class DPlayer {
for(Player player:dgroup.players){
DPlayer dplayer=get(player);
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;
}
}
@ -279,9 +267,8 @@ public class DPlayer {
p.msg(player, ChatColor.GREEN+"[Chatspy] "+ChatColor.WHITE+msg);
}
}
}else{
GameWorld gworld=GameWorld.get(this.world);
GameWorld gworld = GameWorld.get(this.world);
gworld.msg(msg);
for(Player player:p.chatSpyer){
if(!gworld.world.getPlayers().contains(player)){
@ -388,66 +375,38 @@ public class DPlayer {
new DLootInventory(this.player,this.treasureInv.getContents());
}
//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++;
}
}
//Static
public static void remove(DPlayer player){
players.remove(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){
if(dplayer.player.equals(player)){
if(dplayer.isEditing==isEditing){
return dplayer;
}
return dplayer;
}
}
return null;
}
public static CopyOnWriteArrayList<DPlayer> get(World world){
CopyOnWriteArrayList<DPlayer> dplayers=new CopyOnWriteArrayList<DPlayer>();
public static DPlayer get(String name) {
for(DPlayer dplayer:players){
if(dplayer.world==world){
if(dplayer.player.getName().equalsIgnoreCase(name)){
return dplayer;
}
}
return null;
}
public static CopyOnWriteArrayList<DPlayer> get(World world){
CopyOnWriteArrayList<DPlayer> dplayers = new CopyOnWriteArrayList<DPlayer>();
for(DPlayer dplayer : players){
if(dplayer.world == world){
dplayers.add(dplayer);
}
}
return dplayers;
}
@ -508,45 +467,7 @@ public class DPlayer {
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

@ -96,9 +96,6 @@ public class P extends JavaPlugin{
//Load All
this.loadAll();
//Load MobTypes
DMobType.load(new File(p.getDataFolder(), "mobs.yml"));
//Spout
if(mainConfig.enableSpout){
@ -278,12 +275,20 @@ public class P extends JavaPlugin{
public void loadAll(){
//Load world data
File file = new File(this.getDataFolder(), "data.yml");
FileConfiguration configFile = YamlConfiguration.loadConfiguration(file);
DPortal.load(configFile);
DGSign.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;
}
public boolean setOfflinePlayerPosition(String player, Location location){
public Player getOfflinePlayer(String player, Location location){
Player pplayer = null;
try {
//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.world = ((CraftWorld) location.getWorld()).getHandle();
Player target = (entity == null) ? null : (Player) entity.getBukkitEntity();
target.saveData();
return true;
if(target != null) {
//target.loadData();
return target;
}
}
}
}
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.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
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){
Player player=event.getPlayer();
DPlayer dplayer=DPlayer.get(player);
if(dplayer!=null){
if(dplayer.isEditing){
EditWorld eworld=EditWorld.get(dplayer.world);
@ -221,8 +222,10 @@ public class PlayerListener implements Listener{
}
}else{
GameWorld gworld=GameWorld.get(dplayer.world);
if(gworld!=null){
DGroup dgroup=DGroup.get(dplayer.player);
if(dplayer.checkpoint==null){
event.setRespawnLocation(dgroup.gworld.locStart);
@ -242,10 +245,6 @@ public class PlayerListener implements Listener{
dplayer.wolf.teleport(dplayer.checkpoint.location);
}
}
}
}
}
@ -286,14 +285,12 @@ public class PlayerListener implements Listener{
}
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerQuit(PlayerQuitEvent event){
DPlayer dplayer=DPlayer.get(event.getPlayer());
public void onPlayerJoin(PlayerJoinEvent event){
DPlayer dplayer=DPlayer.get(event.getPlayer().getName());
if(dplayer!=null){
//dplayer.goOffline();
dplayer.leave();
dplayer.player.kickPlayer("");
dplayer.player = event.getPlayer();
}
}
@ -329,7 +326,6 @@ public class PlayerListener implements Listener{
}
}
//Inventory Events
@EventHandler(priority = EventPriority.HIGH)
public void onInventoryOpen(InventoryOpenEvent event){
@ -358,7 +354,7 @@ public class PlayerListener implements Listener{
@EventHandler
public void onPlayerMove(PlayerMoveEvent event){
Player player=event.getPlayer();
DLootInventory inventory=DLootInventory.get(player);
DLootInventory inventory = DLootInventory.get(player);
if(inventory!=null){
if(player.getLocation().getBlock().getType()!=Material.PORTAL){