public void restore(Player player) {
    if (!player.isOnline()) {
      plugin.logDebug("Player " + player.getName() + " is offline. Skipping restore...");
      return;
    }

    Restoration restoration = getRestoration(player);

    if (restoration.enabled) {
      player.getInventory().clear();

      player.getInventory().setContents(restoration.inventory);
      player.getInventory().setArmorContents(restoration.armour);
      if (plugin.isMc19or110()) {
        player.getInventory().setItemInOffHand(restoration.offHand);
      }
      if (player.hasPermission(PERM_LEVEL)
          || !plugin.config.permsEnabled()
          || (player.isOp() && plugin.config.opsAllPerms())) {
        plugin.logDebug("Player " + player.getName() + " does have " + PERM_LEVEL + " permission.");
        player.setLevel(restoration.level);
        plugin.logDebug("Player " + player.getName() + " gets " + restoration.level + " level.");
      } else {
        plugin.logDebug(
            "Player " + player.getName() + " does NOT have " + PERM_LEVEL + " permission.");
      }
      if (player.hasPermission(PERM_EXP)
          || !plugin.config.permsEnabled()
          || (player.isOp() && plugin.config.opsAllPerms())) {
        plugin.logDebug("Player " + player.getName() + " does have " + PERM_EXP + " permission.");
        player.setExp(restoration.exp);
        plugin.logDebug("Player " + player.getName() + " gets " + restoration.exp + " XP.");
      } else {
        plugin.logDebug(
            "Player " + player.getName() + " does NOT have " + PERM_EXP + " permission.");
      }
      if (plugin.config.shouldNotify()) {
        plugin.message(player, plugin.config.msgRecovered());
      }
      removeRestoration(player);
      if (hasRestoration(player)) {
        plugin.message(player, "Restore exists!!!");
      }
    }
  }
 public void cmdRestore(Player player) {
   if (player.hasPermission(PERM_RESTORE_INV)) {
     if (hasRestoration(player)) {
       Restoration restoration = getRestoration(player);
       player.getInventory().clear();
       player.getInventory().setContents(restoration.inventory);
       player.getInventory().setArmorContents(restoration.armour);
       if (plugin.isMc19or110()) {
         player.getInventory().setItemInOffHand(restoration.offHand);
       }
       player.setLevel(restoration.level);
       player.setExp(restoration.exp);
       removeRestoration(player);
     } else {
       plugin.message(player, "Nothing to restore.");
     }
   } else {
     plugin.message(player, plugin.config.msgNoPerm());
   }
 }
 public void save() {
   HashMap<String, RestorationObject> restorationsForDisk = new HashMap<>();
   for (Map.Entry<String, Restoration> entry : RESTORATIONS.entrySet()) {
     String key = entry.getKey();
     Restoration value = entry.getValue();
     RestorationObject tmpRestoration = new RestorationObject();
     for (ItemStack i : value.inventory) {
       if (i instanceof ItemStack) {
         plugin.logDebug("Serializing inventory: " + i.toString());
         tmpRestoration.inventory.add(new ScavengerItem(i));
       }
     }
     for (ItemStack i : value.armour) {
       if (i instanceof ItemStack) {
         plugin.logDebug("Serializing armour: " + i.toString());
         tmpRestoration.armour.add(new ScavengerItem(i));
       }
     }
     if (plugin.isMc19or110()) {
       if (value.offHand instanceof ItemStack) {
         plugin.logDebug("Serializing offhand: " + value.offHand.toString());
         tmpRestoration.offHand = new ScavengerItem(value.offHand);
       }
     }
     tmpRestoration.enabled = value.enabled;
     tmpRestoration.level = value.level;
     tmpRestoration.exp = value.exp;
     tmpRestoration.playerName = value.playerName;
     restorationsForDisk.put(key, tmpRestoration);
     plugin.logInfo("Saving " + tmpRestoration.playerName + "'s inventory to disk.");
   }
   try {
     File file = new File("plugins/Scavenger/inv3.ser");
     FileOutputStream f_out = new FileOutputStream(file);
     try (ObjectOutputStream obj_out = new ObjectOutputStream(f_out)) {
       obj_out.writeObject(restorationsForDisk);
     }
   } catch (IOException e) {
     plugin.logError(e.getMessage());
   }
 }
 public void cmdCollect(Player player) {
   if (player.hasPermission(PERM_SAVE_INV)) {
     if (plugin.config.shouldNotify()) {
       plugin.message(player, plugin.config.msgSaving());
     }
     Restoration restoration = new Restoration();
     restoration.enabled = false;
     restoration.inventory = Arrays.copyOfRange(player.getInventory().getContents(), 0, 36);
     restoration.armour = player.getInventory().getArmorContents();
     if (plugin.isMc19or110()) {
       restoration.offHand = player.getInventory().getItemInOffHand();
     }
     restoration.playerName = player.getDisplayName();
     restoration.level = player.getLevel();
     restoration.exp = player.getExp();
     player.setLevel(0);
     player.setExp(0f);
     player.getInventory().clear();
     addRestoration(player, restoration);
   } else {
     plugin.message(player, plugin.config.msgNoPerm());
   }
 }
  public void load() {
    Map<String, RestorationObject> restorationsFromDisk;
    File file = new File("plugins/Scavenger/inv3.ser");
    if (!file.exists()) {
      plugin.logDebug("Recovery file '" + file.getAbsolutePath() + "' does not exist.");
      return;
    }
    try {
      FileInputStream f_in = new FileInputStream(file);
      try (ObjectInputStream obj_in = new ObjectInputStream(f_in)) {
        restorationsFromDisk = (Map<String, RestorationObject>) obj_in.readObject();
      }
    } catch (IOException | ClassNotFoundException | ClassCastException e) {
      plugin.logError(e.getMessage());
      return;
    }

    for (Map.Entry<String, RestorationObject> entry : restorationsFromDisk.entrySet()) {
      String key = entry.getKey();
      RestorationObject value = entry.getValue();
      Restoration tmpRestoration = new Restoration();
      tmpRestoration.inventory = new ItemStack[value.inventory.size()];
      tmpRestoration.armour = new ItemStack[value.armour.size()];

      for (int i = 0; i < value.inventory.size(); i++) {
        if (value.inventory.get(i) instanceof ScavengerItem) {
          boolean error = false;
          ItemStack tmpStack = new ItemStack(Material.AIR);
          plugin.logDebug("Deserializing inventory: " + value.inventory.get(i));
          try {
            tmpStack = value.inventory.get(i).getItemStack();
          } catch (Exception e) {
            plugin.logError(e.getMessage() + " => " + value.inventory.get(i));
            error = true;
          } catch (Throwable e) {
            plugin.logError(e.getMessage() + " => " + value.inventory.get(i));
            error = true;
          }
          if (tmpStack == null || error) {
            tmpRestoration.inventory[i] = new ItemStack(Material.AIR);
          } else {
            tmpRestoration.inventory[i] = tmpStack;
          }
          plugin.logDebug("Done: " + tmpRestoration.inventory[i].toString());
        }
      }

      for (int i = 0; i < value.armour.size(); i++) {
        if (value.armour.get(i) instanceof ScavengerItem) {
          ItemStack tmpStack = new ItemStack(Material.AIR);
          plugin.logDebug("Deserializing armour: " + value.armour.get(i));
          try {
            tmpStack = value.armour.get(i).getItemStack();
          } catch (Exception e) {
            plugin.logError(e.getMessage());
          }
          if (tmpStack == null) {
            tmpRestoration.armour[i] = new ItemStack(Material.AIR);
          } else {
            tmpRestoration.armour[i] = tmpStack;
          }
          plugin.logDebug("Done: " + tmpRestoration.armour[i].toString());
        }
      }

      if (plugin.isMc19or110()) {
        if (value.offHand instanceof ScavengerItem) {
          ItemStack tmpStack = new ItemStack(Material.AIR);
          plugin.logDebug("Deserializing offhand: " + value.offHand);
          try {
            tmpStack = value.offHand.getItemStack();
          } catch (Exception e) {
            plugin.logError(e.getMessage());
          }
          if (tmpStack == null) {
            tmpRestoration.offHand = new ItemStack(Material.AIR);
          } else {
            tmpRestoration.offHand = tmpStack;
          }
          plugin.logDebug("Done: " + tmpRestoration.offHand.toString());
        }
      }

      tmpRestoration.enabled = value.enabled;
      tmpRestoration.level = value.level;
      tmpRestoration.exp = value.exp;
      tmpRestoration.playerName = value.playerName;

      RESTORATIONS.put(key, tmpRestoration);
      plugin.logInfo("Loading " + tmpRestoration.playerName + "'s inventory from disk.");
    }
  }
  public void collect(Player player, List<ItemStack> itemDrops, EntityDeathEvent event) {
    if (itemDrops.isEmpty() && !levelAllow(player) && !expAllow(player)) {
      return;
    }

    if (plugin.config.dropOnPVPDeath() && player.getKiller() instanceof Player) {
      plugin.message(player, plugin.config.msgPVPDeath());
      return;
    }

    if (plugin.config.residence()) {
      ClaimedResidence res = Residence.getResidenceManager().getByLoc(player.getLocation());
      if (res != null) {
        ResidencePermissions perms = res.getPermissions();
        if (perms.playerHas(player.getName(), plugin.config.resFlag(), true)) {
          plugin.logDebug(
              "Player '"
                  + player.getName()
                  + "' is not allowed to use Scavenger in this residence. Items will be dropped.");
          plugin.message(player, plugin.config.msgInsideRes());
          return;
        } else {
          plugin.logDebug(
              "Player '" + player.getName() + "' is allowed to use Scavenger in this residence.");
        }
      }
    }

    if (plugin.config.factionEnemyDrops() && plugin.factionHook != null) {
      if (plugin.factionHook.isPlayerInEnemyFaction(player)) {
        return;
      }
    }

    if (plugin.config.dungeonMazeDrops() && plugin.dmHook != null) {
      plugin.logDebug("Checking if '" + player.getName() + "' is in DungeonMaze.");
      if (plugin.dmHook.isPlayerInDungeon(player)) {
        plugin.logDebug("Player '" + player.getName() + "' is in DungeonMaze.");
        plugin.message(player, plugin.config.msgInsideDungeonMaze());
        return;
      }
    }

    if (plugin.getWorldGuard() != null) {
      plugin.logDebug("Checking region support for '" + player.getWorld().getName() + "'");
      if (plugin.getWorldGuard().getRegionManager(player.getWorld()) != null) {
        try {
          RegionManager regionManager = plugin.getWorldGuard().getRegionManager(player.getWorld());
          ApplicableRegionSet set = regionManager.getApplicableRegions(player.getLocation());
          if (set.allows(DefaultFlag.PVP) && plugin.config.wgPVPIgnore()) {
            plugin.logDebug(
                "This is a WorldGuard PVP zone and WorldGuardPVPIgnore is "
                    + plugin.config.wgPVPIgnore());
            if (!plugin.config.msgInsideWGPVP().isEmpty()) {
              plugin.message(player, plugin.config.msgInsideWGPVP());
            }
            return;
          }
          if (!set.allows(DefaultFlag.PVP) && plugin.config.wgGuardPVPOnly()) {
            plugin.logDebug(
                "This is NOT a WorldGuard PVP zone and WorldGuardPVPOnly is "
                    + plugin.config.wgGuardPVPOnly());
            if (!plugin.config.msgInsideWGPVP().isEmpty()) {
              plugin.message(player, plugin.config.msgInsideWGPVPOnly());
            }
            return;
          }
        } catch (NullPointerException ex) {
          plugin.logDebug(ex.getMessage());
        }
      } else {
        plugin.logDebug("Region support disabled for '" + player.getWorld().getName() + "'");
      }
    }

    if (plugin.getUltimateArena() != null) {
      if (UltimateArenaAPI.hookIntoUA(plugin).isInArena(player)) {
        if (!plugin.config.msgInsideUA().isEmpty()) {
          plugin.message(player, plugin.config.msgInsideUA());
        }
        return;
      }
    }

    if (plugin.maHandler != null && plugin.maHandler.isPlayerInArena(player)) {
      if (!plugin.config.msgInsideMA().isEmpty()) {
        plugin.message(player, plugin.config.msgInsideMA());
      }
      return;
    }

    if (plugin.pvpHandler != null && !PVPArenaAPI.getArenaName(player).equals("")) {
      String x = plugin.config.msgInsidePA();
      if (!x.isEmpty()) {
        x = x.replace("%ARENA%", PVPArenaAPI.getArenaName(player));
        plugin.message(player, x);
      }
      return;
    }

    if (plugin.battleArena) {
      mc.alk.arena.objects.ArenaPlayer ap = mc.alk.arena.BattleArena.toArenaPlayer(player);
      if (ap != null) {
        Match match = BattleArena.getBAController().getMatch(ap);
        if (match != null) {
          if (match.isInMatch(ap)) {
            String x = plugin.config.msgInsideBA();
            if (!x.isEmpty()) {
              plugin.message(player, x);
            }
            return;
          }
        }
      }
    }

    if (plugin.minigames != null) {
      if (plugin.minigames.playerInMinigame(player)) {
        plugin.logDebug(
            "Player '" + player.getName() + "' is in a Minigame. Not recovering items.");
        return;
      }
    }

    if (hasRestoration(player)) {
      plugin.error(player, "Restoration already exists, ignoring.");
      return;
    }

    if (plugin.getEconomy() != null
        && !(player.hasPermission(PERM_FREE) || (player.isOp() && plugin.config.opsAllPerms()))
        && plugin.config.economyEnabled()) {
      NumberFormat formatter = NumberFormat.getInstance(new Locale(plugin.config.countryCode()));
      formatter.setMaximumFractionDigits(plugin.config.decimalPlaces());
      double restoreCost = plugin.config.restoreCost();
      double withdrawAmount;
      double playerBalance = plugin.getEconomy().getBalance(player);
      double percentCost = plugin.config.percentCost();
      double minCost = plugin.config.minCost();
      double maxCost = plugin.config.maxCost();
      EconomyResponse er;
      String currency;
      if (plugin.config.percent()) {
        if (playerBalance < 0) {
          withdrawAmount = 0;
        } else {
          withdrawAmount = playerBalance * (percentCost / 100.0);
        }
        plugin.logDebug("withdrawAmount: " + withdrawAmount);
        plugin.logDebug("playeBalance: " + playerBalance);
        plugin.logDebug("percentCost: " + percentCost + "(" + (percentCost / 100.0) + ")");
        if (plugin.config.addMin()) {
          withdrawAmount = withdrawAmount + minCost;
          plugin.logDebug("withdrawAmount (addMin): " + withdrawAmount);
        } else if (withdrawAmount < minCost) {
          withdrawAmount = minCost;
        }
        if (withdrawAmount > maxCost && maxCost > 0) {
          withdrawAmount = maxCost;
        }
      } else {
        withdrawAmount = restoreCost;
      }
      er = plugin.getEconomy().withdrawPlayer(player, withdrawAmount);
      if (er.transactionSuccess()) {
        plugin.logDebug("Withdraw success!");
        if (withdrawAmount == 1) {
          currency = plugin.getEconomy().currencyNameSingular();
        } else {
          currency = plugin.getEconomy().currencyNamePlural();
        }
        String x = plugin.config.msgSaveForFee();
        if (!x.isEmpty()) {
          x = x.replace("%COST%", formatter.format(withdrawAmount));
          x = x.replace("%CURRENCY%", currency);
          plugin.message(player, x);
        }
        if (!plugin.config.depositDestination().isEmpty()) {
          plugin.logDebug("DepositDesination: " + plugin.config.depositDestination());
          if (plugin.config.depositType().equalsIgnoreCase("bank")) {
            plugin.logDebug("DepositType: BANK");
            if (plugin.getEconomy().hasBankSupport()) {
              plugin.logDebug("Bank support is enabled");
              plugin.getEconomy().bankDeposit(plugin.config.depositDestination(), withdrawAmount);
            } else {
              plugin.logDebug("Bank support is NOT enabled");
            }
          } else if (plugin.config.depositType().equalsIgnoreCase("player")) {
            plugin.logDebug("DepositType: PLAYER");
            plugin.logDebug("DepositDestination: " + plugin.config.depositDestination());
            if (plugin.getEconomy().hasAccount(plugin.config.depositDestination())) {
              plugin.logDebug("DepositDestination: VALID");
              plugin.getEconomy().depositPlayer(plugin.config.depositDestination(), withdrawAmount);
            } else {
              plugin.logDebug("DepositDestination: INVALID");
            }
          } else {
            plugin.logDebug("DepositType: INVALID");
          }
        } else {
          plugin.logDebug("No deposit destination!");
        }
      } else {
        plugin.logDebug("Withdraw fail! " + er.errorMessage);
        if (playerBalance == 1) {
          currency = plugin.getEconomy().currencyNameSingular();
        } else {
          currency = plugin.getEconomy().currencyNamePlural();
        }
        String x = plugin.config.msgNotEnoughMoney();
        if (!x.isEmpty()) {
          x = x.replace("%BALANCE%", formatter.format(playerBalance));
          x = x.replace("%COST%", formatter.format(withdrawAmount));
          x = x.replace("%CURRENCY%", currency);
          x = x.replace("%ERRORMESSAGE%", er.errorMessage);
          plugin.message(player, x);
        }
        return;
      }
    } else {
      plugin.message(player, plugin.config.msgSaving());
    }

    Restoration restoration = new Restoration();
    restoration.enabled = false;

    // temporary fix for 1.9
    restoration.inventory = Arrays.copyOfRange(player.getInventory().getContents(), 0, 36);
    restoration.armour = player.getInventory().getArmorContents();
    if (plugin.isMc19or110()) {
      restoration.offHand = player.getInventory().getItemInOffHand();
    }
    restoration.playerName = player.getDisplayName();
    itemDrops.clear();

    if (levelAllow(player)) {
      plugin.logDebug("Collecting level " + player.getLevel() + " for " + player.getName());
      restoration.level = player.getLevel();
    }
    if (expAllow(player)) {
      plugin.logDebug("Collecting exp " + player.getExp() + " for " + player.getName());
      restoration.exp = player.getExp();
      event.setDroppedExp(0);
    }

    String deathCause = "NULL";
    if (player.getLastDamageCause() != null) {
      if (player.getLastDamageCause().getCause() != null) {
        deathCause = player.getLastDamageCause().getCause().toString();
      }
    }
    String deathCausePermission = PERM_SCAVENGE_PREFIX + deathCause;
    plugin.logDebug(
        "[p:"
            + player.getName()
            + "] ["
            + PERM_SCAVENGE
            + ":"
            + player.hasPermission(PERM_SCAVENGE)
            + "]"
            + " ["
            + deathCausePermission
            + ":"
            + player.hasPermission(deathCausePermission)
            + "]");
    if (player.hasPermission(PERM_SCAVENGE) || player.hasPermission(deathCausePermission)) {
      plugin.logDebug("Permissions are okay. Time to scavenge...");
      if (plugin.config.chanceToDrop() > 0 && !player.hasPermission(PERM_NO_CHANCE)) {
        checkChanceToDropItems(restoration.armour, itemDrops);
        checkChanceToDropItems(restoration.inventory, itemDrops);
        if (plugin.isMc19or110()) {
          checkChanceToDropItems(restoration.offHand, itemDrops);
        }
      }
      if (plugin.config.singleItemDrops()) {
        checkSingleItemDrops(player, restoration.armour, itemDrops);
        checkSingleItemDrops(player, restoration.inventory, itemDrops);
        checkSingleItemDrops(player, restoration.offHand, itemDrops);
      } else if (plugin.config.singleItemKeeps()) {
        checkSingleItemKeeps(player, "armour", restoration.armour, itemDrops);
        checkSingleItemKeeps(player, "inv", restoration.inventory, itemDrops);
        if (plugin.isMc19or110()) {
          checkSingleItemKeeps(player, "offhand", restoration.offHand, itemDrops, 1);
        }
      } else if (plugin.config.slotBasedRecovery()) {
        checkSlots(player, "armour", restoration.armour, itemDrops);
        checkSlots(player, "inv", restoration.inventory, itemDrops);
        if (plugin.isMc19or110()) {
          checkSlots(player, "offhand", restoration.offHand, itemDrops, 1);
        }
      }
    } else {
      plugin.logDebug("Permissions are NOT okay. Dropping items...");
      dropItems(restoration.armour, itemDrops);
      dropItems(restoration.inventory, itemDrops);
      dropItem(restoration.offHand, itemDrops);
    }
    addRestoration(player, restoration);
  }