Skip to content

Commit

Permalink
Merge pull request #1 from TheCoder11/save-load
Browse files Browse the repository at this point in the history
AB2
  • Loading branch information
somemone0 authored Mar 6, 2021
2 parents bb710e8 + 5032321 commit 9245cc0
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 22 deletions.
136 changes: 125 additions & 11 deletions src/main/java/com/somemone/bigventories/Bigventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
import com.somemone.bigventories.command.PersonalStorageCommand;
import com.somemone.bigventories.listener.InventoryListener;
import com.somemone.bigventories.storage.*;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public final class Bigventories extends JavaPlugin {

Expand All @@ -41,17 +49,29 @@ public void onEnable() {
getCommand("pstorage").setExecutor(new PersonalStorageCommand());
getCommand("cstorage").setExecutor(new ChunkStorageCommand());
getCommand("gstorage").setExecutor(new GroupStorageCommand());

try {
loadStorages();
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}


@Override
public void onDisable() {
// Plugin shutdown logic

try {
saveStorages();
} catch (IOException e) {
e.printStackTrace();
}
}



public void saveStorages () {
public void saveStorages () throws IOException {
// Closing all OpenStorages to permanent storage

for (OpenStorage os : Bigventories.openStorages) {
Expand Down Expand Up @@ -103,47 +123,141 @@ public void saveStorages () {

for ( PersonalStorage ps : Bigventories.personalStorages) {

ItemStack[] items = (ItemStack[]) ps.items.toArray();
ItemStack[] items = ps.items.toArray(new ItemStack[0]);
String uuid = ps.uuid.toString();
String owner = ps.owner.getUniqueId().toString();
String owner = "";
try {
owner = ps.owner.getUniqueId().toString();
} catch (NullPointerException ignored) {}

con.set("pstorages." + uuid + ".contents", items);
con.set("pstorages." + uuid + ".owner", owner);
con.set("pstorages." + uuid + ".rows", Integer.toString( ps.rows ));
con.set("pstorages." + uuid + ".uuid", uuid);
con.set("pstorages." + uuid + ".rows", ps.rows);

}

for ( ChunkStorage cs : Bigventories.chunkStorages) {

ItemStack[] items = (ItemStack[]) cs.items.toArray();
ItemStack[] items = cs.items.toArray(new ItemStack[0]);
String uuid = cs.uuid.toString();

con.set("cstorages." + uuid + ".contents", items);
con.set("cstorages." + uuid + ".xpos", cs.x);
con.set("cstorages." + uuid + ".zpos", cs.z);
con.set("cstorages." + uuid + ".uuid", uuid);
con.set("cstorages." + uuid + ".rows", cs.rows);

}
for ( GroupStorage gs : Bigventories.groupStorages) {

ItemStack[] items = (ItemStack[]) gs.items.toArray();
ItemStack[] items = gs.items.toArray(new ItemStack[0]);
String uuid = gs.uuid.toString();
String owner = gs.owner.getUniqueId().toString();

String[]
List<String> uuids = new ArrayList<>();
for (Player player : gs.accessList) {
uuids.add( player.getUniqueId().toString() );
}


con.set("gstorages." + uuid + ".contents", items);
con.set("gstorages." + uuid + ".rows", gs.rows);
con.set("gstorages." + uuid + ".name", gs.name);
con.set("gstorages." + uuid + ".players", gs.accessList.toArray());
con.set("gstorages." + uuid + ".players", uuids);
con.set("gstorages." + uuid + ".owner", owner);

}

con.save( new File (this.getDataFolder(), "storages.yml"));
}

public void loadStorages () {
public void loadStorages () throws IOException, InvalidConfigurationException {
YamlConfiguration con = new YamlConfiguration();
con.load( new File( this.getDataFolder(), "storages.yml"));
ArrayList<String> keys;

// load PersonalStorages

if (con.contains("pstorages")) {
ConfigurationSection pStorages = con.getConfigurationSection("pstorages");

keys = new ArrayList<>();
if (pStorages.getKeys(false) != null) {
keys.addAll(pStorages.getKeys(false));

for (String key : keys) {

UUID uuid = UUID.fromString(key);

Player owner = Bukkit.getPlayer(UUID.fromString(pStorages.getString(key + ".owner")));

ArrayList<ItemStack> contents = (ArrayList<ItemStack>) pStorages.getList(key + ".contents");

int rows = pStorages.getInt(key + ".rows");

Bigventories.personalStorages.add(new PersonalStorage(rows, uuid, owner, contents));

}
}
}
// load ChunkStorages

if (con.contains("cstorages")) {
ConfigurationSection cStorages = con.getConfigurationSection("cstorages");

keys = new ArrayList<>();
if (cStorages.getKeys(false) != null) {
keys.addAll(cStorages.getKeys(false));

for (String key : keys) {

UUID uuid = UUID.fromString(key);

ArrayList<ItemStack> contents = (ArrayList<ItemStack>) cStorages.getList(key + ".contents");

int rows = cStorages.getInt(key + ".rows");

int x = cStorages.getInt(key + ".xpos");

int z = cStorages.getInt(key + ".zpos");

Bigventories.chunkStorages.add(new ChunkStorage(rows, uuid, contents, x, z));

}
}
}
// load GroupStorages

ConfigurationSection gStorages = con.getConfigurationSection("gstorages");

if (con.contains("gstorages")) {
keys = new ArrayList<>();
if (gStorages.getKeys(false) != null) {
keys.addAll(gStorages.getKeys(false));

for (String key : keys) {

UUID uuid = UUID.fromString(key);

ArrayList<ItemStack> contents = (ArrayList<ItemStack>) gStorages.getList(key + ".contents");

int rows = gStorages.getInt(key + ".rows");

Player owner = Bukkit.getPlayer(UUID.fromString(gStorages.getString(key + ".owner")));

ArrayList<String> accessStringList = (ArrayList<String>) gStorages.getStringList(key + ".players");

ArrayList<Player> accessList = new ArrayList<>();
for (String player : accessStringList) {
accessList.add(Bukkit.getPlayer(UUID.fromString(player)));
}

String name = gStorages.getString(key + ".name");

Bigventories.groupStorages.add(new GroupStorage(name, rows, contents, owner, uuid, accessList));

}
}
}

}
}
15 changes: 15 additions & 0 deletions src/main/java/com/somemone/bigventories/command/AdminCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.somemone.bigventories.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class AdminCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {



return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (cs.uuid == newCS.uuid) {

sender.sendMessage(ChatColor.RED + "A Chunk Storage already exists in this area!");
return true;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (args.length == 3) {

if (Bigventories.groupStorages.size() > 0) {

for (GroupStorage fgs : Bigventories.groupStorages) {
if (fgs.name.equals(args[2])) {
if (fgs.owner == player) {

if (fgs.name.equals(args[2]) && fgs.owner == player) {
sender.sendMessage("Got here!");
fgs.accessList.add(Bukkit.getPlayer(args[1]));
return true;

fgs.accessList.add(Bukkit.getPlayer(args[1]));
return true;
} else {

} else {
sender.sendMessage(ChatColor.RED + "You do not have access to this storage!");

sender.sendMessage(ChatColor.RED + "You do not have access to this storage!");
}

}

}
}

Expand All @@ -116,14 +119,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (fgs.name.equals(args[1])) {

sender.sendMessage(ChatColor.RED + "A Group Storage with this name already exists!");
return false;
return true;

}

}
}

newGS.accessList.add(player);
Bigventories.groupStorages.add(newGS);

}
break;
case "remove": // /gs remove player123
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (Bigventories.openStorages.size() > 0) {
for (OpenStorage os : Bigventories.openStorages) {
if (os.uuid == ps.uuid) {
sender.sendMessage("Found an Open Storage!");
player.openInventory(os.inventory.get(0));

return true;
Expand Down Expand Up @@ -64,7 +63,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
for (PersonalStorage ups : Bigventories.personalStorages) {
if (ups.owner == player) {
sender.sendMessage(ChatColor.RED + "You already have a personal storage!");
break;
return true;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/somemone/bigventories/storage/ChunkStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.bukkit.Chunk;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.UUID;

public class ChunkStorage extends Storage {

Expand All @@ -15,6 +19,13 @@ public ChunkStorage(int rows, int x, int z) {
this.z = z;
}

public ChunkStorage(int rows, UUID uuid, ArrayList<ItemStack> items, int x, int z) {
super(rows, uuid, items);

this.x = x;
this.z = z;
}

public boolean checkChunk( Chunk chunk ) {
return chunk.getX() == this.x && chunk.getZ() == this.z;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/somemone/bigventories/storage/GroupStorage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.somemone.bigventories.storage;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.UUID;

public class GroupStorage extends Storage {

Expand All @@ -19,6 +21,15 @@ public GroupStorage(String name, int rows, Player owner) {
this.name = name;
}

public GroupStorage(String name, int rows, ArrayList<ItemStack> items, Player owner, UUID uuid, ArrayList<Player> accessList) {
super(rows, uuid, items);

this.owner = owner;
this.accessList = accessList;
this.accessList.add(owner);
this.name = name;
}

public void addPlayer ( Player added ) {
this.accessList.add(added);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.somemone.bigventories.storage;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.UUID;

public class PersonalStorage extends Storage {

Expand All @@ -11,4 +15,10 @@ public PersonalStorage(int rows, Player owner) {

this.owner = owner;
}

public PersonalStorage(int rows, UUID uuid, Player owner, ArrayList<ItemStack> items) {
super(rows, uuid, items);

this.owner = owner;
}
}
Loading

0 comments on commit 9245cc0

Please sign in to comment.