22
33import io .papermc .paper .ban .BanListType ;
44import org .bukkit .OfflinePlayer ;
5- import org .bukkit .entity .Player ;
6- import org .bukkit .metadata .MetadataValue ;
75import org .jetbrains .annotations .NotNull ;
86import org .jetbrains .annotations .Nullable ;
97
10- import java .sql .Connection ;
118import java .sql .PreparedStatement ;
129import java .sql .ResultSet ;
1310import java .sql .SQLException ;
2320public final class Member {
2421 public final @ NotNull UUID uuid ;
2522 public @ Nullable String nationID ;
23+ @ SuppressWarnings ("CanBeFinal" )
2624 public boolean staff ;
2725 public final @ Nullable UUID altOwnerUUID ;
2826 public final @ NotNull Date added ;
@@ -39,25 +37,14 @@ public Member(final @NotNull OfflinePlayer player, final @Nullable Member altOwn
3937 this (player .getUniqueId (), null , false , altOwner == null ? null : altOwner .uuid , new Date ());
4038 }
4139
42- private Member (final @ NotNull ResultSet rs ) throws @ NotNull SQLException {
40+ private Member (final @ NotNull ResultSet rs ) throws SQLException {
4341 this (UUID .fromString (rs .getString ("uuid" )), rs .getString ("nation" ), rs .getBoolean ("staff" ), rs .getString ("alt_owner" ) == null ? null : UUID .fromString (rs .getString ("alt_owner" )), rs .getTimestamp ("added" ));
4442 }
4543
4644 public @ NotNull OfflinePlayer player () {
4745 return SMPCore .getInstance ().getServer ().getOfflinePlayer (uuid );
4846 }
4947
50- /**
51- * Check if player is online and not vanished
52- */
53- public boolean onlineNotVanished () {
54- final @ NotNull Optional <@ NotNull Player > player = Optional .ofNullable (player ().getPlayer ());
55- if (player .isEmpty ()) return false ;
56- for (final @ NotNull MetadataValue meta : player .get ().getMetadata ("vanished" ))
57- if (meta .asBoolean ()) return false ;
58- return player .get ().isOnline ();
59- }
60-
6148 public boolean isActive () {
6249 return new Date ().getTime () - player ().getLastSeen () < (long ) SMPCore .config ().membersInactiveDays () * 24 * 60 * 60 * 1000 ;
6350 }
@@ -74,19 +61,14 @@ public boolean isAlt() {
7461 return nationID == null ? Optional .empty () : Nation .get (nationID );
7562 }
7663
77- public @ NotNull Token createToken () throws @ NotNull SQLException {
78- return Token .create (this );
79- }
80-
81- public @ NotNull HashSet <@ NotNull Token > tokens () {
64+ public @ NotNull Set <@ NotNull Token > tokens () {
8265 return Token .get (this );
8366 }
8467
85- public @ NotNull HashSet <@ NotNull Member > getAlts () {
86- final @ NotNull HashSet <@ NotNull Member > alts = new HashSet <>();
68+ public @ NotNull Set <@ NotNull Member > getAlts () {
69+ final @ NotNull Set <@ NotNull Member > alts = new HashSet <>();
8770 try (
88- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
89- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT * FROM `members` WHERE `alt_owner` = ?" )
71+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT * FROM `members` WHERE `alt_owner` = ?" )
9072 ) {
9173 stmt .setString (1 , uuid .toString ());
9274 final @ NotNull ResultSet rs = stmt .executeQuery ();
@@ -105,8 +87,7 @@ public void unban() {
10587
10688 public void save () {
10789 try (
108- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
109- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)" )
90+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("INSERT OR REPLACE INTO `members` (`uuid`, `nation`, `staff`, `alt_owner`, `added`) VALUES (?, ?, ?, ?, ?)" )
11091 ) {
11192 stmt .setString (1 , uuid .toString ());
11293 stmt .setString (2 , nationID == null ? null : nationID );
@@ -125,8 +106,7 @@ public void save() {
125106 */
126107 private void remove () {
127108 try (
128- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
129- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("DELETE FROM `members` WHERE `uuid` = ?" )
109+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("DELETE FROM `members` WHERE `uuid` = ?" )
130110 ) {
131111 stmt .setString (1 , uuid .toString ());
132112 stmt .executeUpdate ();
@@ -148,6 +128,7 @@ private void remove() {
148128 *
149129 * @return whether the member was deleted
150130 */
131+ @ SuppressWarnings ("BooleanMethodIsAlwaysInverted" )
151132 public boolean delete () {
152133 if (!getAlts ().isEmpty ()) return false ;
153134 final @ NotNull OfflinePlayer player = player ();
@@ -166,21 +147,13 @@ public boolean delete() {
166147 return true ;
167148 }
168149
169- public static @ NotNull Member create (final @ NotNull OfflinePlayer player , final @ Nullable Member altOwner ) {
170- final @ NotNull Member member = new Member (player .getUniqueId (), null , false , altOwner == null ? null : altOwner .uuid , new Date ());
171- member .save ();
172- member .player ().setWhitelisted (true );
173- return member ;
174- }
175-
176150 public static @ NotNull Optional <@ NotNull Member > get (final @ NotNull OfflinePlayer player ) {
177151 return get (player .getUniqueId ());
178152 }
179153
180154 public static @ NotNull Optional <@ NotNull Member > get (final @ NotNull UUID uuid ) {
181155 try (
182- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
183- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1" )
156+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT * FROM `members` WHERE `uuid` = ? LIMIT 1" )
184157 ) {
185158 stmt .setString (1 , uuid .toString ());
186159 final @ NotNull ResultSet rs = stmt .executeQuery ();
@@ -193,11 +166,10 @@ public boolean delete() {
193166 }
194167 }
195168
196- public static @ NotNull HashSet <@ NotNull Member > get () {
197- final @ NotNull HashSet <@ NotNull Member > members = new HashSet <>();
169+ public static @ NotNull Set <@ NotNull Member > get () {
170+ final @ NotNull Set <@ NotNull Member > members = new HashSet <>();
198171 try (
199- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
200- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT * FROM `members`" )
172+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT * FROM `members`" )
201173 ) {
202174 final @ NotNull ResultSet rs = stmt .executeQuery ();
203175 while (rs .next ()) members .add (new Member (rs ));
@@ -208,12 +180,11 @@ public boolean delete() {
208180 return members ;
209181 }
210182
211- public static @ NotNull HashSet <@ NotNull Member > get (int limit , int page ) {
183+ public static @ NotNull Set <@ NotNull Member > get (int limit , int page ) {
212184 final int offset = (page - 1 ) * limit ;
213- final @ NotNull HashSet <@ NotNull Member > members = new HashSet <>();
185+ final @ NotNull Set <@ NotNull Member > members = new HashSet <>();
214186 try (
215- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
216- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT * FROM `members` LIMIT ? OFFSET ?" )
187+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT * FROM `members` LIMIT ? OFFSET ?" )
217188 ) {
218189 stmt .setInt (1 , limit );
219190 stmt .setInt (2 , offset );
@@ -226,11 +197,10 @@ public boolean delete() {
226197 return members ;
227198 }
228199
229- public static @ NotNull HashSet <@ NotNull Member > get (final @ NotNull Nation nation ) {
230- final @ NotNull HashSet <@ NotNull Member > members = new HashSet <>();
200+ public static @ NotNull Set <@ NotNull Member > get (final @ NotNull Nation nation ) {
201+ final @ NotNull Set <@ NotNull Member > members = new HashSet <>();
231202 try (
232- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
233- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT * FROM `members` WHERE `nation` = ?" )
203+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT * FROM `members` WHERE `nation` = ?" )
234204 ) {
235205 stmt .setString (1 , nation .id );
236206 final @ NotNull ResultSet rs = stmt .executeQuery ();
@@ -244,8 +214,7 @@ public boolean delete() {
244214
245215 public static int count () {
246216 try (
247- final @ NotNull Connection conn = SMPCore .getInstance ().db ()
248- .getConnection (); final @ NotNull PreparedStatement stmt = conn .prepareStatement ("SELECT COUNT(*) as `n` FROM `members`" )
217+ final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement ("SELECT COUNT(*) as `n` FROM `members`" )
249218 ) {
250219 final @ NotNull ResultSet rs = stmt .executeQuery ();
251220 rs .next ();
@@ -257,10 +226,12 @@ public static int count() {
257226 }
258227 }
259228
229+ @ SuppressWarnings ("NullableProblems" )
260230 public static @ NotNull Set <@ NotNull String > getNames () {
261231 return get ().stream ().map (m -> m .player ().getName ()).filter (Objects ::nonNull ).collect (Collectors .toSet ());
262232 }
263233
234+ @ SuppressWarnings ("NullableProblems" )
264235 public static @ NotNull Set <@ NotNull String > getAltNames () {
265236 return get ().stream ().filter (Member ::isAlt ).map (m -> m .player ().getName ()).filter (Objects ::nonNull ).collect (Collectors .toSet ());
266237 }
0 commit comments