forked from rossnoah/PerPlayerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegearCommand.java
More file actions
201 lines (161 loc) · 8.74 KB
/
RegearCommand.java
File metadata and controls
201 lines (161 loc) · 8.74 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
package dev.noah.perplayerkit.commands;
import dev.noah.perplayerkit.KitManager;
import dev.noah.perplayerkit.gui.ItemUtil;
import dev.noah.perplayerkit.util.BroadcastManager;
import dev.noah.perplayerkit.util.CooldownManager;
import dev.noah.perplayerkit.util.DisabledCommand;
import dev.noah.perplayerkit.util.StyleManager;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public class RegearCommand implements CommandExecutor, Listener {
public static final ItemStack REGEAR_SHULKER_ITEM = ItemUtil.createItem(Material.WHITE_SHULKER_BOX, 1, StyleManager.get().getPrimaryColor() + "Regear Shulker", "&7● Restocks Your Kit", "&7● Use " + StyleManager.get().getPrimaryColor() + "/rg &7to get another regear shulker");
public static final ItemStack REGEAR_SHELL_ITEM = ItemUtil.createItem(Material.SHULKER_SHELL, 1, StyleManager.get().getPrimaryColor() + "Regear Shell", "&7● Restocks Your Kit", "&7● Click to use!");
private final Plugin plugin;
private final CooldownManager commandCooldownManager;
private final CooldownManager damageCooldownManager;
private final boolean allowRegearWhileUsingElytra;
private final boolean preventPuttingItemsInRegearInventory;
public RegearCommand(Plugin plugin) {
this.plugin = plugin;
int commandCooldownInSeconds = plugin.getConfig().getInt("regear.command-cooldown", 5);
int damageCooldownInSeconds = plugin.getConfig().getInt("regear.damage-timer", 5);
this.commandCooldownManager = new CooldownManager(commandCooldownInSeconds);
this.damageCooldownManager = new CooldownManager(damageCooldownInSeconds);
this.allowRegearWhileUsingElytra = plugin.getConfig().getBoolean("regear.allow-while-using-elytra", true);
this.preventPuttingItemsInRegearInventory = plugin.getConfig().getBoolean("regear.prevent-putting-items-in-regear-inventory", false);
}
@EventHandler
public void onPlayerTakesDamage(EntityDamageEvent event) {
if (!(event.getEntity() instanceof Player player)) {
return;
}
damageCooldownManager.setCooldown(player);
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use this command!");
return true;
}
if (DisabledCommand.isBlockedInWorld(player)) {
return true;
}
if (plugin.getConfig().getString("regear.mode", "command").equalsIgnoreCase("shulker")) {
int slot = player.getInventory().firstEmpty();
if (slot == -1) {
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>Your inventory is full, can't give you a regear shulker!"));
return true;
}
player.getInventory().setItem(slot, REGEAR_SHULKER_ITEM);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<green>Regear Shulker given!"));
return true;
}
if (plugin.getConfig().getString("regear.mode", "command").equalsIgnoreCase("command")) {
int slot = KitManager.get().getLastKitLoaded(player.getUniqueId());
if (slot == -1) {
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You have not loaded a kit yet!"));
return true;
}
if (!allowRegearWhileUsingElytra && player.isGliding() && player.getInventory().getChestplate() != null && player.getInventory().getChestplate().getType() == Material.ELYTRA) {
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You cannot regear while using an elytra!"));
return true;
}
if (damageCooldownManager.isOnCooldown(player)) {
int secondsLeft = damageCooldownManager.getTimeLeft(player);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You must be out of combat for " + secondsLeft + " more seconds before regearing!"));
return true;
}
if (commandCooldownManager.isOnCooldown(player)) {
int secondsLeft = commandCooldownManager.getTimeLeft(player);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You must wait " + secondsLeft + " seconds before using this command again!"));
return true;
}
KitManager.get().regearKit(player, slot);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<green>Regeared!"));
BroadcastManager.get().broadcastPlayerRegeared(player);
commandCooldownManager.setCooldown(player);
return true;
}
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>This command is not configured correctly, please contact an administrator."));
return true;
}
@EventHandler
public void onShulkerPlace(BlockPlaceEvent event) {
if (!event.getItemInHand().equals(REGEAR_SHULKER_ITEM)) {
return;
}
event.setCancelled(true);
Player player = event.getPlayer();
int slot = KitManager.get().getLastKitLoaded(player.getUniqueId());
if (slot == -1) {
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You have not loaded a kit yet!"));
return;
}
if (damageCooldownManager.isOnCooldown(player)) {
int secondsLeft = damageCooldownManager.getTimeLeft(player);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You must be out of combat for " + secondsLeft + " more seconds before regearing!"));
return;
}
player.getInventory().setItem(event.getHand(), null);
//custom inv with holder
RegearInventoryHolder holder = new RegearInventoryHolder(player);
Inventory inventory = holder.getInventory();
player.openInventory(inventory);
}
@EventHandler
public void onShulkerShellClick(InventoryClickEvent event) {
if (!(event.getInventory().getHolder() instanceof RegearInventoryHolder holder)) {
return;
}
ItemStack currentItem = event.getCurrentItem();
if (currentItem == null) {
return;
}
if (!currentItem.equals(REGEAR_SHELL_ITEM)) {
if (preventPuttingItemsInRegearInventory) {
event.setCancelled(true);
}
return;
}
Player player = holder.player();
int slot = KitManager.get().getLastKitLoaded(player.getUniqueId());
if (slot == -1) {
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You have not loaded a kit yet!"));
return;
}
if (damageCooldownManager.isOnCooldown(player)) {
int secondsLeft = damageCooldownManager.getTimeLeft(player);
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<red>You must be out of combat for " + secondsLeft + " more seconds before regearing!"));
return;
}
player.closeInventory();
KitManager.get().regearKit(player, slot);
player.updateInventory();
BroadcastManager.get().sendComponentMessage(player, MiniMessage.miniMessage().deserialize("<green>Regeared!"));
BroadcastManager.get().broadcastPlayerRegeared(player);
}
public record RegearInventoryHolder(
Player player) implements InventoryHolder {
@Override
public @NotNull Inventory getInventory() {
Inventory inventory = Bukkit.createInventory(this, 27, StyleManager.get().getPrimaryColor() + "Regear Shulker");
inventory.setItem(13, REGEAR_SHELL_ITEM);
return inventory;
}
}
}