Skip to content

Commit

Permalink
refactor(next): item
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Sep 27, 2022
1 parent fd01a3a commit e697039
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 122 deletions.
@@ -1,6 +1,6 @@
package org.auioc.mcmod.arnicalib.common.itemgroup.impl;

import org.auioc.mcmod.arnicalib.utils.game.ItemUtils;
import org.auioc.mcmod.arnicalib.game.item.ItemPredicates;
import org.auioc.mcmod.arnicalib.utils.game.VanillaPredicates;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
Expand All @@ -23,8 +23,8 @@ public ItemStack makeIcon() {
public void fillItemList(NonNullList<ItemStack> list) {
ForgeRegistries.ITEMS.getValues()
.stream()
.filter(ItemUtils.IS_AIR.negate())
.filter(ItemUtils.IS_CATEGORIZED.negate())
.filter(ItemPredicates.IS_AIR.negate())
.filter(ItemPredicates.IS_CATEGORIZED.negate())
.filter(VanillaPredicates.REGISTRY_ENTRY)
.map(ItemStack::new)
.forEach(list::add);
Expand Down
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import org.auioc.mcmod.arnicalib.base.validate.Validate;
import org.auioc.mcmod.arnicalib.utils.game.ItemUtils;
import org.auioc.mcmod.arnicalib.game.item.ItemRegistry;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
Expand Down Expand Up @@ -43,7 +43,7 @@ public static MobEffectInstance fromJson(JsonObject json) {
List<ItemStack> curativeItems = new ArrayList<ItemStack>();
for (var element : curativeItemsJson) {
var curativeItemId = GsonHelper.convertToString(element, "curative_item");
curativeItems.add(new ItemStack(ItemUtils.getItemOrElseThrow(curativeItemId)));
curativeItems.add(new ItemStack(ItemRegistry.getOrElseThrow(curativeItemId)));
}
instance.setCurativeItems(curativeItems);
}
Expand Down
@@ -1,7 +1,7 @@
package org.auioc.mcmod.arnicalib.game.entity;

import javax.annotation.Nullable;
import org.auioc.mcmod.arnicalib.utils.game.ItemUtils;
import org.auioc.mcmod.arnicalib.game.item.ItemUtils;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
Expand Down
@@ -0,0 +1,15 @@
package org.auioc.mcmod.arnicalib.game.item;

import java.util.function.Predicate;
import org.auioc.mcmod.arnicalib.utils.game.VanillaPredicates;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;

public class ItemPredicates {

public static final Predicate<ItemStack> IS_VANILLA_ITEM = (itemStack) -> VanillaPredicates.REGISTRY_ENTRY.test(itemStack.getItem());
public static final Predicate<Item> IS_AIR = (item) -> item == Items.AIR;
public static final Predicate<Item> IS_CATEGORIZED = (item) -> item.getItemCategory() != null;

}
@@ -0,0 +1,37 @@
package org.auioc.mcmod.arnicalib.game.item;

import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import org.auioc.mcmod.arnicalib.game.registry.RegistryEntryException;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.registries.ForgeRegistries;

public class ItemRegistry {

@Nonnull
public static Optional<Item> get(ResourceLocation id) {
return Optional.ofNullable(ForgeRegistries.ITEMS.containsKey(id) ? ForgeRegistries.ITEMS.getValue(id) : null);
}

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

@Nonnull
public static Item getOrElseThrow(ResourceLocation id) {
return get(id).orElseThrow(RegistryEntryException.UNKNOWN_ITEM.create(id.toString()));
}

@Nonnull
public static Item getOrElseThrow(String id) {
return get(id).orElseThrow(RegistryEntryException.UNKNOWN_ITEM.create(id.toString()));
}

public static List<Item> getItems(List<String> idList) {
return idList.stream().map(ItemRegistry::getOrElseThrow).toList();
}

}
@@ -0,0 +1,42 @@
package org.auioc.mcmod.arnicalib.game.item;

import org.auioc.mcmod.arnicalib.base.validate.Validate;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;

public class ItemStackSerializer {

public static ItemStack fromJson(JsonObject json) {
var item = ItemRegistry.getOrElseThrow(GsonHelper.getAsString(json, "id"));

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

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

return ItemUtils.createItemStack(item, count, nbt);
}

}
43 changes: 43 additions & 0 deletions src/main/java/org/auioc/mcmod/arnicalib/game/item/ItemUtils.java
@@ -0,0 +1,43 @@
package org.auioc.mcmod.arnicalib.game.item;

import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

public class ItemUtils {

@SuppressWarnings("deprecation")
public static int getMaxStackSize(Item item) {
return item.getMaxStackSize();
}

@SuppressWarnings("deprecation")
public static int getMaxDamage(Item item) {
return item.getMaxDamage();
}

public static ItemStack createItemStack(Item item, int count, @Nullable CompoundTag nbt) {
ItemStack itemStack = new ItemStack(item, count);
if (nbt != null) {
itemStack.setTag(nbt);
}
return itemStack;
}

public static void hurt(Player player, ItemStack itemStack) {
itemStack.hurtAndBreak(1, player, (p) -> {
p.broadcastBreakEvent(player.getUsedItemHand());
});
}

public static String toString(ItemStack itemStack) {
return String.format("%s%s * %d", toString(itemStack), (itemStack.hasTag()) ? itemStack.getTag() : "{}", itemStack.getCount());
}

public static String toString(Item item) {
return item.getRegistryName().toString();
}

}
116 changes: 0 additions & 116 deletions src/main/java/org/auioc/mcmod/arnicalib/utils/game/ItemUtils.java

This file was deleted.

0 comments on commit e697039

Please sign in to comment.