/
AbstractPlayerActor.java
535 lines (449 loc) · 16.1 KB
/
AbstractPlayerActor.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
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.extension.platform;
import com.sk89q.worldedit.NotABlockException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.TargetBlock;
import com.sk89q.worldedit.util.auth.AuthorizationException;
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 com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import javax.annotation.Nullable;
import java.io.File;
/**
* An abstract implementation of both a {@link Actor} and a {@link Player}
* that is intended for implementations of WorldEdit to use to wrap
* players that make use of WorldEdit.
*/
public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override
public final Extent getExtent() {
return getWorld();
}
/**
* Returns direction according to rotation. May return null.
*
* @param rot yaw
* @return the direction
*/
private static Direction getDirection(double rot) {
if (0 <= rot && rot < 22.5) {
return Direction.SOUTH;
} else if (22.5 <= rot && rot < 67.5) {
return Direction.SOUTHWEST;
} else if (67.5 <= rot && rot < 112.5) {
return Direction.WEST;
} else if (112.5 <= rot && rot < 157.5) {
return Direction.NORTHWEST;
} else if (157.5 <= rot && rot < 202.5) {
return Direction.NORTH;
} else if (202.5 <= rot && rot < 247.5) {
return Direction.NORTHEAST;
} else if (247.5 <= rot && rot < 292.5) {
return Direction.EAST;
} else if (292.5 <= rot && rot < 337.5) {
return Direction.SOUTHEAST;
} else if (337.5 <= rot && rot < 360.0) {
return Direction.SOUTH;
} else {
return null;
}
}
@Override
public boolean isHoldingPickAxe() {
ItemType item = getItemInHand(HandSide.MAIN_HAND).getType();
return item == ItemTypes.IRON_PICKAXE
|| item == ItemTypes.WOODEN_PICKAXE
|| item == ItemTypes.STONE_PICKAXE
|| item == ItemTypes.DIAMOND_PICKAXE
|| item == ItemTypes.GOLDEN_PICKAXE;
}
@Override
public void findFreePosition(Location searchPos) {
Extent world = searchPos.getExtent();
int x = searchPos.getBlockX();
int y = Math.max(0, searchPos.getBlockY());
int origY = y;
int z = searchPos.getBlockZ();
byte free = 0;
while (y <= world.getMaximumPoint().getBlockY() + 2) {
if (!world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isMovementBlocker()) {
++free;
} else {
free = 0;
}
if (free == 2) {
if (y - 1 != origY) {
setPosition(Vector3.at(x + 0.5, y - 2 + 1, z + 0.5));
}
return;
}
++y;
}
}
@Override
public void setOnGround(Location searchPos) {
Extent world = searchPos.getExtent();
int x = searchPos.getBlockX();
int y = Math.max(0, searchPos.getBlockY());
int z = searchPos.getBlockZ();
while (y >= 0) {
final BlockVector3 pos = BlockVector3.at(x, y, z);
final BlockState id = world.getBlock(pos);
if (id.getBlockType().getMaterial().isMovementBlocker()) {
setPosition(Vector3.at(x + 0.5, y + 1, z + 0.5));
return;
}
--y;
}
}
@Override
public void findFreePosition() {
findFreePosition(getBlockIn());
}
@Override
public boolean ascendLevel() {
final Location pos = getBlockIn();
final int x = pos.getBlockX();
int y = Math.max(0, pos.getBlockY());
final int z = pos.getBlockZ();
final Extent world = pos.getExtent();
byte free = 0;
byte spots = 0;
while (y <= world.getMaximumPoint().getY() + 2) {
if (!world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isMovementBlocker()) {
++free;
} else {
free = 0;
}
if (free == 2) {
++spots;
if (spots == 2) {
final BlockVector3 platform = BlockVector3.at(x, y - 2, z);
final BlockState block = world.getBlock(platform);
final com.sk89q.worldedit.world.block.BlockType type = block.getBlockType();
// Don't get put in lava!
if (type == BlockTypes.LAVA) {
return false;
}
setPosition(platform.toVector3().add(0.5, 1, 0.5));
return true;
}
}
++y;
}
return false;
}
@Override
public boolean descendLevel() {
final Location pos = getBlockIn();
final int x = pos.getBlockX();
int y = Math.max(0, pos.getBlockY() - 1);
final int z = pos.getBlockZ();
final Extent world = pos.getExtent();
byte free = 0;
while (y >= 1) {
if (!world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isMovementBlocker()) {
++free;
} else {
free = 0;
}
if (free == 2) {
// So we've found a spot, but we have to drop the player
// lightly and also check to see if there's something to
// stand upon
while (y >= 0) {
final BlockVector3 platform = BlockVector3.at(x, y, z);
final BlockState block = world.getBlock(platform);
final BlockType type = block.getBlockType();
// Don't want to end up in lava
if (!type.getMaterial().isAir() && type != BlockTypes.LAVA) {
// Found a block!
setPosition(platform.toVector3().add(0.5, 1, 0.5));
return true;
}
--y;
}
return false;
}
--y;
}
return false;
}
@Override
public boolean ascendToCeiling(int clearance) {
return ascendToCeiling(clearance, true);
}
@Override
public boolean ascendToCeiling(int clearance, boolean alwaysGlass) {
Location pos = getBlockIn();
int x = pos.getBlockX();
int initialY = Math.max(0, pos.getBlockY());
int y = Math.max(0, pos.getBlockY() + 2);
int z = pos.getBlockZ();
Extent world = getLocation().getExtent();
// No free space above
if (!world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isAir()) {
return false;
}
while (y <= world.getMaximumPoint().getY()) {
// Found a ceiling!
if (world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isMovementBlocker()) {
int platformY = Math.max(initialY, y - 3 - clearance);
floatAt(x, platformY + 1, z, alwaysGlass);
return true;
}
++y;
}
return false;
}
@Override
public boolean ascendUpwards(int distance) {
return ascendUpwards(distance, true);
}
@Override
public boolean ascendUpwards(int distance, boolean alwaysGlass) {
final Location pos = getBlockIn();
final int x = pos.getBlockX();
final int initialY = Math.max(0, pos.getBlockY());
int y = Math.max(0, pos.getBlockY() + 1);
final int z = pos.getBlockZ();
final int maxY = Math.min(getWorld().getMaxY() + 1, initialY + distance);
final Extent world = getLocation().getExtent();
while (y <= world.getMaximumPoint().getY() + 2) {
if (world.getBlock(BlockVector3.at(x, y, z)).getBlockType().getMaterial().isMovementBlocker()) {
break; // Hit something
} else if (y > maxY + 1) {
break;
} else if (y == maxY + 1) {
floatAt(x, y - 1, z, alwaysGlass);
return true;
}
++y;
}
return false;
}
@Override
public void floatAt(int x, int y, int z, boolean alwaysGlass) {
try {
BlockVector3 spot = BlockVector3.at(x, y - 1, z);
if (!getLocation().getExtent().getBlock(spot).getBlockType().getMaterial().isMovementBlocker()) {
getLocation().getExtent().setBlock(spot, BlockTypes.GLASS.getDefaultState());
}
} catch (WorldEditException e) {
e.printStackTrace();
}
setPosition(Vector3.at(x + 0.5, y, z + 0.5));
}
@Override
public Location getBlockIn() {
return getLocation().setPosition(getLocation().toVector().floor());
}
@Override
public Location getBlockOn() {
return getLocation().setPosition(getLocation().setY(getLocation().getY() - 1).toVector().floor());
}
@Override
public Location getBlockTrace(int range, boolean useLastBlock) {
return getBlockTrace(range, useLastBlock, null);
}
@Override
public Location getBlockTraceFace(int range, boolean useLastBlock) {
return getBlockTraceFace(range, useLastBlock, null);
}
@Override
public Location getBlockTrace(int range, boolean useLastBlock, @Nullable Mask stopMask) {
TargetBlock tb = new TargetBlock(this, range, 0.2);
if (stopMask != null) {
tb.setStopMask(stopMask);
}
return (useLastBlock ? tb.getAnyTargetBlock() : tb.getTargetBlock());
}
@Override
public Location getBlockTraceFace(int range, boolean useLastBlock, @Nullable Mask stopMask) {
TargetBlock tb = new TargetBlock(this, range, 0.2);
if (stopMask != null) {
tb.setStopMask(stopMask);
}
return (useLastBlock ? tb.getAnyTargetBlockFace() : tb.getTargetBlockFace());
}
@Override
public Location getBlockTrace(int range) {
return getBlockTrace(range, false);
}
@Override
public Location getSolidBlockTrace(int range) {
TargetBlock tb = new TargetBlock(this, range, 0.2);
return tb.getSolidTargetBlock();
}
@Override
public Direction getCardinalDirection() {
return getCardinalDirection(0);
}
@Override
public Direction getCardinalDirection(int yawOffset) {
if (getLocation().getPitch() > 67.5) {
return Direction.DOWN;
}
if (getLocation().getPitch() < -67.5) {
return Direction.UP;
}
// From hey0's code
double rot = (getLocation().getYaw() + yawOffset) % 360; //let's use real yaw now
if (rot < 0) {
rot += 360.0;
}
return getDirection(rot);
}
@Override
public BaseBlock getBlockInHand(HandSide handSide) throws WorldEditException {
final ItemType typeId = getItemInHand(handSide).getType();
if (typeId.hasBlockType()) {
return typeId.getBlockType().getDefaultState().toBaseBlock();
} else {
throw new NotABlockException();
}
}
private boolean canPassThroughBlock(Location curBlock) {
BlockVector3 blockPos = curBlock.toVector().toBlockPoint();
BlockState block = curBlock.getExtent().getBlock(blockPos);
return block.getBlockType().getMaterial().isMovementBlocker();
}
/**
* Advances the block target block until the current block is a wall
* @return true if a wall is found
*/
private boolean advanceToWall(TargetBlock hitBlox) {
Location curBlock;
while ((curBlock = hitBlox.getCurrentBlock()) != null) {
if (!canPassThroughBlock(curBlock)) {
return true;
}
hitBlox.getNextBlock();
}
return false;
}
/**
* Advances the block target block until the current block is a free
* @return true if a free spot is found
*/
private boolean advanceToFree(TargetBlock hitBlox) {
Location curBlock;
while ((curBlock = hitBlox.getCurrentBlock()) != null) {
if (canPassThroughBlock(curBlock)) {
return true;
}
hitBlox.getNextBlock();
}
return false;
}
@Override
public boolean passThroughForwardWall(int range) {
TargetBlock hitBlox = new TargetBlock(this, range, 0.2);
if (!advanceToWall(hitBlox)) {
return false;
}
if (!advanceToFree(hitBlox)) {
return false;
}
Location foundBlock = hitBlox.getCurrentBlock();
if (foundBlock != null) {
setOnGround(foundBlock);
return true;
}
return false;
}
@Override
public void setPosition(Vector3 pos) {
setPosition(pos, getLocation().getPitch(), getLocation().getYaw());
}
@Override
public File openFileOpenDialog(String[] extensions) {
printError("File dialogs are not supported in your environment.");
return null;
}
@Override
public File openFileSaveDialog(String[] extensions) {
printError("File dialogs are not supported in your environment.");
return null;
}
@Override
public boolean canDestroyBedrock() {
return hasPermission("worldedit.override.bedrock");
}
@Override
public void dispatchCUIEvent(CUIEvent event) {
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Player)) {
return false;
}
Player other2 = (Player) other;
return other2.getName().equals(getName());
}
@Override
public int hashCode() {
return getName().hashCode();
}
@Override
public void checkPermission(String permission) throws AuthorizationException {
if (!hasPermission(permission)) {
throw new AuthorizationException();
}
}
@Override
public boolean isPlayer() {
return true;
}
@Override
public GameMode getGameMode() {
return GameModes.SURVIVAL;
}
@Override
public void setGameMode(GameMode gameMode) {
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Not supported");
}
@Override
public boolean remove() {
return false;
}
@Override
public <B extends BlockStateHolder<B>> void sendFakeBlock(BlockVector3 pos, B block) {
}
}