the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 143 lines 3.8 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.entity.player.h" 3#include "net.minecraft.world.level.h" 4#include "net.minecraft.world.level.tile.h" 5#include "net.minecraft.world.item.h" 6#include "net.minecraft.world.item.crafting.h" 7#include "CraftingContainer.h" 8#include "ResultContainer.h" 9#include "ResultSlot.h" 10#include "CraftingMenu.h" 11 12const int CraftingMenu::RESULT_SLOT = 0; 13const int CraftingMenu::CRAFT_SLOT_START = 1; 14const int CraftingMenu::CRAFT_SLOT_END = CraftingMenu::CRAFT_SLOT_START + 9; 15const int CraftingMenu::INV_SLOT_START = CraftingMenu::CRAFT_SLOT_END; 16const int CraftingMenu::INV_SLOT_END = CraftingMenu::INV_SLOT_START + 9 * 3; 17const int CraftingMenu::USE_ROW_SLOT_START = CraftingMenu::INV_SLOT_END; 18const int CraftingMenu::USE_ROW_SLOT_END = CraftingMenu::USE_ROW_SLOT_START + 9; 19 20CraftingMenu::CraftingMenu(shared_ptr<Inventory> inventory, Level *level, int xt, int yt, int zt) : AbstractContainerMenu() 21{ 22 craftSlots = shared_ptr<CraftingContainer>( new CraftingContainer(this, 3, 3) ); 23 resultSlots = shared_ptr<ResultContainer>( new ResultContainer() ); 24 25 this->level = level; 26 x = xt; 27 y = yt; 28 z = zt; 29 addSlot(new ResultSlot( inventory->player, craftSlots, resultSlots, 0, 120 + 4, 31 + 4)); 30 31 for (int y = 0; y < 3; y++) 32 { 33 for (int x = 0; x < 3; x++) 34 { 35 addSlot(new Slot(craftSlots, x + y * 3, 30 + x * 18, 17 + y * 18)); 36 } 37 } 38 39 for (int y = 0; y < 3; y++) 40 { 41 for (int x = 0; x < 9; x++) 42 { 43 addSlot(new Slot(inventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)); 44 } 45 } 46 for (int x = 0; x < 9; x++) 47 { 48 addSlot(new Slot(inventory, x, 8 + x * 18, 142)); 49 } 50 51 slotsChanged(); // 4J - removed craftSlots parameter, see comment below 52} 53 54void CraftingMenu::slotsChanged() // 4J used to take a shared_ptr<Container> but wasn't using it, so removed to simplify things 55{ 56 resultSlots->setItem(0, Recipes::getInstance()->getItemFor(craftSlots, level)); 57} 58 59void CraftingMenu::removed(shared_ptr<Player> player) 60{ 61 AbstractContainerMenu::removed(player); 62 if (level->isClientSide) return; 63 64 for (int i = 0; i < 9; i++) 65 { 66 shared_ptr<ItemInstance> item = craftSlots->removeItemNoUpdate(i); 67 if (item != NULL) 68 { 69 player->drop(item); 70 } 71 } 72} 73 74bool CraftingMenu::stillValid(shared_ptr<Player> player) 75{ 76 if (level->getTile(x, y, z) != Tile::workBench_Id) return false; 77 if (player->distanceToSqr(x + 0.5, y + 0.5, z + 0.5) > 8 * 8) return false; 78 return true; 79} 80 81shared_ptr<ItemInstance> CraftingMenu::quickMoveStack(shared_ptr<Player> player, int slotIndex) 82{ 83 shared_ptr<ItemInstance> clicked = nullptr; 84 Slot *slot = slots.at(slotIndex); 85 if (slot != NULL && slot->hasItem()) 86 { 87 shared_ptr<ItemInstance> stack = slot->getItem(); 88 clicked = stack->copy(); 89 90 if (slotIndex == RESULT_SLOT) 91 { 92 if(!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, true)) 93 { 94 return nullptr; 95 } 96 slot->onQuickCraft(stack, clicked); 97 } 98 else if (slotIndex >= INV_SLOT_START && slotIndex < INV_SLOT_END) 99 { 100 if(!moveItemStackTo(stack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false)) 101 { 102 return nullptr; 103 } 104 } 105 else if (slotIndex >= USE_ROW_SLOT_START && slotIndex < USE_ROW_SLOT_END) 106 { 107 if(!moveItemStackTo(stack, INV_SLOT_START, INV_SLOT_END, false)) 108 { 109 return nullptr; 110 } 111 } 112 else 113 { 114 if(!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, false)) 115 { 116 return nullptr; 117 } 118 } 119 if (stack->count == 0) 120 { 121 slot->set(nullptr); 122 } 123 else 124 { 125 slot->setChanged(); 126 } 127 if (stack->count == clicked->count) 128 { 129 // nothing moved 130 return nullptr; 131 } 132 else 133 { 134 slot->onTake(player, stack); 135 } 136 } 137 return clicked; 138} 139 140bool CraftingMenu::canTakeItemForPickAll(shared_ptr<ItemInstance> carried, Slot *target) 141{ 142 return target->container != resultSlots && AbstractContainerMenu::canTakeItemForPickAll(carried, target); 143}