Rewrote player persistence system; close #117

This commit is contained in:
Daniel Saukel 2016-07-26 19:47:55 +02:00
parent b175b2b50b
commit 3366f6b883
19 changed files with 370 additions and 507 deletions

View File

@ -8,6 +8,6 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
</project>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
<build>
<resources>

View File

@ -39,7 +39,6 @@ import io.github.dre2n.dungeonsxl.player.DGamePlayer;
import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.player.DPlayers;
import io.github.dre2n.dungeonsxl.player.DSavePlayer;
import io.github.dre2n.dungeonsxl.requirement.RequirementTypes;
import io.github.dre2n.dungeonsxl.reward.DLootInventory;
import io.github.dre2n.dungeonsxl.reward.RewardTypes;
@ -280,14 +279,12 @@ public class DungeonsXL extends BRPlugin {
// Save and load
public void saveData() {
protections.saveAll();
DSavePlayer.save();
dWorlds.saveAll();
}
public void loadData() {
protections.loadAll();
dPlayers.loadAll();
DSavePlayer.load();
dWorlds.check();
}

View File

@ -23,7 +23,6 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;

View File

@ -140,6 +140,7 @@ public enum DMessages implements Messages {
LOG_ERROR_SIGN_SETUP("Log_Error_SignSetup", "&4A sign at &6&v1&4 is erroneous!"),
LOG_GENERATE_NEW_WORLD("Log_GenerateNewWorld", "&6Generating new world..."),
LOG_IMPORT_WORLD("Log_ImportWorld", "&6Importing world..."),
LOG_KILLED_CORRUPTED_PLAYER("Log_KilledCorruptedPlayer", "&4Killed player &6&v1 &4because the data to restore his main inventory is corrupted :("),
LOG_NEW_MAP("Log_NewDungeon", "&6Creating new map."),
LOG_NEW_PLAYER_DATA("Log_NewPlayerData", "&6A new player data file has been created and saved as &v1."),
LOG_WORLD_GENERATION_FINISHED("Log_WorldGenerationFinished", "&6World generation finished!"),

View File

@ -17,12 +17,22 @@
package io.github.dre2n.dungeonsxl.config;
import io.github.dre2n.commons.config.BRConfig;
import io.github.dre2n.commons.util.EnumUtil;
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
/**
* @author Daniel Saukel
@ -31,8 +41,25 @@ public class PlayerData extends BRConfig {
DungeonsXL plugin = DungeonsXL.getInstance();
public static final int CONFIG_VERSION = 1;
public static final int CONFIG_VERSION = 2;
public static final String PREFIX_STATE_PERSISTENCE = "savePlayer.";
public static final String PREFIX_STATS = "stats.";
// State persistence
private Location oldLocation;
private List<ItemStack> oldInventory;
private List<ItemStack> oldArmor;
private ItemStack oldOffHand;
private int oldLvl;
private float oldExp;
private double oldHealth;
private int oldFoodLevel;
private int oldFireTicks;
private GameMode oldGameMode;
private Collection<PotionEffect> oldPotionEffects;
// Stats
private Map<String, Long> timeLastPlayed = new HashMap<>();
public PlayerData(File file) {
@ -44,6 +71,179 @@ public class PlayerData extends BRConfig {
load();
}
/* Getters and setters */
/**
* @return if the player was in a game when he left the game
*/
public boolean wasInGame() {
return config.contains(PREFIX_STATE_PERSISTENCE);
}
/**
* @return the old location
*/
public Location getOldLocation() {
return oldLocation;
}
/**
* @param location
* the location to set
*/
public void setOldLocation(Location location) {
oldLocation = location;
}
/**
* @return the items in the old inventory
*/
public List<ItemStack> getOldInventory() {
return oldInventory;
}
/**
* @param inventory
* the inventory to set
*/
public void setOldInventory(List<ItemStack> inventory) {
oldInventory = inventory;
}
/**
* @return the items in the old armor slots
*/
public List<ItemStack> getOldArmor() {
return oldArmor;
}
/**
* @param inventory
* the inventory to set
*/
public void setOldArmor(List<ItemStack> inventory) {
oldArmor = inventory;
}
/**
* @return the items in the old off-hand slot
*/
public ItemStack getOldOffHand() {
return oldOffHand;
}
/**
* @param offHand
* the off hand item to set
*/
public void setOldOffHand(ItemStack offHand) {
oldOffHand = offHand;
}
/**
* @return the old level
*/
public int getOldLevel() {
return oldLvl;
}
/**
* @param level
* the level to set
*/
public void setOldLevel(int level) {
oldLvl = level;
}
/**
* @return the old exp
*/
public float getOldExp() {
return oldExp;
}
/**
* @param exp
* the amount of exp to set
*/
public void setOldExp(float exp) {
oldExp = exp;
}
/**
* @return the old health
*/
public double getOldHealth() {
return oldHealth;
}
/**
* @param health
* the health to set
*/
public void setOldHealth(double health) {
oldHealth = health;
}
/**
* @return the old food level
*/
public int getOldFoodLevel() {
return oldFoodLevel;
}
/**
* @param foodLevel
* the food level to set
*/
public void setOldFoodLevel(int foodLevel) {
oldFoodLevel = foodLevel;
}
/**
* @return the old fire ticks
*/
public int getOldFireTicks() {
return oldFireTicks;
}
/**
* @param fireTicks
* the fire ticks to set
*/
public void setFireTicks(int fireTicks) {
oldFireTicks = fireTicks;
}
/**
* @return the old GameMode
*/
public GameMode getOldGameMode() {
return oldGameMode;
}
/**
* @param gameMode
* the GameMode to set
*/
public void setOldGameMode(GameMode gameMode) {
oldGameMode = gameMode;
}
/**
* @return the old potion effects
*/
public Collection<PotionEffect> getOldPotionEffects() {
return oldPotionEffects;
}
/**
* @param potionEffects
* the potion effects to set
*/
public void setOldPotionEffects(Collection<PotionEffect> potionEffects) {
oldPotionEffects = potionEffects;
}
/**
* @return a map of the player's finished dungeons with dates.
*/
@ -76,6 +276,7 @@ public class PlayerData extends BRConfig {
save();
}
/* Actions */
/**
* @param dungeon
* the finished dungeon
@ -87,8 +288,8 @@ public class PlayerData extends BRConfig {
@Override
public void initialize() {
if (!config.contains("timeLastPlayed")) {
config.createSection("timeLastPlayed");
if (!config.contains(PREFIX_STATS + "timeLastPlayed")) {
config.createSection(PREFIX_STATS + "timeLastPlayed");
}
if (!file.exists()) {
@ -104,17 +305,99 @@ public class PlayerData extends BRConfig {
@Override
public void load() {
if (config.isConfigurationSection("timeLastPlayed")) {
for (String key : config.getConfigurationSection("timeLastPlayed").getKeys(false)) {
timeLastPlayed.put(key, config.getLong("timeLastPlayed." + key));
if (config.isConfigurationSection(PREFIX_STATS + "timeLastPlayed")) {
for (String key : config.getConfigurationSection(PREFIX_STATS + "timeLastPlayed").getKeys(false)) {
timeLastPlayed.put(key, config.getLong(PREFIX_STATS + "timeLastPlayed." + key));
}
}
if (!wasInGame()) {
return;
}
oldInventory = (List<ItemStack>) config.get(PREFIX_STATE_PERSISTENCE + "oldInventory");
oldArmor = (List<ItemStack>) config.get(PREFIX_STATE_PERSISTENCE + "oldArmor");
oldOffHand = (ItemStack) config.get(PREFIX_STATE_PERSISTENCE + "oldOffHand");
oldLvl = config.getInt(PREFIX_STATE_PERSISTENCE + "oldLvl");
oldExp = config.getInt(PREFIX_STATE_PERSISTENCE + "oldExp");
oldHealth = config.getInt(PREFIX_STATE_PERSISTENCE + "oldHealth");
oldFoodLevel = config.getInt(PREFIX_STATE_PERSISTENCE + "oldFoodLevel");
oldFireTicks = config.getInt(PREFIX_STATE_PERSISTENCE + "oldFireTicks");
if (EnumUtil.isValidEnum(GameMode.class, config.getString(PREFIX_STATE_PERSISTENCE + "oldGameMode"))) {
oldGameMode = GameMode.valueOf(config.getString(PREFIX_STATE_PERSISTENCE + "oldGameMode"));
} else {
oldGameMode = GameMode.SURVIVAL;
}
oldPotionEffects = (Collection<PotionEffect>) config.get(PREFIX_STATE_PERSISTENCE + "oldPotionEffects");
oldLocation = (Location) config.get(PREFIX_STATE_PERSISTENCE + "oldLocation");
if (oldLocation.getWorld() == null) {
oldLocation = plugin.getServer().getWorlds().get(0).getSpawnLocation();
}
}
@Override
public void save() {
config.set("timeLastPlayed", timeLastPlayed);
config.set(PREFIX_STATS + "timeLastPlayed", timeLastPlayed);
super.save();
}
/**
* Saves the player's data to the file.
*
* @param player
* the Player to save
*/
public void savePlayerState(Player player) {
oldGameMode = player.getGameMode();
oldFireTicks = player.getFireTicks();
oldFoodLevel = player.getFoodLevel();
oldHealth = player.getHealth();
oldExp = player.getExp();
oldLvl = player.getLevel();
oldArmor = new ArrayList<>(Arrays.asList(player.getInventory().getArmorContents()));
oldInventory = new ArrayList<>(Arrays.asList(player.getInventory().getContents()));
oldOffHand = player.getInventory().getItemInOffHand();
oldLocation = player.getLocation();
oldPotionEffects = player.getActivePotionEffects();
config.set(PREFIX_STATE_PERSISTENCE + "oldGameMode", oldGameMode.toString());
config.set(PREFIX_STATE_PERSISTENCE + "oldFireTicks", oldFireTicks);
config.set(PREFIX_STATE_PERSISTENCE + "oldFoodLevel", oldFoodLevel);
config.set(PREFIX_STATE_PERSISTENCE + "oldHealth", oldHealth);
config.set(PREFIX_STATE_PERSISTENCE + "oldExp", oldExp);
config.set(PREFIX_STATE_PERSISTENCE + "oldLvl", oldLvl);
config.set(PREFIX_STATE_PERSISTENCE + "oldArmor", oldArmor);
config.set(PREFIX_STATE_PERSISTENCE + "oldInventory", oldInventory);
config.set(PREFIX_STATE_PERSISTENCE + "oldOffHand", oldOffHand);
config.set(PREFIX_STATE_PERSISTENCE + "oldLocation", oldLocation);
config.set(PREFIX_STATE_PERSISTENCE + "oldPotionEffects", oldPotionEffects);
save();
}
/**
* Removes the state data from the file
*/
public void clearPlayerState() {
oldGameMode = null;
oldFireTicks = 0;
oldFoodLevel = 0;
oldHealth = 0;
oldExp = 0;
oldLvl = 0;
oldArmor = null;
oldInventory = null;
oldOffHand = null;
oldLocation = null;
oldPotionEffects = null;
if (wasInGame()) {
config.set("savePlayer", null);
}
save();
}
}

View File

@ -35,7 +35,6 @@ import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.player.DInstancePlayer;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.player.DPlayers;
import io.github.dre2n.dungeonsxl.player.DSavePlayer;
import io.github.dre2n.dungeonsxl.reward.DLootInventory;
import io.github.dre2n.dungeonsxl.reward.RewardChest;
import io.github.dre2n.dungeonsxl.sign.OpenDoorSign;
@ -45,7 +44,6 @@ import io.github.dre2n.dungeonsxl.trigger.UseItemTrigger;
import io.github.dre2n.dungeonsxl.world.DEditWorld;
import io.github.dre2n.dungeonsxl.world.DGameWorld;
import java.util.ArrayList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
@ -486,25 +484,13 @@ public class PlayerListener implements Listener {
if (dPlayer != null) {
DGroup dGroup = DGroup.getByPlayer(dPlayer.getPlayer());
if (dGroup != null) {
dGroup.getPlayers().remove(dPlayer.getPlayer());
dGroup.getPlayers().add(player);
dGroup.removePlayer(dPlayer.getPlayer());
dGroup.addPlayer(player);
}
dPlayer.setPlayer(player);
// Check offlineTime
dPlayer.setOfflineTime(0);
} else {
DSavePlayer dSavePlayer = dPlayers.getDSavePlayerByPlayer(player);
Location target = Bukkit.getServer().getWorlds().get(0).getSpawnLocation();
if (dSavePlayer != null) {
target = dSavePlayer.getOldLocation();
}
if (DEditWorld.getByWorld(player.getWorld()) != null || DGameWorld.getByWorld(player.getWorld()) != null) {
player.teleport(target);
}
}
// Tutorial Mode
@ -547,7 +533,7 @@ public class PlayerListener implements Listener {
}
if (dGroup.getGameWorld() == null) {
dGroup.setGameWorld(plugin.getDWorlds().getResourceByName(DGroup.getByPlayer(player).getMapName()).instantiateAsGameWorld());// TO DO
dGroup.setGameWorld(plugin.getDWorlds().getResourceByName(DGroup.getByPlayer(player).getMapName()).instantiateAsGameWorld());
dGroup.getGameWorld().setTutorial(true);
}

View File

@ -106,7 +106,7 @@ public class DEditPlayer extends DInstancePlayer {
*/
public void escape() {
delete();
getSavePlayer().reset(false);
reset(false);
}
public void poke(Block block) {
@ -142,7 +142,7 @@ public class DEditPlayer extends DInstancePlayer {
public void leave() {
delete();
getSavePlayer().reset(false);
reset(false);
DEditWorld editWorld = DEditWorld.getByWorld(getWorld());
if (editWorld != null) {

View File

@ -381,9 +381,9 @@ public class DGamePlayer extends DInstancePlayer {
delete();
if (finished) {
getSavePlayer().reset(rules.getKeepInventoryOnFinish());
reset(rules.getKeepInventoryOnFinish());
} else {
getSavePlayer().reset(rules.getKeepInventoryOnEscape());
reset(rules.getKeepInventoryOnEscape());
}
// Permission bridge

View File

@ -16,12 +16,19 @@
*/
package io.github.dre2n.dungeonsxl.player;
import io.github.dre2n.commons.compatibility.CompatibilityHandler;
import io.github.dre2n.commons.compatibility.Version;
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.commons.util.playerutil.PlayerUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.PlayerData;
import io.github.dre2n.dungeonsxl.global.DPortal;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
/**
* Represents a player in the non-DXL worlds of the server.
@ -45,8 +52,16 @@ public class DGlobalPlayer {
private ItemStack[] respawnArmor;
public DGlobalPlayer(Player player) {
this(player, false);
}
public DGlobalPlayer(Player player, boolean reset) {
this.player = player;
loadPlayerData(new File(DungeonsXL.PLAYERS, player.getUniqueId().toString() + ".yml"));
if (reset && data.wasInGame()) {
reset(false);
}
plugin.getDPlayers().addPlayer(this);
}
@ -63,6 +78,7 @@ public class DGlobalPlayer {
plugin.getDPlayers().addPlayer(this);
}
/* Getters and setters */
/**
* @return the Bukkit player
*/
@ -213,4 +229,47 @@ public class DGlobalPlayer {
return DPermissions.hasPermission(player, permission);
}
/* Actions */
/**
* Respawns the player at his old position before he was in a dungeon
*/
public void reset(boolean keepInventory) {
try {
if (!keepInventory) {
while (data.getOldInventory().size() > 36) {
data.getOldInventory().remove(36);
}
player.getInventory().setContents(data.getOldInventory().toArray(new ItemStack[36]));
player.getInventory().setArmorContents(data.getOldArmor().toArray(new ItemStack[4]));
if (Version.andHigher(Version.MC1_9).contains(CompatibilityHandler.getInstance().getVersion())) {
player.getInventory().setItemInOffHand(data.getOldOffHand());
}
player.setLevel(data.getOldLevel());
player.setExp(data.getOldExp());
player.setHealth(data.getOldHealth());
player.setFoodLevel(data.getOldFoodLevel());
player.setGameMode(data.getOldGameMode());
player.setFireTicks(data.getOldFireTicks());
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
player.addPotionEffects(data.getOldPotionEffects());
}
if (data.getOldLocation().getWorld() != null) {
PlayerUtil.secureTeleport(player, data.getOldLocation());
} else {
PlayerUtil.secureTeleport(player, Bukkit.getWorlds().get(0).getSpawnLocation());
}
} catch (NullPointerException exception) {
exception.printStackTrace();
player.setHealth(0);
MessageUtil.log(plugin, DMessages.LOG_KILLED_CORRUPTED_PLAYER.getMessage(player.getName()));
}
data.clearPlayerState();
}
}

View File

@ -16,8 +16,6 @@
*/
package io.github.dre2n.dungeonsxl.player;
import io.github.dre2n.commons.compatibility.CompatibilityHandler;
import io.github.dre2n.commons.compatibility.Version;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
@ -27,41 +25,16 @@ import org.bukkit.potion.PotionEffect;
*/
public abstract class DInstancePlayer extends DGlobalPlayer {
private DSavePlayer savePlayer;
private World world;
private boolean inDungeonChat = false;
DInstancePlayer(Player player, World world) {
super(player);
double health = player.getHealth();
if (!Version.andHigher(Version.MC1_9).contains(CompatibilityHandler.getInstance().getVersion())) {
savePlayer = new DSavePlayer(player.getName(), player.getUniqueId(), player.getLocation(), player.getInventory().getContents(), player.getInventory().getArmorContents(), null, player.getLevel(),
player.getTotalExperience(), (int) health, player.getFoodLevel(), player.getFireTicks(), player.getGameMode(), player.getActivePotionEffects());
} else {
savePlayer = new DSavePlayer(player.getName(), player.getUniqueId(), player.getLocation(), player.getInventory().getContents(), player.getInventory().getArmorContents(), player.getInventory().getItemInOffHand(), player.getLevel(),
player.getTotalExperience(), (int) health, player.getFoodLevel(), player.getFireTicks(), player.getGameMode(), player.getActivePotionEffects());
}
super(player, false);
this.world = world;
getData().savePlayerState(player);
}
/* Getters and setters */
/**
* @return the savePlayer
*/
public DSavePlayer getSavePlayer() {
return savePlayer;
}
/**
* @param savePlayer
* the savePlayer to set
*/
public void setSavePlayer(DSavePlayer savePlayer) {
this.savePlayer = savePlayer;
}
/**
* @return
* the instance

View File

@ -28,7 +28,6 @@ import org.bukkit.entity.Player;
public class DPlayers {
private CopyOnWriteArrayList<DGlobalPlayer> dGlobalPlayers = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<DSavePlayer> dSavePlayers = new CopyOnWriteArrayList<>();
/**
* @return the DGlobalPlayer which represents the player
@ -111,42 +110,6 @@ public class DPlayers {
dGlobalPlayers.remove(player);
}
/**
* @return the DSavePlayer that represents the player
*/
public DSavePlayer getDSavePlayerByPlayer(Player player) {
for (DSavePlayer dSavePlayer : dSavePlayers) {
if (dSavePlayer.getName().equals(player.getName())) {
return dSavePlayer;
}
}
return null;
}
/**
* @return the dSavePlayers
*/
public List<DSavePlayer> getDSavePlayers() {
return dSavePlayers;
}
/**
* @param dSavePlayer
* the dSavePlayer to add
*/
public void addDSavePlayer(DSavePlayer dSavePlayer) {
dSavePlayers.add(dSavePlayer);
}
/**
* @param dSavePlayer
* the dSavePlayer to remove
*/
public void removeDSavePlayer(DSavePlayer dSavePlayer) {
dSavePlayers.remove(dSavePlayer);
}
/**
* Load all players
*/

View File

@ -1,399 +0,0 @@
/*
* Copyright (C) 2012-2016 Frank Baumann
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.player;
import io.github.dre2n.commons.compatibility.CompatibilityHandler;
import io.github.dre2n.commons.compatibility.Version;
import io.github.dre2n.commons.util.EnumUtil;
import io.github.dre2n.commons.util.playerutil.PlayerUtil;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
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;
import org.bukkit.potion.PotionEffect;
/**
* Represents a player in a GameWorld who went offline.
*
* @author Frank Baumann, Tobias Schmitz, Milan Albrecht, Daniel Saukel
*/
public class DSavePlayer {
static DungeonsXL plugin = DungeonsXL.getInstance();
static DPlayers dPlayers = plugin.getDPlayers();
// Variables
private String name;
private String uuid;
private Location oldLocation;
private List<ItemStack> oldInventory;
private List<ItemStack> oldArmor;
private ItemStack oldOffHand;
private int oldLvl;
private int oldExp;
private double oldHealth;
private int oldFoodLevel;
private int oldFireTicks;
private GameMode oldGameMode;
private Collection<PotionEffect> oldPotionEffects;
public DSavePlayer(String name, UUID uuid, Location oldLocation, ArrayList<ItemStack> oldInventory, ArrayList<ItemStack> oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, double oldHealth, int oldFoodLevel, int oldFireTicks,
GameMode oldGameMode, Collection<PotionEffect> oldPotionEffects) {
this.name = name;
this.uuid = uuid.toString();
this.oldLocation = oldLocation;
this.oldInventory = oldInventory;
this.oldArmor = oldArmor;
this.oldOffHand = oldOffHand;
this.oldExp = oldExp;
this.oldHealth = oldHealth;
this.oldFoodLevel = oldFoodLevel;
this.oldGameMode = oldGameMode;
this.oldLvl = oldLvl;
this.oldFireTicks = oldFireTicks;
this.oldPotionEffects = oldPotionEffects;
save();
dPlayers.addDSavePlayer(this);
}
public DSavePlayer(String name, UUID uuid, Location oldLocation, ItemStack[] oldInventory, ItemStack[] oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, double oldHealth, int oldFoodLevel, int oldFireTicks,
GameMode oldGameMode, Collection<PotionEffect> oldPotionEffects) {
this(name, uuid, oldLocation, new ArrayList<>(Arrays.asList(oldInventory)), new ArrayList<>(Arrays.asList(oldArmor)), oldOffHand, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGameMode, oldPotionEffects);
}
/* Getters and setters */
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the uuid
*/
public UUID getUniqueId() {
return UUID.fromString(uuid);
}
/**
* @return the old location
*/
public Location getOldLocation() {
return oldLocation;
}
/**
* @param location
* the location to set
*/
public void setOldLocation(Location location) {
oldLocation = location;
}
/**
* @return the items in the old inventory
*/
public List<ItemStack> getOldInventory() {
return oldInventory;
}
/**
* @param inventory
* the inventory to set
*/
public void setOldInventory(List<ItemStack> inventory) {
oldInventory = inventory;
}
/**
* @return the items in the old armor slots
*/
public List<ItemStack> getOldArmor() {
return oldArmor;
}
/**
* @param inventory
* the inventory to set
*/
public void setOldArmor(List<ItemStack> inventory) {
oldArmor = inventory;
}
/**
* @return the items in the old off-hand slot
*/
public ItemStack getOldOffHand() {
return oldOffHand;
}
/**
* @param offHand
* the off hand item to set
*/
public void setOldOffHand(ItemStack offHand) {
oldOffHand = offHand;
}
/**
* @return the old level
*/
public int getOldLevel() {
return oldLvl;
}
/**
* @param level
* the level to set
*/
public void setOldLevel(int level) {
oldLvl = level;
}
/**
* @return the old exp
*/
public int getOldExp() {
return oldExp;
}
/**
* @param exp
* the amount of exp to set
*/
public void setOldExp(int exp) {
oldExp = exp;
}
/**
* @return the old health
*/
public double getOldHealth() {
return oldHealth;
}
/**
* @param health
* the health to set
*/
public void setOldHealth(double health) {
oldHealth = health;
}
/**
* @return the old food level
*/
public int getOldFoodLevel() {
return oldFoodLevel;
}
/**
* @param foodLevel
* the food level to set
*/
public void setOldFoodLevel(int foodLevel) {
oldFoodLevel = foodLevel;
}
/**
* @return the old fire ticks
*/
public int getOldFireTicks() {
return oldFireTicks;
}
/**
* @param fireTicks
* the fire ticks to set
*/
public void setFireTicks(int fireTicks) {
oldFireTicks = fireTicks;
}
/**
* @return the old GameMode
*/
public GameMode getOldGameMode() {
return oldGameMode;
}
/**
* @param gameMode
* the GameMode to set
*/
public void setOldGameMode(GameMode gameMode) {
oldGameMode = gameMode;
}
/**
* @return the old potion effects
*/
public Collection<PotionEffect> getOldPotionEffects() {
return oldPotionEffects;
}
/**
* @param potionEffects
* the potion effects to set
*/
public void setOldPotionEffects(Collection<PotionEffect> potionEffects) {
oldPotionEffects = potionEffects;
}
/* Actions */
public void reset(boolean keepInventory) {
Player player = plugin.getServer().getPlayer(name);
boolean offline = false;
if (player == null) {
player = PlayerUtil.getOfflinePlayer(name, UUID.fromString(uuid), oldLocation);
offline = true;
}
if (player == null) {
return;
}
try {
if (!keepInventory) {
while (oldInventory.size() > 36) {
oldInventory.remove(36);
}
player.getInventory().setContents(oldInventory.toArray(new ItemStack[36]));
player.getInventory().setArmorContents(oldArmor.toArray(new ItemStack[4]));
if (Version.andHigher(Version.MC1_9).contains(CompatibilityHandler.getInstance().getVersion())) {
player.getInventory().setItemInOffHand(oldOffHand);
}
player.setTotalExperience(oldExp);
player.setLevel(oldLvl);
player.setHealth(oldHealth);
player.setFoodLevel(oldFoodLevel);
player.setGameMode(oldGameMode);
player.setFireTicks(oldFireTicks);
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
// Causes NPE if offline
if (!offline) {
player.addPotionEffects(oldPotionEffects);
} else {
player.saveData();
}
}
if (!offline && oldLocation.getWorld() != null) {
PlayerUtil.secureTeleport(player, oldLocation);
} else {
PlayerUtil.secureTeleport(player, Bukkit.getWorlds().get(0).getSpawnLocation());
}
} catch (NullPointerException exception) {
plugin.getLogger().info("Corrupted playerdata detected and removed!");
}
save();
dPlayers.removeDSavePlayer(this);
}
/* Statics */
@Deprecated
public static void save() {
FileConfiguration configFile = new YamlConfiguration();
for (DSavePlayer savePlayer : dPlayers.getDSavePlayers()) {
configFile.set(savePlayer.name + ".uuid", savePlayer.uuid);
configFile.set(savePlayer.name + ".oldGameMode", savePlayer.oldGameMode.toString());
configFile.set(savePlayer.name + ".oldFireTicks", savePlayer.oldFireTicks);
configFile.set(savePlayer.name + ".oldFoodLevel", savePlayer.oldFoodLevel);
configFile.set(savePlayer.name + ".oldHealth", savePlayer.oldHealth);
configFile.set(savePlayer.name + ".oldExp", savePlayer.oldExp);
configFile.set(savePlayer.name + ".oldLvl", savePlayer.oldLvl);
configFile.set(savePlayer.name + ".oldArmor", savePlayer.oldArmor);
configFile.set(savePlayer.name + ".oldInventory", savePlayer.oldInventory);
configFile.set(savePlayer.name + ".oldOffHand", savePlayer.oldOffHand);
configFile.set(savePlayer.name + ".oldLocation.x", savePlayer.oldLocation.getX());
configFile.set(savePlayer.name + ".oldLocation.y", savePlayer.oldLocation.getY());
configFile.set(savePlayer.name + ".oldLocation.z", savePlayer.oldLocation.getZ());
configFile.set(savePlayer.name + ".oldLocation.yaw", savePlayer.oldLocation.getYaw());
configFile.set(savePlayer.name + ".oldLocation.pitch", savePlayer.oldLocation.getPitch());
configFile.set(savePlayer.name + ".oldLocation.world", savePlayer.oldLocation.getWorld().getName());
configFile.set(savePlayer.name + ".oldPotionEffects", savePlayer.oldPotionEffects);
}
try {
configFile.save(new File(plugin.getDataFolder(), "savePlayers.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Deprecated
public static void load() {
FileConfiguration configFile = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "savePlayers.yml"));
for (String name : configFile.getKeys(false)) {
// Load uuid
UUID uuid = UUID.fromString(configFile.getString(name + ".uuid"));
// Load inventory data
ArrayList<ItemStack> oldInventory = (ArrayList<ItemStack>) configFile.get(name + ".oldInventory");
ArrayList<ItemStack> oldArmor = (ArrayList<ItemStack>) configFile.get(name + ".oldArmor");
ItemStack oldOffHand = (ItemStack) configFile.get(name + ".oldOffHand");
// Load other data
int oldLvl = configFile.getInt(name + ".oldLvl");
int oldExp = configFile.getInt(name + ".oldExp");
int oldHealth = configFile.getInt(name + ".oldHealth");
int oldFoodLevel = configFile.getInt(name + ".oldFoodLevel");
int oldFireTicks = configFile.getInt(name + ".oldFireTicks");
GameMode oldGameMode = GameMode.SURVIVAL;
if (EnumUtil.isValidEnum(GameMode.class, configFile.getString(name + ".oldGameMode"))) {
oldGameMode = GameMode.valueOf(configFile.getString(name + ".oldGameMode"));
}
Collection<PotionEffect> oldPotionEffects = (Collection<PotionEffect>) configFile.get(name + ".oldPotionEffects");
// Location
World world = plugin.getServer().getWorld(configFile.getString(name + ".oldLocation.world"));
if (world == null) {
world = plugin.getServer().getWorlds().get(0);
}
Location oldLocation = new Location(world, configFile.getDouble(name + ".oldLocation.x"), configFile.getDouble(name + ".oldLocation.y"), configFile.getDouble(name
+ ".oldLocation.z"), configFile.getInt(name + ".oldLocation.yaw"), configFile.getInt(name + ".oldLocation.pitch"));
// Create Player
DSavePlayer savePlayer = new DSavePlayer(name, uuid, oldLocation, oldInventory, oldArmor, oldOffHand, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGameMode, oldPotionEffects);
savePlayer.reset(false);
}
}
}

View File

@ -18,8 +18,8 @@ package io.github.dre2n.dungeonsxl.requirement;
import io.github.dre2n.commons.util.messageutil.MessageUtil;
import io.github.dre2n.dungeonsxl.config.DMessages;
import io.github.dre2n.dungeonsxl.config.PlayerData;
import io.github.dre2n.dungeonsxl.player.DGamePlayer;
import io.github.dre2n.dungeonsxl.player.DSavePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@ -70,8 +70,9 @@ public class FeeLevelRequirement extends Requirement {
if (dPlayer == null) {
return;
}
DSavePlayer dSavePlayer = dPlayer.getSavePlayer();
dSavePlayer.setOldLevel(dSavePlayer.getOldLevel() - fee);
PlayerData data = dPlayer.getData();
data.setOldLevel(data.getOldLevel() - fee);
MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(DMessages.REQUIREMENT_FEE, fee + " levels"));
}

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
<build>
<plugins>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
<build>
<plugins>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
<build>
<plugins>

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
<packaging>pom</packaging>
<name>DungeonsXL</name>
<url>https://dre2n.github.io</url>

View File

@ -8,7 +8,7 @@
<parent>
<groupId>io.github.dre2n</groupId>
<artifactId>dungeonsxl</artifactId>
<version>0.14.4</version>
<version>0.15-SNAPSHOT</version>
</parent>
<build>
<finalName>dungeonsxl-${project.version}${buildNo}</finalName>