blob: 5c81ee9b2352d9ddc719e5fb6cb457e88eada5f1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include "Inventory.h"
#include "Funcs.h"
#include "Game.h"
#include "Block.h"
#include "Event.h"
#include "Chat.h"
struct _InventoryData Inventory;
cc_bool Inventory_CheckChangeSelected(void) {
if (!Inventory.CanChangeSelected) {
Chat_AddRaw("&cThe server has forbidden you from changing your held block.");
return false;
}
return true;
}
void Inventory_SetSelectedIndex(int index) {
if (!Inventory_CheckChangeSelected()) return;
Inventory.SelectedIndex = index;
Event_RaiseVoid(&UserEvents.HeldBlockChanged);
}
void Inventory_SetHotbarIndex(int index) {
if (!Inventory_CheckChangeSelected() || Game_ClassicMode) return;
Inventory.Offset = index * INVENTORY_BLOCKS_PER_HOTBAR;
Event_RaiseVoid(&UserEvents.HeldBlockChanged);
}
void Inventory_SwitchHotbar(void) {
int index = Inventory.Offset == 0 ? 1 : 0;
Inventory_SetHotbarIndex(index);
}
void Inventory_SetSelectedBlock(BlockID block) {
int i;
if (!Inventory_CheckChangeSelected()) return;
/* Swap with currently selected block if given block is already in the hotbar */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != block) continue;
Inventory_Set(i, Inventory_SelectedBlock);
break;
}
Inventory_Set(Inventory.SelectedIndex, block);
Event_RaiseVoid(&UserEvents.HeldBlockChanged);
}
void Inventory_PickBlock(BlockID block) {
int i;
if (!Inventory_CheckChangeSelected() || Inventory_SelectedBlock == block) return;
/* Vanilla classic client doesn't let you select these blocks */
if (Game_PureClassic) {
if (block == BLOCK_GRASS) block = BLOCK_DIRT;
if (block == BLOCK_DOUBLE_SLAB) block = BLOCK_SLAB;
}
/* Try to replace same block */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != block) continue;
Inventory_SetSelectedIndex(i); return;
}
if (AutoRotate_Enabled) {
/* Try to replace existing autorotate variant */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (AutoRotate_BlocksShareGroup(Inventory_Get(i), block)) {
Inventory_SetSelectedIndex(i);
Inventory_SetSelectedBlock(block);
return;
}
}
}
/* Is the currently selected block an empty slot? */
if (Inventory_SelectedBlock == BLOCK_AIR) {
Inventory_SetSelectedBlock(block); return;
}
/* Try to replace empty slots */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != BLOCK_AIR) continue;
Inventory_Set(i, block);
Inventory_SetSelectedIndex(i); return;
}
/* Finally, replace the currently selected block */
Inventory_SetSelectedBlock(block);
}
/* Returns default block that should go in the given inventory slot */
static BlockID DefaultMapping(int slot) {
if (Game_ClassicMode) {
if (slot < Game_Version.InventorySize) return Game_Version.Inventory[slot];
} else if (slot < Game_Version.MaxCoreBlock) {
return (BlockID)(slot + 1);
}
return BLOCK_AIR;
}
void Inventory_ResetMapping(void) {
int slot;
for (slot = 0; slot < Array_Elems(Inventory.Map); slot++) {
Inventory.Map[slot] = DefaultMapping(slot);
}
}
void Inventory_AddDefault(BlockID block) {
int slot;
if (block > BLOCK_MAX_CPE) {
Inventory.Map[block - 1] = block; return;
}
for (slot = 0; slot < BLOCK_MAX_CPE; slot++) {
if (DefaultMapping(slot) != block) continue;
Inventory.Map[slot] = block;
return;
}
}
void Inventory_Remove(BlockID block) {
int slot;
for (slot = 0; slot < Array_Elems(Inventory.Map); slot++) {
if (Inventory.Map[slot] == block) Inventory.Map[slot] = BLOCK_AIR;
}
}
/*########################################################################################################################*
*--------------------------------------------------Inventory component----------------------------------------------------*
*#########################################################################################################################*/
static void OnReset(void) {
Inventory_ResetMapping();
Inventory.CanChangeSelected = true;
}
static void OnInit(void) {
int i;
BlockID* inv = Inventory.Table;
OnReset();
Inventory.BlocksPerRow = Game_Version.BlocksPerRow;
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
inv[i] = Game_Version.Hotbar[i];
}
}
struct IGameComponent Inventory_Component = {
OnInit, /* Init */
NULL, /* Free */
OnReset, /* Reset */
};
|