Package dev.demeng.pluginbase
Class Common
java.lang.Object
dev.demeng.pluginbase.Common
Commonly used methods and utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe error message for players when an internal error occurs.static final booleanIf the server software the plugin is running on is Spigot or a fork of Spigot. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull StringapplyOperator(@Nullable String str, @Nullable UnaryOperator<String> operator) applyOperator(@Nullable List<String> list, @Nullable UnaryOperator<String> operator) static @NotNull org.bukkit.inventory.ItemStackapplyOperator(@Nullable org.bukkit.inventory.ItemStack stack, @Nullable UnaryOperator<String> operator) static @Nullable Class<?>checkClass(@NotNull String className) Simple method to check if a class exists.static @Nullable DoublecheckDouble(@NotNull String str) Attempts to parse a string into a double.static @Nullable FloatcheckFloat(@NotNull String str) Attempts to parse a string into a float.static @Nullable IntegerAttempts to parse a string into an integer.static @Nullable LongAttempts to parse a string into a long.static voiderror(@Nullable Throwable throwable, @NotNull String description, boolean disable, @NotNull org.bukkit.command.CommandSender... players) Reports an error in the plugin.static voidforEachInt(String str, IntConsumer consumer) Parses a string sequence of integers and accepts a consumer for each integer.static @NotNull StringformatDecimal(double d) Formats a decimal into a human-friendly string that rounds the double to 2 decimal places.static @NotNull StringgetName()Gets the name of the plugin, as defined in plugin.yml.static <T> TgetOrDefault(T nullable, T def) Returns the nullable value if not null, or the default value if it is null.static <T> TgetOrError(T nullable, @NotNull String description, boolean disable) Returns the nullable value if not null, or throws a runtime exception with error if it is null.static intGets the server's major Minecraft version.static @NotNull StringGets the version string, as defined in plugin.yml.static booleanhasPermission(@NotNull org.bukkit.command.CommandSender sender, @Nullable String permission) Checks if the specified command sender has the permission node.static booleanisServerVersionAtLeast(int version) Checks if the server's major version is at least the specified version.
-
Field Details
-
SPIGOT
public static final boolean SPIGOTIf the server software the plugin is running on is Spigot or a fork of Spigot. Used internally for some Spigot-only features or optimizations. -
PLAYERS_ERROR_MESSAGE
The error message for players when an internal error occurs.
-
-
Constructor Details
-
Common
public Common()
-
-
Method Details
-
getName
Gets the name of the plugin, as defined in plugin.yml.- Returns:
- The name of the plugin
-
getVersion
Gets the version string, as defined in plugin.yml.- Returns:
- The version of the plugin
-
getServerMajorVersion
public static int getServerMajorVersion()Gets the server's major Minecraft version.For example, if a server is running 1.16.4, this will return 16.
- Returns:
- The server's major Minecraft version.
-
isServerVersionAtLeast
public static boolean isServerVersionAtLeast(int version) Checks if the server's major version is at least the specified version.- Parameters:
version- The minimum major version- Returns:
- True if equal or greater to the provided version, false otherwise
-
checkInt
Attempts to parse a string into an integer.- Parameters:
str- The string to parse- Returns:
- The parsed integer, or null if the string is not a valid integer
-
checkFloat
Attempts to parse a string into a float.- Parameters:
str- The string to parse- Returns:
- The parsed float, or null if the string is not a valid float
-
checkLong
Attempts to parse a string into a long.- Parameters:
str- The string to parse- Returns:
- The parsed long, or null if the string is not a valid long
-
checkDouble
Attempts to parse a string into a double.- Parameters:
str- The string to parse- Returns:
- The parsed double, or null if the string is not a valid double
-
applyOperator
@NotNull public static @NotNull String applyOperator(@Nullable @Nullable String str, @Nullable @Nullable UnaryOperator<String> operator) -
applyOperator
-
applyOperator
@NotNull public static @NotNull org.bukkit.inventory.ItemStack applyOperator(@Nullable @Nullable org.bukkit.inventory.ItemStack stack, @Nullable @Nullable UnaryOperator<String> operator) -
checkClass
Simple method to check if a class exists.- Parameters:
className- The class's package and name (Example: dev.demeng.pluginbase.Validate)- Returns:
- The actual class if the class exists, null otherwise
-
formatDecimal
Formats a decimal into a human-friendly string that rounds the double to 2 decimal places.- Parameters:
d- THe double to format- Returns:
- The formatted double
-
getOrDefault
@NotNull public static <T> T getOrDefault(@Nullable T nullable, @NotNull T def) Returns the nullable value if not null, or the default value if it is null.- Type Parameters:
T- The object type being checked and returned- Parameters:
nullable- The nullable valuedef- The default value- Returns:
- The nullable if not null, default otherwise
-
getOrError
@NotNull public static <T> T getOrError(@Nullable T nullable, @NotNull @NotNull String description, boolean disable) Returns the nullable value if not null, or throws a runtime exception with error if it is null.- Type Parameters:
T- The object type being checked and returned- Parameters:
nullable- The nullable valuedescription- The error description if the value is nulldisable- If the plugin should disable if the value is null- Returns:
- The nullable if not null
-
hasPermission
public static boolean hasPermission(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String permission) Checks if the specified command sender has the permission node. If the permission node is null, empty, or equal to "none", the method will return true.- Parameters:
sender- The command sender to checkpermission- The permission to check- Returns:
- True if the command sender has the permission, false otherwise
-
error
public static void error(@Nullable @Nullable Throwable throwable, @NotNull @NotNull String description, boolean disable, @NotNull @NotNull org.bukkit.command.CommandSender... players) Reports an error in the plugin.- Parameters:
throwable- The throwabledescription- A brief description of the errordisable- If the plugin should be disabledplayers- Any players associated with this error, a message will be sent to them
-
forEachInt
Parses a string sequence of integers and accepts a consumer for each integer. The sequence can either be a single integer (1), a range of integers (1-10), or a list of integers (1,2,3).- Parameters:
str- The integer sequence to parseconsumer- The consumer to accept for each integer- Throws:
IllegalArgumentException- If the sequenece is invalid
-