-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCombineHoe.java
More file actions
359 lines (294 loc) · 12 KB
/
CombineHoe.java
File metadata and controls
359 lines (294 loc) · 12 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package io.github.thebusybiscuit.sensibletoolbox.items;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.sensibletoolbox.api.SensibleToolbox;
import io.github.thebusybiscuit.sensibletoolbox.api.gui.GUIUtil;
import io.github.thebusybiscuit.sensibletoolbox.api.gui.InventoryGUI;
import io.github.thebusybiscuit.sensibletoolbox.api.gui.SlotType;
import io.github.thebusybiscuit.sensibletoolbox.api.items.BaseSTBItem;
import io.github.thebusybiscuit.sensibletoolbox.utils.STBUtil;
import me.desht.dhutils.cuboid.Cuboid;
import me.desht.dhutils.cuboid.CuboidDirection;
public abstract class CombineHoe extends BaseSTBItem {
private Material seedType;
private int seedAmount;
private InventoryGUI gui;
private EquipmentSlot equipmentSlot;
@Nonnull
public static String getInventoryTitle() {
return ChatColor.DARK_GREEN + "Seed Bag";
}
public CombineHoe() {
super();
seedType = null;
seedAmount = 0;
}
public CombineHoe(ConfigurationSection conf) {
super(conf);
setSeedAmount(conf.getInt("amount"));
setSeedType(Material.getMaterial(conf.getString("seeds")));
}
@Override
public YamlConfiguration freeze() {
YamlConfiguration conf = super.freeze();
conf.set("amount", getSeedAmount());
conf.set("seeds", getSeedType() == null ? "" : getSeedType().toString());
return conf;
}
public Material getSeedType() {
return seedType;
}
public void setSeedType(Material seedType) {
this.seedType = seedType;
}
public int getSeedAmount() {
return seedAmount;
}
public void setSeedAmount(int seedAmount) {
this.seedAmount = seedAmount;
}
public EquipmentSlot getEquipmentSlot() {
return equipmentSlot;
}
public void setEquipmentSlot(EquipmentSlot equipmentSlot) {
this.equipmentSlot = equipmentSlot;
}
@Override
public boolean isEnchantable() {
return false;
}
@Override
public String[] getLore() {
int n = getWorkRadius() * 2 + 1;
String s = n + "x" + n;
return new String[] { "Right-click dirt/grass:" + ChatColor.WHITE + " till 3x3 area", "Right-click soil:" + ChatColor.WHITE + " sow 3x3 area", "Right-click air:" + ChatColor.WHITE + " open seed bag", "Left-click plants:" + ChatColor.WHITE + " harvest " + s + " area", "Left-click leaves:" + ChatColor.WHITE + " break 3x3x3 area", };
}
@Override
public String[] getExtraLore() {
if (getSeedType() != null && getSeedAmount() > 0) {
String s = ItemUtils.getItemName(new ItemStack(getSeedType()));
return new String[] { ChatColor.WHITE + "Seed bag: " + ChatColor.GOLD + getSeedAmount() + " x " + s };
} else {
return new String[0];
}
}
@Override
public boolean hasGlow() {
return true;
}
@Override
public void onInteractItem(PlayerInteractEvent event) {
Block b = event.getClickedBlock();
setEquipmentSlot(event.getHand());
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (b.getType() == Material.FARMLAND) {
plantSeeds(event.getPlayer(), b);
event.setCancelled(true);
return;
} else if (b.getType() == Material.DIRT || b.getType() == Material.GRASS_BLOCK) {
tillSoil(event.getPlayer(), event.getItem(), event.getHand(), b);
event.setCancelled(true);
return;
}
}
if (event.getAction() == Action.RIGHT_CLICK_AIR) {
if (event.getClickedBlock() == null || !event.getClickedBlock().getType().isInteractable()) {
gui = GUIUtil.createGUI(event.getPlayer(), this, 9, getInventoryTitle());
for (int i = 0; i < gui.getInventory().getSize(); i++) {
gui.setSlotType(i, SlotType.ITEM);
}
populateSeedBag(gui);
gui.show(event.getPlayer());
}
}
}
@Override
public void onBreakBlockWithItem(BlockBreakEvent event) {
Player player = event.getPlayer();
if (player.isSneaking()) {
return;
}
Block b = event.getBlock();
if (Tag.LEAVES.isTagged(b.getType())) {
harvestLayer(player, b);
if (!player.isSneaking()) {
harvestLayer(player, b.getRelative(BlockFace.UP));
harvestLayer(player, b.getRelative(BlockFace.DOWN));
}
ItemUtils.damageItem(player.getInventory().getItemInMainHand(), false);
} else if (STBUtil.isPlant(b.getType())) {
harvestLayer(player, b);
ItemUtils.damageItem(player.getInventory().getItemInMainHand(), false);
}
}
private boolean verifyUnique(Inventory inv, ItemStack stack, int exclude) {
for (int i = 0; i < inv.getSize(); i++) {
if (i != exclude && inv.getItem(i) != null && inv.getItem(i).getType() != stack.getType()) {
return false;
}
}
return true;
}
@Override
public boolean onSlotClick(HumanEntity player, int slot, ClickType click, ItemStack inSlot, ItemStack onCursor) {
return onCursor.getType() == Material.AIR || STBUtil.getCropType(onCursor.getType()) != null && verifyUnique(gui.getInventory(), onCursor, slot);
}
@Override
public boolean onPlayerInventoryClick(HumanEntity player, int slot, ClickType click, ItemStack inSlot, ItemStack onCursor) {
return true;
}
@Override
public int onShiftClickInsert(HumanEntity player, int slot, ItemStack toInsert) {
if (STBUtil.getCropType(toInsert.getType()) == null) {
return 0;
} else if (!verifyUnique(gui.getInventory(), toInsert, slot)) {
return 0;
} else {
Map<Integer, ItemStack> excess = gui.getInventory().addItem(toInsert);
int inserted = toInsert.getAmount();
for (ItemStack stack : excess.values()) {
inserted -= stack.getAmount();
}
return inserted;
}
}
@Override
public boolean onShiftClickExtract(HumanEntity player, int slot, ItemStack toExtract) {
return true;
}
@Override
public boolean onClickOutside(HumanEntity player) {
return false;
}
@Override
public void onGUIClosed(HumanEntity player) {
Material seeds = null;
int count = 0;
String err = null;
for (int i = 0; i < gui.getInventory().getSize(); i++) {
ItemStack stack = gui.getInventory().getItem(i);
if (stack != null) {
if (seeds != null && seeds != stack.getType()) {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
err = "Mixed items in the seed bag?";
} else if (STBUtil.getCropType(stack.getType()) == null) {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
err = "Non-seed items in the seed bag?";
} else {
seeds = stack.getType();
count += stack.getAmount();
}
}
}
if (err != null) {
STBUtil.complain((Player) player, err);
}
setSeedAmount(count);
setSeedType(seeds);
updateHeldItemStack((Player) player, getEquipmentSlot());
}
private void populateSeedBag(InventoryGUI gui) {
Inventory inv = gui.getInventory();
if (getSeedType() != null && getSeedAmount() > 0) {
int nFullStacks = getSeedAmount() / getSeedType().getMaxStackSize();
int remainder = getSeedAmount() % getSeedType().getMaxStackSize();
for (int i = 0; i < nFullStacks && i < inv.getSize(); i++) {
inv.setItem(i, new ItemStack(getSeedType(), getSeedType().getMaxStackSize()));
}
if (remainder > 0 && nFullStacks < inv.getSize()) {
inv.setItem(nFullStacks, new ItemStack(getSeedType(), remainder));
}
}
}
private void plantSeeds(Player player, Block b) {
if (getSeedType() == null || getSeedAmount() == 0) {
return;
}
int amountLeft = getSeedAmount();
for (Block neighbour : STBUtil.getSurroundingBlocks(b)) {
Block above = neighbour.getRelative(BlockFace.UP);
if (!SensibleToolbox.getProtectionManager().hasPermission(player, above, ProtectableAction.PLACE_BLOCK)) {
continue;
}
if (neighbour.getType() == Material.FARMLAND && above.isEmpty()) {
// candidate for sowing
above.setType(STBUtil.getCropType(getSeedType()));
amountLeft--;
if (amountLeft == 0) {
break;
}
}
}
if (amountLeft < getSeedAmount()) {
setSeedAmount(amountLeft);
updateHeldItemStack(player, getEquipmentSlot());
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CHICKEN_EGG, 1.0F, 1.0F);
}
}
@ParametersAreNonnullByDefault
private void harvestLayer(Player player, Block b) {
Cuboid cuboid = new Cuboid(b.getLocation());
cuboid = cuboid.outset(CuboidDirection.HORIZONTAL, Tag.LEAVES.isTagged(b.getType()) ? 1 : getWorkRadius());
for (Block block : cuboid) {
if (!block.equals(b) && (STBUtil.isPlant(block.getType()) || Tag.LEAVES.isTagged(block.getType()))) {
if (SensibleToolbox.getProtectionManager().hasPermission(player, b, ProtectableAction.BREAK_BLOCK)) {
block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, block.getType());
block.breakNaturally();
}
}
}
}
@ParametersAreNonnullByDefault
private void tillSoil(Player player, ItemStack stack, EquipmentSlot hand, Block b) {
int count = 0;
int damage = ((Damageable) stack.getItemMeta()).getDamage();
for (Block b1 : STBUtil.getSurroundingBlocks(b)) {
if (!SensibleToolbox.getProtectionManager().hasPermission(player, b1, ProtectableAction.BREAK_BLOCK)) {
continue;
}
Block above = b1.getRelative(BlockFace.UP);
if ((b1.getType() == Material.DIRT || b1.getType() == Material.GRASS) && !above.getType().isSolid() && !above.isLiquid()) {
b1.setType(Material.FARMLAND);
count++;
if (!above.isEmpty()) {
above.breakNaturally();
}
if (damage + count >= stack.getType().getMaxDurability()) {
break;
}
}
if (player.isSneaking()) {
break;
}
}
if (count > 0) {
player.playSound(b.getLocation(), Sound.BLOCK_GRASS_BREAK, 1.0F, 1.0F);
ItemUtils.damageItem(stack, count, false);
}
}
public int getWorkRadius() {
return 1;
}
}