All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
281 lines
10 KiB
Java
281 lines
10 KiB
Java
package net.knarcraft.blacksmith.container;
|
|
|
|
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
|
import net.knarcraft.blacksmith.manager.EconomyManager;
|
|
import net.md_5.bungee.api.ChatColor;
|
|
import org.bukkit.NamespacedKey;
|
|
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;
|
|
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.Optional;
|
|
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) implements ConfigurationSerializable {
|
|
|
|
/**
|
|
* Changes the monetary cost of this action
|
|
*
|
|
* @param monetaryCost <p>The new monetary cost</p>
|
|
* @return <p>The resulting action cost</p>
|
|
*/
|
|
public ActionCost changeMonetaryCost(double monetaryCost) {
|
|
return new ActionCost(monetaryCost, this.expCost, this.itemCost, this.requiredPermissions);
|
|
}
|
|
|
|
/**
|
|
* Changes the experience cost of this action
|
|
*
|
|
* @param expCost <p>The new experience cost</p>
|
|
* @return <p>The resulting action cost</p>
|
|
*/
|
|
public ActionCost changeExpCost(int expCost) {
|
|
return new ActionCost(this.monetaryCost, expCost, this.itemCost, this.requiredPermissions);
|
|
}
|
|
|
|
/**
|
|
* Changes the item cost of this action
|
|
*
|
|
* @param itemCost <p>The new item cost</p>
|
|
* @return <p>The resulting action cost</p>
|
|
*/
|
|
public ActionCost changeItemCost(@Nullable ItemStack itemCost) {
|
|
return new ActionCost(this.monetaryCost, this.expCost, itemCost, this.requiredPermissions);
|
|
}
|
|
|
|
/**
|
|
* Changes the permission cost of this action
|
|
*
|
|
* @param requiredPermissions <p>The new permission cost</p>
|
|
* @return <p>The resulting action cost</p>
|
|
*/
|
|
public ActionCost changePermissionCost(@NotNull Set<String> requiredPermissions) {
|
|
return new ActionCost(this.monetaryCost, this.expCost, this.itemCost, requiredPermissions);
|
|
}
|
|
|
|
/**
|
|
* Displays this action cost as a string
|
|
*
|
|
* @param player <p>The player to calculate the cost for</p>
|
|
* @return <p>The string representation of this action cost</p>
|
|
*/
|
|
@NotNull
|
|
public String displayCost(@NotNull Player player) {
|
|
StringBuilder builder = new StringBuilder();
|
|
if (monetaryCost > 0) {
|
|
builder.append(EconomyManager.format(monetaryCost)).append(", ").append("\n");
|
|
}
|
|
if (expCost > 0) {
|
|
builder.append(expCost).append("exp, ").append("\n");
|
|
}
|
|
if (itemCost != null) {
|
|
NamespacedKey itemName = itemCost.getType().getKeyOrNull();
|
|
if (itemName != null) {
|
|
builder.append(itemCost.getAmount()).append(" x ").append(itemName);
|
|
ItemMeta itemMeta = itemCost.getItemMeta();
|
|
if (itemMeta != null && itemMeta.hasDisplayName()) {
|
|
builder.append("(").append(itemMeta.getDisplayName()).append(")");
|
|
}
|
|
if (itemMeta != null && itemMeta.hasLore() && itemMeta.getLore() != null) {
|
|
for (String lore : itemMeta.getLore()) {
|
|
builder.append("\n").append(lore);
|
|
}
|
|
}
|
|
builder.append("\n");
|
|
}
|
|
}
|
|
if (!requiredPermissions().isEmpty()) {
|
|
for (String permission : requiredPermissions()) {
|
|
if (player.hasPermission(permission)) {
|
|
builder.append(ChatColor.DARK_GREEN).append("O ").append(permission).append("\n");
|
|
} else {
|
|
builder.append(ChatColor.DARK_RED).append("X ").append(permission).append("\n");
|
|
}
|
|
}
|
|
}
|
|
return builder.toString();
|
|
}
|
|
|
|
/**
|
|
* 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 : this.requiredPermissions) {
|
|
if (!player.hasPermission(permission)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (player.getExp() < this.expCost || !EconomyManager.hasEnough(player, this.monetaryCost)) {
|
|
return false;
|
|
}
|
|
|
|
return hasEnoughValidItemsInInventory(player);
|
|
}
|
|
|
|
/**
|
|
* Takes exp, money and items from the player, according to this cost
|
|
*
|
|
* @param player <p>The player to take the payment from</p>
|
|
*/
|
|
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 = 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;
|
|
}
|
|
|
|
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(), 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, 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 {
|
|
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 >= 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;
|
|
}
|
|
|
|
}
|