/
Gate.java
731 lines (596 loc) · 30.4 KB
/
Gate.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* CraftBook Copyright (C) EngineHub and Contributors <https://enginehub.org/>
*
* 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/>.
*/
package org.enginehub.craftbook.mechanics.area.simple;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.blocks.Blocks;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.inventory.ItemStack;
import org.enginehub.craftbook.AbstractCraftBookMechanic;
import org.enginehub.craftbook.ChangedSign;
import org.enginehub.craftbook.CraftBook;
import org.enginehub.craftbook.CraftBookPlayer;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.bukkit.util.CraftBookBukkitUtil;
import org.enginehub.craftbook.util.BlockParser;
import org.enginehub.craftbook.util.BlockUtil;
import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.ProtectionUtil;
import org.enginehub.craftbook.util.SignUtil;
import org.enginehub.craftbook.util.events.SignClickEvent;
import org.enginehub.craftbook.util.events.SourcedBlockRedstoneEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Handler for gates. Gates are merely fence blocks. When they are closed or open, a nearby fence
* will be found,
* the algorithm will traverse to the
* top-most connected fence block, and then proceed to recurse to the sides up to a certain number
* of fences. To the
* fences that it gets to, it will
* iterate over the blocks below to open or close the gate.
*
* @author sk89q
*/
public class Gate extends AbstractCraftBookMechanic {
/**
* get
* Toggles the gate closest to a location.
*
* @param player The player
* @param block The base block
* @param smallSearchSize Small or large search radius
* @param close null to toggle, true to close, false to open
* @return true if a gate was found and blocks were changed; false otherwise.
*/
public boolean toggleGates(CraftBookPlayer player, Block block, boolean smallSearchSize, Boolean close) {
int x = block.getX();
int y = block.getY();
int z = block.getZ();
boolean foundGate = false;
Set<GateColumn> visitedColumns = new HashSet<>();
ChangedSign sign = CraftBookBukkitUtil.toChangedSign(block);
if (smallSearchSize) {
// Toggle nearby gates
for (int x1 = x - 1; x1 <= x + 1; x1++) {
for (int y1 = y - 2; y1 <= y + 1; y1++) {
for (int z1 = z - 1; z1 <= z + 1; z1++) {
if (recurseColumn(player, sign, block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, true)) {
foundGate = true;
}
}
}
}
} else {
// Toggle nearby gates
for (int x1 = x - searchRadius; x1 <= x + searchRadius; x1++) {
for (int y1 = y - searchRadius; y1 <= y + searchRadius * 2; y1++) {
for (int z1 = z - searchRadius; z1 <= z + searchRadius; z1++) {
if (recurseColumn(player, sign, block.getWorld().getBlockAt(x1, y1, z1), visitedColumns, close, false)) {
foundGate = true;
}
}
}
}
}
// bag.flushChanges();
return foundGate && visitedColumns.size() > 0;
}
/**
* Toggles one column of gate.
*
* @param player The Player
* @param sign The sign block.
* @param block A part of the column.
* @param visitedColumns Previously visited columns.
* @param close Should close or open.
* @return true if a gate column was found and blocks were changed; false otherwise.
*/
private boolean recurseColumn(CraftBookPlayer player, ChangedSign sign, Block block, Set<GateColumn> visitedColumns, Boolean close, boolean smallSearchSize) {
if (limitColumns && visitedColumns.size() > columnLimit)
return false;
if (!isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(block.getBlockData()), true))
return false;
CraftBookPlugin.logDebugMessage("Found a possible gate column at " + block.getX() + ':' + block.getY() + ':' + block.getZ(), "gates.search");
int x = block.getX();
int z = block.getZ();
GateColumn column = new GateColumn(sign, block, smallSearchSize);
// The block above the gate cannot be air -- it has to be some
// non-fence block
if (block.getWorld().getBlockAt(x, column.getStartingY() + 1, z).getType().isAir())
return false;
if (visitedColumns.contains(column)) return false;
visitedColumns.add(column);
if (close == null)
close = !isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(block.getWorld().getBlockAt(x, column.getStartingY() - 1, z).getBlockData()), true);
CraftBookPlugin.logDebugMessage("Valid column at " + block.getX() + ':' + block.getY() + ':' + block.getZ() + " is being " + (close ? "closed" : "opened"), "gates.search");
CraftBookPlugin.logDebugMessage("Column Top: " + column.getStartingY() + " End: " + column.getEndingY(), "gates.search");
// Recursively go to connected fence blocks of the same level
// and 'close' or 'open' them
return toggleColumn(player, sign, block, column, close, visitedColumns, smallSearchSize);
}
/**
* Actually does the closing/opening. Also recurses to nearby columns.
*
* @param player The player.
* @param sign The sign block.
* @param block The top point of the gate.
* @param close To open or close.
* @param visitedColumns Previously searched columns.
*/
private boolean toggleColumn(CraftBookPlayer player, ChangedSign sign, Block block, GateColumn column, boolean close, Set<GateColumn> visitedColumns, boolean smallSearchSize) {
// If we want to close the gate then we replace air/water blocks
// below with fence blocks; otherwise, we want to replace fence
// blocks below with air
BlockData item;
if (close)
item = column.getStartingPoint().getBlockData();
else
item = Material.AIR.createBlockData();
CraftBookPlugin.logDebugMessage("Setting column at " + block.getX() + ':' + block.getY() + ':' + block.getZ() + " to " + item.toString(), "gates.search");
if (sign == null) {
CraftBookPlugin.logDebugMessage("Invalid Sign!", "gates.search");
return false;
}
ChangedSign otherSign = null;
Block ot = SignUtil.getNextSign(CraftBookBukkitUtil.toSign(sign).getBlock(), sign.getLine(1), 4);
if (ot != null) {
otherSign = CraftBookBukkitUtil.toChangedSign(ot);
}
for (BlockVector3 bl : column.getRegion()) {
Block blo = BukkitAdapter.adapt(block.getWorld(), bl).getBlock();
if (sign.getLine(2).equalsIgnoreCase("NoReplace")) {
// If NoReplace is on line 3 of sign, do not replace blocks.
if (blo.getType() != Material.AIR && !isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(blo.getBlockData()), true))
break;
} else // Allowing water allows the use of gates as flood gates
if (!canPassThrough(sign, smallSearchSize, BukkitAdapter.adapt(blo.getBlockData())))
break;
// bag.setBlockID(w, x, y1, z, ID);
if (CraftBook.getInstance().getPlatform().getConfiguration().safeDestruction) {
boolean hasBlocks = hasEnoughBlocks(sign, otherSign);
if (!close || hasBlocks) {
if (!close && isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(blo.getBlockData()), true)) {
addBlocks(sign, 1);
} else if (close && canPassThrough(sign, smallSearchSize, BukkitAdapter.adapt(blo.getBlockData())) && isValidGateBlock(sign,
smallSearchSize, BukkitAdapter.adapt(item), true) && item.getMaterial() != blo.getType()) {
removeBlocks(sign, 1);
}
blo.setBlockData(item, true);
} else if (isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(item), true))
if (player != null) {
player.printError("mech.not-enough-blocks");
return false;
}
} else
blo.setBlockData(item, true);
CraftBookPlugin.logDebugMessage("Set block " + bl.getX() + ':' + bl.getY() + ':' + bl.getZ() + " to " + item.toString(), "gates.search");
recurseColumn(player, sign, blo.getRelative(1, 0, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, blo.getRelative(-1, 0, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, blo.getRelative(0, 0, 1), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, blo.getRelative(0, 0, -1), visitedColumns, close, smallSearchSize);
}
recurseColumn(player, sign, column.getStartingPoint().getRelative(1, 0, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(-1, 0, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(0, 0, 1), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(0, 0, -1), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(1, 1, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(-1, 1, 0), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(0, 1, 1), visitedColumns, close, smallSearchSize);
recurseColumn(player, sign, column.getStartingPoint().getRelative(0, 1, -1), visitedColumns, close, smallSearchSize);
return true;
}
/**
* Raised when a block is right clicked.
*
* @param event SignClickEvent
*/
@EventHandler(priority = EventPriority.HIGH)
public void onRightClick(SignClickEvent event) {
if (!EventUtil.passesFilter(event))
return;
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
ChangedSign sign = event.getSign();
if (!sign.getLine(1).equals("[Gate]") && !sign.getLine(1).equals("[DGate]")) return;
boolean smallSearchSize = sign.getLine(1).equals("[DGate]");
BlockState gateBlock = getGateBlock(sign, smallSearchSize);
if (CraftBook.getInstance().getPlatform().getConfiguration().safeDestruction) {
if (player.getItemInHand(HandSide.MAIN_HAND).getType().hasBlockType()) {
BlockType heldType = player.getItemInHand(HandSide.MAIN_HAND).getType().getBlockType();
if ((gateBlock == null || gateBlock.getBlockType().getMaterial().isAir() || gateBlock.getBlockType() == heldType)
&& isValidGateBlock(sign, smallSearchSize, heldType.getDefaultState(), false)) {
if (!player.hasPermission("craftbook.mech.gate.restock")) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("mech.restock-permission");
return;
}
int amount = 1;
if (event.getPlayer().isSneaking())
amount = Math.min(5, event.getPlayer().getItemInHand().getAmount());
addBlocks(sign, amount);
if (enforceType) {
BlockType blockType = player.getItemInHand(HandSide.MAIN_HAND).getType().getBlockType();
sign.setLine(0, BlockParser.toMinifiedId(blockType));
sign.update(false);
}
if (!(event.getPlayer().getGameMode() == GameMode.CREATIVE))
if (event.getPlayer().getItemInHand().getAmount() <= amount)
event.getPlayer().setItemInHand(null);
else
event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() - amount);
player.print("mech.restock");
event.setCancelled(true);
return;
}
}
}
if (!player.hasPermission("craftbook.mech.gate.use")) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("mech.use-permission");
return;
}
if (!ProtectionUtil.canUse(event.getPlayer(), event.getClickedBlock().getLocation(), event.getBlockFace(), event.getAction())) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("area.use-permissions");
return;
}
if (toggleGates(player, event.getClickedBlock(), smallSearchSize, null))
player.print("mech.gate.toggle");
else
player.printError("mech.gate.not-found");
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH)
public void onBlockRedstoneChange(final SourcedBlockRedstoneEvent event) {
if (!EventUtil.passesFilter(event)) return;
if (!allowRedstone) return;
if (event.isMinor()) return;
if (!SignUtil.isSign(event.getBlock())) return;
final ChangedSign sign = CraftBookBukkitUtil.toChangedSign(event.getBlock());
if (!sign.getLine(1).equals("[Gate]") && !sign.getLine(1).equals("[DGate]")) return;
CraftBookPlugin.inst().getServer().getScheduler().runTaskLater(CraftBookPlugin.inst(),
() -> toggleGates(null, event.getBlock(), sign.getLine(1).equals("[DGate]"), event.getNewCurrent() > 0), 2);
}
@EventHandler(priority = EventPriority.HIGH)
public void onSignChange(SignChangeEvent event) {
if (!EventUtil.passesFilter(event)) return;
if (!event.getLine(1).equalsIgnoreCase("[Gate]") && !event.getLine(1).equalsIgnoreCase("[DGate]"))
return;
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
if (event.getLine(1).equalsIgnoreCase("[Gate]")) {
if (!player.hasPermission("craftbook.mech.gate")) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("mech.create-permission");
SignUtil.cancelSignChange(event);
return;
}
// get the material that this gate should toggle and verify it
String line0 = event.getLine(0).trim();
if (!line0.isEmpty()) {
if (!isValidGateBlock(BlockParser.getBlock(line0, true))) {
player.printError("Line 1 needs to be a valid block id.");
SignUtil.cancelSignChange(event);
return;
}
}
event.setLine(1, "[Gate]");
if (event.getLine(3).equalsIgnoreCase("infinite") && !player.hasPermission("craftbook.mech.gate.infinite"))
event.setLine(3, "0");
else if (!event.getLine(3).equalsIgnoreCase("infinite"))
event.setLine(3, "0");
player.print("mech.gate.create");
} else if (event.getLine(1).equalsIgnoreCase("[DGate]")) {
if (!player.hasPermission("craftbook.mech.gate") && !player.hasPermission("craftbook.mech.dgate")) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("mech.create-permission");
SignUtil.cancelSignChange(event);
return;
}
// get the material that this gate should toggle and verify it
String line0 = event.getLine(0).trim();
if (!line0.isEmpty()) {
if (!isValidGateBlock(BlockParser.getBlock(line0, true))) {
player.printError("mech.gate.valid-item");
SignUtil.cancelSignChange(event);
return;
}
}
event.setLine(1, "[DGate]");
if (event.getLine(3).equalsIgnoreCase("infinite") && !player.hasPermission("craftbook.mech.gate.infinite"))
event.setLine(3, "0");
else if (!event.getLine(3).equalsIgnoreCase("infinite"))
event.setLine(3, "0");
player.print("mech.dgate.create");
}
}
public boolean isValidGateBlock(BlockStateHolder block) {
return Blocks.containsFuzzy(blocks, block);
}
/**
* Checks if a block can be used in gate.
*
* @param sign The sign block.
* @param smallSearchSize Search small or large.
* @param block The block to check.
* @param check Should search.
* @return
*/
public boolean isValidGateBlock(ChangedSign sign, boolean smallSearchSize, BlockStateHolder<?> block, boolean check) {
BlockState type;
if (sign != null && !sign.getLine(0).isEmpty()) {
try {
BlockStateHolder<?> def = BlockParser.getBlock(sign.getLine(0), true);
return block.equalsFuzzy(def);
} catch (Exception e) {
if (check) {
type = getGateBlock(sign, smallSearchSize);
if (type == null || type.getBlockType().getMaterial().isAir())
return block.getBlockType().getMaterial().isAir();
}
return isValidGateBlock(block);
}
} else if (check && (type = getGateBlock(sign, smallSearchSize)) != null)
return block.equalsFuzzy(type);
else
return isValidGateBlock(block);
}
@EventHandler(priority = EventPriority.HIGH)
public void onBlockBreak(BlockBreakEvent event) {
if (!EventUtil.passesFilter(event)) return;
if (!SignUtil.isSign(event.getBlock())) return;
final ChangedSign sign = CraftBookBukkitUtil.toChangedSign(event.getBlock());
if (!sign.getLine(1).equals("[Gate]") && !sign.getLine(1).equals("[DGate]")) return;
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
if (ProtectionUtil.isBreakingPrevented(event.getPlayer(), event.getBlock())) {
if (CraftBook.getInstance().getPlatform().getConfiguration().showPermissionMessages)
player.printError("area.break-permissions");
return;
}
int amount = getBlocks(sign);
if (amount > 0) {
BlockState type = getGateBlock(sign, sign.getLine(1).equals("[DGate]"));
if (type == null || type.getBlockType().getMaterial().isAir())
type = BlockTypes.OAK_FENCE.getFuzzyMatcher();
ItemStack toDrop = new ItemStack(BukkitAdapter.adapt(type.getBlockType()), amount);
event.getBlock().getWorld().dropItemNaturally(BlockUtil.getBlockCentre(event.getBlock()), toDrop);
}
}
private boolean canPassThrough(ChangedSign sign, boolean smallSearchSize, BlockStateHolder t) {
if (!t.getBlockType().getMaterial().isMovementBlocker()) {
return true;
}
return isValidGateBlock(sign, smallSearchSize, t, true);
}
public BlockState getGateBlock(ChangedSign sign, boolean smallSearchSize) {
BlockState gateBlock = null;
if (sign != null) {
if (!sign.getLine(0).isEmpty()) {
try {
return BlockParser.getBlock(sign.getLine(0), true).toImmutableState();
} catch (Exception ignored) {
}
}
int x = sign.getX();
int y = sign.getY();
int z = sign.getZ();
if (smallSearchSize) {
for (int x1 = x - 1; x1 <= x + 1; x1++) {
for (int y1 = y - 2; y1 <= y + 1; y1++) {
for (int z1 = z - 1; z1 <= z + 1; z1++) {
if (getFirstBlock(sign, sign.getBlock().getWorld().getBlockAt(x1, y1, z1), true) != null) {
gateBlock = BukkitAdapter.adapt(getFirstBlock(sign, sign.getBlock().getWorld().getBlockAt(x1, y1, z1), true).getBlockData());
break;
}
}
}
}
} else {
for (int x1 = x - searchRadius; x1 <= x + searchRadius; x1++) {
for (int y1 = y - searchRadius; y1 <= y + searchRadius * 2; y1++) {
for (int z1 = z - searchRadius; z1 <= z + searchRadius; z1++) {
if (getFirstBlock(sign, sign.getBlock().getWorld().getBlockAt(x1, y1, z1), false) != null) {
gateBlock = BukkitAdapter.adapt(getFirstBlock(sign, sign.getBlock().getWorld().getBlockAt(x1, y1, z1), false).getBlockData());
break;
}
}
}
}
}
if (enforceType && gateBlock != null && !gateBlock.getBlockType().getMaterial().isAir()) {
sign.setLine(0, BlockParser.toMinifiedId(gateBlock.getBlockType()));
sign.update(false);
}
}
return gateBlock;
}
public Block getFirstBlock(ChangedSign sign, Block block, boolean smallSearchSize) {
if (!isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(block.getBlockData()), false))
return null;
return block;
}
public void removeBlocks(ChangedSign s, int amount) {
if (s.getLine(3).equalsIgnoreCase("infinite")) return;
setBlocks(s, getBlocks(s) - amount);
}
public void addBlocks(ChangedSign s, int amount) {
if (s.getLine(3).equalsIgnoreCase("infinite")) return;
setBlocks(s, getBlocks(s) + amount);
}
public void setBlocks(ChangedSign s, int amount) {
if (s.getLine(3).equalsIgnoreCase("infinite")) return;
s.setLine(3, String.valueOf(amount));
s.update(false);
}
public int getBlocks(ChangedSign s) {
if (s.getLine(3).equalsIgnoreCase("infinite")) return 0;
return getBlocks(s, null);
}
public int getBlocks(ChangedSign s, ChangedSign other) {
if (s.getLine(3).equalsIgnoreCase("infinite") || other != null && other.getLine(3).equalsIgnoreCase("infinite"))
return 0;
int curBlocks;
try {
curBlocks = Integer.parseInt(s.getLine(3));
if (other != null && other.getLine(0).equals(s.getLine(0))) {
try {
curBlocks += Integer.parseInt(other.getLine(3));
setBlocks(s, curBlocks);
setBlocks(other, 0);
} catch (Exception ignored) {
}
}
} catch (Exception e) {
curBlocks = 0;
}
return curBlocks;
}
public boolean hasEnoughBlocks(ChangedSign s) {
return s.getLine(3).equalsIgnoreCase("infinite") || getBlocks(s) > 0;
}
public boolean hasEnoughBlocks(ChangedSign s, ChangedSign other) {
if (other == null)
return hasEnoughBlocks(s);
else
return s.getLine(3).equalsIgnoreCase("infinite") || other.getLine(3).equalsIgnoreCase("infinite") || getBlocks(s, other) > 0;
}
protected class GateColumn {
private final ChangedSign sign;
private final Block block;
private final boolean smallSearchSize;
private int minY = -1, maxY = -1;
private int remainingColumnHeight;
public GateColumn(ChangedSign sign, Block block, boolean smallSearchSize) {
this.sign = sign;
this.block = block;
this.smallSearchSize = smallSearchSize;
remainingColumnHeight = columnHeight;
}
public Block getStartingPoint() {
return block.getWorld().getBlockAt(block.getX(), getStartingY(), block.getZ());
}
public Block getEndingPoint() {
return block.getWorld().getBlockAt(block.getX(), getEndingY(), block.getZ());
}
public int getStartingY() {
if (maxY == -1) {
int max = Math.min(block.getWorld().getMaxHeight() - 1, block.getY() + remainingColumnHeight);
for (int y1 = block.getY() + 1; y1 <= max; y1++) {
if (remainingColumnHeight <= 0) break;
if (isValidGateBlock(sign, smallSearchSize, BukkitAdapter.adapt(block.getWorld().getBlockAt(block.getX(), y1, block.getZ()).getBlockData()), true)) {
maxY = y1;
remainingColumnHeight--;
} else
break;
}
if (maxY == -1) maxY = block.getY();
}
return maxY;
}
public int getEndingY() {
if (minY == -1) {
int min = Math.max(0, block.getY() - remainingColumnHeight);
for (int y = block.getY(); y >= min; y--) {
if (remainingColumnHeight <= 0) break;
BlockState currentBlock = BukkitAdapter.adapt(block.getWorld().getBlockAt(block.getX(), y, block.getZ()).getBlockData());
if (canPassThrough(sign, smallSearchSize, currentBlock)
|| isValidGateBlock(sign, smallSearchSize, currentBlock, true)) {
minY = y;
remainingColumnHeight--;
} else
break;
}
if (minY == -1) minY = block.getY();
}
return minY;
}
public int getX() {
return block.getX();
}
public int getZ() {
return block.getZ();
}
public CuboidRegion getRegion() {
return new CuboidRegion(
BukkitAdapter.adapt(getStartingPoint().getRelative(0, -1, 0).getLocation()).toVector().toBlockPoint(),
BukkitAdapter.adapt(getEndingPoint().getLocation()).toVector().toBlockPoint()
);
}
@Override
public boolean equals(Object o) {
return o instanceof GateColumn && ((GateColumn) o).getX() == getX() && ((GateColumn) o).getZ() == getZ() && block.getWorld().getName().equals(((GateColumn) o).block.getWorld().getName());
}
@Override
public int hashCode() {
// Constants correspond to glibc's lcg algorithm parameters
return (getX() * 1103515245 + 12345 ^ getZ() * 1103515245 + 12345) * 1103515245 + 12345;
}
}
private boolean allowRedstone;
private boolean limitColumns;
private int columnLimit;
private List<BaseBlock> blocks;
private boolean enforceType;
private int columnHeight;
private int searchRadius;
public List<String> getDefaultBlocks() {
List<String> materials = new ArrayList<>();
materials.add(BlockTypes.ACACIA_FENCE.getId());
materials.add(BlockTypes.BIRCH_FENCE.getId());
materials.add(BlockTypes.JUNGLE_FENCE.getId());
materials.add(BlockTypes.OAK_FENCE.getId());
materials.add(BlockTypes.SPRUCE_FENCE.getId());
materials.add(BlockTypes.DARK_OAK_FENCE.getId());
materials.add(BlockTypes.NETHER_BRICK_FENCE.getId());
materials.add(BlockTypes.IRON_BARS.getId());
materials.add(BlockTypes.GLASS_PANE.getId());
return materials;
}
@Override
public void loadFromConfiguration(YAMLProcessor config) {
config.setComment("allow-redstone", "Allows the gate mechanic to be toggled via redstone.");
allowRedstone = config.getBoolean("allow-redstone", true);
config.setComment("limit-columns", "Limit the amount of columns a gate can toggle.");
limitColumns = config.getBoolean("limit-columns", true);
config.setComment("max-columns", "If limit-columns is enabled, the maximum number of columns that a gate can toggle.");
columnLimit = config.getInt("max-columns", 14);
config.setComment("blocks", "The list of blocks that a gate can use.");
blocks = BlockParser.getBlocks(config.getStringList("blocks", getDefaultBlocks()), true);
config.setComment("enforce-type", "Make sure gates are only able to toggle a specific material type. This prevents transmutation.");
enforceType = config.getBoolean("enforce-type", true);
config.setComment("max-column-height", "The max height of a column.");
columnHeight = config.getInt("max-column-height", 12);
config.setComment("gate-search-radius", "The radius around the sign the gate checks for fences in. Note: This is doubled upwards.");
searchRadius = config.getInt("gate-search-radius", 3);
}
}