Skip to content

Commit

Permalink
Fix Blacksmit Max Enchants. (#852)
Browse files Browse the repository at this point in the history
Change the method used to dictate if you can add an enchantment to a CEItem.
  • Loading branch information
TrueDarkLord authored Sep 4, 2024
1 parent c6a6ca8 commit 1cd88c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,22 @@ public boolean canAddEnchantment(@NotNull Player player, @NotNull ItemStack item
Math.min(getPlayerBaseEnchantments(player) - getEnchantmentLimiter(item), getPlayerMaxEnchantments(player));
}

/**
* Checks if the player can add more enchants to the current item based on set limits without the enchant limiter.
* @param player The {@link Player} that has the item.
* @param cEnchantments The amount of crazy enchants on the item.
* @param vanillaEnchantments The amount of vanilla enchantments on the item.
* @return True if they are able to add more enchants.
*/
public boolean canAddEnchantment(@NotNull Player player, int cEnchantments, int vanillaEnchantments) {
if (!this.maxEnchantmentCheck || player.hasPermission("crazyenchantments.bypass.limit")) return true;

int enchantAmount = cEnchantments;
if (this.checkVanillaLimit) enchantAmount += vanillaEnchantments;

return enchantAmount < getPlayerMaxEnchantments(player);
}

public int randomLevel(CEnchantment enchantment, Category category) {
int enchantmentMax = enchantment.getMaxLevel(); // Max set by the enchantment.
int randomLevel = 1 + new Random().nextInt(enchantmentMax);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) {

if (enchantment.canEnchantItem(subItem) && subCE.hasVanillaEnchantment(enchantment)) {
if (level == subLevel && level < enchantment.getMaxLevel()) {
mainCE.setVanillaEnchantment(enchantment, level + 1);
mainCE.addVanillaEnchantment(enchantment, level + 1);
this.cost += BlackSmithManager.getLevelUp();
} else if (level < subLevel) {
mainCE.setVanillaEnchantment(enchantment, subLevel);
mainCE.addVanillaEnchantment(enchantment, subLevel);
this.cost += BlackSmithManager.getLevelUp();
}
}
Expand All @@ -72,10 +72,10 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) {

if (enchantment.canEnchantItem(subItem) && subCE.hasCEnchantment(enchantment)) {
if (level == subLevel && level < enchantment.getMaxLevel()) {
mainCE.setCEnchantment(enchantment, level + 1);
mainCE.addCEnchantment(enchantment, level + 1);
this.cost += BlackSmithManager.getLevelUp();
} else if (level < subLevel) {
mainCE.setCEnchantment(enchantment, subLevel);
mainCE.addCEnchantment(enchantment, subLevel);
this.cost += BlackSmithManager.getLevelUp();
}
}
Expand All @@ -85,17 +85,17 @@ public BlackSmithResult(Player player, ItemStack mainItem, ItemStack subItem) {
for (Entry<Enchantment, Integer> entry : compare.getNewVanillaEnchantments().entrySet()) {
Enchantment enchantment = entry.getKey();

if (enchantment.canEnchantItem(subItem) && crazyManager.canAddEnchantment(player, mainItem)) {
mainCE.setVanillaEnchantment(enchantment, entry.getValue());
if (enchantment.canEnchantItem(subItem) && mainCE.canAddEnchantment(player)) {
mainCE.addVanillaEnchantment(enchantment, entry.getValue());
this.cost += BlackSmithManager.getAddEnchantment();
}
}

for (Entry<CEnchantment, Integer> entry : compare.getNewCEnchantments().entrySet()) {
CEnchantment enchantment = entry.getKey();

if (enchantment.canEnchantItem(subItem) && crazyManager.canAddEnchantment(player, mainItem) && crazyManager.canAddEnchantment(player, subItem)) {
mainCE.setCEnchantment(enchantment, entry.getValue());
if (enchantment.canEnchantItem(mainItem) && mainCE.canAddEnchantment(player)) {
mainCE.addCEnchantment(enchantment, entry.getValue());
this.cost += BlackSmithManager.getAddEnchantment();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.badbones69.crazyenchantments.paper.api.CrazyManager;
import com.badbones69.crazyenchantments.paper.controllers.settings.EnchantmentBookSettings;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Map<Enchantment, Integer> getVanillaEnchantments() {
return this.vanillaEnchantments;
}

public void setVanillaEnchantment(Enchantment enchantment, int level) {
public void addVanillaEnchantment(Enchantment enchantment, int level) {
this.vanillaEnchantments.put(enchantment, level);
}

Expand All @@ -79,14 +80,18 @@ public Map<CEnchantment, Integer> getCEnchantments() {
return this.cEnchantments;
}

public void setCEnchantment(CEnchantment enchantment, int level) {
public void addCEnchantment(CEnchantment enchantment, int level) {
this.cEnchantments.put(enchantment, level);
}

public void removeCEnchantment(CEnchantment enchantment) {
this.cEnchantmentRemove.add(enchantment);
}


public boolean canAddEnchantment(Player player) {
return crazyManager.canAddEnchantment(player, this.cEnchantments.size(), this.vanillaEnchantments.size());
}

public ItemStack build() {
this.vanillaEnchantmentRemove.forEach(this.item::removeEnchantment);
this.vanillaEnchantments.keySet().forEach(enchantment -> this.item.addUnsafeEnchantment(enchantment, this.vanillaEnchantments.get(enchantment)));
Expand Down

0 comments on commit 1cd88c6

Please sign in to comment.