Skip to content

Commit b675cb7

Browse files
committed
Check if sharables are null
1 parent a23903d commit b675cb7

2 files changed

Lines changed: 38 additions & 27 deletions

File tree

src/main/java/org/mvplugins/multiverse/inventories/profile/InventoryDataProvider.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dumptruckman.minecraft.util.Logging;
44
import org.bukkit.Bukkit;
5+
import org.bukkit.ChatColor;
56
import org.bukkit.Location;
67
import org.bukkit.OfflinePlayer;
78
import org.bukkit.attribute.Attribute;
@@ -67,12 +68,12 @@ public static class PlayerInventoryData {
6768
public final ProfileType profileTypeUsed;
6869

6970
// Non-inventory data
70-
public final double health;
71-
public final double maxHealth;
72-
public final int level;
73-
public final float exp;
74-
public final int foodLevel;
75-
public final float saturation;
71+
public final Double health;
72+
public final Double maxHealth;
73+
public final Integer level;
74+
public final Float exp;
75+
public final Integer foodLevel;
76+
public final Float saturation;
7677
public final String lastLocation;
7778

7879
/**
@@ -94,8 +95,8 @@ public static class PlayerInventoryData {
9495
*/
9596
@ApiStatus.AvailableSince("5.2")
9697
public PlayerInventoryData(ItemStack[] contents, ItemStack[] armor, ItemStack offHand, InventoryStatus status,
97-
ProfileType profileTypeUsed, double health, double maxHealth, int level, float exp, int foodLevel,
98-
float saturation, String lastLocation) {
98+
ProfileType profileTypeUsed, Double health, Double maxHealth, Integer level, Float exp, Integer foodLevel,
99+
Float saturation, String lastLocation) {
99100
this.contents = contents;
100101
this.armor = armor;
101102
this.offHand = offHand;
@@ -198,17 +199,17 @@ private CompletableFuture<PlayerInventoryData> loadInventoryDataFromProfileStora
198199
ItemStack offHand = SingleShareReader.of(inventories, targetPlayer, worldName, profileTypeToUse, Sharables.OFF_HAND).read().join();
199200

200201
// Non-inventory data
201-
double storedHealth = getSharableValue(Sharables.HEALTH, targetPlayer, worldName, profileTypeToUse, 20.0);
202-
double storedMaxHealth = getSharableValue(Sharables.MAX_HEALTH, targetPlayer, worldName, profileTypeToUse, 20.0);
203-
int storedLevel = getSharableValue(Sharables.LEVEL, targetPlayer, worldName, profileTypeToUse, 0);
204-
float storedExp = getSharableValue(Sharables.EXPERIENCE, targetPlayer, worldName, profileTypeToUse, 0.0f);
205-
int storedFoodLevel = getSharableValue(Sharables.FOOD_LEVEL, targetPlayer, worldName, profileTypeToUse, 20);
206-
float storedSaturationLevel = getSharableValue(Sharables.SATURATION, targetPlayer, worldName, profileTypeToUse, 5.0f);
202+
Double storedHealth = getSharableValue(Sharables.HEALTH, targetPlayer, worldName, profileTypeToUse);
203+
Double storedMaxHealth = getSharableValue(Sharables.MAX_HEALTH, targetPlayer, worldName, profileTypeToUse);
204+
Integer storedLevel = getSharableValue(Sharables.LEVEL, targetPlayer, worldName, profileTypeToUse);
205+
Float storedExp = getSharableValue(Sharables.EXPERIENCE, targetPlayer, worldName, profileTypeToUse);
206+
Integer storedFoodLevel = getSharableValue(Sharables.FOOD_LEVEL, targetPlayer, worldName, profileTypeToUse);
207+
Float storedSaturationLevel = getSharableValue(Sharables.SATURATION, targetPlayer, worldName, profileTypeToUse);
207208
String storedLastLocation;
208209

209210
// Check if LAST_LOCATION is enabled in config
210211
if (!inventoriesConfig.getActiveOptionalShares().contains(Sharables.LAST_LOCATION)) {
211-
storedLastLocation = "Disabled in config";
212+
storedLastLocation = ChatColor.RED + "Disabled in Config";
212213
} else { // if the location is null or the world is null
213214
Location storedLocationObject = SingleShareReader.of(inventories, targetPlayer, worldName, profileTypeToUse, Sharables.LAST_LOCATION).read().join();
214215
if (storedLocationObject == null || storedLocationObject.getWorld() == null) {
@@ -265,21 +266,19 @@ private PlayerProfile loadMVInvPlayerProfile(ProfileContainer container, Offline
265266
* @param targetPlayer The OfflinePlayer.
266267
* @param worldName The world name.
267268
* @param profileType The profile type.
268-
* @param defaultValue The default value to return if sharable is disabled or data is null.
269269
* @param <T> The type of the sharable value.
270270
* @return The sharable value or the default value.
271271
*/
272272
private <T> T getSharableValue(@NotNull Sharable<T> sharable,
273273
@NotNull OfflinePlayer targetPlayer,
274274
@NotNull String worldName,
275-
@NotNull ProfileType profileType,
276-
@NotNull T defaultValue) {
275+
@NotNull ProfileType profileType) {
277276
try {
278277
T value = SingleShareReader.of(inventories, targetPlayer, worldName, profileType, sharable).read().join();
279-
return value != null ? value : defaultValue;
278+
return value;
280279
} catch (CompletionException e) {
281280
Logging.warning("Failed to read sharable '" + sharable.getNames()[0] + "' for player " + targetPlayer.getName() + " in world " + worldName + ": " + e.getCause().getMessage());
282-
return defaultValue;
281+
return null;
283282
}
284283
}
285284

src/main/java/org/mvplugins/multiverse/inventories/view/InventoryGUIHelper.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.persistence.PersistentDataType;
1111
import org.jetbrains.annotations.ApiStatus;
1212
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1314
import org.jvnet.hk2.annotations.Service;
1415
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
1516
import org.mvplugins.multiverse.inventories.MultiverseInventories;
@@ -181,22 +182,33 @@ private ItemStack createDisplayItem(Material material, String name, List<String>
181182
return item;
182183
}
183184

184-
private ItemStack createHealthDisplayItem(double health, double maxHealth) {
185+
private ItemStack createHealthDisplayItem(@Nullable Double health, @Nullable Double maxHealth) {
185186
List<String> lore = new ArrayList<>();
186-
DecimalFormat df = new DecimalFormat("0.0");
187-
lore.add(ChatColor.WHITE + "Current: " + ChatColor.RED + df.format(health) + ChatColor.WHITE + " / " + ChatColor.RED + df.format(maxHealth));
187+
if (health == null) {
188+
lore.add(ChatColor.RED + "N/A (No Data)");
189+
} else {
190+
DecimalFormat df = new DecimalFormat("0.0");
191+
lore.add(ChatColor.WHITE + "Current: " + ChatColor.RED + df.format(health) + ChatColor.WHITE + " / " + ChatColor.RED + df.format(maxHealth));
192+
}
188193
return createDisplayItem(Material.RED_DYE, "Health", lore);
189194
}
190195

191-
private ItemStack createLevelDisplayItem(int level, float exp) {
196+
private ItemStack createLevelDisplayItem(@Nullable Integer level, @Nullable Float exp) {
192197
List<String> lore = new ArrayList<>();
193-
lore.add(ChatColor.WHITE + "Level: " + ChatColor.GREEN + level);
194-
lore.add(ChatColor.WHITE + "Progress: " + ChatColor.AQUA + String.format("%.1f%%", exp * 100));
198+
if (level == null) {
199+
lore.add(ChatColor.RED + "N/A (No Data)");
200+
} else {
201+
lore.add(ChatColor.WHITE + "Level: " + ChatColor.GREEN + level);
202+
lore.add(ChatColor.WHITE + "Progress: " + ChatColor.AQUA + String.format("%.1f%%", exp * 100));
203+
}
195204
return createDisplayItem(Material.EXPERIENCE_BOTTLE, "Experience", lore);
196205
}
197206

198-
private ItemStack createFoodDisplayItem(int foodLevel, float saturation) {
207+
private ItemStack createFoodDisplayItem(@Nullable Integer foodLevel, @Nullable Float saturation) {
199208
List<String> lore = new ArrayList<>();
209+
if (foodLevel == null) {
210+
lore.add(ChatColor.RED + "N/A (No Data)");
211+
}
200212
lore.add(ChatColor.WHITE + "Food: " + ChatColor.GOLD + foodLevel + ChatColor.WHITE + " / " + ChatColor.GOLD + "20");
201213
lore.add(ChatColor.WHITE + "Saturation: " + ChatColor.LIGHT_PURPLE + String.format("%.1f", saturation));
202214
return createDisplayItem(Material.COOKED_BEEF, "Food & Saturation", lore);

0 commit comments

Comments
 (0)