Minor code cleanup

- Resolve some Checkstyle violations (mostly line lengths)
- deduplicated some code
This commit is contained in:
Pim van der Loos 2022-06-20 11:10:03 +02:00
parent 536e81860c
commit 3db0627cc7
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
2 changed files with 46 additions and 41 deletions

View File

@ -11,7 +11,11 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
public class ConfigLoader public class ConfigLoader
@ -86,12 +90,16 @@ public class ConfigLoader
String[] mutuallyExclusiveEnchantmentsComment = String[] mutuallyExclusiveEnchantmentsComment =
{ {
"The lists of enchantments that are mutually exclusive.", "The lists of enchantments that are mutually exclusive.",
"Each group [] on this list is treated as mutually exclusive, so only one of them can be on an ArmoredElytra.", "Each group [] on this list is treated as mutually exclusive, " +
"The default follows modern vanilla rules by making the different types of protection mutually exclusive.", "so only one of them can be on an ArmoredElytra.",
"If you do not want any enchantments to be mutually exclusive, replace all the entries in this list with \"[]\"", "The default follows modern vanilla rules by making the different " +
"types of protection mutually exclusive.",
"If you do not want any enchantments to be mutually exclusive, " +
"replace all the entries in this list with \"[]\"",
"You can find supported enchantments by running the command:", "You can find supported enchantments by running the command:",
"\"armoredelytra listAvailableEnchantments\" in console", "\"armoredelytra listAvailableEnchantments\" in console",
"If you install additional enchant plugins, you can make their enchantments mutually exclusive as well.", "If you install additional enchant plugins, " +
"you can make their enchantments mutually exclusive as well.",
"Just add their 'NamespacedKey'. Ask the enchantment plugin dev for more info if you need it." "Just add their 'NamespacedKey'. Ask the enchantment plugin dev for more info if you need it."
}; };
String[] dropNetheriteAsChestplateComment = String[] dropNetheriteAsChestplateComment =
@ -205,8 +213,9 @@ public class ConfigLoader
allowedEnchantments = new LinkedHashSet<>(); allowedEnchantments = new LinkedHashSet<>();
defaultAllowedEnchantments.forEach(this::addNameSpacedKey); defaultAllowedEnchantments.forEach(this::addNameSpacedKey);
defaultMutuallyExclusiveEnchantments = addNewConfigOption(config, "mutuallyExclusiveEnchantments", defaultMutuallyExclusiveEnchantments =
defaultMutuallyExclusiveEnchantments, mutuallyExclusiveEnchantmentsComment); addNewConfigOption(config, "mutuallyExclusiveEnchantments",
defaultMutuallyExclusiveEnchantments, mutuallyExclusiveEnchantmentsComment);
mutuallyExclusiveEnchantments = new LinkedList<>(); mutuallyExclusiveEnchantments = new LinkedList<>();
defaultMutuallyExclusiveEnchantments.forEach(this::addMutuallyExclusiveEnchantments); defaultMutuallyExclusiveEnchantments.forEach(this::addMutuallyExclusiveEnchantments);
@ -227,7 +236,7 @@ public class ConfigLoader
writeConfig(); writeConfig();
} }
private void addNameSpacedKey(String fullKey) private @Nullable Enchantment enchantmentFromNameSpacedKey(String fullKey)
{ {
try try
{ {
@ -235,7 +244,7 @@ public class ConfigLoader
if (keyParts.length < 2) if (keyParts.length < 2)
{ {
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!"); Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
return; return null;
} }
//noinspection deprecation //noinspection deprecation
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]); final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
@ -243,41 +252,32 @@ public class ConfigLoader
if (enchantment == null) if (enchantment == null)
{ {
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!"); Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
return; return null;
} }
allowedEnchantments.add(enchantment); return enchantment;
} }
catch (Exception e) catch (Exception e)
{ {
plugin.getLogger().log(Level.WARNING, e, () -> "Failed to register NamespacedKey key: '" + fullKey + "'"); plugin.getLogger().log(Level.WARNING, e, () -> "Failed to register NamespacedKey key: '" + fullKey + "'");
} }
return null;
}
private void addNameSpacedKey(String fullKey)
{
final @Nullable Enchantment enchantment = enchantmentFromNameSpacedKey(fullKey);
if (enchantment != null)
allowedEnchantments.add(enchantment);
} }
private void addMutuallyExclusiveEnchantments(List<String> fullKeys) private void addMutuallyExclusiveEnchantments(List<String> fullKeys)
{ {
List<Enchantment> enchantments = new LinkedList<>(); final List<Enchantment> enchantments = new LinkedList<>();
for (String fullKey : fullKeys) { for (String fullKey : fullKeys)
try { {
final String[] keyParts = fullKey.strip().split(":", 2); final @Nullable Enchantment enchantment = enchantmentFromNameSpacedKey(fullKey);
if (keyParts.length < 2) if (enchantment != null)
{
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
return;
}
//noinspection deprecation
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
final Enchantment enchantment = Enchantment.getByKey(key);
if (enchantment == null)
{
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
return;
}
enchantments.add(enchantment); enchantments.add(enchantment);
}
catch (Exception e)
{
plugin.getLogger().log(Level.WARNING, e, () -> "Failed to register NamespacedKey key: '" + fullKey + "'");
}
} }
mutuallyExclusiveEnchantments.add(enchantments); mutuallyExclusiveEnchantments.add(enchantments);
} }

