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

Commit

Permalink
Fix rare NPE for gson.fromJson in PlayerHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Jul 15, 2023
1 parent 71313af commit 9cbd7fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,28 @@
import com.google.gson.Gson;
import io.leangen.geantyref.TypeToken;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;

public class PlayerHandler{

private final AdvancedServerList<?> core;
private final PluginLogger logger;
private final Path cache;
private List<CachedPlayer> cachedPlayers = new ArrayList<>();

private final Type listType = new TypeToken<ArrayList<CachedPlayer>>(){}.getType();
private final Gson gson = new Gson();

// UUID of MHF_Question
private final UUID defaultUUID = UUID.fromString("606e2ff0-ed77-4842-9d6c-e1d3321c7838");

private List<CachedPlayer> cachedPlayers = new ArrayList<>();
private CachedPlayer defaultPlayer = null;

public PlayerHandler(AdvancedServerList<?> core){
Expand All @@ -75,7 +73,7 @@ public void load(){
}

try{
Reader reader = Files.newBufferedReader(cache);
BufferedReader reader = Files.newBufferedReader(cache);

cachedPlayers = gson.fromJson(reader, listType);

Expand All @@ -85,6 +83,14 @@ public void load(){
return;
}

// In case Gson messes up... I guess.
if(cachedPlayers == null){
logger.warn("Couldn't load players from playercache.json file. Is the JSON valid?");
// Create new ArrayList instance to avoid further issues.
cachedPlayers = new ArrayList<>();
return;
}

logger.info("Loaded %d players into cache!", cachedPlayers.size());
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.commit/>

<plugin.version>3.2.0</plugin.version>
<plugin.version>3.2.1</plugin.version>
<plugin.description>Create multiple Server lists based on conditions.</plugin.description>

<api.version>v3.0.0</api.version>
Expand Down

0 comments on commit 9cbd7fb

Please sign in to comment.