diff --git a/src/crafting.c b/src/crafting.c new file mode 100644 index 0000000..e08d341 --- /dev/null +++ b/src/crafting.c @@ -0,0 +1,84 @@ +#include +#include + +#include "globals.h" +#include "registries.h" +#include "crafting.h" + +void getCraftingOutput (PlayerData *player, uint8_t *count, uint16_t *item) { + + uint8_t i, filled = 0, first = 10; + for (i = 0; i < 9; i ++) { + if (player->craft_items[i]) { + filled ++; + if (first == 10) first = i; + } + } + + uint8_t first_col = first % 3, first_row = first / 3; + + switch (filled) { + + case 0: + *item = 0; + *count = 0; + return; + + case 1: + switch (player->craft_items[first]) { + case I_oak_log: + *item = I_oak_planks; + *count = 4; + return; + case I_oak_planks: + *item = 715; // oak_button + *count = 1; + return; + + default: break; + } + break; + + case 2: + switch (player->craft_items[first]) { + case I_oak_planks: + if (first_col != 2 && player->craft_items[first + 1] == I_oak_planks) { + *item = 731; // oak_pressure_plate + *count = 1; + return; + } else if (first_row != 2 && player->craft_items[first + 3] == I_oak_planks) { + *item = I_stick; + *count = 4; + return; + } + break; + + default: break; + } + + case 4: + switch (player->craft_items[first]) { + case I_oak_planks: + if ( + first_col != 2 && first_row != 2 && + player->craft_items[first + 1] == I_oak_planks && + player->craft_items[first + 3] == I_oak_planks && + player->craft_items[first + 4] == I_oak_planks + ) { + *item = 320; // crafting_table + *count = 1; + return; + } + break; + + default: break; + } + + default: break; + + } + + *count = 0; + *item = 0; + +} diff --git a/src/crafting.h b/src/crafting.h new file mode 100644 index 0000000..88d296f --- /dev/null +++ b/src/crafting.h @@ -0,0 +1,10 @@ +#ifndef CRAFTING_H +#define CRAFTING_H + +#include + +#include "globals.h" + +void getCraftingOutput (PlayerData *player, uint8_t *count, uint16_t *item); + +#endif diff --git a/src/globals.h b/src/globals.h index bf47278..bd4c343 100644 --- a/src/globals.h +++ b/src/globals.h @@ -2,6 +2,7 @@ #define H_GLOBALS #include +#include #define true 1 #define false 0 @@ -41,6 +42,7 @@ typedef struct { int8_t pitch; uint8_t hotbar; uint16_t inventory_items[41]; + uint16_t craft_items[9]; uint8_t inventory_count[41]; } PlayerData; diff --git a/src/packets.c b/src/packets.c index 0fbb2af..958becd 100644 --- a/src/packets.c +++ b/src/packets.c @@ -10,6 +10,7 @@ #include "varnum.h" #include "registries.h" #include "worldgen.h" +#include "crafting.h" // C->S Handshake int cs_handshake (int client_fd) { @@ -432,6 +433,7 @@ int cs_useItemOn (int client_fd) { } +// C->S Click Container int cs_clickContainer (int client_fd) { int window_id = readVarInt(client_fd); @@ -446,18 +448,19 @@ int cs_clickContainer (int client_fd) { PlayerData *player; if (getPlayerData(client_fd, &player)) return 1; - uint8_t slot, count; + uint8_t slot, count, craft = false; uint16_t item; int tmp; for (int i = 0; i < changes_count; i ++) { slot = clientSlotToServerSlot(readUint16(client_fd)); + if (slot > 40) craft = true; if (!readByte(client_fd)) { // no item? if (slot != 255) { player->inventory_items[slot] = 0; - player->inventory_count[slot] = 0; + if (slot <= 40) player->inventory_count[slot] = 0; } continue; } @@ -471,13 +474,18 @@ int cs_clickContainer (int client_fd) { tmp = readVarInt(client_fd); recv(client_fd, recv_buffer, tmp, MSG_WAITALL); - if (item <= 255 && count > 0) { + if (count > 0) { player->inventory_items[slot] = item; - player->inventory_count[slot] = count; + if (slot <= 40) player->inventory_count[slot] = count; } } + if (craft) { + getCraftingOutput(player, &count, &item); + sc_setContainerSlot(client_fd, window_id, 0, count, item); + } + return 0; } diff --git a/src/tools.c b/src/tools.c index 4898e24..440f829 100644 --- a/src/tools.c +++ b/src/tools.c @@ -186,6 +186,14 @@ uint8_t clientSlotToServerSlot (uint8_t slot) { if (slot >= 9 && slot <= 35) return slot; if (slot == 45) return 40; if (slot >= 5 && slot <= 8) return 4 - (slot - 5) + 36; + + // map inventory crafting slots to player data crafting grid (semi-hack) + // this abuses the fact that the buffers are adjacent in player data + if (slot == 1) return 41; + if (slot == 2) return 42; + if (slot == 3) return 44; + if (slot == 4) return 45; + return 255; }