/
SpoutPlayerBlockBag.java
268 lines (228 loc) · 7.57 KB
/
SpoutPlayerBlockBag.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* WorldEdit
* Copyright (C) 2012 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// $Id$
package com.sk89q.worldedit.spout;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.bags.BlockBagException;
import com.sk89q.worldedit.bags.OutOfBlocksException;
import com.sk89q.worldedit.bags.OutOfSpaceException;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import org.spout.api.inventory.Inventory;
import org.spout.api.inventory.ItemStack;
import org.spout.api.material.Material;
import org.spout.api.entity.Player;
import org.spout.vanilla.plugin.component.inventory.PlayerInventory;
import org.spout.vanilla.plugin.component.living.neutral.Human;
import org.spout.vanilla.plugin.material.VanillaMaterials;
public class SpoutPlayerBlockBag extends BlockBag {
/**
* Player instance.
*/
private Player player;
/**
* The player's inventory;
*/
private ItemInfo items;
private static class ItemInfo {
ItemStack[] inventory;
boolean includesFullInventory;
public ItemInfo(ItemStack[] inventory, boolean full) {
this.inventory = inventory;
this.includesFullInventory = full;
}
}
/**
* Construct the object.
*
* @param player
*/
public SpoutPlayerBlockBag(Player player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = getViewableItems(player);
}
}
private ItemInfo getViewableItems(Player player) {
boolean includesFullInventory = true;
ItemStack[] items;
Human human = player.get(Human.class);
PlayerInventory inv = player.get(PlayerInventory.class);
if (human.isCreative()) {
includesFullInventory = false;
items = new ItemStack[inv.getQuickbar().size()];
} else {
items = new ItemStack[inv.getQuickbar().size() + inv.getMain().size()];
}
int index = 0;
for (; index < inv.getQuickbar().size(); ++index) {
items[index] = inv.getQuickbar().get(index);
}
if (!human.isCreative()) {
for (int i = 0; i < inv.getMain().size() && index < items.length; ++i) {
items[index++] = inv.getMain().get(i);
}
}
return new ItemInfo(items, includesFullInventory);
}
/**
* Get the player.
*
* @return
*/
public Player getPlayer() {
return player;
}
/**
* Get a block.
*
* @param item
*/
@Override
public void fetchItem(BaseItem item) throws BlockBagException {
final short id = (short)item.getType();
final short damage = item.getData();
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount == 1);
Material mat = VanillaMaterials.getMaterial(id, damage);
if (mat == VanillaMaterials.AIR) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.inventory.length; ++slot) {
ItemStack spoutItem = items.inventory[slot];
if (spoutItem == null) {
continue;
}
if (!spoutItem.getMaterial().equals(mat)) {
// Type id or damage value doesn't fit
continue;
}
int currentAmount = spoutItem.getAmount();
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount > 1) {
spoutItem.setAmount(currentAmount - 1);
found = true;
} else {
items.inventory[slot] = null;
found = true;
}
break;
}
if (!found) {
throw new OutOfBlocksException();
}
}
/**
* Store a block.
*
* @param item
*/
@Override
public void storeItem(BaseItem item) throws BlockBagException {
final short id = (short) item.getType();
final short damage = item.getData();
Material mat = VanillaMaterials.getMaterial(id, damage);
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
assert(amount <= mat.getMaxStackSize());
if (mat == VanillaMaterials.AIR) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
int freeSlot = -1;
for (int slot = 0; slot < items.inventory.length; ++slot) {
ItemStack spoutItem = items.inventory[slot];
if (spoutItem == null) {
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
if (!spoutItem.getMaterial().equals(mat)) {
// Type id or damage value doesn't fit
continue;
}
int currentAmount = spoutItem.getAmount();
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= mat.getMaxStackSize()) {
// Full stack
continue;
}
int spaceLeft = mat.getMaxStackSize() - currentAmount;
if (spaceLeft >= amount) {
spoutItem.setAmount(currentAmount + amount);
return;
}
spoutItem.setAmount(mat.getMaxStackSize());
amount -= spaceLeft;
}
if (freeSlot > -1) {
items.inventory[freeSlot] = new ItemStack(mat, amount);
return;
}
throw new OutOfSpaceException(id);
}
/**
* Flush any changes. This is called at the end.
*/
@Override
public void flushChanges() {
if (items != null) {
PlayerInventory inv = player.get(PlayerInventory.class);
for (int i = 0; i < inv.getQuickbar().size(); i++) {
inv.getQuickbar().set(i, items.inventory[i]);
}
for (int i = 0; i < inv.getMain().size(); ++i) {
inv.getMain().set(i, items.inventory[inv.getQuickbar().size() + i]);
}
items = null;
}
}
/**
* Adds a position to be used a source.
* (TODO: Figure out what this does)
* @param pos
*/
@Override
public void addSourcePosition(WorldVector pos) {
}
/**
* Adds a position to be used a source.
* (TODO: Figure out what this does)
* @param pos
*/
@Override
public void addSingleSourcePosition(WorldVector pos) {
}
}