-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd01a3a
commit e697039
Showing
8 changed files
with
143 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/org/auioc/mcmod/arnicalib/game/entity/PlayerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/org/auioc/mcmod/arnicalib/game/item/ItemPredicates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/auioc/mcmod/arnicalib/game/item/ItemRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/auioc/mcmod/arnicalib/game/item/ItemStackSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
43
src/main/java/org/auioc/mcmod/arnicalib/game/item/ItemUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
116
src/main/java/org/auioc/mcmod/arnicalib/utils/game/ItemUtils.java
This file was deleted.
Oops, something went wrong.