Add reload command and fix a small bug.

The bug is it not loading systems by players that are not online when the plugin loads, or when the player doesn't join.
This commit is contained in:
SeanOMik 2020-07-06 23:31:14 -05:00
parent 1ffd0340dc
commit 1273936d76
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
10 changed files with 114 additions and 12 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EnergeticStorage</name>

View File

@ -1,6 +1,7 @@
package net.seanomik.energeticstorage;
import net.seanomik.energeticstorage.commands.ESGiveCommand;
import net.seanomik.energeticstorage.commands.ESReloadCommand;
import net.seanomik.energeticstorage.files.PlayersFile;
import net.seanomik.energeticstorage.listeners.BlockBreakListener;
import net.seanomik.energeticstorage.listeners.BlockPlaceListener;
@ -28,13 +29,12 @@ public final class EnergeticStorage extends JavaPlugin implements Listener {
PlayersFile.getConfig().saveDefaultConfig();
for (Player player : Bukkit.getOnlinePlayers()) {
cachePlayersSystems(player);
}
Reference.ES_SYSTEMS = PlayersFile.getAllSystems();
}
private void registerCommands() {
getCommand("esgive").setExecutor(new ESGiveCommand());
getCommand("esreload").setExecutor(new ESReloadCommand());
}
private void registerListener() {
@ -48,17 +48,17 @@ public final class EnergeticStorage extends JavaPlugin implements Listener {
getServer().getPluginManager().registerEvents(this, this);
}
public void cachePlayersSystems(Player player) {
/*public void cachePlayersSystems(Player player) {
if (PlayersFile.doesPlayerHaveSystem(player.getUniqueId())) {
Reference.ES_SYSTEMS.put(player.getUniqueId(), PlayersFile.getPlayersSystems(player.getUniqueId()));
}
}
}*/
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
cachePlayersSystems(player);
//cachePlayersSystems(player);
}
@Override

View File

@ -0,0 +1,31 @@
package net.seanomik.energeticstorage.commands;
import net.seanomik.energeticstorage.files.ConfigFile;
import net.seanomik.energeticstorage.files.PlayersFile;
import net.seanomik.energeticstorage.utils.ItemConstructor;
import net.seanomik.energeticstorage.utils.PermissionChecks;
import net.seanomik.energeticstorage.utils.Reference;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ESReloadCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
ConfigFile.getConfig().reloadConfig();
PlayersFile.getConfig().reloadConfig();
// Recache the systems
Reference.ES_SYSTEMS = PlayersFile.getAllSystems();
return true;
}
}

View File

@ -99,6 +99,68 @@ public class PlayersFile extends YamlConfiguration {
return getConfig().contains("players." + uuid + ".systems");
}
public static Map<UUID, List<ESSystem>> getAllSystems() {
Map<UUID, List<ESSystem>> allSystems = new HashMap<>();
for (String playerUUID : getConfig().getConfigurationSection("players").getKeys(false)) {
List<ESSystem> playersSystems = new ArrayList<>();
for (String systemUUID : getConfig().getConfigurationSection("players." + playerUUID + ".systems").getKeys(false)) {
String systemPath = "players." + playerUUID + ".systems." + systemUUID + ".";
List<ESDrive> drives = new ArrayList<>();
if (getConfig().contains(systemPath + "drives")) {
for (String driveUUID : getConfig().getConfigurationSection(systemPath + "drives").getKeys(false)) {
Map<ItemStack, Integer> items = new HashMap();
if (getConfig().contains(systemPath + "drives." + driveUUID + ".items")) {
try {
JSONParser jsonParser = new JSONParser();
JSONArray itemJsonArray = (JSONArray) jsonParser.parse(getConfig().getString(systemPath + "drives." + driveUUID + ".items"));
for (int i = 0; i < itemJsonArray.size(); i++) {
JSONObject itemObject = (JSONObject) itemJsonArray.get(i);
Map.Entry<ItemStack, Integer> item = ItemSerialization.deserializeItem((String) itemObject.get("itemYAML"));
items.put(item.getKey(), item.getValue());
}
} catch (ParseException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
int size = getConfig().getInt(systemPath + "drives." + driveUUID + ".size");
drives.add(new ESDrive(size, items));
}
}
List<UUID> trustedUUIDs = new ArrayList<>();
if (getConfig().contains(systemPath + "trustedUUIDs")) {
try {
JSONArray trustedJson = (JSONArray) new JSONParser().parse(getConfig().getString(systemPath + "trustedUUIDs"));
for (int i = 0; i < trustedJson.size(); i++) {
JSONObject object = (JSONObject) trustedJson.get(i);
trustedUUIDs.add(UUID.fromString((String) object.get("UUID")));
}
} catch (ParseException e) {
e.printStackTrace();
}
}
boolean isPublic = getConfig().getBoolean(systemPath + "public");
Location loc = Utils.convertStringToLocation(getConfig().getString(systemPath + "loc"));
playersSystems.add(new ESSystem(UUID.fromString(playerUUID), UUID.fromString(systemUUID), loc, drives, trustedUUIDs, isPublic));
}
allSystems.put(UUID.fromString(playerUUID), playersSystems);
}
return allSystems;
}
public static List<ESSystem> getPlayersSystems(UUID uuid) {
List<ESSystem> systems = new ArrayList<>();
for (String systemUUID : getConfig().getConfigurationSection("players." + uuid + ".systems").getKeys(false)) {

View File

@ -1,6 +1,5 @@
package net.seanomik.energeticstorage.utils;
import jdk.internal.jline.internal.Nullable;
import net.seanomik.energeticstorage.objects.ESSystem;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -9,6 +8,7 @@ import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import javax.annotation.Nullable;
import java.util.*;
public class Utils {

View File

@ -8,6 +8,10 @@ commands:
aliases: [egive]
description: Give a Energetic Storage item.
usage: /esgive
esreload:
aliases: [ereload]
description: Reload all Energetic Storage configs.
usage: /esreload
permissions:
energeticstorage.*:
description: All Energetic Storage permissions.

View File

@ -1,5 +1,5 @@
name: EnergeticStorage
version: 0.1-SNAPSHOT
version: 0.2-SNAPSHOT
main: net.seanomik.energeticstorage.EnergeticStorage
api-version: 1.15
authors: [SeanOMik]
@ -8,6 +8,10 @@ commands:
aliases: [egive]
description: Give a Energetic Storage item.
usage: /esgive
esreload:
aliases: [ereload]
description: Reload all Energetic Storage configs.
usage: /esreload
permissions:
energeticstorage.*:
description: All Energetic Storage permissions.

View File

@ -1,5 +1,5 @@
#Generated by Maven
#Tue Apr 07 00:41:39 CDT 2020
#Mon Jul 06 23:29:34 CDT 2020
groupId=net.seanomik
artifactId=energeticstorage
version=0.1-SNAPSHOT
version=0.2-SNAPSHOT

View File

@ -1,6 +1,7 @@
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\gui\ESTerminalGUI.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\ItemRecipies.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\commands\ESGiveCommand.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\commands\ESReloadCommand.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\Utils.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\gui\ESSystemSecurityGUI.java
D:\Code\java\spigotPlugins\myPlugins\EnergeticStorage\src\main\java\net\seanomik\energeticstorage\utils\ItemConstructor.java