Skip to content

Commit

Permalink
Add nullable option for NewPlayerCountType.STAY_AT
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberedCake committed Jul 10, 2022
1 parent ac1f42b commit 6a9a882
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.cybercake.cyberapi.common.basic.NumberUtils;
import net.md_5.bungee.api.ProxyServer;

import javax.annotation.Nullable;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -135,13 +136,14 @@ public void resetMaxPlayers() {
* @param newCurrentPlayers the new current players that show up on the multiplayer menu
* @since 28
*/
public void setOnlinePlayerCount(NewPlayerCountType newPlayerCountType, Object newCurrentPlayers) {
public void setOnlinePlayerCount(NewPlayerCountType newPlayerCountType, @Nullable Object newCurrentPlayers) {
validateSetCurrentPlayerCountArgs(newPlayerCountType, newCurrentPlayers);
this.newPlayerCountType = newPlayerCountType;
this.newCurrentPlayers = switch(newPlayerCountType) {
case CONSTANT, KEEP_SAME, STAY_AT -> Integer.parseInt(String.valueOf(newCurrentPlayers));
case CONSTANT, STAY_AT -> Integer.parseInt(String.valueOf(newCurrentPlayers));
case PERCENT -> Double.parseDouble(String.valueOf(newCurrentPlayers));
case RANDOM_BETWEEN -> String.valueOf(newCurrentPlayers);
case KEEP_SAME -> -1;
};
}

Expand Down Expand Up @@ -226,6 +228,8 @@ public boolean shouldShowPlayers() {
* @since 28
*/
private void validateSetCurrentPlayerCountArgs(NewPlayerCountType newPlayerCountType, Object newCurrentPlayers) {
if(newPlayerCountType == NewPlayerCountType.KEEP_SAME) return;

if(newPlayerCountType.getType() != null && !(newCurrentPlayers.getClass() == newPlayerCountType.getType()))
throw new IllegalArgumentException("newCurrentPlayers must be a " + newPlayerCountType.getType().getCanonicalName() + " (currently " + newCurrentPlayers.getClass().getCanonicalName() + ") because newPlayerCountType is set to " + newPlayerCountType.name() + "!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.cybercake.cyberapi.spigot.server.ServerProperties;
import net.cybercake.cyberapi.spigot.server.serverlist.ServerListInfo;
import net.cybercake.cyberapi.spigot.server.serverlist.players.NewPlayerCountType;
import org.jetbrains.annotations.Nullable;

import java.math.RoundingMode;
import java.util.ArrayList;
Expand Down Expand Up @@ -135,13 +136,14 @@ public void resetMaxPlayers() {
* @param newCurrentPlayers the new current players that show up on the multiplayer menu
* @since 9
*/
public void setOnlinePlayerCount(NewPlayerCountType newPlayerCountType, Object newCurrentPlayers) {
public void setOnlinePlayerCount(NewPlayerCountType newPlayerCountType, @Nullable Object newCurrentPlayers) {
validateSetCurrentPlayerCountArgs(newPlayerCountType, newCurrentPlayers);
this.newPlayerCountType = newPlayerCountType;
this.newCurrentPlayers = switch(newPlayerCountType) {
case CONSTANT, KEEP_SAME, STAY_AT -> Integer.parseInt(String.valueOf(newCurrentPlayers));
case CONSTANT, STAY_AT -> Integer.parseInt(String.valueOf(newCurrentPlayers));
case PERCENT -> Double.parseDouble(String.valueOf(newCurrentPlayers));
case RANDOM_BETWEEN -> String.valueOf(newCurrentPlayers);
case KEEP_SAME -> -1;
};
}

Expand Down Expand Up @@ -226,6 +228,8 @@ public boolean shouldShowPlayers() {
* @since 9
*/
private void validateSetCurrentPlayerCountArgs(NewPlayerCountType newPlayerCountType, Object newCurrentPlayers) {
if(newPlayerCountType == NewPlayerCountType.KEEP_SAME) return;

if(newPlayerCountType.getType() != null && !(newCurrentPlayers.getClass() == newPlayerCountType.getType()))
throw new IllegalArgumentException("newCurrentPlayers must be a " + newPlayerCountType.getType().getCanonicalName() + " (currently " + newCurrentPlayers.getClass().getCanonicalName() + ") because newPlayerCountType is set to " + newPlayerCountType.name() + "!");

Expand Down

0 comments on commit 6a9a882

Please sign in to comment.