Skip to content

Commit

Permalink
feat: add PlayerEnchantItemEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 3, 2024
1 parent 8f54607 commit 37e6541
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.allaymc.api.eventbus.event.player;

import lombok.Getter;
import lombok.Setter;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.eventbus.event.CancellableEvent;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.enchantment.EnchantmentInstance;

import java.util.List;

/**
* @author daoge_cmd
*/
@Getter
public class PlayerEnchantItemEvent extends PlayerEvent implements CancellableEvent {

protected ItemStack itemStack;
@Setter
protected List<EnchantmentInstance> enchantments;
@Setter
protected int requiredLapisLazuliCount;

public PlayerEnchantItemEvent(EntityPlayer player, ItemStack itemStack, List<EnchantmentInstance> enchantments, int requiredLapisLazuliCount) {
super(player);
this.itemStack = itemStack;
this.enchantments = enchantments;
this.requiredLapisLazuliCount = requiredLapisLazuliCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.allaymc.api.container.FullContainerType;
import org.allaymc.api.container.impl.CraftingContainer;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.eventbus.event.player.PlayerEnchantItemEvent;
import org.allaymc.api.item.enchantment.EnchantmentInstance;
import org.allaymc.api.item.interfaces.ItemAirStack;
import org.allaymc.api.item.type.ItemTypes;
import org.allaymc.api.registry.Registries;
Expand Down Expand Up @@ -51,23 +53,34 @@ protected ActionResponse handleEnchantTableRecipe(EntityPlayer player, int recip
return error();
}

List<EnchantmentInstance> enchantments = data.enchantments();
int requiredLapisLazuliCount = data.requiredLapisLazuliCount();
int requiredXpLevel = data.requiredXpLevel();
var event = new PlayerEnchantItemEvent(player, inputItem, enchantments, requiredLapisLazuliCount);
event.call();
if (event.isCancelled()) {
return error();
}
enchantments = event.getEnchantments();
requiredLapisLazuliCount = event.getRequiredLapisLazuliCount();

if (player.getGameType() != GameType.CREATIVE) {
var material = enchantTableContainer.getMaterial();
if (material.getItemType() != ItemTypes.LAPIS_LAZULI || material.getCount() < data.requiredLapisLazuliCount()) {
log.warn("Not enough lapis lazuli! Need: {}, Current: {}", data.requiredLapisLazuliCount(), enchantTableContainer.getMaterial().getCount());
if (material.getItemType() != ItemTypes.LAPIS_LAZULI || material.getCount() < requiredLapisLazuliCount) {
log.warn("Not enough lapis lazuli! Need: {}, Current: {}", requiredLapisLazuliCount, enchantTableContainer.getMaterial().getCount());
return error();
}

if (player.getExperienceLevel() < data.requiredXpLevel()) {
log.warn("Not enough experience level! Need: {}, Current: {}", data.requiredXpLevel(), player.getExperienceLevel());
if (player.getExperienceLevel() < requiredXpLevel) {
log.warn("Not enough experience level! Need: {}, Current: {}", requiredXpLevel, player.getExperienceLevel());
return error();
}
// Required lapis lazuli count is also the cost of xp level
player.setExperienceLevel(player.getExperienceLevel() - data.requiredLapisLazuliCount());
player.setExperienceLevel(player.getExperienceLevel() - requiredLapisLazuliCount);
}

var enchantedItem = inputItem.copy(true);
enchantedItem.addEnchantments(data.enchantments());
enchantedItem.addEnchantments(enchantments);
// Copy the enchanted item to CREATED_OUTPUT, and client will send a PlaceAction
// to move the enchanted item back to the input slot of the enchant table container
player.getContainer(FullContainerType.CREATED_OUTPUT).setItemStack(0, enchantedItem);
Expand Down

0 comments on commit 37e6541

Please sign in to comment.