Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mondlw.laserserver.laserservermanager.data;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Objects;
- import java.util.Optional;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- public class InventoryManager {
- private static final HashMap<String, Inventory> inventories = new HashMap<>();
- public static void saveData() {
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- String json = gson.toJson(inventories);
- // Save the JSON string to a file
- try {
- File file = new File("inventories.json");
- FileWriter writer = new FileWriter(file);
- writer.write(json);
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static Inventory loadInventory(String playerName, String world, String mode) {
- Optional<Inventory> inventoryOptional = Optional.ofNullable(inventories.get(playerName + "_" + world + "_" + mode));
- if (inventoryOptional.isPresent()) {
- return inventoryOptional.get();
- } else {
- return Bukkit.createInventory(null, 9, world + "-" + mode);
- }
- }
- public static void replaceInventoryTo(Player player, String world, String mode) {
- Inventory newInventory = inventories.get(player.getName() + "_" + world + "_" + mode);
- Inventory oldInventory = player.getInventory();
- if(newInventory == null) {
- newInventory = Bukkit.createInventory(null, 36);
- }
- if(mode.equals("build")) {
- // Save the current normal inventory
- if(inventories.containsKey(player.getName() + "_" + world + "_" + "normal")) {
- inventories.replace(player.getName() + "_" + world + "_" + "normal", oldInventory);
- } else {
- inventories.put(player.getName() + "_" + world + "_" + "normal", oldInventory);
- }
- } else {
- // Save the current build inventory
- if(inventories.containsKey(player.getName() + "_" + world + "_" + "build")) {
- inventories.replace(player.getName() + "_" + world + "_" + "build", oldInventory);
- } else {
- inventories.put(player.getName() + "_" + world + "_" + "build", oldInventory);
- }
- }
- player.getInventory().setContents(newInventory.getContents());
- player.updateInventory();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment