Skip to content

Commit b717656

Browse files
committed
Add log options to AnnoyingFile methods
1 parent 9b89896 commit b717656

1 file changed

Lines changed: 72 additions & 14 deletions

File tree

src/main/java/xyz/srnyx/annoyingapi/file/AnnoyingFile.java

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,34 @@ public Optional<PlayableSound> getPlayableSound(@NotNull String path) {
327327
*/
328328
@NotNull
329329
public Optional<PotionEffect> getPotionEffect(@NotNull String path) {
330+
return getPotionEffect(path, true);
331+
}
332+
333+
/**
334+
* Gets a {@link PotionEffect} from the path. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
335+
*
336+
* @param path the path to the node
337+
* @param log whether to log warnings if the potion effect is invalid
338+
*
339+
* @return the {@link PotionEffect} or empty if it's invalid
340+
*/
341+
@NotNull
342+
public Optional<PotionEffect> getPotionEffect(@NotNull String path, boolean log) {
330343
final Optional<PotionEffect> def = getDef(path);
331344
final ConfigurationSection section = getConfigurationSection(path);
332345
if (section == null) return def;
333346

334347
// Get type name
335348
final String typeString = section.getString("type");
336349
if (typeString == null) {
337-
log(Level.WARNING, path, "&cInvalid potion effect, missing type");
350+
if (log) log(Level.WARNING, path, "&cInvalid potion effect, missing type");
338351
return def;
339352
}
340353

341354
// Get type
342355
final Optional<PotionEffectType> typeOptional = RefRegistry.getEffect(typeString);
343356
if (!typeOptional.isPresent()) {
344-
log(Level.WARNING, path, "&cInvalid potion effect type: &4" + typeString);
357+
if (log) log(Level.WARNING, path, "&cInvalid potion effect type: &4" + typeString);
345358
return def;
346359
}
347360
final PotionEffectType type = typeOptional.get();
@@ -372,8 +385,23 @@ public Optional<PotionEffect> getPotionEffect(@NotNull String path) {
372385
*
373386
* @return the {@code AttributeModifier} or empty if it's invalid
374387
*/
375-
@NotNull @SuppressWarnings("unchecked")
388+
@NotNull
376389
public <G> Optional<G> getAttributeModifier(@NotNull String path) {
390+
return getAttributeModifier(path, true);
391+
}
392+
393+
/**
394+
* {@code 1.9+} Gets an {@code AttributeModifier} from the path. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
395+
*
396+
* @param path the path to the node
397+
* @param log whether to log warnings if the attribute modifier is invalid
398+
*
399+
* @param <G> the {@code AttributeModifier} class
400+
*
401+
* @return the {@code AttributeModifier} or empty if it's invalid
402+
*/
403+
@NotNull @SuppressWarnings("unchecked")
404+
public <G> Optional<G> getAttributeModifier(@NotNull String path, boolean log) {
377405
final Optional<G> def = getDef(path);
378406
if (ATTRIBUTE_MODIFIER_OPERATION_ENUM == null) return def;
379407
final ConfigurationSection section = getConfigurationSection(path);
@@ -382,7 +410,7 @@ public <G> Optional<G> getAttributeModifier(@NotNull String path) {
382410
final String name = section.getString("name");
383411
final String operationString = section.getString("operation");
384412
if (name == null || operationString == null) {
385-
log(Level.WARNING, path, "&cInvalid attribute modifier, missing name and/or operation");
413+
if (log) log(Level.WARNING, path, "&cInvalid attribute modifier, missing name and/or operation");
386414
return def;
387415
}
388416

@@ -391,7 +419,7 @@ public <G> Optional<G> getAttributeModifier(@NotNull String path) {
391419
try {
392420
operation = Enum.valueOf(ATTRIBUTE_MODIFIER_OPERATION_ENUM, operationString);
393421
} catch (final IllegalArgumentException e) {
394-
log(Level.WARNING, path, "&cInvalid attribute modifier operation: &4" + operationString);
422+
if (log) log(Level.WARNING, path, "&cInvalid attribute modifier operation: &4" + operationString);
395423
return def;
396424
}
397425

@@ -406,7 +434,7 @@ public <G> Optional<G> getAttributeModifier(@NotNull String path) {
406434
if (equipmentSlotString != null) try {
407435
slot = EquipmentSlot.valueOf(equipmentSlotString);
408436
} catch (final IllegalArgumentException e) {
409-
log(Level.WARNING, path, "&cInvalid equipment slot: &4" + equipmentSlotString);
437+
if (log) log(Level.WARNING, path, "&cInvalid equipment slot: &4" + equipmentSlotString);
410438
}
411439

412440
// Return
@@ -429,18 +457,20 @@ public <G> Optional<G> getAttributeModifier(@NotNull String path) {
429457

430458
/**
431459
* Gets an {@link ItemStack} from the path. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
460+
* <br><i>If you want to disable warning/error logging, use {@link #getItemStackOptional(String, boolean)}</i>
432461
*
433462
* @param path the path to the node
434463
*
435464
* @return the {@link ItemStack} or {@code null} if it's invalid
436465
*/
437466
@Override @Nullable
438467
public ItemStack getItemStack(@NotNull String path) {
439-
return getItemStackOptional(path).orElse(null);
468+
return getItemStackOptional(path, true).orElse(null);
440469
}
441470

442471
/**
443472
* Gets an {@link ItemStack} from the path. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
473+
* <br><i>If you want to disable warning/error logging, use {@link #getItemStackOptional(String, boolean)}</i>
444474
*
445475
* @param path the path to the node
446476
* @param def the default value
@@ -449,7 +479,7 @@ public ItemStack getItemStack(@NotNull String path) {
449479
*/
450480
@Override @Nullable
451481
public ItemStack getItemStack(@NotNull String path, @Nullable ItemStack def) {
452-
return getItemStackOptional(path).orElse(def);
482+
return getItemStackOptional(path, true).orElse(def);
453483
}
454484

455485
/**
@@ -461,21 +491,34 @@ public ItemStack getItemStack(@NotNull String path, @Nullable ItemStack def) {
461491
*/
462492
@NotNull
463493
public Optional<ItemStack> getItemStackOptional(@NotNull String path) {
494+
return getItemStackOptional(path, true);
495+
}
496+
497+
/**
498+
* Gets an {@link ItemStack} from the path. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
499+
*
500+
* @param path the path to the node
501+
* @param log whether to log warnings if the item stack is invalid
502+
*
503+
* @return the {@link ItemStack} or empty if it's invalid
504+
*/
505+
@NotNull
506+
public Optional<ItemStack> getItemStackOptional(@NotNull String path, boolean log) {
464507
final Optional<ItemStack> def = getDef(path);
465508
final ConfigurationSection section = getConfigurationSection(path);
466509
if (section == null) return def;
467510

468511
// Get material name
469512
final String materialString = section.getString("material");
470513
if (materialString == null) {
471-
log(Level.WARNING, path, "&cInvalid material, missing material");
514+
if (log) log(Level.WARNING, path, "&cInvalid material, missing material");
472515
return def;
473516
}
474517

475518
// Get material
476519
final Material material = Material.matchMaterial(materialString);
477520
if (material == null) {
478-
log(Level.WARNING, path, "&cInvalid material for: &4" + materialString);
521+
if (log) log(Level.WARNING, path, "&cInvalid material for: &4" + materialString);
479522
return def;
480523
}
481524

@@ -509,7 +552,7 @@ public Optional<ItemStack> getItemStackOptional(@NotNull String path) {
509552
if (enchantmentsSection != null) for (final String enchantmentKey : enchantmentsSection.getKeys(false)) {
510553
final Optional<Enchantment> enchantment = RefRegistry.getEnchantment(enchantmentKey);
511554
if (!enchantment.isPresent()) {
512-
log(Level.WARNING, path, "&cInvalid enchantment: &4" + enchantmentKey);
555+
if (log) log(Level.WARNING, path, "&cInvalid enchantment: &4" + enchantmentKey);
513556
continue;
514557
}
515558
meta.addEnchant(enchantment.get(), enchantmentsSection.getInt(enchantmentKey), true);
@@ -521,7 +564,7 @@ public Optional<ItemStack> getItemStackOptional(@NotNull String path) {
521564
try {
522565
return ItemFlag.valueOf(string.toUpperCase());
523566
} catch (final IllegalArgumentException e) {
524-
log(Level.WARNING, section.getCurrentPath() + "." + "flags", "&cInvalid item flag: &4" + string);
567+
if (log) log(Level.WARNING, section.getCurrentPath() + "." + "flags", "&cInvalid item flag: &4" + string);
525568
return null;
526569
}
527570
})
@@ -546,7 +589,7 @@ public Optional<ItemStack> getItemStackOptional(@NotNull String path) {
546589
//noinspection unchecked
547590
attribute = Enum.valueOf(ATTRIBUTE_ENUM, attributeKey.toUpperCase());
548591
} catch (final IllegalArgumentException e) {
549-
log(Level.WARNING, pathString, "&cInvalid attribute: &4" + attributeKey);
592+
if (log) log(Level.WARNING, pathString, "&cInvalid attribute: &4" + attributeKey);
550593
continue;
551594
}
552595

@@ -622,6 +665,21 @@ public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<
622665
*/
623666
@NotNull
624667
public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<ItemStack> itemFunction, @Nullable String name) {
668+
return getRecipe(path, itemFunction, name, true);
669+
}
670+
671+
/**
672+
* Gets a {@link Recipe} from the YAML. See <a href="https://annoying-api.srnyx.com/wiki/file-objects">the wiki</a> for more information
673+
*
674+
* @param path the path to get the recipe from
675+
* @param itemFunction the function to apply to the {@link ItemStack} before returning it
676+
* @param name the name of the recipe (only used in 1.12+ for the {@code NamespacedKey}), or {@code null} to use the node name
677+
* @param log whether to log warnings if the recipe is invalid
678+
*
679+
* @return the {@link Recipe} or the {@code def} if it's invalid
680+
*/
681+
@NotNull
682+
public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<ItemStack> itemFunction, @Nullable String name, boolean log) {
625683
final Optional<Recipe> def = getDef(path);
626684

627685
// section, shape, result, & ingredientMaterials
@@ -639,7 +697,7 @@ public Optional<Recipe> getRecipe(@NotNull String path, @Nullable UnaryOperator<
639697
final String value = String.valueOf(entry.getValue());
640698
final Material material = Material.matchMaterial(value);
641699
if (material == null) {
642-
log(Level.WARNING, ingredients.getCurrentPath() + "." + key, "&cInvalid material: &4" + value);
700+
if (log) log(Level.WARNING, ingredients.getCurrentPath() + "." + key, "&cInvalid material: &4" + value);
643701
continue;
644702
}
645703
ingredientMaterials.put(key.toUpperCase().charAt(0), material);

0 commit comments

Comments
 (0)