| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.tags.EnchantmentTags;
|
| 8 | import net.minecraft.world.Container;
|
| 9 | import net.minecraft.world.SimpleContainer;
|
| 10 | import net.minecraft.world.entity.ExperienceOrb;
|
| 11 | import net.minecraft.world.entity.player.Inventory;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.Items;
|
| 15 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 16 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 17 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.block.Blocks;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 |
|
| 22 | public class GrindstoneMenu extends AbstractContainerMenu {
|
| 23 | public static final int MAX_NAME_LENGTH = 35;
|
| 24 | public static final int INPUT_SLOT = 0;
|
| 25 | public static final int ADDITIONAL_SLOT = 1;
|
| 26 | public static final int RESULT_SLOT = 2;
|
| 27 | private static final int INV_SLOT_START = 3;
|
| 28 | private static final int INV_SLOT_END = 30;
|
| 29 | private static final int USE_ROW_SLOT_START = 30;
|
| 30 | private static final int USE_ROW_SLOT_END = 39;
|
| 31 | private final Container resultSlots = new ResultContainer();
|
| 32 | private final Container repairSlots = new SimpleContainer(2) {
|
| 33 | @Override
|
| 34 | public void setChanged() {
|
| 35 | super.setChanged();
|
| 36 | GrindstoneMenu.this.slotsChanged(this);
|
| 37 | }
|
| 38 | };
|
| 39 | private final ContainerLevelAccess access;
|
| 40 |
|
| 41 | public GrindstoneMenu(final int containerId, final Inventory inventory) {
|
| 42 | this(containerId, inventory, ContainerLevelAccess.NULL);
|
| 43 | }
|
| 44 |
|
| 45 | public GrindstoneMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
|
| 46 | super(MenuType.GRINDSTONE, containerId);
|
| 47 | this.access = access;
|
| 48 | this.addSlot(new Slot(this.repairSlots, 0, 49, 19) {
|
| 49 | @Override
|
| 50 | public boolean mayPlace(final ItemStack itemStack) {
|
| 51 | return itemStack.isDamageableItem() || EnchantmentHelper.hasAnyEnchantments(itemStack);
|
| 52 | }
|
| 53 | });
|
| 54 | this.addSlot(new Slot(this.repairSlots, 1, 49, 40) {
|
| 55 | @Override
|
| 56 | public boolean mayPlace(final ItemStack itemStack) {
|
| 57 | return itemStack.isDamageableItem() || EnchantmentHelper.hasAnyEnchantments(itemStack);
|
| 58 | }
|
| 59 | });
|
| 60 | this.addSlot(new Slot(this.resultSlots, 2, 129, 34) {
|
| 61 | @Override
|
| 62 | public boolean mayPlace(final ItemStack itemStack) {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void onTake(final Player player, final ItemStack carried) {
|
| 68 | access.execute((level, pos) -> {
|
| 69 | if (level instanceof ServerLevel serverLevel) {
|
| 70 | ExperienceOrb.award(serverLevel, Vec3.atCenterOf(pos), this.getExperienceAmount(level));
|
| 71 | }
|
| 72 |
|
| 73 | level.levelEvent(1042, pos, 0);
|
| 74 | });
|
| 75 | GrindstoneMenu.this.repairSlots.setItem(0, ItemStack.EMPTY);
|
| 76 | GrindstoneMenu.this.repairSlots.setItem(1, ItemStack.EMPTY);
|
| 77 | }
|
| 78 |
|
| 79 | private int getExperienceAmount(final Level level) {
|
| 80 | int amount = 0;
|
| 81 | amount += this.getExperienceFromItem(GrindstoneMenu.this.repairSlots.getItem(0));
|
| 82 | amount += this.getExperienceFromItem(GrindstoneMenu.this.repairSlots.getItem(1));
|
| 83 | if (amount > 0) {
|
| 84 | int halfAmount = (int)Math.ceil(amount / 2.0);
|
| 85 | return halfAmount + level.getRandom().nextInt(halfAmount);
|
| 86 | } else {
|
| 87 | return 0;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private int getExperienceFromItem(final ItemStack item) {
|
| 92 | int amount = 0;
|
| 93 | ItemEnchantments enchantments = EnchantmentHelper.getEnchantmentsForCrafting(item);
|
| 94 |
|
| 95 | for (Entry<Holder<Enchantment>> entry : enchantments.entrySet()) {
|
| 96 | Holder<Enchantment> enchant = entry.getKey();
|
| 97 | int lvl = entry.getIntValue();
|
| 98 | if (!enchant.is(EnchantmentTags.CURSE)) {
|
| 99 | amount += enchant.value().getMinCost(lvl);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | return amount;
|
| 104 | }
|
| 105 | });
|
| 106 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void slotsChanged(final Container container) {
|
| 111 | super.slotsChanged(container);
|
| 112 | if (container == this.repairSlots) {
|
| 113 | this.createResult();
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | private void createResult() {
|
| 118 | this.resultSlots.setItem(0, this.computeResult(this.repairSlots.getItem(0), this.repairSlots.getItem(1)));
|
| 119 | this.broadcastChanges();
|
| 120 | }
|
| 121 |
|
| 122 | private ItemStack computeResult(final ItemStack input, final ItemStack additional) {
|
| 123 | boolean hasAnItem = !input.isEmpty() || !additional.isEmpty();
|
| 124 | if (!hasAnItem) {
|
| 125 | return ItemStack.EMPTY;
|
| 126 | }
|
| 127 |
|
| 128 | if (input.getCount() <= 1 && additional.getCount() <= 1) {
|
| 129 | boolean hasBothItems = !input.isEmpty() && !additional.isEmpty();
|
| 130 | if (!hasBothItems) {
|
| 131 | ItemStack item = !input.isEmpty() ? input : additional;
|
| 132 | return !EnchantmentHelper.hasAnyEnchantments(item) ? ItemStack.EMPTY : this.removeNonCursesFrom(item.copy());
|
| 133 | } else {
|
| 134 | return this.mergeItems(input, additional);
|
| 135 | }
|
| 136 | } else {
|
| 137 | return ItemStack.EMPTY;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | private ItemStack mergeItems(final ItemStack input, final ItemStack additional) {
|
| 142 | if (!input.is(additional.getItem())) {
|
| 143 | return ItemStack.EMPTY;
|
| 144 | }
|
| 145 |
|
| 146 | int durability = Math.max(input.getMaxDamage(), additional.getMaxDamage());
|
| 147 | int remaining1 = input.getMaxDamage() - input.getDamageValue();
|
| 148 | int remaining2 = additional.getMaxDamage() - additional.getDamageValue();
|
| 149 | int remaining = remaining1 + remaining2 + durability * 5 / 100;
|
| 150 | int count = 1;
|
| 151 | if (!input.isDamageableItem()) {
|
| 152 | if (input.getMaxStackSize() < 2 || !ItemStack.matches(input, additional)) {
|
| 153 | return ItemStack.EMPTY;
|
| 154 | }
|
| 155 |
|
| 156 | count = 2;
|
| 157 | }
|
| 158 |
|
| 159 | ItemStack newItem = input.copyWithCount(count);
|
| 160 | if (newItem.isDamageableItem()) {
|
| 161 | newItem.set(DataComponents.MAX_DAMAGE, durability);
|
| 162 | newItem.setDamageValue(Math.max(durability - remaining, 0));
|
| 163 | }
|
| 164 |
|
| 165 | this.mergeEnchantsFrom(newItem, additional);
|
| 166 | return this.removeNonCursesFrom(newItem);
|
| 167 | }
|
| 168 |
|
| 169 | private void mergeEnchantsFrom(final ItemStack target, final ItemStack source) {
|
| 170 | EnchantmentHelper.updateEnchantments(target, newEnchantments -> {
|
| 171 | ItemEnchantments enchantments = EnchantmentHelper.getEnchantmentsForCrafting(source);
|
| 172 |
|
| 173 | for (Entry<Holder<Enchantment>> entry : enchantments.entrySet()) {
|
| 174 | Holder<Enchantment> enchant = entry.getKey();
|
| 175 | if (!enchant.is(EnchantmentTags.CURSE) || newEnchantments.getLevel(enchant) == 0) {
|
| 176 | newEnchantments.upgrade(enchant, entry.getIntValue());
|
| 177 | }
|
| 178 | }
|
| 179 | });
|
| 180 | }
|
| 181 |
|
| 182 | private ItemStack removeNonCursesFrom(ItemStack item) {
|
| 183 | ItemEnchantments newEnchantments = EnchantmentHelper.updateEnchantments(
|
| 184 | item, enchantments -> enchantments.removeIf(enchantment -> !enchantment.is(EnchantmentTags.CURSE))
|
| 185 | );
|
| 186 | if (item.is(Items.ENCHANTED_BOOK) && newEnchantments.isEmpty()) {
|
| 187 | item = item.transmuteCopy(Items.BOOK);
|
| 188 | }
|
| 189 |
|
| 190 | int repairCost = 0;
|
| 191 |
|
| 192 | for (int i = 0; i < newEnchantments.size(); i++) {
|
| 193 | repairCost = AnvilMenu.calculateIncreasedRepairCost(repairCost);
|
| 194 | }
|
| 195 |
|
| 196 | item.set(DataComponents.REPAIR_COST, repairCost);
|
| 197 | return item;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public void removed(final Player player) {
|
| 202 | super.removed(player);
|
| 203 | this.access.execute((level, pos) -> this.clearContainer(player, this.repairSlots));
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public boolean stillValid(final Player player) {
|
| 208 | return stillValid(this.access, player, Blocks.GRINDSTONE);
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 213 | ItemStack clicked = ItemStack.EMPTY;
|
| 214 | Slot slot = this.slots.get(slotIndex);
|
| 215 | if (slot != null && slot.hasItem()) {
|
| 216 | ItemStack item = slot.getItem();
|
| 217 | clicked = item.copy();
|
| 218 | ItemStack input = this.repairSlots.getItem(0);
|
| 219 | ItemStack additional = this.repairSlots.getItem(1);
|
| 220 | if (slotIndex == 2) {
|
| 221 | if (!this.moveItemStackTo(item, 3, 39, true)) {
|
| 222 | return ItemStack.EMPTY;
|
| 223 | }
|
| 224 |
|
| 225 | slot.onQuickCraft(item, clicked);
|
| 226 | } else if (slotIndex != 0 && slotIndex != 1) {
|
| 227 | if (!input.isEmpty() && !additional.isEmpty()) {
|
| 228 | if (slotIndex >= 3 && slotIndex < 30) {
|
| 229 | if (!this.moveItemStackTo(item, 30, 39, false)) {
|
| 230 | return ItemStack.EMPTY;
|
| 231 | }
|
| 232 | } else if (slotIndex >= 30 && slotIndex < 39 && !this.moveItemStackTo(item, 3, 30, false)) {
|
| 233 | return ItemStack.EMPTY;
|
| 234 | }
|
| 235 | } else if (!this.moveItemStackTo(item, 0, 2, false)) {
|
| 236 | return ItemStack.EMPTY;
|
| 237 | }
|
| 238 | } else if (!this.moveItemStackTo(item, 3, 39, false)) {
|
| 239 | return ItemStack.EMPTY;
|
| 240 | }
|
| 241 |
|
| 242 | if (item.isEmpty()) {
|
| 243 | slot.setByPlayer(ItemStack.EMPTY);
|
| 244 | } else {
|
| 245 | slot.setChanged();
|
| 246 | }
|
| 247 |
|
| 248 | if (item.getCount() == clicked.getCount()) {
|
| 249 | return ItemStack.EMPTY;
|
| 250 | }
|
| 251 |
|
| 252 | slot.onTake(player, item);
|
| 253 | }
|
| 254 |
|
| 255 | return clicked;
|
| 256 | }
|
| 257 | }
|
| 258 |
|