Skip to content
Closed
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.common.mixin.core.item;

import com.google.common.base.MoreObjects;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.spongepowered.api.block.BlockType;
Expand All @@ -33,6 +34,7 @@
import org.spongepowered.api.data.manipulator.mutable.DisplayNameData;
import org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData;
import org.spongepowered.api.data.manipulator.mutable.item.LoreData;
import org.spongepowered.api.item.ItemGroup;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.text.translation.Translation;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -43,6 +45,8 @@
import org.spongepowered.common.registry.SpongeGameDictionaryEntry;
import org.spongepowered.common.text.translation.SpongeTranslation;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand All @@ -57,6 +61,7 @@ public abstract class MixinItem implements ItemType, IMixinItem, SpongeGameDicti

@Shadow public abstract int getItemStackLimit();
@Shadow public abstract String getUnlocalizedName();
@Shadow @Nullable public abstract CreativeTabs getCreativeTab();

// A item stack used to retrieve properties
@Nullable private org.spongepowered.api.item.inventory.ItemStack propertyItemStack;
Expand All @@ -71,6 +76,16 @@ public String getName() {
return getId();
}

@Override
public Collection<ItemGroup> getItemGroups() {
CreativeTabs creativeTab = getCreativeTab();
if (creativeTab == null) {
return Collections.emptySet();
} else {
return Collections.singleton((ItemGroup) creativeTab);
}
}

@Override
public <T extends Property<?, ?>> Optional<T> getDefaultProperty(Class<T> propertyClass) {
if (this.propertyItemStack == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.spongepowered.api.boss.ServerBossBar;
import org.spongepowered.api.data.DataRegistration;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.item.ItemGroup;
import org.spongepowered.api.item.enchantment.Enchantment;
import org.spongepowered.api.data.meta.PatternLayer;
import org.spongepowered.api.data.persistence.DataFormat;
Expand Down Expand Up @@ -458,6 +459,7 @@ protected void registerCommonModules(SpongeGameRegistry registry) {
.registerModule(GoldenApple.class, new GoldenAppleRegistryModule())
.registerModule(Hinge.class, new HingeRegistryModule())
.registerModule(IntegerTrait.class, IntegerTraitRegistryModule.getInstance())
.registerModule(ItemGroup.class, new ItemGroupRegistryModule())
.registerModule(ItemType.class, ItemTypeRegistryModule.getInstance())
.registerModule(new LocaleRegistryModule())
.registerModule(LogAxis.class, new LogAxisRegistryModule())
Expand Down
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);
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/common_at.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ public-f org.spongepowered.api.extra.fluid.FluidTypes *

public-f org.spongepowered.api.item.enchantment.EnchantmentTypes *
public-f org.spongepowered.api.item.FireworkShapes *
public-f org.spongepowered.api.item.ItemGroups *
public-f org.spongepowered.api.item.ItemTypes *

public-f org.spongepowered.api.item.inventory.InventoryArchetypes *
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.common.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"command.server.MixinCommandSummon",
"command.server.MixinCommandTeleport",
"command.server.MixinCommandTP",
"creativetab.MixinCreativeTabs",
"data.MixinCustomDataHolder",
"data.MixinDataHolder",
"data.MixinPropertyHolder",
Expand Down
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;

This comment was marked as resolved.


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(", "))));
}
}