Skip to content

Commit 0f649ec

Browse files
committed
Handle more null cases better
1 parent db66afd commit 0f649ec

File tree

3 files changed

+20
-43
lines changed

3 files changed

+20
-43
lines changed

Diff for: README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@ If you would like to contribute to the repo, you are welcome to open a pull requ
77
Reasons you may open a pull request include but are not limited to:
88
- Adding data that is not already included
99
- Adding functionality that is not already present
10-
- Fixing an error or failure to meet the design methodology (defined below)
10+
- Fixing an error
1111
- Rewriting code to deduplicate repetition or improve performance
12-
13-
## Design Methodology
14-
- If data can return as null or empty such as an empty string, the JSON element should under most circumstances be set to a null value. Arrays are exempted from this and should be returned as an empty array.
15-
- JSON patterns should be repeated throughout responses. Objects should be in the same order if they are shared between any responses, and keys that represent similar or same things such as mayor and king should be in the same or similar spot. This is to keep working with different response types consistent and easily understandable for developers.

Diff for: src/main/java/net/earthmc/emcapi/endpoint/TownsEndpoint.java

+13-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import net.earthmc.quarters.api.QuartersAPI;
1313
import net.earthmc.quarters.object.Quarter;
1414

15-
import java.util.Collection;
1615
import java.util.List;
1716

1817
public class TownsEndpoint {
@@ -67,43 +66,34 @@ public static JsonObject getTownObject(Town town) {
6766
JsonObject coordinatesObject = EndpointUtils.getCoordinatesObject(town.getSpawnOrNull());
6867
JsonArray homeBlockArray = new JsonArray();
6968
TownBlock homeBlock = town.getHomeBlockOrNull();
70-
if (homeBlock != null) {
71-
homeBlockArray.add(homeBlock.getX());
72-
homeBlockArray.add(homeBlock.getZ());
73-
coordinatesObject.add("homeBlock", homeBlockArray);
74-
} else {
75-
coordinatesObject.add("homeBlock", null);
76-
}
69+
homeBlockArray.add(homeBlock == null ? null : homeBlock.getX());
70+
homeBlockArray.add(homeBlock == null ? null : homeBlock.getZ());
71+
coordinatesObject.add("homeBlock", homeBlockArray);
7772

7873
JsonArray townBlocksArray = new JsonArray();
79-
Collection<TownBlock> townBlocks = town.getTownBlocks();
80-
if (!townBlocks.isEmpty()) {
81-
for (TownBlock townBlock : town.getTownBlocks()) {
82-
JsonArray townBlockArray = new JsonArray();
83-
townBlockArray.add(townBlock.getX());
84-
townBlockArray.add(townBlock.getZ());
85-
86-
townBlocksArray.add(townBlockArray);
87-
}
74+
for (TownBlock townBlock : town.getTownBlocks()) {
75+
JsonArray townBlockArray = new JsonArray();
76+
townBlockArray.add(townBlock.getX());
77+
townBlockArray.add(townBlock.getZ());
8878

89-
coordinatesObject.add("townBlocks", townBlocksArray);
90-
} else {
91-
coordinatesObject.add("townBlocks", null);
79+
townBlocksArray.add(townBlockArray);
9280
}
81+
coordinatesObject.add("townBlocks", townBlocksArray);
82+
9383
townObject.add("coordinates", coordinatesObject);
9484

9585
townObject.add("residents", EndpointUtils.getResidentArray(town.getResidents()));
9686
townObject.add("trusted", EndpointUtils.getResidentArray(town.getTrustedResidents().stream().toList()));
9787
townObject.add("outlaws", EndpointUtils.getResidentArray(town.getTrustedResidents().stream().toList()));
9888

99-
List<Quarter> quartersList = QuartersAPI.getInstance().getQuartersTown(town).getQuarters();
89+
List<Quarter> quartersList = QuartersAPI.getInstance().getQuartersTown(town).getQuarters(); // TODO: make this fucking API method not return null
90+
JsonArray quartersArray = new JsonArray();
10091
if (quartersList != null) {
101-
JsonArray quartersArray = new JsonArray();
10292
for (Quarter quarter : quartersList) {
10393
quartersArray.add(quarter.getUUID().toString());
10494
}
105-
townObject.add("quarters", quartersArray);
10695
}
96+
townObject.add("quarters", quartersArray);
10797

10898
JsonObject ranksObject = new JsonObject();
10999
for (String rank : TownyPerms.getTownRanks()) {

Diff for: src/main/java/net/earthmc/emcapi/util/EndpointUtils.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,17 @@ public static JsonArray getResidentArray(List<Resident> residents) {
184184
JsonArray jsonArray = new JsonArray();
185185

186186
for (Resident resident : residents) {
187-
if (resident == null) continue;
188187
jsonArray.add(getResidentJsonObject(resident));
189188
}
190189

191190
return jsonArray;
192191
}
193192

194193
public static JsonObject getResidentJsonObject(Resident resident) {
195-
if (resident == null) return null;
196-
197194
JsonObject jsonObject = new JsonObject();
198195

199-
jsonObject.addProperty("name", resident.getName());
200-
jsonObject.addProperty("uuid", resident.getUUID().toString());
196+
jsonObject.addProperty("name", resident == null ? null : resident.getName());
197+
jsonObject.addProperty("uuid", resident == null ? null : resident.getUUID().toString());
201198

202199
return jsonObject;
203200
}
@@ -206,20 +203,17 @@ public static JsonArray getTownArray(List<Town> towns) {
206203
JsonArray jsonArray = new JsonArray();
207204

208205
for (Town town : towns) {
209-
if (town == null) continue;
210206
jsonArray.add(getTownJsonObject(town));
211207
}
212208

213209
return jsonArray;
214210
}
215211

216212
public static JsonObject getTownJsonObject(Town town) {
217-
if (town == null) return null;
218-
219213
JsonObject jsonObject = new JsonObject();
220214

221-
jsonObject.addProperty("name", town.getName());
222-
jsonObject.addProperty("uuid", town.getUUID().toString());
215+
jsonObject.addProperty("name", town == null ? null : town.getName());
216+
jsonObject.addProperty("uuid", town == null ? null : town.getUUID().toString());
223217

224218
return jsonObject;
225219
}
@@ -228,20 +222,17 @@ public static JsonArray getNationArray(List<Nation> nations) {
228222
JsonArray jsonArray = new JsonArray();
229223

230224
for (Nation nation : nations) {
231-
if (nation == null) continue;
232225
jsonArray.add(getNationJsonObject(nation));
233226
}
234227

235228
return jsonArray;
236229
}
237230

238231
public static JsonObject getNationJsonObject(Nation nation) {
239-
if (nation == null) return null;
240-
241232
JsonObject jsonObject = new JsonObject();
242233

243-
jsonObject.addProperty("name", nation.getName());
244-
jsonObject.addProperty("uuid", nation.getUUID().toString());
234+
jsonObject.addProperty("name", nation == null ? null : nation.getName());
235+
jsonObject.addProperty("uuid", nation == null ? null : nation.getUUID().toString());
245236

246237
return jsonObject;
247238
}

0 commit comments

Comments
 (0)