| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.core.NonNullList;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.util.Prediction;
|
| 6 | import net.minecraft.world.Container;
|
| 7 | import net.minecraft.world.entity.player.Player;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.crafting.CraftingInput;
|
| 10 | import net.minecraft.world.item.crafting.CraftingRecipe;
|
| 11 | import net.minecraft.world.item.crafting.RecipeType;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 |
|
| 14 | public class ResultSlot extends Slot {
|
| 15 | private final CraftingContainer craftSlots;
|
| 16 | private final Player player;
|
| 17 | private int removeCount;
|
| 18 |
|
| 19 | public ResultSlot(final Player player, final CraftingContainer craftSlots, final Container container, final int id, final int x, final int y) {
|
| 20 | super(container, id, x, y);
|
| 21 | this.player = player;
|
| 22 | this.craftSlots = craftSlots;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public boolean mayPlace(final ItemStack itemStack) {
|
| 27 | return false;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public ItemStack remove(final int amount) {
|
| 32 | if (this.hasItem()) {
|
| 33 | this.removeCount = this.removeCount + Math.min(amount, this.getItem().getCount());
|
| 34 | }
|
| 35 |
|
| 36 | return super.remove(amount);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void onQuickCraft(final ItemStack picked, final int count) {
|
| 41 | this.removeCount += count;
|
| 42 | this.checkTakeAchievements(picked);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void onSwapCraft(final int count) {
|
| 47 | this.removeCount += count;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public ItemStack safeClone(final Player player) {
|
| 52 | ItemStack result = super.safeClone(player);
|
| 53 | result.getItem().onCraftedBy(result, player);
|
| 54 | return result;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void checkTakeAchievements(final ItemStack carried) {
|
| 59 | if (this.removeCount > 0) {
|
| 60 | carried.onCraftedBy(this.player, this.removeCount);
|
| 61 | }
|
| 62 |
|
| 63 | if (this.container instanceof RecipeCraftingHolder recipeCraftingHolder) {
|
| 64 | recipeCraftingHolder.awardUsedRecipes(this.player, this.craftSlots.getItems());
|
| 65 | }
|
| 66 |
|
| 67 | this.removeCount = 0;
|
| 68 | }
|
| 69 |
|
| 70 | private static NonNullList<ItemStack> copyAllInputItems(final CraftingInput input) {
|
| 71 | NonNullList<ItemStack> result = NonNullList.withSize(input.size(), ItemStack.EMPTY);
|
| 72 |
|
| 73 | for (int slot = 0; slot < result.size(); slot++) {
|
| 74 | result.set(slot, input.getItem(slot));
|
| 75 | }
|
| 76 |
|
| 77 | return result;
|
| 78 | }
|
| 79 |
|
| 80 | private NonNullList<ItemStack> getRemainingItems(final CraftingInput input, final Level level) {
|
| 81 | return level instanceof ServerLevel serverLevel
|
| 82 | ? serverLevel.recipeAccess()
|
| 83 | .getRecipeFor(RecipeType.CRAFTING, input, serverLevel)
|
| 84 | .map(recipe -> recipe.value().getRemainingItems(input))
|
| 85 | .orElseGet(() -> copyAllInputItems(input))
|
| 86 | : CraftingRecipe.defaultCraftingReminder(input);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void onTake(final Player player, final ItemStack carried) {
|
| 91 | this.checkTakeAchievements(carried);
|
| 92 | CraftingInput.Positioned positionedRecipe = this.craftSlots.asPositionedCraftInput();
|
| 93 | CraftingInput input = positionedRecipe.input();
|
| 94 | int recipeLeft = positionedRecipe.left();
|
| 95 | int recipeTop = positionedRecipe.top();
|
| 96 | NonNullList<ItemStack> remaining = this.getRemainingItems(input, player.level());
|
| 97 |
|
| 98 | for (int y = 0; y < input.height(); y++) {
|
| 99 | for (int x = 0; x < input.width(); x++) {
|
| 100 | int slot = x + recipeLeft + (y + recipeTop) * this.craftSlots.getWidth();
|
| 101 | ItemStack itemStack = this.craftSlots.getItem(slot);
|
| 102 | ItemStack replacement = remaining.get(x + y * input.width());
|
| 103 | if (!itemStack.isEmpty()) {
|
| 104 | this.craftSlots.removeItem(slot, 1);
|
| 105 | itemStack = this.craftSlots.getItem(slot);
|
| 106 | }
|
| 107 |
|
| 108 | if (!replacement.isEmpty()) {
|
| 109 | if (itemStack.isEmpty()) {
|
| 110 | this.craftSlots.setItem(slot, replacement);
|
| 111 | } else if (ItemStack.isSameItemSameComponents(itemStack, replacement)) {
|
| 112 | replacement.grow(itemStack.getCount());
|
| 113 | this.craftSlots.setItem(slot, replacement);
|
| 114 | } else if (!this.player.getInventory().add(replacement)) {
|
| 115 | this.player.drop(replacement, false, Prediction.PREDICTED);
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean isFake() {
|
| 124 | return true;
|
| 125 | }
|
| 126 | }
|
| 127 |
|