Skip to content

Commit

Permalink
- Finished javadocs, annotations and null checkers in project.
Browse files Browse the repository at this point in the history
- Optimized imports.
  • Loading branch information
Hakan Kargın committed Mar 18, 2022
1 parent cdcea25 commit f52d990
Show file tree
Hide file tree
Showing 13 changed files with 867 additions and 106 deletions.
463 changes: 422 additions & 41 deletions api/src/main/java/com/hakan/core/HCore.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static Map<String, HHologram> getContent() {
*
* @return holograms.
*/
@Nonnull
public static Collection<HHologram> getValuesSafe() {
return new ArrayList<>(HHologramHandler.holograms.values());
}
Expand All @@ -54,6 +55,7 @@ public static Collection<HHologram> getValuesSafe() {
*
* @return holograms.
*/
@Nonnull
public static Collection<HHologram> getValues() {
return HHologramHandler.holograms.values();
}
Expand Down
88 changes: 64 additions & 24 deletions api/src/main/java/com/hakan/core/ui/inventory/HInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.hakan.core.ui.inventory.item.ClickableItem;
import com.hakan.core.ui.inventory.pagination.Page;
import com.hakan.core.ui.inventory.pagination.Pagination;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
Expand All @@ -29,7 +28,16 @@ public class HInventory {
private final Set<Option> options;
private final Map<Integer, ClickableItem> items;

public HInventory(String id, String title, int size, InventoryType type, Set<Option> options) {
/**
* Creates new instance of this class.
*
* @param id ID of inventory.
* @param title Title.
* @param size Size.
* @param type Inventory type.
* @param options Options.
*/
public HInventory(@Nonnull String id, @Nonnull String title, int size, @Nonnull InventoryType type, @Nonnull Set<Option> options) {
this.id = id;
this.title = title;
this.options = options;
Expand All @@ -43,7 +51,16 @@ public HInventory(String id, String title, int size, InventoryType type, Set<Opt
this.onCreate(this);
}

public HInventory(String id, String title, int size, InventoryType type) {
/**
* Creates new instance of this class
* with all options.
*
* @param id ID of inventory.
* @param title Title.
* @param size Size.
* @param type Inventory type.
*/
public HInventory(@Nonnull String id, @Nonnull String title, int size, @Nonnull InventoryType type) {
this(id, title, size, type, new HashSet<>(Arrays.asList(Option.values())));
}

Expand Down Expand Up @@ -103,8 +120,7 @@ public final int getSize() {
* @return If UI contains option, returns true
*/
public final boolean hasOption(@Nonnull Option option) {
Validate.notNull(option, "option cannot be null");
return this.options.contains(option);
return this.options.contains(Objects.requireNonNull(option, "option cannot be null!"));
}

/**
Expand All @@ -113,9 +129,9 @@ public final boolean hasOption(@Nonnull Option option) {
* @param option enum HInventory.Option
* @return instance of this class
*/
@Nonnull
public final <T extends HInventory> T addOption(@Nonnull Option option) {
Validate.notNull(option, "option cannot be null");
this.options.add(option);
this.options.add(Objects.requireNonNull(option, "option cannot be null!"));
return (T) this;
}

Expand All @@ -125,9 +141,9 @@ public final <T extends HInventory> T addOption(@Nonnull Option option) {
* @param option enum HInventory.Option
* @return instance of this class
*/
@Nonnull
public final <T extends HInventory> T removeOption(@Nonnull Option option) {
Validate.notNull(option, "option cannot be null");
this.options.remove(option);
this.options.remove(Objects.requireNonNull(option, "option cannot be null!"));
return (T) this;
}

Expand Down Expand Up @@ -198,7 +214,6 @@ public final <T extends HInventory> T setItem(int slot, @Nullable ClickableItem
*/
@Nonnull
public final <T extends HInventory> T setItem(int slot, @Nonnull ItemStack itemStack, @Nullable Consumer<InventoryClickEvent> consumer) {
Validate.notNull(itemStack, "itemstack cannot be null");
return this.setItem(slot, new ClickableItem(itemStack, consumer));
}

Expand All @@ -221,8 +236,8 @@ public final <T extends HInventory> T setItem(int slot, @Nonnull ItemStack itemS
* @return instance of this class
*/
@Nonnull
public final <T extends HInventory> T fillPage(Page page) {
this.pagination.setCurrentPage(page.getNumber());
public final <T extends HInventory> T fillPage(@Nonnull Page page) {
this.pagination.setCurrentPage(Objects.requireNonNull(page, "page cannot be null!").getNumber());
return (T) this;
}

Expand Down Expand Up @@ -309,7 +324,7 @@ public final <T extends HInventory> T fill(@Nonnull ItemStack itemStack) {
*/
@Nonnull
public final <T extends HInventory> T fillMaterial(@Nonnull Material material, boolean replaceWithItems) {
return this.fill(new ItemStack(material), replaceWithItems);
return this.fill(new ItemStack(Objects.requireNonNull(material, "material cannot be null!")), replaceWithItems);
}

/**
Expand All @@ -320,7 +335,7 @@ public final <T extends HInventory> T fillMaterial(@Nonnull Material material, b
*/
@Nonnull
public final <T extends HInventory> T fillMaterial(@Nonnull Material material) {
return this.fill(new ItemStack(material), true);
return this.fill(new ItemStack(Objects.requireNonNull(material, "material cannot be null!")), true);
}

/**
Expand Down Expand Up @@ -371,8 +386,8 @@ public final <T extends HInventory> T fillNull() {
* @return instance of this class
*/
@Nonnull
public <T extends HInventory> T refresh(Player player) {
this.onOpen(this, player);
public final <T extends HInventory> T refresh(@Nonnull Player player) {
this.onOpen(this, Objects.requireNonNull(player, "player cannot be null!"));
return (T) this;
}

Expand All @@ -383,8 +398,8 @@ public <T extends HInventory> T refresh(Player player) {
* @return instance of this class
*/
@Nonnull
public final <T extends HInventory> T open(Player player) {
HInventory hInventory = HInventoryHandler.findByPlayer(player).orElse(null);
public final <T extends HInventory> T open(@Nonnull Player player) {
HInventory hInventory = HInventoryHandler.findByPlayer(Objects.requireNonNull(player, "player cannot be null!")).orElse(null);

if (hInventory == null || !hInventory.equals(this)) {
player.openInventory(this.inventory);
Expand All @@ -402,21 +417,46 @@ public final <T extends HInventory> T open(Player player) {
* @return instance of this class
*/
@Nonnull
public final <T extends HInventory> T close(Player player) {
this.addOption(Option.CLOSABLE);
player.closeInventory();
public final <T extends HInventory> T close(@Nonnull Player player) {
Objects.requireNonNull(player, "player cannot be null!");

if (this.hasOption(Option.CLOSABLE)) {
player.closeInventory();
} else {
this.addOption(Option.CLOSABLE);
player.closeInventory();
this.removeOption(Option.CLOSABLE);
}

return (T) this;
}

public void onCreate(HInventory hInventory) {
/**
* This method run when inventory created.
*
* @param hInventory This class.
*/
public void onCreate(@Nonnull HInventory hInventory) {

}

public void onOpen(HInventory hInventory, Player player) {
/**
* This method run when inventory
* opened to any player.
*
* @param hInventory This class.
*/
public void onOpen(@Nonnull HInventory hInventory, @Nonnull Player player) {

}

public void onClose(HInventory hInventory, Player player) {
/**
* This method run when inventory
* closed from any player.
*
* @param hInventory This class.
*/
public void onClose(@Nonnull HInventory hInventory, @Nonnull Player player) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,118 @@
import javax.annotation.Nonnull;
import java.util.*;

/**
* HInventoryHandler class to create
* and find HInventory.
*/
public final class HInventoryHandler {

private static final Map<UUID, HInventory> inventoryMap = new HashMap<>();

public static void initialize(JavaPlugin plugin) {
/**
* Initializes the inventory system.
*
* @param plugin Main class of plugin.
*/
public static void initialize(@Nonnull JavaPlugin plugin) {
HListenerAdapter.register(new PlayerQuitListener(plugin),
new PluginDisableListener(plugin),
new InventoryClickListener(plugin),
new InventoryCloseListener(plugin));
}


/**
* Gets content as safe.
*
* @return Content.
*/
@Nonnull
public static Map<UUID, HInventory> getContentSafe() {
return new HashMap<>(HInventoryHandler.inventoryMap);
}

/**
* Gets content.
*
* @return Content.
*/
@Nonnull
public static Map<UUID, HInventory> getContent() {
return HInventoryHandler.inventoryMap;
}

/**
* Gets values as safe.
*
* @return Values.
*/
@Nonnull
public static Collection<HInventory> getValuesSafe() {
return new ArrayList<>(HInventoryHandler.inventoryMap.values());
}

/**
* Gets values.
*
* @return Values.
*/
@Nonnull
public static Collection<HInventory> getValues() {
return HInventoryHandler.inventoryMap.values();
}

public static Optional<HInventory> findByPlayer(Player player) {
/**
* Finds HInventory by player.
*
* @param player Player.
* @return HInventory as optional.
*/
@Nonnull
public static Optional<HInventory> findByPlayer(@Nonnull Player player) {
return HInventoryHandler.findByUID(player.getUniqueId());
}

public static HInventory getByPlayer(Player player) {
/**
* Gets HInventory by player.
*
* @param player Player.
* @return HInventory.
*/
@Nonnull
public static HInventory getByPlayer(@Nonnull Player player) {
return HInventoryHandler.findByPlayer(player).orElseThrow(() -> new NullPointerException("this player doesn't have a inventory!"));
}

public static Optional<HInventory> findByUID(UUID uid) {
return Optional.ofNullable(HInventoryHandler.inventoryMap.get(uid));
/**
* Finds HInventory by UID.
*
* @param uid Player UID.
* @return HInventory as optional.
*/
@Nonnull
public static Optional<HInventory> findByUID(@Nonnull UUID uid) {
return Optional.ofNullable(HInventoryHandler.inventoryMap.get(Objects.requireNonNull(uid, "UID cannot be null!")));
}

public static HInventory getByUID(UUID uid) {
/**
* Gets HInventory by UID.
*
* @param uid Player UId.
* @return HInventory.
*/
@Nonnull
public static HInventory getByUID(@Nonnull UUID uid) {
return HInventoryHandler.findByUID(uid).orElseThrow(() -> new NullPointerException("this player doesn't have a inventory!"));
}

/**
* Creates builder with ID.
*
* @param id ID.
* @return HInventoryBuilder.
*/
@Nonnull
public static HInventoryBuilder builder(@Nonnull String id) {
return new HInventoryBuilder(id);
}
Expand Down
Loading

0 comments on commit f52d990

Please sign in to comment.