This repository was archived by the owner on Dec 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPaperLib.java
More file actions
294 lines (268 loc) · 12 KB
/
PaperLib.java
File metadata and controls
294 lines (268 loc) · 12 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
package io.papermc.lib;
import io.papermc.lib.environments.CraftBukkitEnvironment;
import io.papermc.lib.environments.Environment;
import io.papermc.lib.environments.PaperEnvironment;
import io.papermc.lib.environments.SpigotEnvironment;
import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
/**
* Utility methods that assist plugin developers accessing Paper features.
* Bridges backwards compatability with Spigot and CraftBukkit so your plugin
* will still work on those platforms, and fall back to less performant methods.
*/
@SuppressWarnings("WeakerAccess")
public class PaperLib {
private PaperLib() {
// Hide public constructor
}
private static Environment ENVIRONMENT = initialize();
private static Environment initialize() {
try {
Class.forName("com.destroystokyo.paper.PaperConfig");
return new PaperEnvironment();
} catch (ClassNotFoundException e) {
try {
Class.forName("org.spigotmc.SpigotConfig");
return new SpigotEnvironment();
} catch (ClassNotFoundException e1) {
return new CraftBukkitEnvironment();
}
}
}
/**
* Gets a reference to the current environment. All of the static util methods in this class points to this
* environment.
*
* @return The Environment
*/
@Nonnull
public static Environment getEnvironment() {
return ENVIRONMENT;
}
/**
* If you have need to inject a custom Environment, such as running on your own fork, or unit tests, do it here.
* @param environment Custom Environment
*/
public static void setCustomEnvironment(@Nonnull Environment environment) {
ENVIRONMENT = environment;
}
/**
* Teleports an Entity to the target location, loading the chunk asynchronously first if needed.
* @param entity The Entity to teleport
* @param location The Location to Teleport to
* @return Future that completes with the result of the teleport
*/
@Nonnull
public static CompletableFuture<Boolean> teleportAsync(@Nonnull Entity entity, @Nonnull Location location) {
return ENVIRONMENT.teleport(entity, location, TeleportCause.PLUGIN);
}
/**
* Teleports an Entity to the target location, loading the chunk asynchronously first if needed.
* @param entity The Entity to teleport
* @param location The Location to Teleport to
* @param cause The cause for the teleportation
* @return Future that completes with the result of the teleport
*/
@Nonnull
public static CompletableFuture<Boolean> teleportAsync(@Nonnull Entity entity, @Nonnull Location location, TeleportCause cause) {
return ENVIRONMENT.teleport(entity, location, cause);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed.
* @param loc Location to get chunk for
* @return Future that completes with the chunk
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull Location loc) {
return getChunkAtAsync(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4, true);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed.
* @param loc Location to get chunk for
* @param gen Should the chunk generate or not. Only respected on some MC versions, 1.13 for CB, 1.12 for Paper
* @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested.
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull Location loc, boolean gen) {
return getChunkAtAsync(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4, gen);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed.
* @param world World to load chunk for
* @param x X coordinate of the chunk to load
* @param z Z coordinate of the chunk to load
* @return Future that completes with the chunk
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull World world, int x, int z) {
return getChunkAtAsync(world, x, z, true);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed.
* @param world World to load chunk for
* @param x X coordinate of the chunk to load
* @param z Z coordinate of the chunk to load
* @param gen Should the chunk generate or not. Only respected on some MC versions, 1.13 for CB, 1.12 for Paper
* @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested.
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull World world, int x, int z, boolean gen) {
return ENVIRONMENT.getChunkAtAsync(world, x, z, gen, false);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed.
* @param world World to load chunk for
* @param x X coordinate of the chunk to load
* @param z Z coordinate of the chunk to load
* @param gen Should the chunk generate or not. Only respected on some MC versions, 1.13 for CB, 1.12 for Paper
* @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested.
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsync(@Nonnull World world, int x, int z, boolean gen, boolean isUrgent) {
return ENVIRONMENT.getChunkAtAsync(world, x, z, gen, isUrgent);
}
/**
* Gets the chunk at the target location, loading it asynchronously if needed, with highest priority if supported
* @param world World to load chunk for
* @param x X coordinate of the chunk to load
* @param z Z coordinate of the chunk to load
* @param gen Should the chunk generate or not. Only respected on some MC versions, 1.13 for CB, 1.12 for Paper
* @return Future that completes with the chunk, or null if the chunk did not exists and generation was not requested.
*/
@Nonnull
public static CompletableFuture<Chunk> getChunkAtAsyncUrgently(@Nonnull World world, int x, int z, boolean gen) {
return ENVIRONMENT.getChunkAtAsync(world, x, z, gen, true);
}
/**
* Checks if the chunk has been generated or not. Only works on Paper 1.12+ or any 1.13.1+ version
* @param loc Location to check if the chunk is generated
* @return If the chunk is generated or not
*/
public static boolean isChunkGenerated(@Nonnull Location loc) {
return isChunkGenerated(loc.getWorld(), loc.getBlockX() >> 4, loc.getBlockZ() >> 4);
}
/**
* Checks if the chunk has been generated or not. Only works on Paper 1.12+ or any 1.13.1+ version
* @param world World to check for
* @param x X coordinate of the chunk to check
* @param z Z coordinate of the chunk to checl
* @return If the chunk is generated or not
*/
public static boolean isChunkGenerated(@Nonnull World world, int x, int z) {
return ENVIRONMENT.isChunkGenerated(world, x, z);
}
/**
* This gets a {@link BlockState}, optionally not using a snapshot (if the {@link Environment} does not override that)
* @param block The {@link Block} to get the {@link BlockState} of
* @param useSnapshot Whether or not to use a snapshot, may be ignored by certain {@link Environment Environments}
* @return The {@link BlockState}
*/
@Nonnull
public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boolean useSnapshot) {
return ENVIRONMENT.getBlockState(block, useSnapshot);
}
/**
* Gets the location where the target player will spawn at their bed, asynchronously if needed
* @param player The player whose bed spawn location to get.
* @param isUrgent Whether or not this should be performed with highest priority when supported
* @return Future that completes with the location of the bed spawn location, or null if the player
* has not slept in a bed or if the bed spawn is invalid.
*/
public static CompletableFuture<Location> getBedSpawnLocationAsync(@Nonnull Player player, boolean isUrgent) {
return ENVIRONMENT.getBedSpawnLocationAsync(player, isUrgent);
}
/**
* Detects if the current MC version is at least the following version.
*
* Assumes 0 patch version.
*
* @param minor Min Minor Version
* @return Meets the version requested
*/
public static boolean isVersion(int minor) {
return ENVIRONMENT.isVersion(minor);
}
/**
* Detects if the current MC version is at least the following version.
* @param minor Min Minor Version
* @param patch Min Patch Version
* @return Meets the version requested
*/
public static boolean isVersion(int minor, int patch) {
return ENVIRONMENT.isVersion(minor, patch);
}
/**
* Gets the current Minecraft Minor version. IE: 1.13.1 returns 13
* @return The Minor Version
*/
public static int getMinecraftVersion() {
return ENVIRONMENT.getMinecraftVersion();
}
/**
* Gets the current Minecraft Patch version. IE: 1.13.1 returns 1
* @return The Patch Version
*/
public static int getMinecraftPatchVersion() {
return ENVIRONMENT.getMinecraftPatchVersion();
}
/**
* Check if the server has access to the Spigot API
* @return True for Spigot <em>and</em> Paper environments
*/
public static boolean isSpigot() {
return ENVIRONMENT.isSpigot();
}
/**
* Check if the server has access to the Paper API
* @return True for Paper environments
*/
public static boolean isPaper() {
return ENVIRONMENT.isPaper();
}
/**
* Can be called during plugin initialization to inform the server owner they should switch to Paper
*
* If you do not mind helping spread Paper, please call this in your plugin onEnable to help spread
* awareness about Paper, and encourage them that your plugin is better when used with Paper!
*
* @param plugin Your plugin object
*/
public static void suggestPaper(@Nonnull Plugin plugin) {
if (isPaper()) {
return;
}
final String benefitsProperty = "paperlib.shown-benefits";
final String pluginName = plugin.getDescription().getName();
final Logger logger = plugin.getLogger();
logger.warning("====================================================");
logger.warning(" " + pluginName + " works better if you use Paper ");
logger.warning(" as your server software. ");
if (System.getProperty(benefitsProperty) == null) {
System.setProperty(benefitsProperty, "1");
logger.warning(" ");
logger.warning(" Paper offers significant performance improvements,");
logger.warning(" bug fixes, security enhancements and optional");
logger.warning(" features for server owners to enhance their server.");
logger.warning(" ");
logger.warning(" Paper includes Timings v2, which is significantly");
logger.warning(" better at diagnosing lag problems over v1.");
logger.warning(" ");
logger.warning(" All of your plugins should still work, and the");
logger.warning(" Paper community will gladly help you fix any issues.");
logger.warning(" ");
logger.warning(" Join the Paper Community @ https://papermc.io");
}
logger.warning("====================================================");
}
}