11package com .hiddentech .playerstorage ;
22
33import com .hiddentech .playerstorage .types .PlayerData ;
4+ import com .mongodb .client .model .Filters ;
5+ import org .bson .Document ;
46import org .bukkit .Bukkit ;
57import org .bukkit .ChatColor ;
68import org .bukkit .command .Command ;
1517
1618public class GetDataCommand implements CommandExecutor {
1719 private PlayerStorage plugin ;
18- public GetDataCommand (PlayerStorage plugin ){
20+
21+ public GetDataCommand (PlayerStorage plugin ) {
1922 this .plugin = plugin ;
2023 plugin .getCommand ("playerdata" ).setExecutor (this );
2124 }
25+
2226 @ Override
2327 public boolean onCommand (@ NotNull CommandSender sender , @ NotNull Command command , @ NotNull String label , @ NotNull String [] args ) {
24- if (!sender .isOp ()){
28+ if (!sender .isOp ()) {
2529 sender .sendMessage ("No perms" );
26- return true ;}
27- if (args .length <1 ){
30+ return true ;
31+ }
32+ if (args .length < 1 ) {
2833 sender .sendMessage ("specify player" );
29- return true ;}
30- sender .sendMessage ("Retrieving data for " +args [0 ]);
31- Player player =Bukkit .getPlayer (args [0 ]);
34+ return true ;
35+ }
36+ sender .sendMessage ("Retrieving data for " + args [0 ]);
37+ Player player = Bukkit .getPlayer (args [0 ]);
3238 if (player == null ) {
3339 sender .sendMessage ("They've never played!" );
3440 return false ;
@@ -37,37 +43,72 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3743 sender .sendMessage ("Cached :" );
3844 if (plugin .getRegistry ().getPlayers ().containsKey (player .getUniqueId ())) {
3945 PlayerData data = plugin .getRegistry ().getPlayers ().get (player .getUniqueId ());
40- for (String key : data .getStrings ().keySet ()) {
41- sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getStrings ().get (key ));
42- }
43- for (String key : data .getBooleans ().keySet ()) {
44- sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getBooleans ().get (key ));
45- }
46- for (String key : data .getInts ().keySet ()) {
47- sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getInts ().get (key ));
46+ if (data .getBooleans ().isEmpty () && data .getInts ().isEmpty () && data .getStrings ().isEmpty ()) {
47+ sender .sendMessage ("No data cached!" );
48+ } else {
49+ for (String key : data .getStrings ().keySet ()) {
50+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getStrings ().get (key ));
51+ }
52+ for (String key : data .getBooleans ().keySet ()) {
53+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getBooleans ().get (key ));
54+ }
55+ for (String key : data .getInts ().keySet ()) {
56+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + data .getInts ().get (key ));
57+ }
4858 }
4959 } else {
5060 sender .sendMessage ("No cached data found!" );
5161 }
52- sender .sendMessage ("In cloud :" );
5362 new BukkitRunnable () {
5463 @ Override
5564 public void run () {
65+
66+ if (plugin .redisEnabled ) {
67+ sender .sendMessage ("In Redis :" );
68+ try {
69+ if (!plugin .getRegistry ().getPlayers ().containsKey (player .getUniqueId ())) {
70+ sender .sendMessage ("No data found!" );
71+ return ;
72+ }
73+ Jedis jedis = plugin .getPool ().getResource ();
74+ if (!jedis .exists (player .getUniqueId ().toString ())) {
75+ sender .sendMessage ("No data stored in redis!" );
76+ } else {
77+ Map <String , String > strings = jedis .hgetAll (player .getUniqueId ().toString ());
78+ for (String key : strings .keySet ()) {
79+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + strings .get (key ));
80+ }
81+ }
82+ jedis .close ();
83+ } catch (Exception e ) {
84+ e .printStackTrace ();
85+ }
86+ }
87+ if (!plugin .mongoEnabled ) return ;
88+ sender .sendMessage ("In MongoDB :" );
5689 try {
57- if (!plugin .getRegistry ().getPlayers ().containsKey (player .getUniqueId ())){
90+ if (!plugin .getRegistry ().getPlayers ().containsKey (player .getUniqueId ())) {
5891 sender .sendMessage ("No data found!" );
5992 return ;
6093 }
61- Jedis jedis = plugin .getPool ().getResource ();
62- if (! jedis . exists ( player . getUniqueId (). toString ())) {
63- sender .sendMessage ("No data stored in cloud !" );
94+ Document document = plugin .getMongo ().getCollection (). find ( Filters . eq ( "uuid" , player . getUniqueId (). toString ())). first ();
95+ if ( document == null ) {
96+ sender .sendMessage ("No data stored in mongo !" );
6497 return ;
6598 }
66- Map <String , String > strings = jedis .hgetAll (player .getUniqueId ().toString ());
67- for (String key : strings .keySet ()){
68- sender .sendMessage (ChatColor .AQUA +"" +key +ChatColor .DARK_BLUE +" : " +ChatColor .BLUE +strings .get (key ));
99+ Map <String , String > storedStrings = (Map <String , String >) document .get ("strings" );
100+ Map <String , Boolean > storedBooleans = (Map <String , Boolean >) document .get ("bools" );
101+ Map <String , Integer > storedInts = (Map <String , Integer >) document .get ("ints" );
102+
103+ for (String key : storedStrings .keySet ()) {
104+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + storedStrings .get (key ));
105+ }
106+ for (String key : storedBooleans .keySet ()) {
107+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + storedBooleans .get (key ));
108+ }
109+ for (String key : storedInts .keySet ()) {
110+ sender .sendMessage (ChatColor .AQUA + "" + key + ChatColor .DARK_BLUE + " : " + ChatColor .BLUE + storedInts .get (key ));
69111 }
70- jedis .close ();
71112 } catch (Exception e ) {
72113 e .printStackTrace ();
73114 }
0 commit comments