| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.HolderGetter;
|
| 7 | import net.minecraft.core.HolderSet;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.sounds.SoundEvents;
|
| 11 | import net.minecraft.sounds.SoundSource;
|
| 12 | import net.minecraft.tags.BannerPatternTags;
|
| 13 | import net.minecraft.tags.ItemTags;
|
| 14 | import net.minecraft.world.Container;
|
| 15 | import net.minecraft.world.SimpleContainer;
|
| 16 | import net.minecraft.world.entity.player.Inventory;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.item.BannerItem;
|
| 19 | import net.minecraft.world.item.DyeColor;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 23 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 24 |
|
| 25 | public class LoomMenu extends AbstractContainerMenu {
|
| 26 | private static final int PATTERN_NOT_SET = -1;
|
| 27 | private static final int INV_SLOT_START = 4;
|
| 28 | private static final int INV_SLOT_END = 31;
|
| 29 | private static final int USE_ROW_SLOT_START = 31;
|
| 30 | private static final int USE_ROW_SLOT_END = 40;
|
| 31 | private final ContainerLevelAccess access;
|
| 32 | private final DataSlot selectedBannerPatternIndex = DataSlot.standalone();
|
| 33 | private List<Holder<BannerPattern>> selectablePatterns = List.of();
|
| 34 | private Runnable slotUpdateListener = () -> {};
|
| 35 | private final HolderGetter<BannerPattern> patternGetter;
|
| 36 | private final Slot bannerSlot;
|
| 37 | private final Slot dyeSlot;
|
| 38 | private final Slot patternSlot;
|
| 39 | private final Slot resultSlot;
|
| 40 | private long lastSoundTime;
|
| 41 | private final Container inputContainer = new SimpleContainer(3) {
|
| 42 | @Override
|
| 43 | public void setChanged() {
|
| 44 | super.setChanged();
|
| 45 | LoomMenu.this.slotsChanged(this);
|
| 46 | LoomMenu.this.slotUpdateListener.run();
|
| 47 | }
|
| 48 | };
|
| 49 | private final Container outputContainer = new SimpleContainer(1) {
|
| 50 | @Override
|
| 51 | public void setChanged() {
|
| 52 | super.setChanged();
|
| 53 | LoomMenu.this.slotUpdateListener.run();
|
| 54 | }
|
| 55 | };
|
| 56 |
|
| 57 | public LoomMenu(final int containerId, final Inventory inventory) {
|
| 58 | this(containerId, inventory, ContainerLevelAccess.NULL);
|
| 59 | }
|
| 60 |
|
| 61 | public LoomMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
|
| 62 | super(MenuType.LOOM, containerId);
|
| 63 | this.access = access;
|
| 64 | this.bannerSlot = this.addSlot(new Slot(this.inputContainer, 0, 13, 26) {
|
| 65 | @Override
|
| 66 | public boolean mayPlace(final ItemStack itemStack) {
|
| 67 | return itemStack.getItem() instanceof BannerItem;
|
| 68 | }
|
| 69 | });
|
| 70 | this.dyeSlot = this.addSlot(new Slot(this.inputContainer, 1, 33, 26) {
|
| 71 | @Override
|
| 72 | public boolean mayPlace(final ItemStack itemStack) {
|
| 73 | return LoomMenu.isDyeItem(itemStack);
|
| 74 | }
|
| 75 | });
|
| 76 | this.patternSlot = this.addSlot(new Slot(this.inputContainer, 2, 23, 45) {
|
| 77 | @Override
|
| 78 | public boolean mayPlace(final ItemStack itemStack) {
|
| 79 | return LoomMenu.isPatternItem(itemStack);
|
| 80 | }
|
| 81 | });
|
| 82 | this.resultSlot = this.addSlot(new Slot(this.outputContainer, 0, 143, 57) {
|
| 83 | @Override
|
| 84 | public boolean mayPlace(final ItemStack itemStack) {
|
| 85 | return false;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void onTake(final Player player, final ItemStack carried) {
|
| 90 | LoomMenu.this.bannerSlot.remove(1);
|
| 91 | LoomMenu.this.dyeSlot.remove(1);
|
| 92 | if (!LoomMenu.this.bannerSlot.hasItem() || !LoomMenu.this.dyeSlot.hasItem()) {
|
| 93 | LoomMenu.this.selectedBannerPatternIndex.set(-1);
|
| 94 | }
|
| 95 |
|
| 96 | access.execute((level, pos) -> {
|
| 97 | long gameTime = level.getGameTime();
|
| 98 | if (LoomMenu.this.lastSoundTime != gameTime) {
|
| 99 | level.playSound(null, pos, SoundEvents.UI_LOOM_TAKE_RESULT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 100 | LoomMenu.this.lastSoundTime = gameTime;
|
| 101 | }
|
| 102 | });
|
| 103 | super.onTake(player, carried);
|
| 104 | }
|
| 105 | });
|
| 106 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 107 | this.addDataSlot(this.selectedBannerPatternIndex);
|
| 108 | this.patternGetter = inventory.player.registryAccess().lookupOrThrow(Registries.BANNER_PATTERN);
|
| 109 | }
|
| 110 |
|
| 111 | private static boolean isPatternItem(final ItemStack itemStack) {
|
| 112 | return itemStack.is(ItemTags.LOOM_PATTERNS) && itemStack.has(DataComponents.PROVIDES_BANNER_PATTERNS);
|
| 113 | }
|
| 114 |
|
| 115 | private static boolean isDyeItem(final ItemStack itemStack) {
|
| 116 | return itemStack.is(ItemTags.LOOM_DYES) && itemStack.has(DataComponents.DYE);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public boolean stillValid(final Player player) {
|
| 121 | return stillValid(this.access, player, Blocks.LOOM);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public boolean clickMenuButton(final Player player, final int buttonId) {
|
| 126 | if (buttonId >= 0 && buttonId < this.selectablePatterns.size()) {
|
| 127 | this.selectedBannerPatternIndex.set(buttonId);
|
| 128 | this.setupResultSlot(this.selectablePatterns.get(buttonId));
|
| 129 | return true;
|
| 130 | } else {
|
| 131 | return false;
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | private List<Holder<BannerPattern>> getSelectablePatterns(final ItemStack patternStack) {
|
| 136 | if (patternStack.isEmpty()) {
|
| 137 | return this.patternGetter.get(BannerPatternTags.NO_ITEM_REQUIRED).map(ImmutableList::copyOf).orElse(ImmutableList.of());
|
| 138 | }
|
| 139 |
|
| 140 | HolderSet<BannerPattern> itemPatterns = patternStack.get(DataComponents.PROVIDES_BANNER_PATTERNS);
|
| 141 | return itemPatterns != null ? ImmutableList.copyOf(itemPatterns) : ImmutableList.of();
|
| 142 | }
|
| 143 |
|
| 144 | private boolean isValidPatternIndex(final int selectedPattern) {
|
| 145 | return selectedPattern >= 0 && selectedPattern < this.selectablePatterns.size();
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public void slotsChanged(final Container container) {
|
| 150 | ItemStack bannerStack = this.bannerSlot.getItem();
|
| 151 | ItemStack dyeStack = this.dyeSlot.getItem();
|
| 152 | ItemStack patternStack = this.patternSlot.getItem();
|
| 153 | if (!bannerStack.isEmpty() && !dyeStack.isEmpty()) {
|
| 154 | int selectedPattern = this.selectedBannerPatternIndex.get();
|
| 155 | boolean validPatternIndex = this.isValidPatternIndex(selectedPattern);
|
| 156 | List<Holder<BannerPattern>> previousSelectablePatterns = this.selectablePatterns;
|
| 157 | this.selectablePatterns = this.getSelectablePatterns(patternStack);
|
| 158 | Holder<BannerPattern> patternToDisplay;
|
| 159 | if (this.selectablePatterns.size() == 1) {
|
| 160 | this.selectedBannerPatternIndex.set(0);
|
| 161 | patternToDisplay = this.selectablePatterns.get(0);
|
| 162 | } else if (!validPatternIndex) {
|
| 163 | this.selectedBannerPatternIndex.set(-1);
|
| 164 | patternToDisplay = null;
|
| 165 | } else {
|
| 166 | Holder<BannerPattern> selectedValue = previousSelectablePatterns.get(selectedPattern);
|
| 167 | int newSelectedIndex = this.selectablePatterns.indexOf(selectedValue);
|
| 168 | if (newSelectedIndex != -1) {
|
| 169 | patternToDisplay = selectedValue;
|
| 170 | this.selectedBannerPatternIndex.set(newSelectedIndex);
|
| 171 | } else {
|
| 172 | patternToDisplay = null;
|
| 173 | this.selectedBannerPatternIndex.set(-1);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | if (patternToDisplay != null) {
|
| 178 | BannerPatternLayers patterns = bannerStack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 179 | boolean hasMaxPatterns = patterns.layers().size() >= 6;
|
| 180 | if (hasMaxPatterns) {
|
| 181 | this.selectedBannerPatternIndex.set(-1);
|
| 182 | this.resultSlot.set(ItemStack.EMPTY);
|
| 183 | } else {
|
| 184 | this.setupResultSlot(patternToDisplay);
|
| 185 | }
|
| 186 | } else {
|
| 187 | this.resultSlot.set(ItemStack.EMPTY);
|
| 188 | }
|
| 189 |
|
| 190 | this.broadcastChanges();
|
| 191 | } else {
|
| 192 | this.resultSlot.set(ItemStack.EMPTY);
|
| 193 | this.selectablePatterns = List.of();
|
| 194 | this.selectedBannerPatternIndex.set(-1);
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | public List<Holder<BannerPattern>> getSelectablePatterns() {
|
| 199 | return this.selectablePatterns;
|
| 200 | }
|
| 201 |
|
| 202 | public int getSelectedBannerPatternIndex() {
|
| 203 | return this.selectedBannerPatternIndex.get();
|
| 204 | }
|
| 205 |
|
| 206 | public void registerUpdateListener(final Runnable slotUpdateListener) {
|
| 207 | this.slotUpdateListener = slotUpdateListener;
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 212 | ItemStack clicked = ItemStack.EMPTY;
|
| 213 | Slot slot = this.slots.get(slotIndex);
|
| 214 | if (slot != null && slot.hasItem()) {
|
| 215 | ItemStack stack = slot.getItem();
|
| 216 | clicked = stack.copy();
|
| 217 | if (slotIndex == this.resultSlot.index) {
|
| 218 | if (!this.moveItemStackTo(stack, 4, 40, true)) {
|
| 219 | return ItemStack.EMPTY;
|
| 220 | }
|
| 221 |
|
| 222 | slot.onQuickCraft(stack, clicked);
|
| 223 | } else if (slotIndex != this.dyeSlot.index && slotIndex != this.bannerSlot.index && slotIndex != this.patternSlot.index) {
|
| 224 | if (stack.getItem() instanceof BannerItem) {
|
| 225 | if (!this.moveItemStackTo(stack, this.bannerSlot.index, this.bannerSlot.index + 1, false)) {
|
| 226 | return ItemStack.EMPTY;
|
| 227 | }
|
| 228 | } else if (isDyeItem(stack)) {
|
| 229 | if (!this.moveItemStackTo(stack, this.dyeSlot.index, this.dyeSlot.index + 1, false)) {
|
| 230 | return ItemStack.EMPTY;
|
| 231 | }
|
| 232 | } else if (isPatternItem(stack)) {
|
| 233 | if (!this.moveItemStackTo(stack, this.patternSlot.index, this.patternSlot.index + 1, false)) {
|
| 234 | return ItemStack.EMPTY;
|
| 235 | }
|
| 236 | } else if (slotIndex >= 4 && slotIndex < 31) {
|
| 237 | if (!this.moveItemStackTo(stack, 31, 40, false)) {
|
| 238 | return ItemStack.EMPTY;
|
| 239 | }
|
| 240 | } else if (slotIndex >= 31 && slotIndex < 40 && !this.moveItemStackTo(stack, 4, 31, false)) {
|
| 241 | return ItemStack.EMPTY;
|
| 242 | }
|
| 243 | } else if (!this.moveItemStackTo(stack, 4, 40, false)) {
|
| 244 | return ItemStack.EMPTY;
|
| 245 | }
|
| 246 |
|
| 247 | if (stack.isEmpty()) {
|
| 248 | slot.setByPlayer(ItemStack.EMPTY);
|
| 249 | } else {
|
| 250 | slot.setChanged();
|
| 251 | }
|
| 252 |
|
| 253 | if (stack.getCount() == clicked.getCount()) {
|
| 254 | return ItemStack.EMPTY;
|
| 255 | }
|
| 256 |
|
| 257 | slot.onTake(player, stack);
|
| 258 | }
|
| 259 |
|
| 260 | return clicked;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public void removed(final Player player) {
|
| 265 | super.removed(player);
|
| 266 | this.access.execute((level, pos) -> this.clearContainer(player, this.inputContainer));
|
| 267 | }
|
| 268 |
|
| 269 | private void setupResultSlot(final Holder<BannerPattern> pattern) {
|
| 270 | ItemStack bannerStack = this.bannerSlot.getItem();
|
| 271 | ItemStack dyeStack = this.dyeSlot.getItem();
|
| 272 | ItemStack result = ItemStack.EMPTY;
|
| 273 | if (!bannerStack.isEmpty() && !dyeStack.isEmpty()) {
|
| 274 | DyeColor patternColor = dyeStack.get(DataComponents.DYE);
|
| 275 | if (patternColor != null) {
|
| 276 | result = bannerStack.copyWithCount(1);
|
| 277 | result.update(
|
| 278 | DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY, layers -> new BannerPatternLayers.Builder().addAll(layers).add(pattern, patternColor).build()
|
| 279 | );
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | if (!ItemStack.matches(result, this.resultSlot.getItem())) {
|
| 284 | this.resultSlot.set(result);
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | public Slot getBannerSlot() {
|
| 289 | return this.bannerSlot;
|
| 290 | }
|
| 291 |
|
| 292 | public Slot getDyeSlot() {
|
| 293 | return this.dyeSlot;
|
| 294 | }
|
| 295 |
|
| 296 | public Slot getPatternSlot() {
|
| 297 | return this.patternSlot;
|
| 298 | }
|
| 299 |
|
| 300 | public Slot getResultSlot() {
|
| 301 | return this.resultSlot;
|
| 302 | }
|
| 303 | }
|
| 304 |
|