-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
ItemChangeMessage.java
93 lines (81 loc) · 3.36 KB
/
ItemChangeMessage.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
package com.denizenscript.denizen.utilities.packets;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.DenizenAPI;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class ItemChangeMessage implements Listener {
static {
DenizenAPI.getCurrentInstance().getServer().getPluginManager().registerEvents(new ItemChangeMessage(),
DenizenAPI.getCurrentInstance());
}
private static final Map<UUID, Integer> slotChanged = new HashMap<>();
public static void sendMessage(Player player, String message) {
ItemStack item = player.getEquipment().getItemInMainHand();
// If the player is holding air, force a light gray stained glass pane,
// which is probably the least intrusive
if (item == null || item.getType() == Material.AIR) {
item = new ItemStack(Material.LIGHT_GRAY_STAINED_GLASS_PANE);
}
else {
item = item.clone();
}
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(message);
item.setItemMeta(meta);
int slot = player.getInventory().getHeldItemSlot() + 36;
NMSHandler.getPacketHelper().setSlot(player, slot, item, true);
slotChanged.put(player.getUniqueId(), slot);
}
public static void resetItem(Player player) {
if (player == null) {
return;
}
UUID uuid = player.getUniqueId();
if (slotChanged.containsKey(uuid)) {
int slot = slotChanged.get(uuid);
ItemStack itemStack = player.getEquipment().getItemInMainHand();
NMSHandler.getPacketHelper().setSlot(player, slot, itemStack, true);
slotChanged.remove(uuid);
}
}
@EventHandler
public void playerItemHeld(PlayerItemHeldEvent event) {
resetItem(event.getPlayer());
}
@EventHandler
public void inventoryOpen(InventoryOpenEvent event) {
if (event.getPlayer() instanceof Player) {
resetItem((Player) event.getPlayer());
}
}
// Breaking blocks with tools and such will reset the item automatically...
// sadly, this sends the display name of the real item when that happens.
// This also occurs when right clicking
// TODO: find a super hacky way around that?
// TODO: Use a packet override for the above
@EventHandler
public void blockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
if (player != null && slotChanged.containsKey(player.getUniqueId())) {
slotChanged.remove(player.getUniqueId());
}
}
@EventHandler
public void playerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player != null && slotChanged.containsKey(player.getUniqueId())) {
slotChanged.remove(player.getUniqueId());
}
}
}