From a3ee3c698c44865fde984ca5ea20479f33c143ce Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Tue, 22 Mar 2016 12:17:12 +0100 Subject: [PATCH] Fix 1.9 inventories (needs BRCommons update for version check) --- .../dre2n/dungeonsxl/player/DPlayer.java | 81 ++++++++++--------- .../dre2n/dungeonsxl/player/DSavePlayer.java | 72 ++++++++++++----- 2 files changed, 98 insertions(+), 55 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java index 7461932e..21df4d28 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DPlayer.java @@ -16,6 +16,8 @@ */ 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.NumberUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.commons.util.playerutil.PlayerUtil; @@ -99,9 +101,14 @@ public class DPlayer { double health = ((Damageable) player).getHealth(); - savePlayer = new DSavePlayer(player.getName(), player.getUniqueId(), player.getLocation(), player.getInventory().getContents(), player.getInventory().getArmorContents(), player.getLevel(), - player.getTotalExperience(), (int) health, player.getFoodLevel(), player.getFireTicks(), player.getGameMode(), player.getActivePotionEffects()); + if (CompatibilityHandler.getInstance().getVersion() != Version.MC1_9) { + 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()); + } this.editing = editing; if (this.editing) { @@ -531,48 +538,50 @@ public class DPlayer { } // Belohnung - if (gameWorld.getGame().getType().hasRewards()) { - if (finished) { - if (gameWorld.getGame() != null) { - for (Reward reward : gameWorld.getConfig().getRewards()) { - reward.giveTo(player); + if (gameWorld.getGame() != null) { + if (gameWorld.getGame().getType().hasRewards()) { + if (finished) { + if (gameWorld.getGame() != null) { + for (Reward reward : gameWorld.getConfig().getRewards()) { + reward.giveTo(player); + } } - } - addTreasure(); + addTreasure(); - // Set Time - File file = new File(plugin.getDataFolder() + "/maps/" + gameWorld.getMapName(), "players.yml"); + // Set Time + File file = new File(plugin.getDataFolder() + "/maps/" + gameWorld.getMapName(), "players.yml"); + + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file); + + playerConfig.set(getPlayer().getUniqueId().toString(), System.currentTimeMillis()); - if (!file.exists()) { try { - file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } + playerConfig.save(file); - FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(file); - - playerConfig.set(getPlayer().getUniqueId().toString(), System.currentTimeMillis()); - - try { - playerConfig.save(file); - - } catch (IOException exception) { - exception.printStackTrace(); - } - - // Tutorial Permissions - if (gameWorld.isTutorial()) { - String endGroup = plugin.getMainConfig().getTutorialEndGroup(); - if (plugin.isGroupEnabled(endGroup)) { - plugin.getPermissionProvider().playerAddGroup(getPlayer(), endGroup); + } catch (IOException exception) { + exception.printStackTrace(); } - String startGroup = plugin.getMainConfig().getTutorialStartGroup(); - if (plugin.isGroupEnabled(startGroup)) { - plugin.getPermissionProvider().playerRemoveGroup(getPlayer(), startGroup); + // Tutorial Permissions + if (gameWorld.isTutorial()) { + String endGroup = plugin.getMainConfig().getTutorialEndGroup(); + if (plugin.isGroupEnabled(endGroup)) { + plugin.getPermissionProvider().playerAddGroup(getPlayer(), endGroup); + } + + String startGroup = plugin.getMainConfig().getTutorialStartGroup(); + if (plugin.isGroupEnabled(startGroup)) { + plugin.getPermissionProvider().playerRemoveGroup(getPlayer(), startGroup); + } } } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java index 16a8f320..096f28f1 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DSavePlayer.java @@ -16,11 +16,15 @@ */ 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.UUID; import java.util.concurrent.CopyOnWriteArrayList; @@ -47,18 +51,19 @@ public class DSavePlayer { private String uuid; private Location oldLocation; - private ItemStack[] oldInventory; - private ItemStack[] oldArmor; + private ArrayList oldInventory; + private ArrayList oldArmor; + private ItemStack oldOffHand; private int oldLvl; private int oldExp; private int oldHealth; private int oldFoodLevel; private int oldFireTicks; - private GameMode oldGamemode; + private GameMode oldGameMode; private Collection oldPotionEffects; - public DSavePlayer(String playerName, UUID uuid, Location oldLocation, ItemStack[] oldInventory, ItemStack[] oldArmor, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, - GameMode oldGamemode, Collection oldPotionEffects) { + public DSavePlayer(String playerName, UUID uuid, Location oldLocation, ArrayList oldInventory, ArrayList oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, + GameMode oldGameMode, Collection oldPotionEffects) { savePlayers.add(this); this.playerName = playerName; @@ -67,10 +72,33 @@ public class DSavePlayer { 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.oldGameMode = oldGameMode; + this.oldLvl = oldLvl; + this.oldFireTicks = oldFireTicks; + this.oldPotionEffects = oldPotionEffects; + + save(); + } + + public DSavePlayer(String playerName, UUID uuid, Location oldLocation, ItemStack[] oldInventory, ItemStack[] oldArmor, ItemStack oldOffHand, int oldLvl, int oldExp, int oldHealth, int oldFoodLevel, int oldFireTicks, + GameMode oldGameMode, Collection oldPotionEffects) { + savePlayers.add(this); + + this.playerName = playerName; + this.uuid = uuid.toString(); + + this.oldLocation = oldLocation; + this.oldInventory = new ArrayList<>(Arrays.asList(oldInventory)); + this.oldArmor = new ArrayList<>(Arrays.asList(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; @@ -91,13 +119,19 @@ public class DSavePlayer { try { if (!keepInventory) { - player.getInventory().setContents(oldInventory); - player.getInventory().setArmorContents(oldArmor); + while (oldInventory.size() > 36) { + oldInventory.remove(36); + } + player.getInventory().setContents(oldInventory.toArray(new ItemStack[36])); + player.getInventory().setArmorContents(oldArmor.toArray(new ItemStack[4])); + if (CompatibilityHandler.getInstance().getVersion() == Version.MC1_9) { + player.getInventory().setItemInOffHand(oldOffHand); + } player.setTotalExperience(oldExp); player.setLevel(oldLvl); player.setHealth(oldHealth); player.setFoodLevel(oldFoodLevel); - player.setGameMode(oldGamemode); + player.setGameMode(oldGameMode); player.setFireTicks(oldFireTicks); for (PotionEffect effect : player.getActivePotionEffects()) { player.removePotionEffect(effect.getType()); @@ -124,13 +158,12 @@ public class DSavePlayer { } /* Statics */ - @SuppressWarnings("deprecation") public static void save() { FileConfiguration configFile = new YamlConfiguration(); for (DSavePlayer savePlayer : savePlayers) { configFile.set(savePlayer.playerName + ".uuid", savePlayer.uuid); - configFile.set(savePlayer.playerName + ".oldGamemode", savePlayer.oldGamemode.getValue()); + configFile.set(savePlayer.playerName + ".oldGameMode", savePlayer.oldGameMode.toString()); configFile.set(savePlayer.playerName + ".oldFireTicks", savePlayer.oldFireTicks); configFile.set(savePlayer.playerName + ".oldFoodLevel", savePlayer.oldFoodLevel); configFile.set(savePlayer.playerName + ".oldHealth", savePlayer.oldHealth); @@ -138,6 +171,7 @@ public class DSavePlayer { 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 + ".oldOffHand", savePlayer.oldOffHand); 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()); @@ -154,7 +188,6 @@ public class DSavePlayer { } } - @SuppressWarnings({"unchecked", "deprecation"}) public static void load() { FileConfiguration configFile = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "savePlayers.yml")); @@ -163,11 +196,9 @@ public class DSavePlayer { UUID uuid = UUID.fromString(configFile.getString(playerName + ".uuid")); // Load inventory data - ArrayList oldInventoryList = (ArrayList) configFile.get(playerName + ".oldInventory"); - ArrayList oldArmorList = (ArrayList) configFile.get(playerName + ".oldArmor"); - - ItemStack[] oldInventory = oldInventoryList.toArray(new ItemStack[oldInventoryList.size()]); - ItemStack[] oldArmor = oldArmorList.toArray(new ItemStack[oldArmorList.size()]); + ArrayList oldInventory = (ArrayList) configFile.get(playerName + ".oldInventory"); + ArrayList oldArmor = (ArrayList) configFile.get(playerName + ".oldArmor"); + ItemStack oldOffHand = (ItemStack) configFile.get(playerName + ".oldOffHand"); // Load other data int oldLvl = configFile.getInt(playerName + ".oldLvl"); @@ -175,7 +206,10 @@ public class DSavePlayer { 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")); + GameMode oldGameMode = GameMode.SURVIVAL; + if (EnumUtil.isValidEnum(GameMode.class, configFile.getString(playerName + ".oldGameMode"))) { + oldGameMode = GameMode.valueOf(configFile.getString(playerName + ".oldGameMode")); + } Collection oldPotionEffects = (Collection) configFile.get(playerName + ".oldPotionEffects"); // Location @@ -188,7 +222,7 @@ public class DSavePlayer { + ".oldLocation.z"), configFile.getInt(playerName + ".oldLocation.yaw"), configFile.getInt(playerName + ".oldLocation.pitch")); // Create Player - DSavePlayer savePlayer = new DSavePlayer(playerName, uuid, oldLocation, oldInventory, oldArmor, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGamemode, oldPotionEffects); + DSavePlayer savePlayer = new DSavePlayer(playerName, uuid, oldLocation, oldInventory, oldArmor, oldOffHand, oldLvl, oldExp, oldHealth, oldFoodLevel, oldFireTicks, oldGameMode, oldPotionEffects); savePlayer.reset(false); } }