Skip to content

Commit

Permalink
Add few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Apr 23, 2022
1 parent 87a7af0 commit 0c70724
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public DropCommand(MessageParser messageParser, MenuService menuService) {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
String[] args) {
// Check if sender is console.
if (!(sender instanceof Player)) {
sender.sendMessage(this.messageParser.getExecutedAsConsole());
return true;
Expand All @@ -48,6 +49,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

// Try to open the inventory.
if (!this.menuService.open(player)) {
player.sendMessage(this.messageParser.getMenuOpenError());
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public void onSourceBreak(BlockBreakEvent event) {

Material source = event.getBlock().getType();

// Get optional drops from source blocks.
Set<Item> sourceDrops = this.itemCache.getDrops(source);
if (sourceDrops.isEmpty()) {
return;
}

// Get user from the cache.
Optional<User> userMaybe = this.userCache.getUser(player.getUniqueId());
if (!userMaybe.isPresent()) {
this.logger.severe("User " + player.getName() + " is not added to cached users.");
Expand All @@ -67,6 +69,7 @@ public void onSourceBreak(BlockBreakEvent event) {

User user = userMaybe.get();

// Remove all disabled drops from the source drops list.
sourceDrops.removeIf(sourceDrop -> user.getDisabledDrops().contains(sourceDrop));

ThreadLocalRandom random = ThreadLocalRandom.current();
Expand Down Expand Up @@ -97,6 +100,7 @@ public void onSourceBreak(BlockBreakEvent event) {

Map<Integer, ItemStack> itemsLeft = player.getInventory().addItem(droppedItem);

// Drop items on the floor if player has full inventory.
itemsLeft.forEach((key, value) ->
player.getWorld().dropItemNaturally(player.getEyeLocation(), value));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public List<Item> parseMany(ConfigurationSection section) throws InvalidConfigur

List<Item> items = new ArrayList<>();

// Add each item section to items list.
section.getKeys(false).forEach(key -> {
try {
items.add(this.parse(section.getConfigurationSection(key)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public List<MenuItem> parseMany(ConfigurationSection section) throws InvalidConf

List<MenuItem> items = new ArrayList<>();

// Add each item section to items list.
section.getKeys(false).forEach(key -> {
try {
items.add(this.parse(section.getConfigurationSection(key)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public MenuService(Logger logger, Menu menu, UserCache userCache) {
}

public boolean open(Player player) {
// Try getting user from the cache.
Optional<User> userMaybe = this.userCache.getUser(player.getUniqueId());
if (!userMaybe.isPresent()) {
this.logger.severe("User " + player.getName() + " is not added to cached users.");
Expand All @@ -55,13 +56,15 @@ public boolean open(Player player) {

Gui menu = new Gui(this.menu.getRows(), this.menu.getTitle(), InteractionModifier.VALUES);

// Forbid the player from getting the item from the inventory.
menu.setDefaultClickAction(event -> event.setCancelled(true));

this.menu.getItems().forEach(item -> {
Entry<Integer, Integer> slot = item.getSlot();
int row = slot.getKey();
int column = slot.getValue();

// Set item that does not have drop item assigned to it.
Item dropItem = item.getDropItem();
if (dropItem == null) {
menu.setItem(row, column, ItemBuilder.from(item.getType())
Expand All @@ -82,6 +85,7 @@ public boolean open(Player player) {

Entry<String, String> amountFormat = this.menu.getAmountFormat();

// Set lore depending on the amount setting.
List<String> lore = item.getLore().stream()
.map(line -> {
String finalAmount;
Expand Down Expand Up @@ -111,6 +115,7 @@ public boolean open(Player player) {
.collect(Collectors.toList()))
.asGuiItem();

// Perform specific actions when player clicks the item.
menuItem.setAction(event -> {
if (item.getAction() == MenuAction.CLOSE_MENU) {
menu.close(player, true);
Expand All @@ -135,7 +140,7 @@ public boolean open(Player player) {
menu.setItem(row, column, menuItem);
});


// Fill the rest inventory with the specified item if enabled.
MenuFiller filler = this.menu.getFiller();
if (filler.isEnabled()) {
ItemStack fillerItem = new ItemStack(filler.getType());
Expand Down

0 comments on commit 0c70724

Please sign in to comment.