Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .idea/dictionaries/project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>26.0.2-1</version>
<scope>compile</scope>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;

@NullMarked
public class ConfigEnchantmentEntry {
/**
* Name of the enchantment.
*/
public final @NotNull String name;
public final String name;

/**
* Maximum level of the enchantment.
Expand All @@ -40,57 +41,14 @@ public class ConfigEnchantmentEntry {
protected final boolean multiplyCostByLevel;

/**
* Maximum level of the enchantment.
*/
public final @NotNull Optional<Integer> getMaxLevel() {
if (Optional.ofNullable(maxLevel).isEmpty())
return Optional.empty();

if (maxLevelRelative)
return Optional.of(getEnchantment().getMaxLevel() + maxLevel);

return Optional.of(maxLevel);
}

/**
* Cost of the enchantment.
*/
public final int getCost() {
return cost;
}

/**
* Multiply cost by level.
*/
public final boolean getMultiplyCostByLevel() {
return multiplyCostByLevel;
}

/**
* Get enchantment
*/
public final Enchantment getEnchantment() {
return Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name));
}

/**
* Is enchantment
*
* @param enchantment The enchantment
*/
public final boolean isEnchantment(final @NotNull Enchantment enchantment) {
return name.equalsIgnoreCase(enchantment.getKey().getKey());
}

/**
* @param name Name of the enchantment.
* @param maxLevel Maximum level of the enchantment.
* @param maxLevelRelative Max level relative
* @param cost Cost of the enchantment.
* @param name Name of the enchantment.
* @param maxLevel Maximum level of the enchantment.
* @param maxLevelRelative Max level relative
* @param cost Cost of the enchantment.
* @param multiplyCostByLevel Multiply cost by level.
*/
public ConfigEnchantmentEntry(
final @NotNull String name,
final String name,
final @Nullable Integer maxLevel,
final boolean maxLevelRelative,
final int cost,
Expand All @@ -108,77 +66,67 @@ public ConfigEnchantmentEntry(
*
* @param configValue Config object
*/
public static @NotNull ConfigEnchantmentEntry configValue(
final @NotNull Map<@NotNull String, @NotNull Object> configValue
public static ConfigEnchantmentEntry configValue(
final Map<String, Object> configValue
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
final String name = (String) Objects.requireNonNull(configValue.get("name"));

final @Nullable Integer maxLevel;
final Integer maxLevel;

final boolean maxLevelRelative;

if (!configValue.containsKey("max-level")) {
maxLevel = null;
maxLevelRelative = false;
}

else {
} else {
if (!(configValue.get("max-level") instanceof final String string)) {
maxLevel = (Integer) configValue.get("max-level");
maxLevelRelative = false;
}

else {
} else {
if (string.startsWith("+")) {
maxLevel = Integer.parseInt(string.substring(1));
maxLevelRelative = true;
}
else {
} else {
maxLevel = Integer.parseInt(string);
maxLevelRelative = false;
}
}
}

if (!configValue.containsKey("cost"))
if (!configValue.containsKey("cost")) {
return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, 0, false);
}

if (!(configValue.get("cost") instanceof final @NotNull String costString))
if (!(configValue.get("cost") instanceof final String costString)) {
return new ConfigEnchantmentEntry(
name,
maxLevel,
maxLevelRelative,
(Integer) configValue.get("cost"),
false
);
}

if (costString.startsWith("*"))
if (costString.startsWith("*")) {
return new ConfigEnchantmentEntry(
name,
maxLevel,
maxLevelRelative,
Integer.parseInt(costString.substring(1)),
true
);
}

return new ConfigEnchantmentEntry(
name,
maxLevel,
maxLevelRelative,
Integer.parseInt(costString),
false
);
return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, Integer.parseInt(costString), false);
}


/**
* From config object array
*
* @param configValue Config object array
*/
public static @NotNull List<@NotNull ConfigEnchantmentEntry> configArray(
final @NotNull ArrayList<@NotNull Map<@NotNull String, @NotNull Object>> configValue
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
public static List<ConfigEnchantmentEntry> configArray(final ArrayList<Map<String, Object>> configValue)
throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
return configValue.stream().map(ConfigEnchantmentEntry::configValue).collect(Collectors.toList());
}

Expand All @@ -188,31 +136,36 @@ public ConfigEnchantmentEntry(
* @param configValue Config object
*/
private static boolean isValidConfigValue(final @Nullable Object configValue) {
if (configValue == null)
if (configValue == null) {
return false;
}

if (!(configValue instanceof final ArrayList<?> arrayList))
if (!(configValue instanceof final ArrayList<?> arrayList)) {
return false;
}

for (final Object object : arrayList) {
if (!(object instanceof final Map<?, ?> hashMap))
if (!(object instanceof final Map<?, ?> hashMap)) {
return false;
}

if (!hashMap.containsKey("name"))
if (!hashMap.containsKey("name")) {
return false;
}

if (!(hashMap.get("name") instanceof String))
if (!(hashMap.get("name") instanceof String)) {
return false;
}

if (hashMap.containsKey("max-level") &&
!(hashMap.get("max-level") instanceof String) &&
!(hashMap.get("max-level") instanceof Integer))
if (hashMap.containsKey("max-level") && !(hashMap.get("max-level") instanceof String) && !(hashMap.get(
"max-level") instanceof Integer)) {
return false;
}

if (hashMap.containsKey("cost") &&
!(hashMap.get("cost") instanceof String) &&
!(hashMap.get("cost") instanceof Integer))
if (hashMap.containsKey("cost") && !(hashMap.get("cost") instanceof String)
&& !(hashMap.get("cost") instanceof Integer)) {
return false;
}
}

return true;
Expand All @@ -223,16 +176,61 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
*
* @param configValue Config object
*/
public static @NotNull List<@NotNull ConfigEnchantmentEntry> config(
final @Nullable Object configValue
) throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
if (!isValidConfigValue(configValue))
public static List<ConfigEnchantmentEntry> config(final @Nullable Object configValue)
throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
if (!isValidConfigValue(configValue)) {
throw new IllegalArgumentException("Invalid config value");
}

//noinspection unchecked
return configArray((ArrayList<Map<String, Object>>) configValue);
}

/**
* Maximum level of the enchantment.
*/
public final OptionalInt getMaxLevel() {
if (maxLevel == null) {
return OptionalInt.empty();
}

if (maxLevelRelative) {
return OptionalInt.of(getEnchantment().getMaxLevel() + maxLevel);
}

return OptionalInt.of(maxLevel);
}

/**
* Cost of the enchantment.
*/
public final int getCost() {
return cost;
}

/**
* Multiply cost by level.
*/
public final boolean getMultiplyCostByLevel() {
return multiplyCostByLevel;
}

/**
* Get enchantment
*/
public final Enchantment getEnchantment() {
return Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)));
}

