-
-
Notifications
You must be signed in to change notification settings - Fork 216
Add item groups (aka creative tabs) #2000
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
de6e9f1
Add item groups (aka creative tabs)
gbui 86b0e8c
Add license header to ItemGroupTest
gbui 2654e51
Add license header to ItemGroupRegistryModule
gbui ecf33dd
Remove getItemGroup() in MixinBlock
gbui d26d4a9
Add customRegistration() to ItemGroupRegistryModule
gbui 774e071
Remove getCreativeTabToDisplayOn() in MixinBlock
gbui d4490f4
Use CreativeTabs.CREATIVE_TAB_ARRAY in customRegistration()
gbui b2ac4a1
Replace getItemGroup() with getItemGroups()
gbui e51f523
Implement getItemGroups() in MixinItem
gbui efc0fd2
Update MixinCreativeTabs to match #1792
gbui cc2cb6e
Update SpongeAPI
gbui 4aceb68
Add null check in MixinItem.getItemGroups()
gbui 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
69 changes: 69 additions & 0 deletions
69
src/main/java/org/spongepowered/common/mixin/core/creativetab/MixinCreativeTabs.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,69 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.mixin.core.creativetab; | ||
|
|
||
| import com.google.common.base.MoreObjects; | ||
| import net.minecraft.creativetab.CreativeTabs; | ||
| import org.spongepowered.api.item.ItemGroup; | ||
| import org.spongepowered.api.text.translation.Translation; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.common.text.translation.SpongeTranslation; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| @Mixin(CreativeTabs.class) | ||
| public abstract class MixinCreativeTabs implements ItemGroup { | ||
|
|
||
| @Nullable private Translation translation; | ||
|
|
||
| @Shadow @Final private String tabLabel; | ||
|
|
||
| @Override | ||
| public String getId() { | ||
| return this.tabLabel; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return getTranslation().get(); | ||
| } | ||
|
|
||
| @Override | ||
| public Translation getTranslation() { | ||
| if (this.translation == null) { | ||
| this.translation = new SpongeTranslation("itemGroup." + this.tabLabel); | ||
| } | ||
| return this.translation; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("Name", this.tabLabel) | ||
| .toString(); | ||
| } | ||
| } |
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
83 changes: 83 additions & 0 deletions
83
src/main/java/org/spongepowered/common/registry/type/item/ItemGroupRegistryModule.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,83 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.registry.type.item; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import net.minecraft.creativetab.CreativeTabs; | ||
| import org.spongepowered.api.item.ItemGroup; | ||
| import org.spongepowered.api.item.ItemGroups; | ||
| import org.spongepowered.api.registry.CatalogRegistryModule; | ||
| import org.spongepowered.api.registry.util.AdditionalRegistration; | ||
| import org.spongepowered.api.registry.util.RegisterCatalog; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class ItemGroupRegistryModule implements CatalogRegistryModule<ItemGroup> { | ||
|
|
||
| @RegisterCatalog(ItemGroups.class) | ||
| private final Map<String, ItemGroup> itemGroupMap = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public Optional<ItemGroup> getById(String id) { | ||
| String key = checkNotNull(id).toLowerCase(Locale.ENGLISH); | ||
| return Optional.ofNullable(this.itemGroupMap.get(key)); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<ItemGroup> getAll() { | ||
| return Collections.unmodifiableCollection(this.itemGroupMap.values()); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerDefaults() { | ||
| this.itemGroupMap.put("brewing", (ItemGroup) CreativeTabs.BREWING); | ||
| this.itemGroupMap.put("building_blocks", (ItemGroup) CreativeTabs.BUILDING_BLOCKS); | ||
| this.itemGroupMap.put("combat", (ItemGroup) CreativeTabs.COMBAT); | ||
| this.itemGroupMap.put("decorations", (ItemGroup) CreativeTabs.DECORATIONS); | ||
| this.itemGroupMap.put("food", (ItemGroup) CreativeTabs.FOOD); | ||
| this.itemGroupMap.put("materials", (ItemGroup) CreativeTabs.MATERIALS); | ||
| this.itemGroupMap.put("misc", (ItemGroup) CreativeTabs.MISC); | ||
| this.itemGroupMap.put("redstone", (ItemGroup) CreativeTabs.REDSTONE); | ||
| this.itemGroupMap.put("tools", (ItemGroup) CreativeTabs.TOOLS); | ||
| this.itemGroupMap.put("transportation", (ItemGroup) CreativeTabs.TRANSPORTATION); | ||
| } | ||
|
|
||
| @AdditionalRegistration | ||
| public void customRegistration() { | ||
| for (CreativeTabs creativeTab : CreativeTabs.CREATIVE_TAB_ARRAY) { | ||
| ItemGroup itemGroup = (ItemGroup) creativeTab; | ||
| String key = itemGroup.getId().toLowerCase(Locale.ENGLISH); | ||
| if (!this.itemGroupMap.containsKey(key)) { | ||
| this.itemGroupMap.put(key, itemGroup); | ||
| } | ||
| } | ||
| } | ||
| } |
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
48 changes: 48 additions & 0 deletions
48
testplugins/src/main/java/org/spongepowered/test/ItemGroupTest.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,48 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.test; | ||
|
|
||
| import org.spongepowered.api.entity.living.player.Player; | ||
| import org.spongepowered.api.event.Listener; | ||
| import org.spongepowered.api.event.filter.cause.First; | ||
| import org.spongepowered.api.event.item.inventory.InteractItemEvent; | ||
| import org.spongepowered.api.item.ItemGroup; | ||
| import org.spongepowered.api.plugin.Plugin; | ||
| import org.spongepowered.api.text.Text; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Plugin(id = "item_group_test", name = "Item Group Test", version = "0.0.0", description = ItemGroupTest.DESCRIPTION) | ||
| public class ItemGroupTest { | ||
|
|
||
| public static final String DESCRIPTION = "Right click an item to get the ItemGroup"; | ||
|
|
||
| @Listener | ||
| public void onUseItem(InteractItemEvent.Secondary event, @First Player player) { | ||
| Collection<ItemGroup> itemGroups = event.getItemStack().getType().getItemGroups(); | ||
| player.sendMessage(Text.of("This item is in the item group(s): " + itemGroups.stream().map(ItemGroup::getName).collect(Collectors.joining(", ")))); | ||
| } | ||
| } | ||
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.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.