| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.Collection;
|
| 4 | import java.util.Set;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.minecraft.core.HolderLookup;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 10 | import net.minecraft.world.level.ItemLike;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class CreativeModeTab {
|
| 14 | private static final Identifier DEFAULT_BACKGROUND = createTextureLocation("items");
|
| 15 | private final Component displayName;
|
| 16 | private Identifier backgroundTexture = DEFAULT_BACKGROUND;
|
| 17 | private boolean canScroll = true;
|
| 18 | private boolean showTitle = true;
|
| 19 | private boolean alignedRight = false;
|
| 20 | private final CreativeModeTab.Row row;
|
| 21 | private final int column;
|
| 22 | private final CreativeModeTab.Type type;
|
| 23 | private @Nullable ItemStack iconItemStack;
|
| 24 | private Collection<ItemStack> displayItems = ItemStackLinkedSet.createTypeAndComponentsSet();
|
| 25 | private Set<ItemStack> displayItemsSearchTab = ItemStackLinkedSet.createTypeAndComponentsSet();
|
| 26 | private final Supplier<ItemStack> iconGenerator;
|
| 27 | private final CreativeModeTab.DisplayItemsGenerator displayItemsGenerator;
|
| 28 |
|
| 29 | private CreativeModeTab(
|
| 30 | final CreativeModeTab.Row row,
|
| 31 | final int column,
|
| 32 | final CreativeModeTab.Type type,
|
| 33 | final Component displayName,
|
| 34 | final Supplier<ItemStack> iconGenerator,
|
| 35 | final CreativeModeTab.DisplayItemsGenerator displayItemsGenerator
|
| 36 | ) {
|
| 37 | this.row = row;
|
| 38 | this.column = column;
|
| 39 | this.displayName = displayName;
|
| 40 | this.iconGenerator = iconGenerator;
|
| 41 | this.displayItemsGenerator = displayItemsGenerator;
|
| 42 | this.type = type;
|
| 43 | }
|
| 44 |
|
| 45 | public static Identifier createTextureLocation(final String name) {
|
| 46 | return Identifier.withDefaultNamespace("textures/gui/container/creative_inventory/tab_" + name + ".png");
|
| 47 | }
|
| 48 |
|
| 49 | public static CreativeModeTab.Builder builder(final CreativeModeTab.Row row, final int column) {
|
| 50 | return new CreativeModeTab.Builder(row, column);
|
| 51 | }
|
| 52 |
|
| 53 | public Component getDisplayName() {
|
| 54 | return this.displayName;
|
| 55 | }
|
| 56 |
|
| 57 | public ItemStack getIconItem() {
|
| 58 | if (this.iconItemStack == null) {
|
| 59 | this.iconItemStack = this.iconGenerator.get();
|
| 60 | }
|
| 61 |
|
| 62 | return this.iconItemStack;
|
| 63 | }
|
| 64 |
|
| 65 | public Identifier getBackgroundTexture() {
|
| 66 | return this.backgroundTexture;
|
| 67 | }
|
| 68 |
|
| 69 | public boolean showTitle() {
|
| 70 | return this.showTitle;
|
| 71 | }
|
| 72 |
|
| 73 | public boolean canScroll() {
|
| 74 | return this.canScroll;
|
| 75 | }
|
| 76 |
|
| 77 | public int column() {
|
| 78 | return this.column;
|
| 79 | }
|
| 80 |
|
| 81 | public CreativeModeTab.Row row() {
|
| 82 | return this.row;
|
| 83 | }
|
| 84 |
|
| 85 | public boolean hasAnyItems() {
|
| 86 | return !this.displayItems.isEmpty();
|
| 87 | }
|
| 88 |
|
| 89 | public boolean shouldDisplay() {
|
| 90 | return this.type != CreativeModeTab.Type.CATEGORY || this.hasAnyItems();
|
| 91 | }
|
| 92 |
|
| 93 | public boolean isAlignedRight() {
|
| 94 | return this.alignedRight;
|
| 95 | }
|
| 96 |
|
| 97 | public CreativeModeTab.Type getType() {
|
| 98 | return this.type;
|
| 99 | }
|
| 100 |
|
| 101 | public void buildContents(final CreativeModeTab.ItemDisplayParameters parameters) {
|
| 102 | CreativeModeTab.ItemDisplayBuilder displayList = new CreativeModeTab.ItemDisplayBuilder(this, parameters.enabledFeatures);
|
| 103 | this.displayItemsGenerator.accept(parameters, displayList);
|
| 104 | this.displayItems = displayList.tabContents;
|
| 105 | this.displayItemsSearchTab = displayList.searchTabContents;
|
| 106 | }
|
| 107 |
|
| 108 | public Collection<ItemStack> getDisplayItems() {
|
| 109 | return this.displayItems;
|
| 110 | }
|
| 111 |
|
| 112 | public Collection<ItemStack> getSearchTabDisplayItems() {
|
| 113 | return this.displayItemsSearchTab;
|
| 114 | }
|
| 115 |
|
| 116 | public boolean contains(final ItemStack stack) {
|
| 117 | return this.displayItemsSearchTab.contains(stack);
|
| 118 | }
|
| 119 |
|
| 120 | public static class Builder {
|
| 121 | private static final CreativeModeTab.DisplayItemsGenerator EMPTY_GENERATOR = (parameters, output) -> {};
|
| 122 | private final CreativeModeTab.Row row;
|
| 123 | private final int column;
|
| 124 | private Component displayName = Component.empty();
|
| 125 | private Supplier<ItemStack> iconGenerator = () -> ItemStack.EMPTY;
|
| 126 | private CreativeModeTab.DisplayItemsGenerator displayItemsGenerator = EMPTY_GENERATOR;
|
| 127 | private boolean canScroll = true;
|
| 128 | private boolean showTitle = true;
|
| 129 | private boolean alignedRight = false;
|
| 130 | private CreativeModeTab.Type type = CreativeModeTab.Type.CATEGORY;
|
| 131 | private Identifier backgroundTexture = CreativeModeTab.DEFAULT_BACKGROUND;
|
| 132 |
|
| 133 | public Builder(final CreativeModeTab.Row row, final int column) {
|
| 134 | this.row = row;
|
| 135 | this.column = column;
|
| 136 | }
|
| 137 |
|
| 138 | public CreativeModeTab.Builder title(final Component displayName) {
|
| 139 | this.displayName = displayName;
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | public CreativeModeTab.Builder icon(final Supplier<ItemStack> iconGenerator) {
|
| 144 | this.iconGenerator = iconGenerator;
|
| 145 | return this;
|
| 146 | }
|
| 147 |
|
| 148 | public CreativeModeTab.Builder displayItems(final CreativeModeTab.DisplayItemsGenerator displayItemsGenerator) {
|
| 149 | this.displayItemsGenerator = displayItemsGenerator;
|
| 150 | return this;
|
| 151 | }
|
| 152 |
|
| 153 | public CreativeModeTab.Builder alignedRight() {
|
| 154 | this.alignedRight = true;
|
| 155 | return this;
|
| 156 | }
|
| 157 |
|
| 158 | public CreativeModeTab.Builder hideTitle() {
|
| 159 | this.showTitle = false;
|
| 160 | return this;
|
| 161 | }
|
| 162 |
|
| 163 | public CreativeModeTab.Builder noScrollBar() {
|
| 164 | this.canScroll = false;
|
| 165 | return this;
|
| 166 | }
|
| 167 |
|
| 168 | protected CreativeModeTab.Builder type(final CreativeModeTab.Type type) {
|
| 169 | this.type = type;
|
| 170 | return this;
|
| 171 | }
|
| 172 |
|
| 173 | public CreativeModeTab.Builder backgroundTexture(final Identifier backgroundTexture) {
|
| 174 | this.backgroundTexture = backgroundTexture;
|
| 175 | return this;
|
| 176 | }
|
| 177 |
|
| 178 | public CreativeModeTab build() {
|
| 179 | if ((this.type == CreativeModeTab.Type.HOTBAR || this.type == CreativeModeTab.Type.INVENTORY) && this.displayItemsGenerator != EMPTY_GENERATOR) {
|
| 180 | throw new IllegalStateException("Special tabs can't have display items");
|
| 181 | }
|
| 182 |
|
| 183 | CreativeModeTab tab = new CreativeModeTab(this.row, this.column, this.type, this.displayName, this.iconGenerator, this.displayItemsGenerator);
|
| 184 | tab.alignedRight = this.alignedRight;
|
| 185 | tab.showTitle = this.showTitle;
|
| 186 | tab.canScroll = this.canScroll;
|
| 187 | tab.backgroundTexture = this.backgroundTexture;
|
| 188 | return tab;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | @FunctionalInterface
|
| 193 | public interface DisplayItemsGenerator {
|
| 194 | void accept(CreativeModeTab.ItemDisplayParameters parameters, CreativeModeTab.Output output);
|
| 195 | }
|
| 196 |
|
| 197 | private static class ItemDisplayBuilder implements CreativeModeTab.Output {
|
| 198 | public final Collection<ItemStack> tabContents = ItemStackLinkedSet.createTypeAndComponentsSet();
|
| 199 | public final Set<ItemStack> searchTabContents = ItemStackLinkedSet.createTypeAndComponentsSet();
|
| 200 | private final CreativeModeTab tab;
|
| 201 | private final FeatureFlagSet featureFlagSet;
|
| 202 |
|
| 203 | public ItemDisplayBuilder(final CreativeModeTab tab, final FeatureFlagSet featureFlagSet) {
|
| 204 | this.tab = tab;
|
| 205 | this.featureFlagSet = featureFlagSet;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public void accept(final ItemStack stack, final CreativeModeTab.TabVisibility tabVisibility) {
|
| 210 | if (stack.getCount() != 1) {
|
| 211 | throw new IllegalArgumentException("Stack size must be exactly 1");
|
| 212 | }
|
| 213 |
|
| 214 | boolean foundDuplicateStack = this.tabContents.contains(stack) && tabVisibility != CreativeModeTab.TabVisibility.SEARCH_TAB_ONLY;
|
| 215 | if (foundDuplicateStack) {
|
| 216 | throw new IllegalStateException(
|
| 217 | "Accidentally adding the same item stack twice "
|
| 218 | + stack.getDisplayName().getString()
|
| 219 | + " to a Creative Mode Tab: "
|
| 220 | + this.tab.getDisplayName().getString()
|
| 221 | );
|
| 222 | }
|
| 223 |
|
| 224 | if (stack.getItem().isEnabled(this.featureFlagSet)) {
|
| 225 | switch (tabVisibility) {
|
| 226 | case PARENT_AND_SEARCH_TABS:
|
| 227 | this.tabContents.add(stack);
|
| 228 | this.searchTabContents.add(stack);
|
| 229 | break;
|
| 230 | case PARENT_TAB_ONLY:
|
| 231 | this.tabContents.add(stack);
|
| 232 | break;
|
| 233 | case SEARCH_TAB_ONLY:
|
| 234 | this.searchTabContents.add(stack);
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | public record ItemDisplayParameters(FeatureFlagSet enabledFeatures, boolean hasPermissions, HolderLookup.Provider holders) {
|
| 241 | public boolean needsUpdate(final FeatureFlagSet enabledFeatures, final boolean hasPermissions, final HolderLookup.Provider holders) {
|
| 242 | return !this.enabledFeatures.equals(enabledFeatures) || this.hasPermissions != hasPermissions || this.holders != holders;
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | /**
|
| 247 | * Access widened by fabric-creative-tab-api-v1 to accessible
|
| 248 | */
|
| 249 | public interface Output {
|
| 250 | void accept(final ItemStack stack, final CreativeModeTab.TabVisibility tabVisibility);
|
| 251 |
|
| 252 | default void accept(final ItemStack stack) {
|
| 253 | this.accept(stack, CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
|
| 254 | }
|
| 255 |
|
| 256 | default void accept(final ItemLike item, final CreativeModeTab.TabVisibility tabVisibility) {
|
| 257 | this.accept(new ItemStack(item), tabVisibility);
|
| 258 | }
|
| 259 |
|
| 260 | default void accept(final ItemLike item) {
|
| 261 | this.accept(new ItemStack(item), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
|
| 262 | }
|
| 263 |
|
| 264 | default void acceptAll(final Collection<ItemStack> stacks, final CreativeModeTab.TabVisibility tabVisibility) {
|
| 265 | stacks.forEach(stack -> this.accept(stack, tabVisibility));
|
| 266 | }
|
| 267 |
|
| 268 | default void acceptAll(final Collection<ItemStack> stacks) {
|
| 269 | this.acceptAll(stacks, CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | public enum Row {
|
| 274 | TOP,
|
| 275 | BOTTOM;
|
| 276 | }
|
| 277 |
|
| 278 | /**
|
| 279 | * Access widened by fabric-creative-tab-api-v1 to accessible
|
| 280 | */
|
| 281 | public enum TabVisibility {
|
| 282 | PARENT_AND_SEARCH_TABS,
|
| 283 | PARENT_TAB_ONLY,
|
| 284 | SEARCH_TAB_ONLY;
|
| 285 | }
|
| 286 |
|
| 287 | public enum Type {
|
| 288 | CATEGORY,
|
| 289 | INVENTORY,
|
| 290 | HOTBAR,
|
| 291 | SEARCH;
|
| 292 | }
|
| 293 | }
|
| 294 |
|