Broken commit implementing a lot of book-splitting code
Some checks failed
EpicKnarvik97/Blacksmith/pipeline/head There was a failure building this commit

This commit is contained in:
2024-11-27 12:16:57 +01:00
parent 90d3c49c12
commit e3dbeccc14
11 changed files with 438 additions and 13 deletions

View File

@@ -0,0 +1,99 @@
package net.knarcraft.blacksmith.container;
import net.knarcraft.blacksmith.manager.EconomyManager;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* The cost of performing an action
*
* @param monetaryCost <p>The monetary cost of the action</p>
* @param expCost <p>The experience cost of the action</p>
* @param itemCost <p>The item-based cost of the action</p>
* @param requiredPermissions <p>The permission required for the action</p>
*/
public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack itemCost,
@NotNull Set<String> requiredPermissions) {
/**
* Checks whether the given player is able to pay this action cost
*
* @param player <p>The player to check</p>
* @return <p>True if the player is able to pay</p>
*/
public boolean canPay(@NotNull Player player) {
for (String permission : requiredPermissions) {
if (!player.hasPermission(permission)) {
return false;
}
}
if (player.getExp() < expCost || !EconomyManager.hasEnough(player, monetaryCost)) {
return false;
}
return itemCost == null || player.getInventory().contains(itemCost);
}
public void takePayment(@NotNull Player player) {
player.giveExp(-expCost);
EconomyManager.withdraw(monetaryCost);
}
/**
* Takes payment for printing a number of books by withdrawing the correct item
*
* @param player <p>The player which needs to pay</p>
* @param item <p>The item to pay</p>
*/
private static void payForBookPrintingItem(Player player, ItemStack item) {
PlayerInventory playerInventory = player.getInventory();
ItemMeta targetMeta = item.getItemMeta();
String displayName = null;
List<String> lore = null;
if (targetMeta != null) {
displayName = targetMeta.hasDisplayName() ? targetMeta.getDisplayName() : null;
lore = targetMeta.hasLore() ? targetMeta.getLore() : null;
}
HashMap<Integer, ? extends ItemStack> itemsOfType = playerInventory.all(item.getType());
int clearedAmount = 0;
for (Map.Entry<Integer, ? extends ItemStack> entry : itemsOfType.entrySet()) {
if (targetMeta == null) {
if (Objects.requireNonNull(entry.getValue()).getAmount() <= item.getAmount() - clearedAmount) {
clearedAmount += entry.getValue().getAmount();
player.getInventory().clear(entry.getKey());
} else {
clearedAmount = item.getAmount();
entry.getValue().setAmount(entry.getValue().getAmount() - (clearedAmount));
}
} else {
// TODO: Only consider item if the name and lore is the same
ItemMeta meta = entry.getValue().getItemMeta();
if (meta == null) {
break;
}
if (displayName != null && (!meta.hasDisplayName() || !meta.getDisplayName().equals(displayName))) {
break;
}
}
if (clearedAmount <= item.getAmount()) {
break;
}
}
}
}