Skip to content

Commit

Permalink
Improved null safety (#132)
Browse files Browse the repository at this point in the history
* Infer nullity (automatically)
* Fix #130
* Add null checks
  • Loading branch information
osipxd authored Apr 9, 2018
1 parent 768d68f commit 0400a05
Show file tree
Hide file tree
Showing 59 changed files with 476 additions and 321 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/comphenix/packetwrapper/AbstractPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import com.comphenix.protocol.events.PacketContainer;
import com.google.common.base.Objects;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.*;

import java.lang.reflect.InvocationTargetException;

@SuppressWarnings("ALL")
public abstract class AbstractPacket {
// The packet we will be modifying
@Nullable
protected PacketContainer handle;

/**
Expand All @@ -38,7 +40,7 @@ public abstract class AbstractPacket {
* @param handle - handle to the raw packet data.
* @param type - the packet type.
*/
protected AbstractPacket(PacketContainer handle, PacketType type) {
protected AbstractPacket(@Nullable PacketContainer handle, PacketType type) {
// Make sure we're given a valid packet
if (handle == null) {
throw new IllegalArgumentException("Packet handle cannot be NULL.");
Expand All @@ -55,6 +57,7 @@ protected AbstractPacket(PacketContainer handle, PacketType type) {
*
* @return Raw packet data.
*/
@Nullable
public PacketContainer getHandle() {
return handle;
}
Expand Down Expand Up @@ -103,4 +106,4 @@ public void receivePacket(Player sender) {
throw new RuntimeException("Cannot recieve packet.", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.*;
import ru.endlesscode.rpginventory.nms.VersionHandler;

import java.util.Arrays;
Expand Down Expand Up @@ -79,12 +80,12 @@ public List<ItemStack> getSlotData() {
*
* @param value - new value.
*/
public void setSlotData(List<ItemStack> value) {
public void setSlotData(@NotNull List<ItemStack> value) {
if (VersionHandler.is1_9() || VersionHandler.is1_10()) {
handle.getItemArrayModifier().write(0, value.toArray(new ItemStack[value.size()]));
} else {
handle.getItemListModifier().write(0, value);
}
}

}
}
6 changes: 3 additions & 3 deletions src/main/java/ru/endlesscode/rpginventory/RPGInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -171,7 +170,7 @@ public void onEnable() {
protocolManager.addPacketListener(
new PacketAdapter(this, PacketType.Play.Server.RECIPES) {
@Override
public void onPacketSending(PacketEvent event) {
public void onPacketSending(@NotNull PacketEvent event) {
event.setCancelled(true);
}
});
Expand Down Expand Up @@ -376,6 +375,7 @@ private void updateConfig() {
}
}

@NotNull
public Path getDataPath() {
return getDataFolder().toPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import net.milkbowl.vault.permission.Permission;

import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -29,6 +30,7 @@

import java.util.List;

import org.jetbrains.annotations.*;
import ru.endlesscode.rpginventory.api.InventoryAPI;
import ru.endlesscode.rpginventory.inventory.InventoryManager;
import ru.endlesscode.rpginventory.inventory.backpack.BackpackManager;
Expand All @@ -43,7 +45,7 @@
*/
@SuppressWarnings("deprecation")
class RPGInventoryCommandExecutor implements CommandExecutor {
private static void givePet(CommandSender sender, String playerName, String petId) {
private static void givePet(@NotNull CommandSender sender, String playerName, String petId) {
if (validatePlayer(sender, playerName)) {
Player player = RPGInventory.getInstance().getServer().getPlayer(playerName);
ItemStack petItem = PetManager.getPetItem(petId);
Expand All @@ -59,7 +61,7 @@ private static void givePet(CommandSender sender, String playerName, String petI
sender.sendMessage(StringUtils.coloredLine("&3Use &6/rpginv pet [&eplayer&6] [&epetId&6]"));
}

private static void giveFood(CommandSender sender, String playerName, String foodId, String stringAmount) {
private static void giveFood(@NotNull CommandSender sender, String playerName, String foodId, @NotNull String stringAmount) {
if (validatePlayer(sender, playerName)) {
Player player = RPGInventory.getInstance().getServer().getPlayer(playerName);
ItemStack foodItem = PetManager.getFoodItem(foodId);
Expand All @@ -81,28 +83,28 @@ private static void giveFood(CommandSender sender, String playerName, String foo
sender.sendMessage(StringUtils.coloredLine("&3Use &6/rpginv food [&eplayer&6] [&efoodId&6] (&eamount&6)"));
}

private static void giveItem(CommandSender sender, String playerName, String itemId) {
private static void giveItem(@NotNull CommandSender sender, String playerName, String itemId) {
if (validatePlayer(sender, playerName)) {
Player player = RPGInventory.getInstance().getServer().getPlayer(playerName);
ItemStack petItem = ItemManager.getItem(itemId);
ItemStack item = ItemManager.getItem(itemId);

if (petItem != null) {
player.getInventory().addItem(petItem);
return;
} else {
if (item.getType() == Material.AIR) {
sender.sendMessage(StringUtils.coloredLine("&cItem '" + itemId + "' not found!"));
} else {
player.getInventory().addItem(item);
return;
}
}

sender.sendMessage(StringUtils.coloredLine("&3Use &6/rpginv item [&eplayer&6] [&eitemId&6]"));
}

private static void giveBackpack(CommandSender sender, String playerName, String id) {
private static void giveBackpack(@NotNull CommandSender sender, String playerName, String id) {
if (validatePlayer(sender, playerName)) {
Player player = RPGInventory.getInstance().getServer().getPlayer(playerName);
ItemStack bpItem = BackpackManager.getItem(id);

if (bpItem != null) {
if (bpItem.getType() == Material.AIR) {
player.getInventory().addItem(bpItem);
return;
} else {
Expand Down Expand Up @@ -135,7 +137,7 @@ private static void printHelp(CommandSender sender) {
sender.sendMessage(StringUtils.coloredLine("&3====================================================="));
}

private static void printList(CommandSender sender, String type) {
private static void printList(@NotNull CommandSender sender, String type) {
switch (type) {
case "pet":
case "pets":
Expand Down Expand Up @@ -170,7 +172,7 @@ private static void reloadPlugin(CommandSender sender) {
sender.sendMessage(StringUtils.coloredLine("&e[RPGInventory] Plugin successfully reloaded!"));
}

private static void openInventory(CommandSender sender) {
private static void openInventory(@NotNull CommandSender sender) {
if (!validatePlayer(sender)) {
return;
}
Expand All @@ -183,7 +185,7 @@ private static void openInventory(CommandSender sender) {
InventoryManager.get(player).openInventory();
}

private static void openInventory(CommandSender sender, String playerName) {
private static void openInventory(@NotNull CommandSender sender, String playerName) {
if (!validatePlayer(sender) || !validatePlayer(sender, playerName)) {
return;
}
Expand All @@ -202,7 +204,7 @@ private static boolean validatePlayer(CommandSender sender) {
return validatePlayer(sender, (Player) sender);
}

private static boolean validatePlayer(CommandSender sender, String playerName) {
private static boolean validatePlayer(@NotNull CommandSender sender, String playerName) {
Player player = RPGInventory.getInstance().getServer().getPlayer(playerName);
if (player == null) {
sender.sendMessage(StringUtils.coloredLine("&cPlayer '" + playerName + "' not found!"));
Expand All @@ -211,7 +213,7 @@ private static boolean validatePlayer(CommandSender sender, String playerName) {
return validatePlayer(sender, player);
}

private static boolean validatePlayer(CommandSender sender, Player player) {
private static boolean validatePlayer(@NotNull CommandSender sender, Player player) {
if (!InventoryManager.playerIsLoaded(player)) {
sender.sendMessage(StringUtils.coloredLine("&cThis command not allowed here."));
return false;
Expand All @@ -226,7 +228,7 @@ private static void missingRights(CommandSender sender) {
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, Command command, String label, @NotNull String[] args) {
Permission perms = RPGInventory.getPermissions();

if (args.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@
@SuppressWarnings({"unused", "WeakerAccess"})
public class InventoryAPI {
/**
* Checks if opened inventory is RPGInventory
* Checks if opened inventory is RPGInventory.
*
* @param inventory - opened inventory
* @return true - if opened RPGInventory, false - otherwise
*/
public static boolean isRPGInventory(Inventory inventory) {
InventoryHolder holder = inventory.getHolder();
return holder != null && holder instanceof PlayerWrapper;
return holder instanceof PlayerWrapper;
}

/**
* Get all passive item from RPGInventory of specific player
* Get all passive item from RPGInventory of specific player.
*
* @param player - the player
* @return List of not null passive item
Expand All @@ -78,7 +78,7 @@ public static List<ItemStack> getPassiveItems(Player player) {
}

/**
* Get all active item from RPGInventory of specific player
* Get all active item from RPGInventory of specific player.
*
* @param player - the player
* @return List of not null active item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public PetEquipEvent(Player player, ItemStack petItem) {
this.petItem = petItem;
}

@NotNull
@SuppressWarnings("unused")
public static HandlerList getHandlerList() {
return handlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.*;

/**
* Created by OsipXD on 11.09.2015
Expand All @@ -35,11 +36,13 @@ private PlayerInventoryLoadEvent(Player who) {
super(who);
}

@NotNull
@SuppressWarnings("unused")
public static HandlerList getHandlerList() {
return handlers;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
Expand Down Expand Up @@ -68,4 +71,4 @@ public Post(Player who) {
super(who);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.*;

/**
* Created by OsipXD on 11.09.2015
Expand All @@ -34,11 +35,13 @@ private PlayerInventoryUnloadEvent(Player who) {
super(who);
}

@NotNull
@SuppressWarnings("unused")
public static HandlerList getHandlerList() {
return handlers;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
Expand Down
Loading

0 comments on commit 0400a05

Please sign in to comment.