/**
* Is enchantment
*
* @param enchantment The enchantment
*/
public final boolean isEnchantment(final Enchantment enchantment) {
return name.equalsIgnoreCase(enchantment.getKey().getKey());
}

public static final class AllConfigEnchantmentEntry extends ConfigEnchantmentEntry {
private AllConfigEnchantmentEntry(
final @Nullable Integer maxLevel,
Expand All @@ -243,9 +241,7 @@ private AllConfigEnchantmentEntry(
super("ALL", maxLevel, maxLevelRelative, cost, multiplyCostByLevel);
}

public static @NotNull AllConfigEnchantmentEntry from(
final @NotNull ConfigEnchantmentEntry configEnchantmentEntry
) {
public static AllConfigEnchantmentEntry from(final ConfigEnchantmentEntry configEnchantmentEntry) {
return new AllConfigEnchantmentEntry(
configEnchantmentEntry.maxLevel,
configEnchantmentEntry.maxLevelRelative,
Expand All @@ -254,7 +250,7 @@ private AllConfigEnchantmentEntry(
);
}

public @NotNull ConfigEnchantmentEntry enchant(final @NotNull Enchantment enchantment) {
public ConfigEnchantmentEntry enchant(final Enchantment enchantment) {
return new ConfigEnchantmentEntry(
enchantment.getKey().getKey(),
this.maxLevel,
Expand Down
Loading
Loading