SHARE
TWEET

Player Saving

a guest Feb 23rd, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ruseps.world.entity.impl.player;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.util.logging.Level;
  8. import com.google.gson.Gson;
  9. import com.google.gson.GsonBuilder;
  10. import com.google.gson.JsonObject;
  11. import com.ruseps.GameServer;
  12. import com.ruseps.util.Misc;
  13.  
  14. public class PlayerSaving {
  15.    
  16.     public static void save(Player player) {
  17.         if(player.newPlayer())
  18.             return;
  19.         // Create the path and file objects.
  20.         Path path = Paths.get("./data/saves/characters/", player.getUsername() + ".json");
  21.         File file = path.toFile();
  22.         file.getParentFile().setWritable(true);
  23.  
  24.         // Attempt to make the player save directory if it doesn't
  25.         // exist.
  26.         if (!file.getParentFile().exists()) {
  27.             try {
  28.                 file.getParentFile().mkdirs();
  29.             } catch (SecurityException e) {
  30.                 System.out.println("Unable to create directory for player data!");
  31.             }
  32.         }
  33.         try (FileWriter writer = new FileWriter(file)) {
  34.  
  35.             Gson builder = new GsonBuilder().setPrettyPrinting().create();
  36.             JsonObject object = new JsonObject();
  37.             object.addProperty("total-play-time-ms", player.getTotalPlayTime());
  38.             object.addProperty("username", player.getUsername().trim());
  39.             object.addProperty("password", player.getPassword().trim());
  40.             object.addProperty("email", player.getEmailAddress() == null ? "null" : player.getEmailAddress().trim());
  41.             object.addProperty("staff-rights", player.getRights().name());
  42.             object.addProperty("game-mode", player.getGameMode().name());
  43.             object.addProperty("date-joined", player.getPointsHandler().getDateJoined());
  44.             object.addProperty("loyalty-title", player.getLoyaltyTitle().name());
  45.             object.add("position", builder.toJsonTree(player.getPosition()));
  46.             object.addProperty("online-status", player.getRelations().getStatus().name());
  47.             object.addProperty("pw-changed", new Boolean(player.isChangedPassword()));
  48.             object.addProperty("given-starter", new Boolean(player.didReceiveStarter()));
  49.             object.addProperty("money-pouch", new Long(player.getMoneyInPouch()));
  50.             object.addProperty("donated", new Long(player.getAmountDonated()));
  51.             object.addProperty("minutes-bonus-exp", new Integer(player.getMinutesBonusExp()));
  52.             object.addProperty("total-gained-exp", new Long(player.getSkillManager().getTotalGainedExp()));
  53.             object.addProperty("prestige-points", new Integer(player.getPointsHandler().getPrestigePoints()));
  54.             object.addProperty("achievement-points", new Integer(player.getPointsHandler().getAchievementPoints()));
  55.             object.addProperty("dung-tokens", new Integer(player.getPointsHandler().getDungeoneeringTokens()));
  56.             object.addProperty("commendations", new Integer(player.getPointsHandler().getCommendations()));
  57.             object.addProperty("loyalty-points", new Integer(player.getPointsHandler().getLoyaltyPoints()));
  58.             object.addProperty("total-loyalty-points", new Double(player.getAchievementAttributes().getTotalLoyaltyPointsEarned()));
  59.             object.addProperty("voting-points", new Integer(player.getPointsHandler().getVotingPoints()));
  60.             object.addProperty("slayer-points", new Integer(player.getPointsHandler().getSlayerPoints()));
  61.             object.addProperty("im-points", new Integer(player.getPointsHandler().getIronManPoints()));
  62.             object.addProperty("refeer-friend", new Integer(player.getPointsHandler().getRefeerFriendPoints()));
  63.             object.addProperty("refered-friend", new Boolean(player.isReferedAFriend()));
  64.             object.addProperty("received-br-reward", new Boolean(player.isReceivedBrReward()));
  65.             object.addProperty("pk-points", new Integer(player.getPointsHandler().getPkPoints()));
  66.             object.addProperty("trivia-points", new Integer(player.getPointsHandler().getTriviaPoints()));
  67.             object.addProperty("player-kills", new Integer(player.getPlayerKillingAttributes().getPlayerKills()));
  68.             object.addProperty("player-killstreak", new Integer(player.getPlayerKillingAttributes().getPlayerKillStreak()));
  69.             object.addProperty("player-deaths", new Integer(player.getPlayerKillingAttributes().getPlayerDeaths()));
  70.             object.addProperty("target-percentage", new Integer(player.getPlayerKillingAttributes().getTargetPercentage()));
  71.             object.addProperty("bh-rank", new Integer(player.getAppearance().getBountyHunterSkull()));
  72.             object.addProperty("gender", player.getAppearance().getGender().name());
  73.             object.addProperty("spell-book", player.getSpellbook().name());
  74.             object.addProperty("prayer-book", player.getPrayerbook().name());
  75.             object.addProperty("running", new Boolean(player.isRunning()));
  76.             object.addProperty("run-energy", new Integer(player.getRunEnergy()));
  77.             object.addProperty("music", new Boolean(player.musicActive()));
  78.             object.addProperty("sounds", new Boolean(player.soundsActive()));
  79.             object.addProperty("auto-retaliate", new Boolean(player.isAutoRetaliate()));
  80.             object.addProperty("xp-locked", new Boolean(player.experienceLocked()));
  81.             object.addProperty("veng-cast", new Boolean(player.hasVengeance()));
  82.             object.addProperty("last-veng", new Long(player.getLastVengeance().elapsed()));
  83.             object.addProperty("fight-type", player.getFightType().name());
  84.             object.addProperty("sol-effect", new Integer(player.getStaffOfLightEffect()));
  85.             object.addProperty("skull-timer", new Integer(player.getSkullTimer()));
  86.             object.addProperty("accept-aid", new Boolean(player.isAcceptAid()));
  87.             object.addProperty("poison-damage", new Integer(player.getPoisonDamage()));
  88.             object.addProperty("poison-immunity", new Integer(player.getPoisonImmunity()));
  89.             object.addProperty("overload-timer", new Integer(player.getOverloadPotionTimer()));
  90.             object.addProperty("fire-immunity", new Integer(player.getFireImmunity()));
  91.             object.addProperty("fire-damage-mod", new Integer(player.getFireDamageModifier()));
  92.             object.addProperty("prayer-renewal-timer", new Integer(player.getPrayerRenewalPotionTimer()));
  93.             object.addProperty("teleblock-timer", new Integer(player.getTeleblockTimer()));
  94.             object.addProperty("special-amount", new Integer(player.getSpecialPercentage()));
  95.             object.addProperty("entered-gwd-room", new Boolean(player.getMinigameAttributes().getGodwarsDungeonAttributes().hasEnteredRoom()));
  96.             object.addProperty("gwd-altar-delay", new Long(player.getMinigameAttributes().getGodwarsDungeonAttributes().getAltarDelay()));
  97.             object.add("gwd-killcount", builder.toJsonTree(player.getMinigameAttributes().getGodwarsDungeonAttributes().getKillcount()));
  98.             object.addProperty("effigy", new Integer(player.getEffigy()));
  99.             object.addProperty("summon-npc", new Integer(player.getSummoning().getFamiliar() != null ? player.getSummoning().getFamiliar().getSummonNpc().getId() : -1));
  100.             object.addProperty("summon-death", new Integer(player.getSummoning().getFamiliar() != null ? player.getSummoning().getFamiliar().getDeathTimer() : -1));
  101.             object.addProperty("process-farming", new Boolean(player.shouldProcessFarming()));
  102.             object.addProperty("clanchat", player.getClanChatName() == null ? "null" : player.getClanChatName().trim());
  103.             object.addProperty("autocast", new Boolean(player.isAutocast()));
  104.             object.addProperty("autocast-spell", player.getAutocastSpell() != null ? player.getAutocastSpell().spellId() : -1);
  105.             object.addProperty("dfs-charges", player.getDfsCharges());
  106.             object.addProperty("coins-gambled", new Integer(player.getAchievementAttributes().getCoinsGambled()));
  107.             object.addProperty("slayer-master", player.getSlayer().getSlayerMaster().name());
  108.             object.addProperty("slayer-task", player.getSlayer().getSlayerTask().name());
  109.             object.addProperty("prev-slayer-task", player.getSlayer().getLastTask().name());
  110.             object.addProperty("task-amount", player.getSlayer().getAmountToSlay());
  111.             object.addProperty("task-streak", player.getSlayer().getTaskStreak());
  112.             object.addProperty("duo-partner", player.getSlayer().getDuoPartner() == null ? "null" : player.getSlayer().getDuoPartner());
  113.             object.addProperty("double-slay-xp", player.getSlayer().doubleSlayerXP);
  114.             object.addProperty("recoil-deg", new Integer(player.getRecoilCharges()));
  115.             object.add("brawler-deg", builder.toJsonTree(player.getBrawlerChargers()));
  116.             object.add("killed-players", builder.toJsonTree(player.getPlayerKillingAttributes().getKilledPlayers()));
  117.             object.add("killed-gods", builder.toJsonTree(player.getAchievementAttributes().getGodsKilled()));
  118.             object.add("barrows-brother", builder.toJsonTree(player.getMinigameAttributes().getBarrowsMinigameAttributes().getBarrowsData()));
  119.             object.addProperty("random-coffin", new Integer(player.getMinigameAttributes().getBarrowsMinigameAttributes().getRandomCoffin()));
  120.             object.addProperty("barrows-killcount", new Integer(player.getMinigameAttributes().getBarrowsMinigameAttributes().getKillcount()));
  121.             object.add("nomad", builder.toJsonTree(player.getMinigameAttributes().getNomadAttributes().getQuestParts()));
  122.             object.add("recipe-for-disaster", builder.toJsonTree(player.getMinigameAttributes().getRecipeForDisasterAttributes().getQuestParts()));
  123.             object.addProperty("recipe-for-disaster-wave", new Integer(player.getMinigameAttributes().getRecipeForDisasterAttributes().getWavesCompleted()));
  124.             object.add("dung-items-bound", builder.toJsonTree(player.getMinigameAttributes().getDungeoneeringAttributes().getBoundItems()));
  125.             object.addProperty("rune-ess", new Integer(player.getStoredRuneEssence()));
  126.             object.addProperty("pure-ess", new Integer(player.getStoredPureEssence()));
  127.             object.addProperty("has-bank-pin", new Boolean(player.getBankPinAttributes().hasBankPin()));
  128.             object.addProperty("last-pin-attempt", new Long(player.getBankPinAttributes().getLastAttempt()));
  129.             object.addProperty("invalid-pin-attempts", new Integer(player.getBankPinAttributes().getInvalidAttempts()));
  130.             object.add("bank-pin", builder.toJsonTree(player.getBankPinAttributes().getBankPin()));
  131.             object.add("appearance", builder.toJsonTree(player.getAppearance().getLook()));
  132.             object.add("agility-obj", builder.toJsonTree(player.getCrossedObstacles()));
  133.             object.add("skills", builder.toJsonTree(player.getSkillManager().getSkills()));
  134.             object.add("inventory", builder.toJsonTree(player.getInventory().getItems()));
  135.             object.add("equipment", builder.toJsonTree(player.getEquipment().getItems()));         
  136.             object.add("bank-0", builder.toJsonTree(player.getBank(0).getValidItems()));
  137.             object.add("bank-1", builder.toJsonTree(player.getBank(1).getValidItems()));
  138.             object.add("bank-2", builder.toJsonTree(player.getBank(2).getValidItems()));
  139.             object.add("bank-3", builder.toJsonTree(player.getBank(3).getValidItems()));
  140.             object.add("bank-4", builder.toJsonTree(player.getBank(4).getValidItems()));
  141.             object.add("bank-5", builder.toJsonTree(player.getBank(5).getValidItems()));
  142.             object.add("bank-6", builder.toJsonTree(player.getBank(6).getValidItems()));
  143.             object.add("bank-7", builder.toJsonTree(player.getBank(7).getValidItems()));
  144.             object.add("bank-8", builder.toJsonTree(player.getBank(8).getValidItems()));
  145.            
  146.             object.add("ge-slots", builder.toJsonTree(player.getGrandExchangeSlots()));
  147.            
  148.             /** STORE SUMMON **/
  149.             if(player.getSummoning().getBeastOfBurden() != null) {
  150.                 object.add("store", builder.toJsonTree(player.getSummoning().getBeastOfBurden().getValidItems()));
  151.             }
  152.             object.add("charm-imp", builder.toJsonTree(player.getSummoning().getCharmImpConfigs()));
  153.  
  154.             object.add("friends", builder.toJsonTree(player.getRelations().getFriendList().toArray()));
  155.             object.add("ignores", builder.toJsonTree(player.getRelations().getIgnoreList().toArray()));
  156.             object.add("loyalty-titles", builder.toJsonTree(player.getUnlockedLoyaltyTitles()));
  157.             object.add("kills", builder.toJsonTree(player.getKillsTracker().toArray()));
  158.             object.add("drops", builder.toJsonTree(player.getDropLog().toArray()));
  159.             object.add("achievements-completion", builder.toJsonTree(player.getAchievementAttributes().getCompletion()));
  160.             object.add("achievements-progress", builder.toJsonTree(player.getAchievementAttributes().getProgress()));
  161.             writer.write(builder.toJson(object));
  162.             writer.close();
  163.         } catch (Exception e) {
  164.             // An error happened while saving.
  165.             GameServer.getLogger().log(Level.WARNING,
  166.                     "An error has occured while saving a character file!", e);
  167.         }
  168.     }
  169.  
  170.     public static boolean playerExists(String p) {
  171.         p = Misc.formatPlayerName(p.toLowerCase());
  172.         return new File("./data/saves/characters/"+p+".json").exists();
  173.     }
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top