1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Even more annotations

This commit is contained in:
TheBusyBiscuit 2020-09-02 11:53:42 +02:00
parent 71322d95a8
commit 8a6e00a402
10 changed files with 112 additions and 57 deletions

View File

@ -5,6 +5,8 @@ import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -34,7 +36,7 @@ public class AutoSavingService {
* @param interval
* The interval in which to run this task
*/
public void start(SlimefunPlugin plugin, int interval) {
public void start(@Nonnull SlimefunPlugin plugin, int interval) {
this.interval = interval;
plugin.getServer().getScheduler().runTaskTimer(plugin, this::saveAllPlayers, 2000L, interval * 60L * 20L);

View File

@ -15,6 +15,10 @@ import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Nonnull;
import org.apache.commons.lang.Validate;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.api.Slimefun;
@ -65,7 +69,8 @@ public class BackupService implements Runnable {
}
}
private void createBackup(ZipOutputStream output) throws IOException {
private void createBackup(@Nonnull ZipOutputStream output) throws IOException {
Validate.notNull(output, "The Output Stream cannot be null!");
for (File folder : new File("data-storage/Slimefun/stored-blocks/").listFiles()) {
addDirectory(output, folder, "stored-blocks/" + folder.getName());
@ -93,7 +98,7 @@ public class BackupService implements Runnable {
}
}
private void addDirectory(ZipOutputStream output, File directory, String zipPath) throws IOException {
private void addDirectory(@Nonnull ZipOutputStream output, @Nonnull File directory, @Nonnull String zipPath) throws IOException {
byte[] buffer = new byte[1024];
for (File file : directory.listFiles()) {
@ -121,7 +126,7 @@ public class BackupService implements Runnable {
* @throws IOException
* An {@link IOException} is thrown if a {@link File} could not be deleted
*/
private void purgeBackups(List<File> backups) throws IOException {
private void purgeBackups(@Nonnull List<File> backups) throws IOException {
Collections.sort(backups, (a, b) -> {
LocalDateTime time1 = LocalDateTime.parse(a.getName().substring(0, a.getName().length() - 4), format);
LocalDateTime time2 = LocalDateTime.parse(b.getName().substring(0, b.getName().length() - 4), format);

View File

@ -2,6 +2,9 @@ package io.github.thebusybiscuit.slimefun4.core.services;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -27,7 +30,7 @@ public class CustomTextureService {
private String version = null;
private boolean modified = false;
public CustomTextureService(Config config) {
public CustomTextureService(@Nonnull Config config) {
this.config = config;
config.getConfiguration().options().header("This file is used to assign items from Slimefun or any of its addons\n" + "the 'CustomModelData' NBT tag. This can be used in conjunction with a custom resource pack\n" + "to give items custom textures.\n0 means there is no data assigned to that item.\n\n" + "There is no official Slimefun resource pack at the moment.");
config.getConfiguration().options().copyHeader(true);
@ -42,7 +45,7 @@ public class CustomTextureService {
* @param save
* Whether to save this file
*/
public void register(Collection<SlimefunItem> items, boolean save) {
public void register(@Nonnull Collection<SlimefunItem> items, boolean save) {
Validate.notEmpty(items, "items must neither be null or empty.");
config.setDefaultValue("SLIMEFUN_GUIDE", 0);
@ -74,6 +77,7 @@ public class CustomTextureService {
}
}
@Nullable
public String getVersion() {
return version;
}
@ -82,18 +86,18 @@ public class CustomTextureService {
return modified;
}
public int getModelData(String id) {
public int getModelData(@Nonnull String id) {
Validate.notNull(id, "Cannot get the ModelData for 'null'");
return config.getInt(id);
}
public void setTexture(ItemStack item, String id) {
public void setTexture(@Nonnull ItemStack item, @Nonnull String id) {
ItemMeta im = item.getItemMeta();
setTexture(im, id);
item.setItemMeta(im);
}
public void setTexture(ItemMeta im, String id) {
public void setTexture(@Nonnull ItemMeta im, @Nonnull String id) {
int data = getModelData(id);
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {

View File

@ -6,6 +6,9 @@ import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.Server;
import org.bukkit.inventory.FurnaceRecipe;
@ -45,7 +48,7 @@ public class MinecraftRecipeService {
* @param plugin
* The {@link Plugin} that requests this Service
*/
public MinecraftRecipeService(Plugin plugin) {
public MinecraftRecipeService(@Nonnull Plugin plugin) {
this.plugin = plugin;
}
@ -68,7 +71,7 @@ public class MinecraftRecipeService {
* @param subscription
* A callback to run when the {@link RecipeSnapshot} has been created.
*/
public void subscribe(Consumer<RecipeSnapshot> subscription) {
public void subscribe(@Nonnull Consumer<RecipeSnapshot> subscription) {
Validate.notNull(subscription, "Callback must not be null!");
subscriptions.add(subscription);
}
@ -82,7 +85,8 @@ public class MinecraftRecipeService {
*
* @return An {@link Optional} describing the furnace output of the given {@link ItemStack}
*/
public Optional<ItemStack> getFurnaceOutput(ItemStack input) {
@Nonnull
public Optional<ItemStack> getFurnaceOutput(@Nullable ItemStack input) {
if (snapshot == null || input == null) {
return Optional.empty();
}
@ -98,7 +102,7 @@ public class MinecraftRecipeService {
*
* @return Whether this item can be smelted
*/
public boolean isSmeltable(ItemStack input) {
public boolean isSmeltable(@Nullable ItemStack input) {
return getFurnaceOutput(input).isPresent();
}
@ -113,7 +117,8 @@ public class MinecraftRecipeService {
* The {@link Recipe} to get the shape from
* @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe}
*/
public RecipeChoice[] getRecipeShape(Recipe recipe) {
@Nonnull
public RecipeChoice[] getRecipeShape(@Nonnull Recipe recipe) {
Validate.notNull(recipe, "Recipe must not be null!");
if (recipe instanceof ShapedRecipe) {
@ -147,7 +152,8 @@ public class MinecraftRecipeService {
* The {@link ItemStack} for which to get the recipes
* @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack}
*/
public Recipe[] getRecipesFor(ItemStack item) {
@Nonnull
public Recipe[] getRecipesFor(@Nullable ItemStack item) {
if (snapshot == null || item == null) {
return new Recipe[0];
}

View File

@ -7,6 +7,9 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.Permission;
@ -29,13 +32,13 @@ public class PermissionsService {
private final Map<String, String> permissions = new HashMap<>();
private final Config config;
public PermissionsService(SlimefunPlugin plugin) {
public PermissionsService(@Nonnull SlimefunPlugin plugin) {
config = new Config(plugin, "permissions.yml");
config.getConfiguration().options().header("This file is used to assign permission nodes to items from Slimefun or any of its addons.\nTo assign an item a certain permission node you simply have to set the 'permission' attribute\nto your desired permission node. You can also customize the text that is displayed when a Player does not have that permission.");
config.getConfiguration().options().copyHeader(true);
}
public void register(Iterable<SlimefunItem> items, boolean save) {
public void register(@Nonnull Iterable<SlimefunItem> items, boolean save) {
for (SlimefunItem item : items) {
if (item != null && item.getID() != null) {
String path = item.getID() + ".permission";
@ -83,7 +86,9 @@ public class PermissionsService {
*
* @return An {@link Optional} holding the {@link Permission} as a {@link String} or an empty {@link Optional}
*/
public Optional<String> getPermission(SlimefunItem item) {
@Nonnull
public Optional<String> getPermission(@Nonnull SlimefunItem item) {
Validate.notNull(item, "Cannot get permissions for null");
String permission = permissions.get(item.getID());
if (permission == null || permission.equals("none")) {
@ -102,7 +107,7 @@ public class PermissionsService {
* @param permission
* The {@link Permission} to set
*/
public void setPermission(SlimefunItem item, String permission) {
public void setPermission(@Nonnull SlimefunItem item, @Nullable String permission) {
Validate.notNull(item, "You cannot set the permission for null");
permissions.put(item.getID(), permission != null ? permission : "none");
}
@ -118,7 +123,8 @@ public class PermissionsService {
config.save();
}
public List<String> getLore(SlimefunItem item) {
@Nonnull
public List<String> getLore(@Nonnull SlimefunItem item) {
List<String> lore = config.getStringList(item.getID() + ".lore");
return lore == null ? Arrays.asList("LORE NOT FOUND") : lore;
}

View File

@ -3,6 +3,8 @@ package io.github.thebusybiscuit.slimefun4.core.services;
import java.io.File;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import org.bukkit.plugin.Plugin;
import io.github.thebusybiscuit.cscorelib2.config.Config;
@ -37,7 +39,7 @@ public class UpdaterService {
* @param file
* The {@link File} of this {@link Plugin}
*/
public UpdaterService(SlimefunPlugin plugin, String version, File file) {
public UpdaterService(@Nonnull SlimefunPlugin plugin, @Nonnull String version, @Nonnull File file) {
this.plugin = plugin;
Updater autoUpdater = null;
@ -82,6 +84,7 @@ public class UpdaterService {
*
* @return The branch this build of Slimefun is on.
*/
@Nonnull
public SlimefunBranch getBranch() {
return branch;
}

View File

@ -2,6 +2,10 @@ package io.github.thebusybiscuit.slimefun4.core.services.profiler;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
/**
@ -30,13 +34,14 @@ public enum PerformanceRating implements Predicate<Float> {
private final ChatColor color;
private final float threshold;
PerformanceRating(ChatColor color, float threshold) {
PerformanceRating(@Nonnull ChatColor color, float threshold) {
Validate.notNull(color, "Color cannot be null");
this.color = color;
this.threshold = threshold;
}
@Override
public boolean test(Float value) {
public boolean test(@Nullable Float value) {
if (value == null) {
// null will only test true for UNKNOWN
return threshold < 0;
@ -45,6 +50,7 @@ public enum PerformanceRating implements Predicate<Float> {
return value <= threshold;
}
@Nonnull
public ChatColor getColor() {
return color;
}

View File

@ -8,6 +8,8 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -38,7 +40,7 @@ class PerformanceSummary {
private final Map<String, Long> plugins;
private final Map<String, Long> items;
PerformanceSummary(SlimefunProfiler profiler, long totalElapsedTime, int totalTickedBlocks) {
PerformanceSummary(@Nonnull SlimefunProfiler profiler, long totalElapsedTime, int totalTickedBlocks) {
this.profiler = profiler;
this.rating = profiler.getPerformance();
this.percentage = profiler.getPercentageOfTick();
@ -51,7 +53,7 @@ class PerformanceSummary {
items = profiler.getByItem();
}
public void send(CommandSender sender) {
public void send(@Nonnull CommandSender sender) {
sender.sendMessage("");
sender.sendMessage(ChatColor.GREEN + "===== Slimefun Lag Profiler =====");
sender.sendMessage(ChatColor.GOLD + "Total time: " + ChatColor.YELLOW + NumberUtils.getAsMillis(totalElapsedTime));
@ -112,21 +114,21 @@ class PerformanceSummary {
hoverComponent.setColor(ChatColor.GRAY);
StringBuilder builder = new StringBuilder();
int displayed = 0;
int hidden = 0;
int shownEntries = 0;
int hiddenEntries = 0;
for (Map.Entry<String, Long> entry : results) {
if (displayed < MAX_ITEMS && (displayed < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) {
if (shownEntries < MAX_ITEMS && (shownEntries < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) {
builder.append("\n").append(ChatColor.YELLOW).append(formatter.apply(entry));
displayed++;
shownEntries++;
}
else {
hidden++;
hiddenEntries++;
}
}
if (hidden > 0) {
builder.append("\n\n&c+ &6").append(hidden).append(" more");
if (hiddenEntries > 0) {
builder.append("\n\n&c+ &6").append(hiddenEntries).append(" more");
}
Content content = new Text(TextComponent.fromLegacyText(ChatColors.color(builder.toString())));
@ -139,31 +141,28 @@ class PerformanceSummary {
}
private String summarizeAsString(int count, String prefix, List<Entry<String, Long>> results, Function<Entry<String, Long>, String> formatter) {
int displayed = 0;
int hidden = 0;
int shownEntries = 0;
int hiddenEntries = 0;
StringBuilder builder = new StringBuilder();
builder.append(ChatColor.GOLD);
builder.append(prefix);
builder.append(ChatColor.GOLD).append(prefix);
if (count > 0) {
builder.append(ChatColor.YELLOW);
for (Map.Entry<String, Long> entry : results) {
if (displayed < MAX_ITEMS && (displayed < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) {
if (shownEntries < MAX_ITEMS && (shownEntries < MIN_ITEMS || entry.getValue() > VISIBILITY_THRESHOLD)) {
builder.append("\n ");
builder.append(ChatColor.stripColor(formatter.apply(entry)));
displayed++;
shownEntries++;
}
else {
hidden++;
hiddenEntries++;
}
}
if (hidden > 0) {
builder.append("\n+ ");
builder.append(hidden);
builder.append(" more...");
if (hiddenEntries > 0) {
builder.append("\n+ ").append(hiddenEntries).append(" more...");
}
}

View File

@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.services.profiler;
import javax.annotation.Nonnull;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -7,23 +9,36 @@ import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/**
* This represents an entry in our {@link SlimefunProfiler}.
*
* @author TheBusyBiscuit
*
*/
class ProfiledBlock {
private final BlockPosition position;
private final SlimefunItem item;
ProfiledBlock(Location l, SlimefunItem item) {
ProfiledBlock(@Nonnull Location l, @Nonnull SlimefunItem item) {
this.position = new BlockPosition(l);
this.item = item;
}
ProfiledBlock(BlockPosition position, SlimefunItem item) {
ProfiledBlock(@Nonnull BlockPosition position, @Nonnull SlimefunItem item) {
this.position = position;
this.item = item;
}
ProfiledBlock(Block b) {
this(new BlockPosition(b), null);
/**
* This is just a <strong>dummy</strong> constructor.
*
* @param b
* A {@link Block}
*/
ProfiledBlock(@Nonnull Block b) {
this.position = new BlockPosition(b);
this.item = null;
}
public BlockPosition getPosition() {

View File

@ -12,6 +12,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import org.apache.commons.lang.Validate;
import org.bukkit.Chunk;
import org.bukkit.Location;
@ -105,7 +107,7 @@ public class SlimefunProfiler {
*
* @return The total timings of this entry
*/
public long closeEntry(Location l, SlimefunItem item, long timestamp) {
public long closeEntry(@Nonnull Location l, @Nonnull SlimefunItem item, long timestamp) {
Validate.notNull(l, "Location must not be null!");
Validate.notNull(item, "You need to specify a SlimefunItem!");
@ -194,12 +196,13 @@ public class SlimefunProfiler {
* @param sender
* The {@link CommandSender} who shall receive this summary.
*/
public void requestSummary(CommandSender sender) {
public void requestSummary(@Nonnull CommandSender sender) {
Validate.notNull(sender, "Cannot request a summary for null");
requests.add(sender);
}
@Nonnull
protected Map<String, Long> getByItem() {
Map<String, Long> map = new HashMap<>();
@ -210,6 +213,7 @@ public class SlimefunProfiler {
return map;
}
@Nonnull
protected Map<String, Long> getByPlugin() {
Map<String, Long> map = new HashMap<>();
@ -220,6 +224,7 @@ public class SlimefunProfiler {
return map;
}
@Nonnull
protected Map<String, Long> getByChunk() {
Map<String, Long> map = new HashMap<>();
@ -234,7 +239,8 @@ public class SlimefunProfiler {
return map;
}
protected int getBlocksInChunk(String chunk) {
protected int getBlocksInChunk(@Nonnull String chunk) {
Validate.notNull(chunk, "The chunk cannot be null!");
int blocks = 0;
for (ProfiledBlock block : timings.keySet()) {
@ -250,7 +256,8 @@ public class SlimefunProfiler {
return blocks;
}
protected int getBlocksOfId(String id) {
protected int getBlocksOfId(@Nonnull String id) {
Validate.notNull(id, "The id cannot be null!");
int blocks = 0;
for (ProfiledBlock block : timings.keySet()) {
@ -262,11 +269,12 @@ public class SlimefunProfiler {
return blocks;
}
protected int getBlocksFromPlugin(String id) {
protected int getBlocksFromPlugin(@Nonnull String pluginName) {
Validate.notNull(pluginName, "The Plugin name cannot be null!");
int blocks = 0;
for (ProfiledBlock block : timings.keySet()) {
if (block.getAddon().getName().equals(id)) {
if (block.getAddon().getName().equals(pluginName)) {
blocks++;
}
}
@ -285,6 +293,7 @@ public class SlimefunProfiler {
*
* @return The current performance grade
*/
@Nonnull
public PerformanceRating getPerformance() {
float percentage = getPercentageOfTick();
@ -314,26 +323,26 @@ public class SlimefunProfiler {
*
* @return Whether timings of this {@link Block} have been collected
*/
public boolean hasTimings(Block b) {
public boolean hasTimings(@Nonnull Block b) {
Validate.notNull("Cannot get timings for a null Block");
return timings.containsKey(new ProfiledBlock(b));
}
public String getTime(Block b) {
public String getTime(@Nonnull Block b) {
Validate.notNull("Cannot get timings for a null Block");
long time = timings.getOrDefault(new ProfiledBlock(b), 0L);
return NumberUtils.getAsMillis(time);
}
public String getTime(Chunk chunk) {
public String getTime(@Nonnull Chunk chunk) {
Validate.notNull("Cannot get timings for a null Chunk");
long time = getByChunk().getOrDefault(chunk.getWorld().getName() + " (" + chunk.getX() + ',' + chunk.getZ() + ')', 0L);
return NumberUtils.getAsMillis(time);
}
public String getTime(SlimefunItem item) {
public String getTime(@Nonnull SlimefunItem item) {
Validate.notNull("Cannot get timings for a null SlimefunItem");
long time = getByItem().getOrDefault(item.getID(), 0L);