/
EndermanEquipper.java
67 lines (61 loc) · 2.76 KB
/
EndermanEquipper.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
package net.citizensnpcs.editor;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Equipment;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.util.Messages;
public class EndermanEquipper implements Equipper {
@Override
public void equip(Player equipper, NPC npc) {
ItemStack hand = equipper.getInventory().getItemInHand();
if (!hand.getType().isBlock()) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
if (SpigotUtil.isUsing1_13API()) {
BlockData carried = ((Enderman) npc.getEntity()).getCarriedBlock();
if (carried == null || carried.getMaterial() == Material.AIR) {
if (hand.getType() == Material.AIR) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
} else {
equipper.getWorld().dropItemNaturally(npc.getEntity().getLocation(),
new ItemStack(carried.getMaterial(), 1));
((Enderman) npc.getEntity()).setCarriedBlock(hand.getType().createBlockData());
// TODO: copy block data info from itemstack?
}
ItemStack set = hand.clone();
if (set.getType() != Material.AIR) {
set.setAmount(1);
hand.setAmount(hand.getAmount() - 1);
equipper.getInventory().setItemInHand(hand);
}
npc.getTrait(Equipment.class).set(0, set);
} else {
MaterialData carried = ((Enderman) npc.getEntity()).getCarriedMaterial();
if (carried.getItemType() == Material.AIR) {
if (hand.getType() == Material.AIR) {
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
return;
}
} else {
equipper.getWorld().dropItemNaturally(npc.getEntity().getLocation(), carried.toItemStack(1));
((Enderman) npc.getEntity()).setCarriedMaterial(hand.getData());
}
ItemStack set = hand.clone();
if (set.getType() != Material.AIR) {
set.setAmount(1);
hand.setAmount(hand.getAmount() - 1);
equipper.getInventory().setItemInHand(hand);
}
npc.getTrait(Equipment.class).set(0, set);
}
}
}