Skip to content

Commit

Permalink
Follow @Rollczi feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Oct 14, 2024
1 parent daa5dbc commit f724ebd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.util.InventoryUtil;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import java.util.Optional;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

@Service
public class GiveService {
Expand All @@ -29,64 +28,62 @@ public GiveService(PluginConfiguration pluginConfiguration, NoticeService notice
}

public void giveItem(Player player, Material material) {
if (this.isInvalidMaterial(material, player)) {
return;
}

this.giveItems(player, material, this.defaultGiveAmount, Optional.empty());
}

public void giveItem(Player player, Material material, int amount) {
if (this.isInvalidMaterial(material, player)) {
return;
}

this.giveItems(player, material, amount, Optional.empty());
}

public void giveItem(Player player, Material material, int amount, Enchantment enchantment, int level) {
if (this.isInvalidMaterial(material, player)) {
this.giveItems(player, material, amount, Optional.of(new EnchantmentLevelPair(enchantment, level)));
}

private void giveItems(
Player player,
Material material,
int amount,
Optional<EnchantmentLevelPair> enchantmentLevel
) {
if (this.isInvalidMaterial(material)) {
this.noticeService.create()
.notice(translation -> translation.item().giveNotItem())
.player(player.getUniqueId())
.send();
return;
}

this.giveItems(player, material, amount, Optional.of(new EnchantmentLevelPair(enchantment, level)));
this.giveItemStack(player, material, amount, enchantmentLevel);
}

private void giveItems(
Player player,
Material material,
int amount,
Optional<EnchantmentLevelPair> enchantmentLevel
private boolean isInvalidMaterial(Material material) {
return !material.isItem();
}

private void giveItemStack(
Player player,
Material material,
int amount,
Optional<EnchantmentLevelPair> enchantmentLevel
) {
int maxStackSize = material.getMaxStackSize();
int fullStacks = amount / maxStackSize;
int remainder = amount % maxStackSize;
int rest = amount % maxStackSize;

for (int i = 0; i < fullStacks; i++) {
this.giveItemStack(player, material, maxStackSize, enchantmentLevel);
this.giveSingleStack(player, material, maxStackSize, enchantmentLevel);
}

if (remainder > 0) {
this.giveItemStack(player, material, remainder, enchantmentLevel);
if (rest > 0) {
this.giveSingleStack(player, material, rest, enchantmentLevel);
}
}

private boolean isInvalidMaterial(Material material, Player player) {
if (!material.isItem()) {
this.noticeService.create()
.notice(translation -> translation.item().giveNotItem())
.player(player.getUniqueId())
.send();
return true;
}
return false;
}

private void giveItemStack(
Player player,
Material material,
int amount,
Optional<EnchantmentLevelPair> enchantmentLevel
private void giveSingleStack(
Player player,
Material material,
int amount,
Optional<EnchantmentLevelPair> enchantmentLevel
) {
ItemBuilder itemBuilder = ItemBuilder.from(material).amount(amount);

Expand All @@ -98,43 +95,22 @@ private void giveItemStack(

boolean dropOnFullInventory = this.pluginConfiguration.items.dropOnFullInventory;

if (!hasSpace(player.getInventory(), itemBuilder.build())) {
if (!InventoryUtil.hasSpace(player.getInventory(), itemBuilder.build())) {
if (dropOnFullInventory) {
player.getWorld().dropItemNaturally(player.getLocation(), itemBuilder.build());
return;
}

this.noticeService.create()
.notice(translation -> translation.item().giveNoSpace())
.player(player.getUniqueId())
.send();
.notice(translation -> translation.item().giveNoSpace())
.player(player.getUniqueId())
.send();
return;
}

player.getInventory().addItem(itemBuilder.build());
}

private static boolean hasSpace(Inventory inventory, ItemStack itemStack) {
if (inventory.firstEmpty() != -1) {
return true;
}

for (ItemStack contents : inventory.getContents()) {
if (contents == null) {
continue;
}

if (!contents.isSimilar(itemStack)) {
continue;
}

if (contents.getMaxStackSize() > contents.getAmount()) {
return true;
}
}

return false;
}

private record EnchantmentLevelPair(Enchantment enchantment, int level) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.eternalcode.core.util;

import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public final class InventoryUtil {
public InventoryUtil() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static boolean hasSpace(Inventory inventory, ItemStack itemStack) {
if (inventory.firstEmpty() != -1) {
return true;
}

for (ItemStack contents : inventory.getContents()) {
if (contents == null) {
continue;
}

if (!contents.isSimilar(itemStack)) {
continue;
}

if (contents.getMaxStackSize() > contents.getAmount()) {
return true;
}
}

return false;
}
}

0 comments on commit f724ebd

Please sign in to comment.