-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayers.java
More file actions
215 lines (195 loc) · 8.19 KB
/
Players.java
File metadata and controls
215 lines (195 loc) · 8.19 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
package net.theevilreaper.aves.util;
import net.theevilreaper.aves.inventory.util.InventoryConstants;
import net.theevilreaper.aves.item.IItem;
import net.theevilreaper.aves.item.TranslatedItem;
import net.theevilreaper.aves.util.functional.ItemPlacer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import net.kyori.adventure.util.Ticks;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.ItemEntity;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.SetCooldownPacket;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
/**
* Contains some methods to work with {@link Player} objects
*
* @author theEvilReaper
* @version 1.0.0
* @since 1.0.0
*/
public final class Players {
private static final Logger PLAYER_LOGGER = LoggerFactory.getLogger(Players.class);
private static Duration itemDuration = Duration.ofMillis(3);
private static @Nullable ItemPlacer placer;
private Players() {
}
/**
* Set a new {@link Duration} for the item drop functionality.
*
* @param duration the duration to set
*/
public static void setItemDuration(Duration duration) {
itemDuration = duration;
}
/**
* Set a new instance from a {@link ItemPlacer}.
*
* @param itemPlacer the new instance to set
*/
public static void setItemPlacer(ItemPlacer itemPlacer) {
placer = itemPlacer;
}
/**
* Send a title to a given player.
*
* @param player the player who receives the title
* @param title the title message as {@link Component}
* @param subTitle the subTitle message as {@link Component}
* @param fadeIn the time to fade in
* @param stay the time how long the title stays
* @param fadeOut the time to fade out
*/
public static void showTitle(Player player, Component title, Component subTitle, int fadeIn, int stay, int fadeOut) {
player.showTitle(Title.title(title, subTitle, Title.Times.times(Ticks.duration(fadeIn), Ticks.duration(stay), Ticks.duration(fadeOut))));
}
/**
* Drops the complete inventory content from a player to a specific location.
*
* @param player The player from which the inventory should be dropped
*/
public static void dropPlayerInventory(Player player) {
Check.argCondition(player.getInstance() == null, "The instance from the player can't be null");
dropItemStacks(player.getInstance(), player.getPosition(), player.getInventory().getItemStacks());
}
/**
* Drops a certain number of items to a given location.
*
* @param instance the {@link Instance} where the items should be dropped
* @param pos the position where the items should be dropped
* @param content the items stored in an array
*/
public static void dropItemStacks(Instance instance, Pos pos, ItemStack ... content) {
Check.argCondition(content.length == 0, "The array can not be null or empty");
for (int i = 0; i < content.length; i++) {
if (content[i] == null) continue;
ItemEntity entity = new ItemEntity(content[i]);
entity.setMergeable(true);
entity.setPickupDelay(itemDuration);
entity.setInstance(instance, pos.withY(y -> y + 1.5));
entity.setVelocity(pos.direction().mul(6));
entity.spawn();
}
}
/**
* Choose a random player from all players who are currently online.
*
* @return a random player
*/
public static Optional<Player> getRandomPlayer() {
var players = MinecraftServer.getConnectionManager().getOnlinePlayers();
if (players.isEmpty()) {
return Optional.empty();
}
if (players.size() == 1) {
return players.stream().findAny();
}
return players.stream().collect(Collectors.collectingAndThen(Collectors.toList(), collected -> {
Collections.shuffle(collected);
return collected.stream();
})).findAny();
}
/**
* Updates the hotbar items from a given {@link Player}.
* The locale and shiftedSlot parameter can be null
*
* @param player The {@link Player} who receives the new equipment
* @param hotBarItems The hot bar items as array
* @param locale The {@link Locale} for {@link TranslatedItem}
* @param shiftedSlots An array with contains shifted layout only for the hotbar
*/
public static void updateHotBar(Player player, IItem[] hotBarItems, @Nullable Locale locale, int... shiftedSlots) {
Check.argCondition(hotBarItems.length > InventoryConstants.INVENTORY_WIDTH, "The array length for the items is greater than " + InventoryConstants.INVENTORY_WIDTH);
Check.argCondition(shiftedSlots.length > hotBarItems.length, "The length from shiftedSlots has not the same length with the underlying array");
if (placer == null) {
placer = ItemPlacer.FALLBACK;
PLAYER_LOGGER.info("Set `ItemPlacer Interface` to fallback implementation");
}
setItems(player, hotBarItems, locale, shiftedSlots);
}
/**
* Updates the armor items from a given {@link Player}.
* The locale and shiftedSlot parameter can be null
*
* @param player The {@link Player} who receives the new equipment
* @param armorItems The array with the items for the armor area
* @param locale The {@link Locale} for {@link TranslatedItem}
*/
public static void updateArmorItems(Player player, IItem[] armorItems, @Nullable Locale locale) {
if (placer == null) {
placer = ItemPlacer.FALLBACK;
PLAYER_LOGGER.info("Set `ItemPlacer Interface` to fallback implementation");
}
setItems(player, armorItems, locale);
}
/**
* Applies a given array of {@link ItemStack}'s to a {@link Player}.
*
* @param player the player who should get the items
* @param items the array with the items
* @param locale the locale if the case need some
* @param shiftedSlots an array which contains shifted slots
*/
private static void setItems(Player player, IItem[] items, Locale locale, int... shiftedSlots) {
for (int i = 0; i < items.length; i++) {
var wrappedItem = items[i];
if (wrappedItem == null) continue;
var slotID = shiftedSlots != null ? shiftedSlots[i] : i;
placer.setItem(player, slotID, wrappedItem, locale, true);
}
}
/**
* Get a random player from a given list.
*
* @param players A list which contains some player objects
* @return a random player
*/
public static Optional<Player> getRandomPlayer(List<Player> players) {
if (players.isEmpty()) return Optional.empty();
return Optional.of(players.get(ThreadLocalRandom.current().nextInt(players.size())));
}
/**
* Send a {@link SetCooldownPacket} to a given {@link Player}.
*
* @param player the player who should receive the packet
* @param itemStack the involved {@link ItemStack}
* @param ticks how long the cooldown is
*/
public static void sendCooldown(Player player, ItemStack itemStack, int ticks) {
sendCooldown(player, itemStack.material(), ticks);
}
/**
* Send a {@link SetCooldownPacket} to a given {@link Player}.
*
* @param player the player who should receive the packet
* @param material the {@link Material} to get the id from it2
* @param ticks how long the cooldown is
*/
public static void sendCooldown(Player player, Material material, int ticks) {
player.sendPacket(new SetCooldownPacket(material.name(), ticks));
}
}