SlotMerchantResult .java

net.minecraft.inventory.SlotMerchantResult (class): members: field_75231_g, theMerchantInventory, onPickupFromSlot, theMerchant, isItemValid, onCrafting.

net.minecraft.inventoryclass125 linescommonextends Slot4.0 KB

API Surface (5)

boolean isItemValid(ItemStack stack)
ItemStack decrStackSize(int amount)
void onCrafting(ItemStack stack, int amount)
void onCrafting(ItemStack stack)
void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack)

Fields & Constants (4)

theMerchantInventory
thePlayer
field_75231_g
theMerchant

Full Source

SlotMerchantResult.java125 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.inventory;
2
3
import net.minecraft.entity.IMerchant;
4
import net.minecraft.entity.player.EntityPlayer;
5
import net.minecraft.item.ItemStack;
6
import net.minecraft.stats.StatList;
7
import net.minecraft.village.MerchantRecipe;
8
9
public class SlotMerchantResult extends Slot
10
{
11
    /** Merchant's inventory. */
12
    private final InventoryMerchant theMerchantInventory;
13
14
    /** The Player whos trying to buy/sell stuff. */
15
    private EntityPlayer thePlayer;
16
    private int field_75231_g;
17
18
    /** "Instance" of the Merchant. */
19
    private final IMerchant theMerchant;
20
21
    public SlotMerchantResult(EntityPlayer player, IMerchant merchant, InventoryMerchant merchantInventory, int slotIndex, int xPosition, int yPosition)
22
    {
23
        super(merchantInventory, slotIndex, xPosition, yPosition);
24
        this.thePlayer = player;
25
        this.theMerchant = merchant;
26
        this.theMerchantInventory = merchantInventory;
27
    }
28
29
    /**
30
     * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
31
     */
32
    public boolean isItemValid(ItemStack stack)
33
    {
34
        return false;
35
    }
36
37
    /**
38
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
39
     * stack.
40
     */
41
    public ItemStack decrStackSize(int amount)
42
    {
43
        if (this.getHasStack())
44
        {
45
            this.field_75231_g += Math.min(amount, this.getStack().stackSize);
46
        }
47
48
        return super.decrStackSize(amount);
49
    }
50
51
    /**
52
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
53
     * internal count then calls onCrafting(item).
54
     */
55
    protected void onCrafting(ItemStack stack, int amount)
56
    {
57
        this.field_75231_g += amount;
58
        this.onCrafting(stack);
59
    }
60
61
    /**
62
     * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
63
     */
64
    protected void onCrafting(ItemStack stack)
65
    {
66
        stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.field_75231_g);
67
        this.field_75231_g = 0;
68
    }
69
70
    public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack)
71
    {
72
        this.onCrafting(stack);
73
        MerchantRecipe merchantrecipe = this.theMerchantInventory.getCurrentRecipe();
74
75
        if (merchantrecipe != null)
76
        {
77
            ItemStack itemstack = this.theMerchantInventory.getStackInSlot(0);
78
            ItemStack itemstack1 = this.theMerchantInventory.getStackInSlot(1);
79
80
            if (this.doTrade(merchantrecipe, itemstack, itemstack1) || this.doTrade(merchantrecipe, itemstack1, itemstack))
81
            {
82
                this.theMerchant.useRecipe(merchantrecipe);
83
                playerIn.triggerAchievement(StatList.timesTradedWithVillagerStat);
84
85
                if (itemstack != null && itemstack.stackSize <= 0)
86
                {
87
                    itemstack = null;
88
                }
89
90
                if (itemstack1 != null && itemstack1.stackSize <= 0)
91
                {
92
                    itemstack1 = null;
93
                }
94
95
                this.theMerchantInventory.setInventorySlotContents(0, itemstack);
96
                this.theMerchantInventory.setInventorySlotContents(1, itemstack1);
97
            }
98
        }
99
    }
100
101
    private boolean doTrade(MerchantRecipe trade, ItemStack firstItem, ItemStack secondItem)
102
    {
103
        ItemStack itemstack = trade.getItemToBuy();
104
        ItemStack itemstack1 = trade.getSecondItemToBuy();
105
106
        if (firstItem != null && firstItem.getItem() == itemstack.getItem())
107
        {
108
            if (itemstack1 != null && secondItem != null && itemstack1.getItem() == secondItem.getItem())
109
            {
110
                firstItem.stackSize -= itemstack.stackSize;
111
                secondItem.stackSize -= itemstack1.stackSize;
112
                return true;
113
            }
114
115
            if (itemstack1 == null && secondItem == null)
116
            {
117
                firstItem.stackSize -= itemstack.stackSize;
118
                return true;
119
            }
120
        }
121
122
        return false;
123
    }
124
}
125

Deobfuscated MCP 1.8.9 source of SlotMerchantResult from Marcelektro/MavenMCP-1.8.9 (net/minecraft/inventory/SlotMerchantResult.java), stored verbatim.