| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.server.level.ServerPlayer;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.util.StringUtil;
|
| 12 | import net.minecraft.world.entity.player.Inventory;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.Items;
|
| 16 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 17 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 18 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 19 | import net.minecraft.world.level.block.AnvilBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | public class AnvilMenu extends ItemCombinerMenu {
|
| 25 | public static final int INPUT_SLOT = 0;
|
| 26 | public static final int ADDITIONAL_SLOT = 1;
|
| 27 | public static final int RESULT_SLOT = 2;
|
| 28 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 29 | private static final boolean DEBUG_COST = false;
|
| 30 | public static final int MAX_NAME_LENGTH = 50;
|
| 31 | private int repairItemCountCost;
|
| 32 | private @Nullable String itemName;
|
| 33 | private final DataSlot cost = DataSlot.standalone();
|
| 34 | private boolean onlyRenaming = false;
|
| 35 | private static final int COST_FAIL = 0;
|
| 36 | private static final int COST_BASE = 1;
|
| 37 | private static final int COST_ADDED_BASE = 1;
|
| 38 | private static final int COST_REPAIR_MATERIAL = 1;
|
| 39 | private static final int COST_REPAIR_SACRIFICE = 2;
|
| 40 | private static final int COST_INCOMPATIBLE_PENALTY = 1;
|
| 41 | private static final int COST_RENAME = 1;
|
| 42 | private static final int INPUT_SLOT_X_PLACEMENT = 27;
|
| 43 | private static final int ADDITIONAL_SLOT_X_PLACEMENT = 76;
|
| 44 | private static final int RESULT_SLOT_X_PLACEMENT = 134;
|
| 45 | private static final int SLOT_Y_PLACEMENT = 47;
|
| 46 |
|
| 47 | public AnvilMenu(final int containerId, final Inventory inventory) {
|
| 48 | this(containerId, inventory, ContainerLevelAccess.NULL);
|
| 49 | }
|
| 50 |
|
| 51 | public AnvilMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
|
| 52 | super(MenuType.ANVIL, containerId, inventory, access, createInputSlotDefinitions());
|
| 53 | this.addDataSlot(this.cost);
|
| 54 | }
|
| 55 |
|
| 56 | private static ItemCombinerMenuSlotDefinition createInputSlotDefinitions() {
|
| 57 | return ItemCombinerMenuSlotDefinition.create()
|
| 58 | .withSlot(0, 27, 47, itemStack -> true)
|
| 59 | .withSlot(1, 76, 47, itemStack -> true)
|
| 60 | .withResultSlot(2, 134, 47)
|
| 61 | .build();
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected boolean isValidBlock(final BlockState state) {
|
| 66 | return state.is(BlockTags.ANVIL);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected boolean mayPickup(final Player player, final boolean hasItem) {
|
| 71 | return (player.hasInfiniteMaterials() || player.experienceLevel >= this.cost.get()) && this.cost.get() > 0;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void onTake(final Player player, final ItemStack carried) {
|
| 76 | if (!player.hasInfiniteMaterials()) {
|
| 77 | player.giveExperienceLevels(-this.cost.get());
|
| 78 | }
|
| 79 |
|
| 80 | if (this.repairItemCountCost > 0) {
|
| 81 | ItemStack addition = this.inputSlots.getItem(1);
|
| 82 | if (!addition.isEmpty() && addition.getCount() > this.repairItemCountCost) {
|
| 83 | addition.shrink(this.repairItemCountCost);
|
| 84 | this.inputSlots.setItem(1, addition);
|
| 85 | } else {
|
| 86 | this.inputSlots.setItem(1, ItemStack.EMPTY);
|
| 87 | }
|
| 88 | } else if (!this.onlyRenaming) {
|
| 89 | this.inputSlots.setItem(1, ItemStack.EMPTY);
|
| 90 | }
|
| 91 |
|
| 92 | this.cost.set(0);
|
| 93 | if (player instanceof ServerPlayer serverPlayer
|
| 94 | && !StringUtil.isBlank(this.itemName)
|
| 95 | && !this.inputSlots.getItem(0).getHoverName().getString().equals(this.itemName)) {
|
| 96 | serverPlayer.getTextFilter().processStreamMessage(this.itemName);
|
| 97 | }
|
| 98 |
|
| 99 | this.inputSlots.setItem(0, ItemStack.EMPTY);
|
| 100 | this.access.execute((level, pos) -> {
|
| 101 | BlockState state = level.getBlockState(pos);
|
| 102 | if (!player.hasInfiniteMaterials() && state.is(BlockTags.ANVIL) && player.getRandom().nextFloat() < 0.12F) {
|
| 103 | BlockState newBlockState = AnvilBlock.damage(state);
|
| 104 | if (newBlockState == null) {
|
| 105 | level.removeBlock(pos, false);
|
| 106 | level.levelEvent(1029, pos, 0);
|
| 107 | } else {
|
| 108 | level.setBlock(pos, newBlockState, 2);
|
| 109 | level.levelEvent(1030, pos, 0);
|
| 110 | }
|
| 111 | } else {
|
| 112 | level.levelEvent(1030, pos, 0);
|
| 113 | }
|
| 114 | });
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void createResult() {
|
| 119 | ItemStack input = this.inputSlots.getItem(0);
|
| 120 | this.onlyRenaming = false;
|
| 121 | this.cost.set(1);
|
| 122 | int price = 0;
|
| 123 | long tax = 0L;
|
| 124 | int namingCost = 0;
|
| 125 | if (!input.isEmpty() && EnchantmentHelper.canStoreEnchantments(input)) {
|
| 126 | ItemStack result = input.copy();
|
| 127 | ItemStack addition = this.inputSlots.getItem(1);
|
| 128 | ItemEnchantments.Mutable enchantments = new ItemEnchantments.Mutable(EnchantmentHelper.getEnchantmentsForCrafting(result));
|
| 129 | tax += (long)input.getOrDefault(DataComponents.REPAIR_COST, 0).intValue() + addition.getOrDefault(DataComponents.REPAIR_COST, 0).intValue();
|
| 130 | this.repairItemCountCost = 0;
|
| 131 | if (!addition.isEmpty()) {
|
| 132 | boolean usingBook = addition.has(DataComponents.STORED_ENCHANTMENTS);
|
| 133 | if (result.isDamageableItem() && input.isValidRepairItem(addition)) {
|
| 134 | int repairAmount = Math.min(result.getDamageValue(), result.getMaxDamage() / 4);
|
| 135 | if (repairAmount <= 0) {
|
| 136 | this.resultSlots.setItem(0, ItemStack.EMPTY);
|
| 137 | this.cost.set(0);
|
| 138 | return;
|
| 139 | }
|
| 140 |
|
| 141 | int count;
|
| 142 | for (count = 0; repairAmount > 0 && count < addition.getCount(); count++) {
|
| 143 | int resultDamage = result.getDamageValue() - repairAmount;
|
| 144 | result.setDamageValue(resultDamage);
|
| 145 | price++;
|
| 146 | repairAmount = Math.min(result.getDamageValue(), result.getMaxDamage() / 4);
|
| 147 | }
|
| 148 |
|
| 149 | this.repairItemCountCost = count;
|
| 150 | } else {
|
| 151 | if (!usingBook && (!result.is(addition.getItem()) || !result.isDamageableItem())) {
|
| 152 | this.resultSlots.setItem(0, ItemStack.EMPTY);
|
| 153 | this.cost.set(0);
|
| 154 | return;
|
| 155 | }
|
| 156 |
|
| 157 | if (result.isDamageableItem() && !usingBook) {
|
| 158 | int remaining1 = input.getMaxDamage() - input.getDamageValue();
|
| 159 | int remaining2 = addition.getMaxDamage() - addition.getDamageValue();
|
| 160 | int additional = remaining2 + result.getMaxDamage() * 12 / 100;
|
| 161 | int remaining = remaining1 + additional;
|
| 162 | int resultDamage = result.getMaxDamage() - remaining;
|
| 163 | if (resultDamage < 0) {
|
| 164 | resultDamage = 0;
|
| 165 | }
|
| 166 |
|
| 167 | if (resultDamage < result.getDamageValue()) {
|
| 168 | result.setDamageValue(resultDamage);
|
| 169 | price += 2;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | ItemEnchantments additionalEnchantments = EnchantmentHelper.getEnchantmentsForCrafting(addition);
|
| 174 | boolean isAnyEnchantmentCompatible = false;
|
| 175 | boolean isAnyEnchantmentNotCompatible = false;
|
| 176 |
|
| 177 | for (Entry<Holder<Enchantment>> entry : additionalEnchantments.entrySet()) {
|
| 178 | Holder<Enchantment> enchantmentHolder = entry.getKey();
|
| 179 | int current = enchantments.getLevel(enchantmentHolder);
|
| 180 | int level = entry.getIntValue();
|
| 181 | level = current == level ? level + 1 : Math.max(level, current);
|
| 182 | Enchantment enchantment = enchantmentHolder.value();
|
| 183 | boolean compatible = enchantment.canEnchant(input);
|
| 184 | if (this.player.hasInfiniteMaterials() || input.is(Items.ENCHANTED_BOOK)) {
|
| 185 | compatible = true;
|
| 186 | }
|
| 187 |
|
| 188 | for (Holder<Enchantment> other : enchantments.keySet()) {
|
| 189 | if (!other.equals(enchantmentHolder) && !Enchantment.areCompatible(enchantmentHolder, other)) {
|
| 190 | compatible = false;
|
| 191 | price++;
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | if (!compatible) {
|
| 196 | isAnyEnchantmentNotCompatible = true;
|
| 197 | } else {
|
| 198 | isAnyEnchantmentCompatible = true;
|
| 199 | if (level > enchantment.getMaxLevel()) {
|
| 200 | level = enchantment.getMaxLevel();
|
| 201 | }
|
| 202 |
|
| 203 | enchantments.set(enchantmentHolder, level);
|
| 204 | int fee = enchantment.getAnvilCost();
|
| 205 | if (usingBook) {
|
| 206 | fee = Math.max(1, fee / 2);
|
| 207 | }
|
| 208 |
|
| 209 | price += fee * level;
|
| 210 | if (input.getCount() > 1) {
|
| 211 | price = 40;
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | if (isAnyEnchantmentNotCompatible && !isAnyEnchantmentCompatible) {
|
| 217 | this.resultSlots.setItem(0, ItemStack.EMPTY);
|
| 218 | this.cost.set(0);
|
| 219 | return;
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | if (this.itemName != null && !StringUtil.isBlank(this.itemName)) {
|
| 225 | if (!this.itemName.equals(input.getHoverName().getString())) {
|
| 226 | namingCost = 1;
|
| 227 | price += namingCost;
|
| 228 | result.set(DataComponents.CUSTOM_NAME, Component.literal(this.itemName));
|
| 229 | }
|
| 230 | } else if (input.has(DataComponents.CUSTOM_NAME)) {
|
| 231 | namingCost = 1;
|
| 232 | price += namingCost;
|
| 233 | result.remove(DataComponents.CUSTOM_NAME);
|
| 234 | }
|
| 235 |
|
| 236 | int finalPrice = price <= 0 ? 0 : (int)Mth.clamp(tax + price, 0L, 2147483647L);
|
| 237 | this.cost.set(finalPrice);
|
| 238 | if (price <= 0) {
|
| 239 | result = ItemStack.EMPTY;
|
| 240 | }
|
| 241 |
|
| 242 | if (namingCost == price && namingCost > 0) {
|
| 243 | if (this.cost.get() >= 40) {
|
| 244 | this.cost.set(39);
|
| 245 | }
|
| 246 |
|
| 247 | this.onlyRenaming = true;
|
| 248 | }
|
| 249 |
|
| 250 | if (this.cost.get() >= 40 && !this.player.hasInfiniteMaterials()) {
|
| 251 | result = ItemStack.EMPTY;
|
| 252 | }
|
| 253 |
|
| 254 | if (!result.isEmpty()) {
|
| 255 | int baseCost = result.getOrDefault(DataComponents.REPAIR_COST, 0);
|
| 256 | if (baseCost < addition.getOrDefault(DataComponents.REPAIR_COST, 0)) {
|
| 257 | baseCost = addition.getOrDefault(DataComponents.REPAIR_COST, 0);
|
| 258 | }
|
| 259 |
|
| 260 | if (namingCost != price || namingCost == 0) {
|
| 261 | baseCost = calculateIncreasedRepairCost(baseCost);
|
| 262 | }
|
| 263 |
|
| 264 | result.set(DataComponents.REPAIR_COST, baseCost);
|
| 265 | EnchantmentHelper.setEnchantments(result, enchantments.toImmutable());
|
| 266 | }
|
| 267 |
|
| 268 | this.resultSlots.setItem(0, result);
|
| 269 | this.broadcastChanges();
|
| 270 | } else {
|
| 271 | this.resultSlots.setItem(0, ItemStack.EMPTY);
|
| 272 | this.cost.set(0);
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | public static int calculateIncreasedRepairCost(final int baseCost) {
|
| 277 | return (int)Math.min(baseCost * 2L + 1L, 2147483647L);
|
| 278 | }
|
| 279 |
|
| 280 | public boolean setItemName(final String name) {
|
| 281 | String validatedName = validateName(name);
|
| 282 | if (validatedName != null && !validatedName.equals(this.itemName)) {
|
| 283 | this.itemName = validatedName;
|
| 284 | if (this.getSlot(2).hasItem()) {
|
| 285 | ItemStack itemStack = this.getSlot(2).getItem();
|
| 286 | if (StringUtil.isBlank(validatedName)) {
|
| 287 | itemStack.remove(DataComponents.CUSTOM_NAME);
|
| 288 | } else {
|
| 289 | itemStack.set(DataComponents.CUSTOM_NAME, Component.literal(validatedName));
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | this.createResult();
|
| 294 | return true;
|
| 295 | } else {
|
| 296 | return false;
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | private static @Nullable String validateName(final String name) {
|
| 301 | String filteredName = StringUtil.filterText(name);
|
| 302 | return filteredName.length() <= 50 ? filteredName : null;
|
| 303 | }
|
| 304 |
|
| 305 | public int getCost() {
|
| 306 | return this.cost.get();
|
| 307 | }
|
| 308 | }
|
| 309 |
|