Skip to content

Commit

Permalink
feat(utils): getEnchantment methods return Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jul 18, 2022
1 parent 3944669 commit 4ecb0a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Expand Up @@ -10,6 +10,7 @@ public class RegistryEntryException extends RuntimeException {
public static final ThrowableSupplier<RegistryEntryException> UNKNOWN_MOB_EFFECT = (id) -> () -> new Unknown("mob effect", id);
public static final ThrowableSupplier<RegistryEntryException> UNKNOWN_POTION = (id) -> () -> new Unknown("potion", id);
public static final ThrowableSupplier<RegistryEntryException> UNKNOWN_SOUND_EVENT = (id) -> () -> new Unknown("sound event", id);
public static final ThrowableSupplier<RegistryEntryException> UNKNOWN_ENCHANTMENT = (id) -> () -> new Unknown("enchantment", id);

public RegistryEntryException(String message) {
super(message);
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/org/auioc/mcmod/arnicalib/utils/game/EnchUtils.java
@@ -1,6 +1,9 @@
package org.auioc.mcmod.arnicalib.utils.game;

import java.util.Optional;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.RandomUtils;
import org.auioc.mcmod.arnicalib.api.game.registry.RegistryEntryException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -9,10 +12,27 @@

public interface EnchUtils {

static Enchantment getEnchantment(String id) {
return ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(id));
@Nonnull
static Optional<Enchantment> getEnchantment(ResourceLocation id) {
return Optional.ofNullable(ForgeRegistries.ENCHANTMENTS.containsKey(id) ? ForgeRegistries.ENCHANTMENTS.getValue(id) : null);
}

@Nonnull
static Optional<Enchantment> getEnchantment(String id) {
return getEnchantment(new ResourceLocation(id));
}

@Nonnull
static Enchantment getEnchantmentOrElseThrow(ResourceLocation id) {
return getEnchantment(id).orElseThrow(RegistryEntryException.UNKNOWN_ENCHANTMENT.create(id.toString()));
}

@Nonnull
static Enchantment getEnchantmentOrElseThrow(String id) {
return getEnchantment(id).orElseThrow(RegistryEntryException.UNKNOWN_ENCHANTMENT.create(id.toString()));
}


static void enchantOne(ListTag enchantments, int index, int level) {
CompoundTag nbt = enchantments.getCompound(index);
nbt.putShort("lvl", (short) (nbt.getShort("lvl") + level));
Expand Down Expand Up @@ -49,7 +69,7 @@ static CompoundTag getHighestEnchantment(ListTag enchantments) {
static boolean isOverLimit(ListTag enchantments) {
for (int i = 0, l = enchantments.size(); i < l; i++) {
CompoundTag ench = enchantments.getCompound(i);
if (ench.getShort("lvl") > (EnchUtils.getEnchantment(ench.getString("id"))).getMaxLevel()) {
if (ench.getShort("lvl") > (getEnchantmentOrElseThrow(ench.getString("id"))).getMaxLevel()) {
return true;
}
}
Expand Down

0 comments on commit 4ecb0a9

Please sign in to comment.