Skip to content

Commit

Permalink
feat(utils): getItem methods return Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Apr 11, 2022
1 parent 787f85e commit d197777
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/main/java/org/auioc/mcmod/arnicalib/utils/game/ItemUtils.java
@@ -1,10 +1,14 @@
package org.auioc.mcmod.arnicalib.utils.game;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.auioc.mcmod.arnicalib.api.game.registry.RegistryEntryException;
import org.auioc.mcmod.arnicalib.utils.java.Validate;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
Expand All @@ -18,17 +22,28 @@

public interface ItemUtils {

static Item getItem(ResourceLocation id) {
Validate.isTrue(ForgeRegistries.ITEMS.containsKey(id), "Unknown item '" + id + "'");
return ForgeRegistries.ITEMS.getValue(id);
@Nonnull
static Optional<Item> getItem(ResourceLocation id) {
return Optional.ofNullable(ForgeRegistries.ITEMS.containsKey(id) ? ForgeRegistries.ITEMS.getValue(id) : null);
}

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

@Nonnull
static Item getItemOrElseThrow(ResourceLocation id) {
return getItem(id).orElseThrow(RegistryEntryException.UNKNOWN_ITEM.create(id.toString()));
}

@Nonnull
static Item getItemOrElseThrow(String id) {
return getItem(id).orElseThrow(RegistryEntryException.UNKNOWN_ITEM.create(id.toString()));
}

static List<Item> getItems(List<String> idList) {
return idList.stream().map(ResourceLocation::new).map(ItemUtils::getItem).toList();
return idList.stream().map(ItemUtils::getItemOrElseThrow).toList();
}

static String getRegistryName(Item item) {
Expand Down Expand Up @@ -75,19 +90,23 @@ static ItemStack createItemStack(Item item, int count, @Nullable CompoundTag nbt
}

static ItemStack deserializeFromJson(JsonObject json) {
Item item = getItem(GsonHelper.getAsString(json, "id"));
var item = getItemOrElseThrow(GsonHelper.getAsString(json, "id"));

int count = GsonHelper.getAsInt(json, "count", 1);
Validate.isPositive(count, "The item count must be positive: " + count);
Validate.isTrue(count <= getMaxStackSize(item), "The specified count " + count + " is too large, the max stack size of item '" + getRegistryName(item) + "' is " + getMaxStackSize(item));
try {
Validate.isPositive(count, "The item count must be positive: " + count);
Validate.isTrue(count <= getMaxStackSize(item), "The specified count " + count + " is too large, the max stack size of item '" + getRegistryName(item) + "' is " + getMaxStackSize(item));
} catch (IllegalArgumentException e) {
throw new JsonSyntaxException(e);
}

CompoundTag nbt = null;
String snbt = GsonHelper.getAsString(json, "tag", null);
var snbt = GsonHelper.getAsString(json, "nbt", null);
if (snbt != null) {
try {
nbt = TagParser.parseTag(snbt);
} catch (CommandSyntaxException e) {
Validate.throwException("Invalid NBT: " + e.getMessage());
throw new JsonSyntaxException("Invalid NBT: " + e.getMessage());
}
}

Expand Down

0 comments on commit d197777

Please sign in to comment.