Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from Andre601/fix/nullpointerexceptions
Browse files Browse the repository at this point in the history
Fix some NullPointerExceptions
  • Loading branch information
Andre601 authored Jan 9, 2023
2 parents f2737f2 + 0dccdaf commit 8f9ae4a
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void warn(String msg, Object... args){
public void warn(String msg, Throwable throwable){
logger.log(Level.WARNING, msg, throwable);
}

@Override
public void warn(String msg, Throwable throwable, Object... args){
logger.log(Level.WARNING, String.format(msg, args), throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ public void startUpdateChecker(){

public void disable(){
executor.shutdown();
client.dispatcher().executorService().shutdown();
try{
if(!executor.awaitTermination(1, TimeUnit.SECONDS)){
executor.shutdownNow();
if(!executor.awaitTermination(1, TimeUnit.SECONDS))
logger.warn("Scheduler didn't terminate in time!");
}

if(!client.dispatcher().executorService().awaitTermination(1, TimeUnit.SECONDS)){
client.dispatcher().executorService().shutdownNow();
if(!client.dispatcher().executorService().awaitTermination(1, TimeUnit.SECONDS))
logger.warn("OkHttp's Scheduler didn't terminate in time!");
}
}catch(InterruptedException ex){
executor.shutdownNow();
Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static <P, F> void handleEvent(GenericEventWrapper<P, F> event){

ProfileEntry entry = ProfileManager.merge(profile);

if(entry.isExtraPlayersEnabled() != null && entry.isExtraPlayersEnabled()){
if(entry.isExtraPlayersEnabled().getValue(false)){
max = online + (entry.getExtraPlayersCount() == null ? 0 : entry.getExtraPlayersCount());
event.setMaxPlayers(max);
}
Expand All @@ -83,7 +83,7 @@ public static <P, F> void handleEvent(GenericEventWrapper<P, F> event){
);
}

boolean hidePlayers = entry.isHidePlayersEnabled() != null && entry.isHidePlayersEnabled();
boolean hidePlayers = entry.isHidePlayersEnabled().getValue(false);

if(hidePlayers){
event.hidePlayers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public boolean reloadProfiles(){
if(tmp == null)
continue;

profiles.add(ServerListProfile.Builder.resolve(tmp, logger).build());
profiles.add(ServerListProfile.Builder.resolve(file.getName(), tmp, logger).build());
logger.info("Loaded " + file.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface PluginLogger{
void warn(String msg, Object... args);

void warn(String msg, Throwable throwable);

void warn(String msg, Throwable throwable, Object... args);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022 Andre_601
* Copyright (c) 2022-2023 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,7 +23,7 @@
*
*/

package ch.andre601.advancedserverlist.core.profiles.replacer;
package ch.andre601.advancedserverlist.core.objects;

import java.util.AbstractMap;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package ch.andre601.advancedserverlist.core.objects;

/*
* A simple class to allow having a nullable boolean without having to deal with possible NPEs
* when trying to get Boolean.getValue() while it is null...
*
* Here, it simply checks if the value is null and if it is, returns a default. Otherwise it gives
* the value.
*/
public class NullBool{

public static final NullBool FALSE = new NullBool(false);
public static final NullBool NULL = new NullBool(null);

private final Boolean value;

public NullBool(Boolean value){
this.value = value;
}

public boolean isNull(){
return value == null;
}

public boolean getValue(boolean def){
return isNull() ? def : value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public ProfileEntry getDefaultProfile(){
* ...doesn't have a favicon set.
*/
public boolean isInvalidProfile(){
if(profiles.isEmpty())
return defaultProfile.isInvalidProfile();

boolean profilesValid = false;

for(ProfileEntry profile : profiles){
Expand All @@ -109,6 +112,7 @@ public boolean isInvalidProfile(){

public static class Builder{

private final String fileName;
private final ConfigurationNode node;
private final int priority;
private final List<Expression> expressions = new ArrayList<>();
Expand All @@ -118,14 +122,15 @@ public static class Builder{
private List<ProfileEntry> profiles = new ArrayList<>();
private ProfileEntry defaultProfile = ProfileEntry.empty();

private Builder(ConfigurationNode node, PluginLogger logger){
private Builder(String fileName, ConfigurationNode node, PluginLogger logger){
this.fileName = fileName;
this.node = node;
this.priority = node.node("priority").getInt();
this.logger = logger;
}

public static Builder resolve(ConfigurationNode node, PluginLogger logger){
return new Builder(node, logger)
public static Builder resolve(String fileName, ConfigurationNode node, PluginLogger logger){
return new Builder(fileName, node, logger)
.resolveExpressions()
.resolveProfiles()
.resolveDefaultProfile();
Expand All @@ -136,6 +141,7 @@ private Builder resolveExpressions(){
try{
temp = node.node("conditions").getList(String.class);
}catch(SerializationException ex){
logger.warn("Encountered a SerializationException while resolving conditions for %s", ex, fileName);
return this;
}

Expand All @@ -159,15 +165,19 @@ private Builder resolveExpressions(){
private Builder resolveProfiles(){
try{
this.profiles = node.node("profiles").getList(ProfileEntry.class);
}catch(SerializationException ignored){}
}catch(SerializationException ex){
logger.warn("Encountered a SerializationException while resolving the profiles entry for %s", ex, fileName);
}

return this;
}

private Builder resolveDefaultProfile(){
try{
this.defaultProfile = node.get(ProfileEntry.class);
}catch(SerializationException ignored){}
}catch(SerializationException ex){
logger.warn("Encountered a SerializationException while resolving the global profile for %s", ex, fileName);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
package ch.andre601.advancedserverlist.core.profiles.players;

import ch.andre601.advancedserverlist.core.AdvancedServerList;
import ch.andre601.advancedserverlist.core.profiles.replacer.EntryList;
import ch.andre601.advancedserverlist.core.objects.EntryList;

import java.io.BufferedWriter;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package ch.andre601.advancedserverlist.core.profiles.profile;

import ch.andre601.advancedserverlist.core.objects.NullBool;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;

Expand All @@ -38,13 +39,13 @@ public class ProfileEntry{
private final List<String> players;
private final String playerCountText;
private final String favicon;
private final Boolean hidePlayersEnabled;
private final Boolean extraPlayersEnabled;
private final NullBool hidePlayersEnabled;
private final NullBool extraPlayersEnabled;
private final Integer extraPlayersCount;

public ProfileEntry(List<String> motd, List<String> players,
String playerCountText, String favicon, Boolean hidePlayersEnabled,
Boolean extraPlayersEnabled, Integer extraPlayersCount){
String playerCountText, String favicon, NullBool hidePlayersEnabled,
NullBool extraPlayersEnabled, Integer extraPlayersCount){
this.motd = motd;
this.players = players;
this.playerCountText = playerCountText;
Expand All @@ -56,7 +57,7 @@ public ProfileEntry(List<String> motd, List<String> players,

public static ProfileEntry empty(){
return new ProfileEntry(Collections.emptyList(), Collections.emptyList(), "", "",
false, false, 0);
NullBool.FALSE, NullBool.FALSE, 0);
}

public List<String> getMOTD(){
Expand All @@ -75,11 +76,11 @@ public String getFavicon(){
return favicon;
}

public Boolean isHidePlayersEnabled(){
public NullBool isHidePlayersEnabled(){
return hidePlayersEnabled;
}

public Boolean isExtraPlayersEnabled(){
public NullBool isExtraPlayersEnabled(){
return extraPlayersEnabled;
}

Expand All @@ -90,7 +91,7 @@ public Integer getExtraPlayersCount(){
public boolean isInvalidProfile(){
return getMOTD().isEmpty() &&
getPlayers().isEmpty() &&
(getPlayerCountText().isEmpty() && !isHidePlayersEnabled()) &&
(getPlayerCountText().isEmpty() && !isHidePlayersEnabled().getValue(false)) &&
getFavicon().isEmpty();
}

Expand All @@ -102,8 +103,8 @@ public static class Builder{
private List<String> players = new ArrayList<>();
private String playerCountText = null;
private String favicon = null;
private Boolean hidePlayersEnabled = false;
private Boolean extraPlayersEnabled = false;
private NullBool hidePlayersEnabled = NullBool.FALSE;
private NullBool extraPlayersEnabled = NullBool.FALSE;
private Integer extraPlayersCount = 0;

private Builder(ConfigurationNode node){
Expand Down Expand Up @@ -149,21 +150,21 @@ public Builder resolveFavicon(){

public Builder resolveHidePlayersEnabled(){
if(node.node("playerCount", "hidePlayers").virtual()){
this.hidePlayersEnabled = null;
this.hidePlayersEnabled = NullBool.NULL;
return this;
}
this.hidePlayersEnabled = node.node("playerCount", "hidePlayers").getBoolean();

this.hidePlayersEnabled = new NullBool(node.node("playerCount", "hidePlayers").getBoolean());
return this;
}

public Builder resolveExtraPlayersEnabled(){
if(node.node("playerCount", "extraPlayers", "enabled").virtual()){
this.extraPlayersEnabled = null;
this.extraPlayersEnabled = NullBool.NULL;
return this;
}

this.extraPlayersEnabled = node.node("playerCount", "extraPlayers", "enabled").getBoolean();
this.extraPlayersEnabled = new NullBool(node.node("playerCount", "extraPlayers", "enabled").getBoolean());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package ch.andre601.advancedserverlist.core.profiles.profile;

import ch.andre601.advancedserverlist.core.AdvancedServerList;
import ch.andre601.advancedserverlist.core.objects.NullBool;
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.Placeholders;

Expand Down Expand Up @@ -76,12 +77,12 @@ public static ProfileEntry merge(ServerListProfile profile){
List<String> players = resolvePlayers(entry, defEntry);
String playerCountText = resolvePlayerCountText(entry, defEntry);
String favicon = resolveFavicon(entry, defEntry);
Boolean isHidePlayersEnabled = resolveHidePlayersEnabled(entry, defEntry);
Boolean isExtraPlayersEnabled = resolveExtraPlayersEnabled(entry, defEntry);
boolean isHidePlayersEnabled = resolveHidePlayersEnabled(entry, defEntry);
boolean isExtraPlayersEnabled = resolveExtraPlayersEnabled(entry, defEntry);
Integer extraPlayersCount = resolveExtraPlayersCount(entry, defEntry);

return new ProfileEntry(motd, players, playerCountText, favicon, isHidePlayersEnabled, isExtraPlayersEnabled,
extraPlayersCount);
return new ProfileEntry(motd, players, playerCountText, favicon,
new NullBool(isHidePlayersEnabled), new NullBool(isExtraPlayersEnabled), extraPlayersCount);
}

private static List<String> resolveMOTD(ProfileEntry profile, ProfileEntry defaultProfile){
Expand Down Expand Up @@ -112,23 +113,23 @@ private static String resolveFavicon(ProfileEntry profile, ProfileEntry defaultP
return profile.getFavicon();
}

private static Boolean resolveHidePlayersEnabled(ProfileEntry profile, ProfileEntry defaultProfile){
if(profile == null || profile.isHidePlayersEnabled() == null)
return defaultProfile.isHidePlayersEnabled();
private static boolean resolveHidePlayersEnabled(ProfileEntry profile, ProfileEntry defaultProfile){
if(profile == null || profile.isHidePlayersEnabled().isNull())
return defaultProfile.isHidePlayersEnabled().getValue(false);

return profile.isHidePlayersEnabled();
return profile.isHidePlayersEnabled().getValue(false);
}

private static Boolean resolveExtraPlayersEnabled(ProfileEntry profile, ProfileEntry defaultProfile){
if(profile == null || profile.isExtraPlayersEnabled() == null)
return defaultProfile.isExtraPlayersEnabled();
private static boolean resolveExtraPlayersEnabled(ProfileEntry profile, ProfileEntry defaultProfile){
if(profile == null || profile.isExtraPlayersEnabled().isNull())
return defaultProfile.isExtraPlayersEnabled().getValue(false);

return profile.isExtraPlayersEnabled();
return profile.isExtraPlayersEnabled().getValue(false);
}

private static Integer resolveExtraPlayersCount(ProfileEntry profile, ProfileEntry defaultProfile){
if(profile == null || profile.getExtraPlayersCount() == null)
return defaultProfile.getExtraPlayersCount();
return defaultProfile.getExtraPlayersCount() == null ? 0 : defaultProfile.getExtraPlayersCount();

return profile.getExtraPlayersCount();
}
Expand Down
Loading

0 comments on commit 8f9ae4a

Please sign in to comment.