View File

@ -103,19 +103,24 @@ public class EnchantmentContainer implements Iterable<Map.Entry<Enchantment, Int
* <br>This does <b>not</b> include the provided enchantment * <br>This does <b>not</b> include the provided enchantment
* *
* @param enchantment The enchantment to get the mutually exclusives of * @param enchantment The enchantment to get the mutually exclusives of
* @return A linked list containing all the mutually exclusive enchantments * @return A linked list containing all the mutually exclusive enchantments
*/ */
private static List<Enchantment> getMutuallyExclusiveEnchantments(Enchantment enchantment) private static List<Enchantment> getMutuallyExclusiveEnchantments(Enchantment enchantment)
{ {
List<Enchantment> enchantments = new LinkedList<>(); final List<Enchantment> enchantments = new LinkedList<>();
for (List<Enchantment> mutuallyExclusiveEnchantments : ArmoredElytra.getInstance().getConfigLoader().getMutuallyExclusiveEnchantments()) for (final List<Enchantment> mutuallyExclusiveEnchantments :
ArmoredElytra.getInstance().getConfigLoader().getMutuallyExclusiveEnchantments())
{
for (Enchantment mutuallyExclusiveEnchantment : mutuallyExclusiveEnchantments) for (Enchantment mutuallyExclusiveEnchantment : mutuallyExclusiveEnchantments)
{
if (mutuallyExclusiveEnchantment.equals(enchantment)) if (mutuallyExclusiveEnchantment.equals(enchantment))
{ {
enchantments.addAll(mutuallyExclusiveEnchantments.stream() enchantments.addAll(
.filter(i -> !i.equals(enchantment)).toList()); mutuallyExclusiveEnchantments.stream().filter(i -> !i.equals(enchantment)).toList());
break; break;
} }
}
}
return enchantments; return enchantments;
} }
@ -226,9 +231,9 @@ public class EnchantmentContainer implements Iterable<Map.Entry<Enchantment, Int
return second; return second;
final List<Enchantment> blackList = final List<Enchantment> blackList =
second.keySet().stream() second.keySet().stream()
.flatMap(ench -> getMutuallyExclusiveEnchantments(ench).stream()) .flatMap(ench -> getMutuallyExclusiveEnchantments(ench).stream())
.toList(); .toList();
blackList.forEach(first.keySet()::remove); blackList.forEach(first.keySet()::remove);
final Map<Enchantment, Integer> combined = new HashMap<>(first); final Map<Enchantment, Integer> combined = new HashMap<>(first);