the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.entity.player.h"
3#include "net.minecraft.world.inventory.h"
4#include "net.minecraft.world.item.trading.h"
5#include "net.minecraft.world.level.h"
6#include "MerchantMenu.h"
7
8MerchantMenu::MerchantMenu(shared_ptr<Inventory> inventory, shared_ptr<Merchant> merchant, Level *level)
9{
10 trader = merchant;
11 this->level = level;
12
13 tradeContainer = shared_ptr<MerchantContainer>( new MerchantContainer(dynamic_pointer_cast<Player>(inventory->player->shared_from_this()), merchant) );
14 addSlot(new Slot(tradeContainer, PAYMENT1_SLOT, SELLSLOT1_X, ROW2_Y));
15 addSlot(new Slot(tradeContainer, PAYMENT2_SLOT, SELLSLOT2_X, ROW2_Y));
16 addSlot(new MerchantResultSlot(inventory->player, merchant, tradeContainer, RESULT_SLOT, BUYSLOT_X, ROW2_Y));
17
18 for (int y = 0; y < 3; y++)
19 {
20 for (int x = 0; x < 9; x++)
21 {
22 addSlot(new Slot(inventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
23 }
24 }
25 for (int x = 0; x < 9; x++)
26 {
27 addSlot(new Slot(inventory, x, 8 + x * 18, 142));
28 }
29}
30
31shared_ptr<MerchantContainer> MerchantMenu::getTradeContainer()
32{
33 return tradeContainer;
34}
35
36void MerchantMenu::addSlotListener(ContainerListener *listener)
37{
38 AbstractContainerMenu::addSlotListener(listener);
39}
40
41void MerchantMenu::broadcastChanges()
42{
43 AbstractContainerMenu::broadcastChanges();
44}
45
46// 4J used to take a shared_ptr<Container> but wasn't using it, so removed to simplify things
47void MerchantMenu::slotsChanged()
48{
49 tradeContainer->updateSellItem();
50 AbstractContainerMenu::slotsChanged();
51}
52
53void MerchantMenu::setSelectionHint(int hint)
54{
55 tradeContainer->setSelectionHint(hint);
56}
57
58void MerchantMenu::setData(int id, int value)
59{
60}
61
62bool MerchantMenu::stillValid(shared_ptr<Player> player)
63{
64 return trader->getTradingPlayer() == player;
65}
66
67shared_ptr<ItemInstance> MerchantMenu::quickMoveStack(shared_ptr<Player> player, int slotIndex)
68{
69 shared_ptr<ItemInstance> clicked = nullptr;
70 Slot *slot = NULL;
71
72 if(slotIndex < slots.size()) slot = slots.at(slotIndex);
73 if (slot != NULL && slot->hasItem())
74 {
75 shared_ptr<ItemInstance> stack = slot->getItem();
76 clicked = stack->copy();
77
78 if (slotIndex == RESULT_SLOT)
79 {
80 if (!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, true))
81 {
82 return nullptr;
83 }
84 slot->onQuickCraft(stack, clicked);
85 }
86 else if (slotIndex == PAYMENT1_SLOT || slotIndex == PAYMENT2_SLOT)
87 {
88 if (!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, false))
89 {
90 return nullptr;
91 }
92 }
93 else if (slotIndex >= INV_SLOT_START && slotIndex < INV_SLOT_END)
94 {
95 if (!moveItemStackTo(stack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false))
96 {
97 return nullptr;
98 }
99 }
100 else if (slotIndex >= USE_ROW_SLOT_START && slotIndex < USE_ROW_SLOT_END)
101 {
102 if (!moveItemStackTo(stack, INV_SLOT_START, INV_SLOT_END, false))
103 {
104 return nullptr;
105 }
106 }
107 if (stack->count == 0)
108 {
109 slot->set(nullptr);
110 }
111 else
112 {
113 slot->setChanged();
114 }
115 if (stack->count == clicked->count)
116 {
117 return nullptr;
118 }
119 else
120 {
121 slot->onTake(player, stack);
122 }
123 }
124 return clicked;
125}
126
127void MerchantMenu::removed(shared_ptr<Player> player)
128{
129 AbstractContainerMenu::removed(player);
130 trader->setTradingPlayer(nullptr);
131
132 AbstractContainerMenu::removed(player);
133 if (level->isClientSide) return;
134
135 shared_ptr<ItemInstance> item = tradeContainer->removeItemNoUpdate(PAYMENT1_SLOT);
136 if (item)
137 {
138 player->drop(item);
139 }
140 item = tradeContainer->removeItemNoUpdate(PAYMENT2_SLOT);
141 if (item != NULL)
142 {
143 player->drop(item);
144 }
145}
146
147shared_ptr<Merchant> MerchantMenu::getMerchant()
148{
149 return trader;
150}