-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathCraftHumanEntity.java
More file actions
931 lines (780 loc) · 36.1 KB
/
CraftHumanEntity.java
File metadata and controls
931 lines (780 loc) · 36.1 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
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import io.papermc.paper.adventure.PaperAdventure;
import net.kyori.adventure.key.Key;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundMountScreenOpenPacket;
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.animal.equine.AbstractHorse;
import net.minecraft.world.entity.animal.nautilus.AbstractNautilus;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.AbstractMountInventoryMenu;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.MerchantMenu;
import net.minecraft.world.item.ItemCooldowns;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftEquipmentSlot;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.event.CraftEventFactory;
import org.bukkit.craftbukkit.inventory.CraftContainer;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftInventorySaddledMount;
import org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest;
import org.bukkit.craftbukkit.inventory.CraftInventoryLectern;
import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.inventory.CraftMerchantCustom;
import org.bukkit.craftbukkit.inventory.CraftRecipe;
import org.bukkit.craftbukkit.inventory.util.CraftMenus;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.Firework;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Item;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MainHand;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
private CraftInventoryPlayer inventory;
private final CraftInventory enderChest;
protected final PermissibleBase perm = new PermissibleBase(this);
private boolean op;
private GameMode mode;
public CraftHumanEntity(final CraftServer server, final Player entity) {
super(server, entity);
this.mode = server.getDefaultGameMode();
this.inventory = new CraftInventoryPlayer(entity.getInventory());
this.enderChest = new CraftInventory(entity.getEnderChestInventory());
}
@Override
public Player getHandle() {
return (Player) this.entity;
}
public void setHandle(final Player entity) {
super.setHandle(entity);
this.inventory = new CraftInventoryPlayer(entity.getInventory());
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "{name=" + this.getName() + ", uuid=" + this.getUniqueId() + '}';
}
@Override
public PlayerInventory getInventory() {
return this.inventory;
}
@Override
public EntityEquipment getEquipment() {
return this.inventory;
}
@Override
public Inventory getEnderChest() {
return this.enderChest;
}
@Override
public MainHand getMainHand() {
return this.getHandle().getMainArm() == HumanoidArm.LEFT ? MainHand.LEFT : MainHand.RIGHT;
}
@Override
public ItemStack getItemInHand() {
return this.getInventory().getItemInMainHand();
}
@Override
public void setItemInHand(ItemStack item) {
this.getInventory().setItemInMainHand(item);
}
@Override
public ItemStack getItemOnCursor() {
return CraftItemStack.asCraftMirror(this.getHandle().containerMenu.getCarried());
}
@Override
public void setItemOnCursor(ItemStack item) {
net.minecraft.world.item.ItemStack stack = CraftItemStack.asNMSCopy(item);
this.getHandle().containerMenu.setCarried(stack);
if (this instanceof CraftPlayer) {
this.getHandle().containerMenu.broadcastCarriedItem(); // Send set slot for cursor
}
}
@Override
public void setHurtDirection(float hurtDirection) {
this.getHandle().hurtDir = hurtDirection;
}
@Override
public boolean isDeeplySleeping() {
return getHandle().isSleepingLongEnough();
}
@Override
public int getSleepTicks() {
return this.getHandle().sleepCounter;
}
@Override
public Location getPotentialRespawnLocation() {
ServerPlayer.RespawnConfig respawnConfig = ((ServerPlayer) this.getHandle()).getRespawnConfig();
if (respawnConfig == null) {
return null;
}
net.minecraft.server.level.ServerLevel level = this.server.getServer().getLevel(respawnConfig.respawnData().dimension());
if (level == null) {
return null;
}
return CraftLocation.toBukkit(respawnConfig.respawnData().pos(), level, respawnConfig.respawnData().yaw(), respawnConfig.respawnData().pitch());
}
@Override
public org.bukkit.entity.FishHook getFishHook() {
if (getHandle().fishing == null) {
return null;
}
return (org.bukkit.entity.FishHook) getHandle().fishing.getBukkitEntity();
}
@Override
public boolean sleep(Location location, boolean force) {
Preconditions.checkArgument(location != null, "Location cannot be null");
Preconditions.checkArgument(location.getWorld() != null, "Location needs to be in a world");
Preconditions.checkArgument(location.getWorld().equals(this.getWorld()), "Cannot sleep across worlds");
BlockPos pos = CraftLocation.toBlockPosition(location);
BlockState state = this.getHandle().level().getBlockState(pos);
if (!(state.getBlock() instanceof BedBlock)) {
return false;
}
if (this.getHandle().startSleepInBed(pos, force).left().isPresent()) {
return false;
}
// From BlockBed
state = state.setValue(BedBlock.OCCUPIED, true);
this.getHandle().level().setBlock(pos, state, net.minecraft.world.level.block.Block.UPDATE_INVISIBLE);
return true;
}
@Override
public boolean sleep(Location location) {
return this.sleep(location, false);
}
@Override
public void wakeup(boolean setSpawnLocation) {
Preconditions.checkState(this.isSleeping(), "Cannot wakeup if not sleeping");
this.getHandle().stopSleepInBed(true, setSpawnLocation);
}
@Override
public void wakeup() {
this.wakeup(false);
}
@Override
public void startRiptideAttack(int duration, float damage, ItemStack attackItem) {
Preconditions.checkArgument(duration > 0, "Duration must be greater than 0");
Preconditions.checkArgument(damage >= 0, "Damage must not be negative");
this.getHandle().startAutoSpinAttack(duration, damage, CraftItemStack.asNMSCopy(attackItem));
}
@Override
public Location getBedLocation() {
Preconditions.checkState(this.isSleeping(), "Not sleeping");
BlockPos bed = this.getHandle().getSleepingPos().get();
return CraftLocation.toBukkit(bed, this.getWorld());
}
@Override
public String getName() {
return this.getHandle().getScoreboardName();
}
@Override
public boolean isOp() {
return this.op;
}
@Override
public boolean isPermissionSet(String name) {
return this.perm.isPermissionSet(name);
}
@Override
public boolean isPermissionSet(Permission perm) {
return this.perm.isPermissionSet(perm);
}
@Override
public boolean hasPermission(String name) {
return this.perm.hasPermission(name);
}
@Override
public boolean hasPermission(Permission perm) {
return this.perm.hasPermission(perm);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
return this.perm.addAttachment(plugin, name, value);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin) {
return this.perm.addAttachment(plugin);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
return this.perm.addAttachment(plugin, name, value, ticks);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
return this.perm.addAttachment(plugin, ticks);
}
@Override
public void removeAttachment(PermissionAttachment attachment) {
this.perm.removeAttachment(attachment);
}
@Override
public void recalculatePermissions() {
this.perm.recalculatePermissions();
}
@Override
public void setOp(boolean value) {
this.op = value;
this.perm.recalculatePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return this.perm.getEffectivePermissions();
}
@Override
public GameMode getGameMode() {
return this.mode;
}
@Override
public void setGameMode(GameMode mode) {
Preconditions.checkArgument(mode != null, "GameMode cannot be null");
this.mode = mode;
}
@Override
public InventoryView getOpenInventory() {
return this.getHandle().containerMenu.getBukkitView();
}
@Override
public InventoryView openInventory(Inventory inventory) {
if (!(this.getHandle() instanceof ServerPlayer)) return null;
ServerPlayer player = (ServerPlayer) this.getHandle();
AbstractContainerMenu formerContainer = this.getHandle().containerMenu;
MenuProvider menuProvider = null;
if (inventory instanceof CraftInventoryDoubleChest) {
menuProvider = ((CraftInventoryDoubleChest) inventory).provider;
} else if (inventory instanceof CraftInventoryLectern) {
menuProvider = ((CraftInventoryLectern) inventory).provider;
} else if (inventory instanceof CraftInventory) {
CraftInventory craft = (CraftInventory) inventory;
if (craft.getInventory() instanceof MenuProvider) {
menuProvider = (MenuProvider) craft.getInventory();
}
}
if (menuProvider != null) {
if (menuProvider instanceof final BlockEntity blockEntity) {
if (!blockEntity.hasLevel()) {
blockEntity.setLevel(this.getHandle().level());
}
}
}
if (menuProvider != null) {
this.getHandle().openMenu(menuProvider);
} else if (inventory instanceof CraftInventorySaddledMount craft && craft.getInventory().getOwner() instanceof CraftAbstractHorse horse) {
this.getHandle().openHorseInventory(horse.getHandle(), craft.getInventory());
} else if (inventory instanceof CraftInventorySaddledMount craft && craft.getInventory().getOwner() instanceof CraftAbstractNautilus nautilus) {
this.getHandle().openNautilusInventory(nautilus.getHandle(), craft.getInventory());
} else {
MenuType<?> container = CraftContainer.getNotchInventoryType(inventory);
CraftHumanEntity.openCustomInventory(inventory, player, container);
}
if (this.getHandle().containerMenu == formerContainer) {
return null;
}
this.getHandle().containerMenu.checkReachable = false;
return this.getHandle().containerMenu.getBukkitView();
}
private static void openCustomInventory(Inventory inventory, ServerPlayer player, MenuType<?> windowType) {
if (player.connection == null) return;
Preconditions.checkArgument(windowType != null, "Unknown windowType");
AbstractContainerMenu container = new CraftContainer(inventory, player, player.nextContainerCounter());
// Paper start - Add titleOverride to InventoryOpenEvent
final com.mojang.datafixers.util.Pair<net.kyori.adventure.text.Component, AbstractContainerMenu> result = CraftEventFactory.callInventoryOpenEventWithTitle(player, container);
container = result.getSecond();
// Paper end - Add titleOverride to InventoryOpenEvent
if (container == null) return;
//String title = container.getBukkitView().getTitle(); // Paper - comment
net.kyori.adventure.text.Component adventure$title = container.getBukkitView().title(); // Paper
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(container.getBukkitView().getTitle()); // Paper
if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
//player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, CraftChatMessage.fromString(title)[0])); // Paper - comment
if (!player.isImmobile()) player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title))); // Paper - Prevent opening inventories when frozen
player.containerMenu = container;
player.initMenu(container);
}
@Override
public InventoryView openWorkbench(Location location, boolean force) {
if (location == null) {
location = this.getLocation();
}
if (!force) {
Block block = location.getBlock();
if (block.getType() != Material.CRAFTING_TABLE) {
return null;
}
}
this.getHandle().openMenu(Blocks.CRAFTING_TABLE.defaultBlockState().getMenuProvider(this.getHandle().level(), CraftLocation.toBlockPosition(location)));
if (force) {
this.getHandle().containerMenu.checkReachable = false;
}
return this.getHandle().containerMenu.getBukkitView();
}
@Override
public InventoryView openEnchanting(Location location, boolean force) {
if (location == null) {
location = this.getLocation();
}
if (!force) {
Block block = location.getBlock();
if (block.getType() != Material.ENCHANTING_TABLE) {
return null;
}
}
// If there isn't an enchant table we can force create one, won't be very useful though.
BlockPos pos = CraftLocation.toBlockPosition(location);
// Paper start
MenuProvider menuProvider = Blocks.ENCHANTING_TABLE.defaultBlockState().getMenuProvider(this.getHandle().level(), pos);
if (menuProvider == null) {
if (!force) {
return null;
}
menuProvider = new net.minecraft.world.SimpleMenuProvider((syncId, inventory, player) -> {
return new net.minecraft.world.inventory.EnchantmentMenu(syncId, inventory, net.minecraft.world.inventory.ContainerLevelAccess.create(this.getHandle().level(), pos));
}, Component.translatable("container.enchant"));
}
this.getHandle().openMenu(menuProvider);
// Paper end
if (force) {
this.getHandle().containerMenu.checkReachable = false;
}
return this.getHandle().containerMenu.getBukkitView();
}
@Override
public void openInventory(InventoryView inventory) {
Preconditions.checkArgument(this.equals(inventory.getPlayer()), "InventoryView must belong to the opening player");
if (!(this.getHandle() instanceof ServerPlayer)) return; // TODO: NPC support?
if (((ServerPlayer) this.getHandle()).connection == null) return;
if (this.getHandle().containerMenu != this.getHandle().inventoryMenu) {
// fire INVENTORY_CLOSE if one already open
((ServerPlayer) this.getHandle()).connection.handleContainerClose(new ServerboundContainerClosePacket(this.getHandle().containerMenu.containerId), org.bukkit.event.inventory.InventoryCloseEvent.Reason.OPEN_NEW); // Paper - Inventory close reason
}
ServerPlayer player = (ServerPlayer) this.getHandle();
AbstractContainerMenu container;
if (inventory instanceof CraftInventoryView) {
container = ((CraftInventoryView) inventory).getHandle();
Preconditions.checkArgument(!(container instanceof InventoryMenu), "Can not open player's InventoryView");
} else {
container = new CraftContainer(inventory, this.getHandle(), player.nextContainerCounter());
}
// Trigger an INVENTORY_OPEN event
// Paper start - Add titleOverride to InventoryOpenEvent
final com.mojang.datafixers.util.Pair<net.kyori.adventure.text.Component, AbstractContainerMenu> result = CraftEventFactory.callInventoryOpenEventWithTitle(player, container);
container = result.getSecond();
// Paper end - Add titleOverride to InventoryOpenEvent
if (container == null) {
return;
}
// Now open the window
MenuType<?> windowType = CraftContainer.getNotchInventoryType(inventory.getTopInventory());
// we can open these now, delegate for now
if (windowType == MenuType.MERCHANT) {
CraftMenus.openMerchantMenu(player, (MerchantMenu) container);
return;
}
net.kyori.adventure.text.Component adventure$title = inventory.title(); // Paper
if (adventure$title == null) adventure$title = net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(inventory.getTitle()); // Paper
if (result.getFirst() != null) adventure$title = result.getFirst(); // Paper - Add titleOverride to InventoryOpenEvent
if (!player.isImmobile()) {
if (container instanceof AbstractMountInventoryMenu mountMenu) {
player.connection.send(new ClientboundMountScreenOpenPacket(mountMenu.containerId, getMountInventoryColumns(mountMenu), mountMenu.mount.getId()));
} else {
player.connection.send(new ClientboundOpenScreenPacket(container.containerId, windowType, io.papermc.paper.adventure.PaperAdventure.asVanilla(adventure$title)));
}
}
player.containerMenu = container;
player.initMenu(container);
}
private static int getMountInventoryColumns(final AbstractMountInventoryMenu mountMenu) {
if (mountMenu.mount instanceof AbstractHorse horse) {
return horse.getInventoryColumns();
} else if (mountMenu.mount instanceof AbstractNautilus nautilus) {
return nautilus.getInventoryColumns();
}
throw new UnsupportedOperationException("Cannot get inventory columns for " + mountMenu.mount.getType().getDescriptionId());
}
@Override
public InventoryView openMerchant(Merchant merchant, boolean force) {
Preconditions.checkNotNull(merchant, "merchant cannot be null");
if (!force && merchant.isTrading()) {
return null;
} else if (merchant.isTrading()) {
// we're not supposed to have multiple people using the same merchant, so we have to close it.
merchant.getTrader().closeInventory();
}
net.minecraft.world.item.trading.Merchant mcMerchant;
Component name;
int level = 1; // note: using level 0 with active 'is-regular-villager'-flag allows hiding the name suffix
if (merchant instanceof CraftAbstractVillager) {
mcMerchant = ((CraftAbstractVillager) merchant).getHandle();
name = ((CraftAbstractVillager) merchant).getHandle().getDisplayName();
if (merchant instanceof CraftVillager) {
level = ((CraftVillager) merchant).getHandle().getVillagerData().level();
}
} else if (merchant instanceof CraftMerchantCustom) {
mcMerchant = ((CraftMerchantCustom) merchant).getMerchant();
name = ((CraftMerchantCustom) merchant).getMerchant().getScoreboardDisplayName();
} else {
throw new IllegalArgumentException("Can't open merchant " + merchant.toString());
}
mcMerchant.setTradingPlayer(this.getHandle());
mcMerchant.openTradingScreen(this.getHandle(), name, level);
return this.getHandle().containerMenu.getBukkitView();
}
@Override
public InventoryView openAnvil(Location location, boolean force) {
return this.openInventory(location, force, Material.ANVIL);
}
@Override
public InventoryView openCartographyTable(Location location, boolean force) {
return this.openInventory(location, force, Material.CARTOGRAPHY_TABLE);
}
@Override
public InventoryView openGrindstone(Location location, boolean force) {
return this.openInventory(location, force, Material.GRINDSTONE);
}
@Override
public InventoryView openLoom(Location location, boolean force) {
return this.openInventory(location, force, Material.LOOM);
}
@Override
public InventoryView openSmithingTable(Location location, boolean force) {
return this.openInventory(location, force, Material.SMITHING_TABLE);
}
@Override
public InventoryView openStonecutter(Location location, boolean force) {
return this.openInventory(location, force, Material.STONECUTTER);
}
private InventoryView openInventory(Location location, boolean force, Material material) {
org.spigotmc.AsyncCatcher.catchOp("open" + material);
if (location == null) {
location = this.getLocation();
}
if (!force) {
Block block = location.getBlock();
if (block.getType() != material) {
return null;
}
}
net.minecraft.world.level.block.Block block;
if (material == Material.ANVIL) {
block = Blocks.ANVIL;
} else if (material == Material.CARTOGRAPHY_TABLE) {
block = Blocks.CARTOGRAPHY_TABLE;
} else if (material == Material.GRINDSTONE) {
block = Blocks.GRINDSTONE;
} else if (material == Material.LOOM) {
block = Blocks.LOOM;
} else if (material == Material.SMITHING_TABLE) {
block = Blocks.SMITHING_TABLE;
} else if (material == Material.STONECUTTER) {
block = Blocks.STONECUTTER;
} else {
throw new IllegalArgumentException("Unsupported inventory type: " + material);
}
this.getHandle().openMenu(block.getMenuProvider(null, this.getHandle().level(), new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ())));
this.getHandle().containerMenu.checkReachable = !force;
return this.getHandle().containerMenu.getBukkitView();
}
@Override
public void closeInventory(org.bukkit.event.inventory.InventoryCloseEvent.Reason reason) {
this.getHandle().closeContainer(reason);
}
@Override
public boolean isBlocking() {
return this.getHandle().isBlocking();
}
@Override
public boolean isHandRaised() {
return this.getHandle().isUsingItem();
}
@Override
public boolean setWindowProperty(InventoryView.Property prop, int value) {
return false;
}
@Override
public int getEnchantmentSeed() {
return this.getHandle().enchantmentSeed;
}
@Override
public void setEnchantmentSeed(int i) {
this.getHandle().enchantmentSeed = i;
}
@Override
public int getExpToLevel() {
return this.getHandle().getXpNeededForNextLevel();
}
@Override
public float getAttackCooldown() {
return this.getHandle().getAttackStrengthScale(0.5f);
}
@Override
public boolean hasCooldown(Material material) {
Preconditions.checkArgument(material != null, "Material cannot be null");
Preconditions.checkArgument(material.isItem(), "Material %s is not an item", material);
return this.hasCooldown(new ItemStack(material));
}
@Override
public int getCooldown(Material material) {
Preconditions.checkArgument(material != null, "Material cannot be null");
Preconditions.checkArgument(material.isItem(), "Material %s is not an item", material);
return this.getCooldown(new ItemStack(material));
}
@Override
public boolean hasCooldown(ItemStack item) {
Preconditions.checkArgument(item != null, "Item cannot be null");
return this.getHandle().getCooldowns().isOnCooldown(CraftItemStack.asNMSCopy(item));
}
@Override
public int getCooldown(ItemStack item) {
Preconditions.checkArgument(item != null, "Item cannot be null");
Identifier group = this.getHandle().getCooldowns().getCooldownGroup(CraftItemStack.asNMSCopy(item));
if (group == null) {
return 0;
}
ItemCooldowns.CooldownInstance cooldown = this.getHandle().getCooldowns().cooldowns.get(group);
return (cooldown == null) ? 0 : Math.max(0, cooldown.endTime() - this.getHandle().getCooldowns().tickCount);
}
@Override
public void setCooldown(ItemStack item, int ticks) {
Preconditions.checkArgument(item != null, "Item cannot be null");
Preconditions.checkArgument(ticks >= 0, "Cannot have negative cooldown");
this.getHandle().getCooldowns().addCooldown(CraftItemStack.asNMSCopy(item), ticks);
}
@Override
public int getCooldown(Key key) {
Preconditions.checkArgument(key != null, "Key cannot be null");
ItemCooldowns.CooldownInstance cooldown = this.getHandle().getCooldowns().cooldowns.get(PaperAdventure.asVanilla(key));
return (cooldown == null) ? 0 : Math.max(0, cooldown.endTime() - this.getHandle().getCooldowns().tickCount);
}
@Override
public void setCooldown(Key key, int ticks) {
Preconditions.checkArgument(key != null, "Key cannot be null");
Preconditions.checkArgument(ticks >= 0, "Cannot have negative cooldown");
this.getHandle().getCooldowns().addCooldown(PaperAdventure.asVanilla(key), ticks);
}
@Override
public org.bukkit.entity.Entity releaseLeftShoulderEntity() {
return null;
}
@Override
public org.bukkit.entity.Entity releaseRightShoulderEntity() {
return null;
}
@Override
public int discoverRecipes(Collection<NamespacedKey> recipes) {
return this.getHandle().awardRecipes(this.bukkitKeysToMinecraftRecipes(recipes));
}
@Override
public int undiscoverRecipes(Collection<NamespacedKey> recipes) {
return this.getHandle().resetRecipes(this.bukkitKeysToMinecraftRecipes(recipes));
}
@Override
public boolean hasDiscoveredRecipe(NamespacedKey recipe) {
return false;
}
@Override
public Set<NamespacedKey> getDiscoveredRecipes() {
return ImmutableSet.of();
}
private Collection<RecipeHolder<?>> bukkitKeysToMinecraftRecipes(Collection<NamespacedKey> recipeKeys) {
Collection<RecipeHolder<?>> recipes = new ArrayList<>();
RecipeManager manager = this.getHandle().level().getServer().getRecipeManager();
for (NamespacedKey recipeKey : recipeKeys) {
Optional<? extends RecipeHolder<?>> recipe = manager.byKey(CraftRecipe.toMinecraft(recipeKey));
if (recipe.isEmpty()) {
continue;
}
recipes.add(recipe.get());
}
return recipes;
}
@Override
public org.bukkit.entity.Entity getShoulderEntityLeft() {
return null;
}
@Override
public void setShoulderEntityLeft(org.bukkit.entity.Entity entity) {
if (entity != null) {
Preconditions.checkArgument(((CraftEntity) entity).getHandle().getType().canSerialize(), "Cannot set entity of type %s as a shoulder entity", entity.getType().getKey());
}
throw new UnsupportedOperationException();
}
@Override
public org.bukkit.entity.Entity getShoulderEntityRight() {
return null;
}
@Override
public void setShoulderEntityRight(org.bukkit.entity.Entity entity) {
if (entity != null) {
Preconditions.checkArgument(((CraftEntity) entity).getHandle().getType().canSerialize(), "Cannot set entity of type %s as a shoulder entity", entity.getType().getKey());
}
throw new UnsupportedOperationException();
}
@Override
public void openSign(final org.bukkit.block.Sign sign, final org.bukkit.block.sign.Side side) {
org.bukkit.craftbukkit.block.CraftSign.openSign(sign, (CraftPlayer) this, side);
}
@Override
public boolean dropItem(boolean dropAll) {
if (!(this.getHandle() instanceof ServerPlayer player)) return false;
boolean success = player.drop(dropAll);
if (!success) {
return false;
}
final net.minecraft.world.entity.player.Inventory inv = player.getInventory();
final java.util.OptionalInt optionalSlot = player.containerMenu.findSlot(inv, inv.getSelectedSlot());
optionalSlot.ifPresent(slot -> player.containerSynchronizer.sendSlotChange(player.containerMenu, slot, inv.getSelectedItem()));
return true;
}
@Override
@Nullable
public Item dropItem(final int slot, final int amount, final boolean throwRandomly, final @Nullable Consumer<Item> entityOperation) {
Preconditions.checkArgument(slot >= 0 && slot < this.inventory.getSize(), "Slot %s is not a valid inventory slot.", slot);
return internalDropItemFromInventory(this.inventory.getItem(slot), amount, throwRandomly, entityOperation);
}
@Override
@Nullable
public Item dropItem(final @NotNull EquipmentSlot slot, final int amount, final boolean throwRandomly, final @Nullable Consumer<Item> entityOperation) {
return internalDropItemFromInventory(this.inventory.getItem(slot), amount, throwRandomly, entityOperation);
}
@Nullable
private Item internalDropItemFromInventory(final ItemStack originalItemStack, final int amount, final boolean throwRandomly, final @Nullable Consumer<Item> entityOperation) {
if (originalItemStack == null || originalItemStack.isEmpty() || amount <= 0) return null;
final net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(originalItemStack);
final net.minecraft.world.item.ItemStack dropContent = nmsItemStack.split(amount);
// This will return the itemstack back to its original amount in case events fail
final ItemEntity droppedEntity = this.getHandle().drop(dropContent, throwRandomly, true, true, entityOperation);
return droppedEntity == null ? null : (Item) droppedEntity.getBukkitEntity();
}
@Override
@Nullable
public Item dropItem(final ItemStack itemStack, final boolean throwRandomly, final @Nullable Consumer<Item> entityOperation) {
// This method implementation differs from the previous dropItem implementations, as it does not source
// its itemstack from the players inventory. As such, we cannot reuse #internalDropItemFromInventory.
Preconditions.checkArgument(itemStack != null, "Cannot drop a null itemstack");
if (itemStack.isEmpty()) return null;
final net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
// Do *not* call the event here, the item is not in the player inventory, they are not dropping it / do not need recovering logic (which would be a dupe).
final ItemEntity droppedEntity = this.getHandle().drop(nmsItemStack, throwRandomly, true, false, entityOperation);
return droppedEntity == null ? null : (Item) droppedEntity.getBukkitEntity();
}
@Override
public float getExhaustion() {
return this.getHandle().getFoodData().exhaustionLevel;
}
@Override
public void setExhaustion(float value) {
this.getHandle().getFoodData().exhaustionLevel = value;
}
@Override
public float getSaturation() {
return this.getHandle().getFoodData().saturationLevel;
}
@Override
public void setSaturation(float value) {
this.getHandle().getFoodData().saturationLevel = value;
}
@Override
public int getFoodLevel() {
return this.getHandle().getFoodData().foodLevel;
}
@Override
public void setFoodLevel(int value) {
this.getHandle().getFoodData().foodLevel = value;
}
@Override
public int getSaturatedRegenRate() {
return this.getHandle().getFoodData().saturatedRegenRate;
}
@Override
public void setSaturatedRegenRate(int i) {
this.getHandle().getFoodData().saturatedRegenRate = i;
}
@Override
public int getUnsaturatedRegenRate() {
return this.getHandle().getFoodData().unsaturatedRegenRate;
}
@Override
public void setUnsaturatedRegenRate(int i) {
this.getHandle().getFoodData().unsaturatedRegenRate = i;
}
@Override
public int getStarvationRate() {
return this.getHandle().getFoodData().starvationRate;
}
@Override
public void setStarvationRate(int i) {
this.getHandle().getFoodData().starvationRate = i;
}
@Override
public Location getLastDeathLocation() {
return this.getHandle().getLastDeathLocation().map(CraftLocation::fromGlobalPos).orElse(null);
}
@Override
public void setLastDeathLocation(Location location) {
this.getHandle().setLastDeathLocation(Optional.ofNullable(location).map(CraftLocation::toGlobalPos));
}
@Override
public Firework fireworkBoost(ItemStack fireworkItemStack) {
Preconditions.checkArgument(fireworkItemStack != null, "fireworkItemStack must not be null");
Preconditions.checkArgument(fireworkItemStack.getType() == Material.FIREWORK_ROCKET, "fireworkItemStack must be of type %s", Material.FIREWORK_ROCKET);
FireworkRocketEntity fireworks = new FireworkRocketEntity(this.getHandle().level(), CraftItemStack.asNMSCopy(fireworkItemStack), this.getHandle());
boolean success = this.getHandle().level().addFreshEntity(fireworks, SpawnReason.CUSTOM);
return success ? (Firework) fireworks.getBukkitEntity() : null;
}
@Override
public boolean canUseEquipmentSlot(org.bukkit.inventory.EquipmentSlot slot) {
net.minecraft.world.entity.EquipmentSlot equipmentSlot = CraftEquipmentSlot.getNMS(slot);
return (equipmentSlot.getType() == net.minecraft.world.entity.EquipmentSlot.Type.HUMANOID_ARMOR || equipmentSlot.getType() == net.minecraft.world.entity.EquipmentSlot.Type.HAND) && super.canUseEquipmentSlot(slot);
}
@Override
public org.bukkit.entity.Entity copy() {
throw new UnsupportedOperationException("Cannot copy human entities");
}
@Override
public org.bukkit.entity.Entity copy(Location location) {
throw new UnsupportedOperationException("Cannot copy human entities");
}
}