-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add support for item variants #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rover656
wants to merge
2
commits into
Buuz135:1.21
Choose a base branch
from
Rover656:feat/item-variants
base: 1.21
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/buuz135/replication/api/CollectItemVariantsEvent.java
This file contains hidden or 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,19 @@ | ||
| package com.buuz135.replication.api; | ||
|
|
||
| import net.minecraft.world.item.Item; | ||
| import net.neoforged.bus.api.Event; | ||
| import net.neoforged.fml.event.IModBusEvent; | ||
|
|
||
| import java.util.function.BiConsumer; | ||
|
|
||
| public class CollectItemVariantsEvent extends Event implements IModBusEvent { | ||
| private final BiConsumer<Item, IItemVariantProvider> consumer; | ||
|
|
||
| public CollectItemVariantsEvent(BiConsumer<Item, IItemVariantProvider> consumer) { | ||
| this.consumer = consumer; | ||
| } | ||
|
|
||
| public void add(Item item, IItemVariantProvider provider) { | ||
| consumer.accept(item, provider); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/buuz135/replication/api/IItemVariantProvider.java
This file contains hidden or 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,17 @@ | ||
| package com.buuz135.replication.api; | ||
|
|
||
| import net.minecraft.core.HolderLookup; | ||
| import net.minecraft.core.RegistryAccess; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| public interface IItemVariantProvider { | ||
| // Get name of variant | ||
| String getVariantKey(ItemStack stack); | ||
|
|
||
| // Do not mutate stack. | ||
| ItemStack normalize(ItemStack stack); | ||
|
|
||
| Set<ItemStack> getVariants(HolderLookup.Provider registries); | ||
| } |
This file contains hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/buuz135/replication/calculation/ItemVariant.java
This file contains hidden or 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,17 @@ | ||
| package com.buuz135.replication.calculation; | ||
|
|
||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| // Needed to ensure hashCode is valid for storing in maps. | ||
| public record ItemVariant(ItemStack stack) { | ||
| @Override | ||
| public int hashCode() { | ||
| return ItemStack.hashItemAndComponents(stack); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof ItemVariant(ItemStack otherStack))) return false; | ||
| return ItemStack.isSameItemSameComponents(stack, otherStack); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/buuz135/replication/calculation/ItemVariants.java
This file contains hidden or 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,53 @@ | ||
| package com.buuz135.replication.calculation; | ||
|
|
||
| import com.buuz135.replication.api.CollectItemVariantsEvent; | ||
| import com.buuz135.replication.api.IItemVariantProvider; | ||
| import com.hrznstudio.titanium.event.handler.EventManager; | ||
| import net.minecraft.core.HolderLookup; | ||
| import net.minecraft.core.registries.BuiltInRegistries; | ||
| import net.minecraft.world.item.Item; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.neoforged.fml.ModLoader; | ||
| import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| // TODO: Should cache results. | ||
| public class ItemVariants { | ||
| private static final Map<Item, IItemVariantProvider> providers = new HashMap<>(); | ||
|
|
||
| public static void init() { | ||
| EventManager.mod(FMLCommonSetupEvent.class).process(commonSetupEvent -> { | ||
| var event = new CollectItemVariantsEvent(providers::put); | ||
| ModLoader.postEvent(event); | ||
| }).subscribe(); | ||
| } | ||
|
|
||
| public static String getName(ItemVariant variant) { | ||
| if (providers.containsKey(variant.stack().getItem())) { | ||
| return BuiltInRegistries.ITEM.getKey(variant.stack().getItem()).toString() + | ||
| providers.get(variant.stack().getItem()).getVariantKey(variant.stack()); | ||
| } | ||
|
|
||
| return BuiltInRegistries.ITEM.getKey(variant.stack().getItem()).toString(); | ||
| } | ||
|
|
||
| public static ItemVariant normalize(ItemStack stack) { | ||
| if (providers.containsKey(stack.getItem())) { | ||
| return new ItemVariant(providers.get(stack.getItem()).normalize(stack)); | ||
| } | ||
|
|
||
| return new ItemVariant(stack.getItem().getDefaultInstance()); | ||
| } | ||
|
|
||
| public static Set<ItemVariant> getVariants(Item item, HolderLookup.Provider registries) { | ||
| if (providers.containsKey(item)) { | ||
| return providers.get(item).getVariants(registries).stream().map(ItemVariant::new).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| return Set.of(new ItemVariant(item.getDefaultInstance())); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will handle this if you're happy with the idea :)