Finishes the ActionCost implementation
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package net.knarcraft.blacksmith.container;
|
||||
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.manager.EconomyManager;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
@@ -8,11 +11,7 @@ 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;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* The cost of performing an action
|
||||
@@ -23,7 +22,24 @@ import java.util.Set;
|
||||
* @param requiredPermissions <p>The permission required for the action</p>
|
||||
*/
|
||||
public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack itemCost,
|
||||
@NotNull Set<String> requiredPermissions) {
|
||||
@NotNull Set<String> requiredPermissions) implements ConfigurationSerializable {
|
||||
|
||||
@NotNull
|
||||
public String displayCost() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (monetaryCost > 0) {
|
||||
builder.append(EconomyManager.format(monetaryCost)).append(", ");
|
||||
}
|
||||
if (expCost > 0) {
|
||||
builder.append(expCost).append("exp, ");
|
||||
}
|
||||
if (itemCost != null) {
|
||||
// TODO: Present name, amount and name + lore
|
||||
builder.append(itemCost);
|
||||
}
|
||||
// TODO: Display required permissions if the player doesn't have them?
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given player is able to pay this action cost
|
||||
@@ -32,68 +48,161 @@ public record ActionCost(double monetaryCost, int expCost, @Nullable ItemStack i
|
||||
* @return <p>True if the player is able to pay</p>
|
||||
*/
|
||||
public boolean canPay(@NotNull Player player) {
|
||||
for (String permission : requiredPermissions) {
|
||||
for (String permission : this.requiredPermissions) {
|
||||
if (!player.hasPermission(permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (player.getExp() < expCost || !EconomyManager.hasEnough(player, monetaryCost)) {
|
||||
if (player.getExp() < this.expCost || !EconomyManager.hasEnough(player, this.monetaryCost)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return itemCost == null || player.getInventory().contains(itemCost);
|
||||
}
|
||||
|
||||
public void takePayment(@NotNull Player player) {
|
||||
player.giveExp(-expCost);
|
||||
EconomyManager.withdraw(monetaryCost);
|
||||
|
||||
return hasEnoughValidItemsInInventory(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes payment for printing a number of books by withdrawing the correct item
|
||||
* Takes exp, money and items from the player, according to this cost
|
||||
*
|
||||
* @param player <p>The player which needs to pay</p>
|
||||
* @param item <p>The item to pay</p>
|
||||
* @param player <p>The player to take the payment from</p>
|
||||
*/
|
||||
private static void payForBookPrintingItem(Player player, ItemStack item) {
|
||||
public void takePayment(@NotNull Player player) {
|
||||
player.giveExp(-expCost);
|
||||
EconomyManager.withdraw(player, monetaryCost);
|
||||
takeItemCost(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given player has enough items specified as the item cost
|
||||
*
|
||||
* @param player <p>The player to check</p>
|
||||
* @return <p>True if the player has enough items in their inventory</p>
|
||||
*/
|
||||
private boolean hasEnoughValidItemsInInventory(@NotNull Player player) {
|
||||
if (this.itemCost == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int amountInInventory = 0;
|
||||
for (Map.Entry<Integer, Integer> entry : getValidItems(player).entrySet()) {
|
||||
amountInInventory += entry.getValue();
|
||||
}
|
||||
return this.itemCost.getAmount() >= amountInInventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all items in a player's inventory equal to the specified item
|
||||
*
|
||||
* @param player <p>The player to get valid items for</p>
|
||||
* @return <p>All valid items in the format: Inventory id -> Amount</p>
|
||||
*/
|
||||
@NotNull
|
||||
private Map<Integer, Integer> getValidItems(@NotNull Player player) {
|
||||
if (this.itemCost == null) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
ItemMeta targetMeta = item.getItemMeta();
|
||||
ItemMeta targetMeta = this.itemCost.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());
|
||||
|
||||
Map<Integer, Integer> output = new HashMap<>();
|
||||
HashMap<Integer, ? extends ItemStack> itemsOfType = playerInventory.all(this.itemCost.getType());
|
||||
|
||||
for (Map.Entry<Integer, ? extends ItemStack> entry : itemsOfType.entrySet()) {
|
||||
if (targetMeta != null) {
|
||||
// Only consider item if the name and lore is the same
|
||||
ItemMeta meta = entry.getValue().getItemMeta();
|
||||
if (meta == null || (displayName != null && (!meta.hasDisplayName() ||
|
||||
!meta.getDisplayName().equals(displayName)) || lore != null && (!meta.hasLore() ||
|
||||
meta.getLore() == null || !meta.getLore().equals(lore)))) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
output.put(entry.getKey(), (Integer) entry.getValue().getAmount());
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the amount of items specified as the cost for this action
|
||||
*
|
||||
* @param player <p>The player to take the items from</p>
|
||||
*/
|
||||
private void takeItemCost(@NotNull Player player) {
|
||||
if (this.itemCost == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
for (Map.Entry<Integer, Integer> entry : getValidItems(player).entrySet()) {
|
||||
int inventory = entry.getKey();
|
||||
int amount = entry.getValue();
|
||||
if (amount <= this.itemCost.getAmount() - clearedAmount) {
|
||||
clearedAmount += amount;
|
||||
player.getInventory().clear(entry.getKey());
|
||||
} 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;
|
||||
clearedAmount = this.itemCost.getAmount();
|
||||
ItemStack item = player.getInventory().getItem(inventory);
|
||||
if (item != null) {
|
||||
item.setAmount(amount - clearedAmount);
|
||||
} else {
|
||||
BlacksmithPlugin.error("An item changed after calculating item cost. Was unable to take " +
|
||||
amount + " items");
|
||||
}
|
||||
}
|
||||
|
||||
if (clearedAmount <= item.getAmount()) {
|
||||
if (clearedAmount >= this.itemCost.getAmount()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads an action cost from a configuration section
|
||||
*
|
||||
* @param configurationSection <p>The configuration section to load from</p>
|
||||
* @param key <p>The key of the cost to load</p>
|
||||
* @return <p>The loaded cost</p>
|
||||
*/
|
||||
private static ActionCost loadActionCost(@NotNull ConfigurationSection configurationSection, @NotNull String key) {
|
||||
double cost = configurationSection.getDouble(key, Double.MIN_VALUE);
|
||||
if (cost != Double.MIN_VALUE) {
|
||||
return new ActionCost(cost, 0, null, Set.of());
|
||||
} else {
|
||||
return (ActionCost) configurationSection.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes an action cost
|
||||
*
|
||||
* @param serialized <p>The serialized action cost</p>
|
||||
* @return <p>The deserialized action cost</p>
|
||||
*/
|
||||
@SuppressWarnings({"unused", "unchecked"})
|
||||
public static ActionCost deserialize(Map<String, Object> serialized) {
|
||||
double monetaryCost = (double) serialized.get("monetaryCost");
|
||||
int expCost = (int) serialized.get("expCost");
|
||||
ItemStack itemCost = (ItemStack) serialized.get("itemCost");
|
||||
Set<String> requiredPermissions = (Set<String>) serialized.get("requiredPermissions");
|
||||
return new ActionCost(monetaryCost, expCost, itemCost, requiredPermissions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Object> serialize() {
|
||||
Map<String, Object> serialized = new HashMap<>();
|
||||
serialized.put("monetaryCost", Optional.of(this.monetaryCost));
|
||||
serialized.put("expCost", Optional.of(this.expCost));
|
||||
serialized.put("itemCost", itemCost);
|
||||
serialized.put("requiredPermissions", this.requiredPermissions);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user