use of org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData in project core by CubeEngine.
the class EnchantMatcher method applyMatchedEnchantment.
public boolean applyMatchedEnchantment(ItemStack item, String enchName, int enchStrength, boolean force) {
EnchantmentType ench = this.enchantment(enchName);
if (ench == null)
return false;
if (enchStrength == 0) {
enchStrength = ench.getMaximumLevel();
}
Enchantment enchantment = Enchantment.builder().type(ench).level(enchStrength).build();
if (force) {
EnchantmentData data = item.getOrCreate(ENCHANTMENT_DATA).get();
data.enchantments().add(enchantment);
item.offer(data);
return true;
}
try {
EnchantmentData data = item.getOrCreate(EnchantmentData.class).get();
data.enchantments().add(enchantment);
item.offer(data);
return true;
} catch (IllegalArgumentException ignored) {
return false;
}
}
use of org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData in project Nucleus by NucleusPowered.
the class EnchantCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
// Check for item in hand
if (!src.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.noitem"));
return CommandResult.empty();
}
// Get the arguments
ItemStack itemInHand = src.getItemInHand(HandTypes.MAIN_HAND).get();
EnchantmentType enchantment = args.<EnchantmentType>getOne(enchantmentKey).get();
int level = args.<Integer>getOne(levelKey).get();
boolean allowUnsafe = args.hasAny("u");
boolean allowOverwrite = args.hasAny("o");
// Can we apply the enchantment?
if (!allowUnsafe) {
if (!enchantment.canBeAppliedToStack(itemInHand)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.enchant", itemInHand.getTranslation().get()));
return CommandResult.empty();
}
if (level > enchantment.getMaximumLevel()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.level", itemInHand.getTranslation().get()));
return CommandResult.empty();
}
}
// We know this should exist.
EnchantmentData ed = itemInHand.getOrCreate(EnchantmentData.class).get();
// Get all the enchantments.
List<Enchantment> currentEnchants = ed.getListValue().get();
List<Enchantment> enchantmentsToRemove = currentEnchants.stream().filter(x -> !x.getType().isCompatibleWith(enchantment) || x.getType().equals(enchantment)).collect(Collectors.toList());
if (!allowOverwrite && !enchantmentsToRemove.isEmpty()) {
// Build the list of the enchantment names, and send it.
final StringBuilder sb = new StringBuilder();
enchantmentsToRemove.forEach(x -> {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(Util.getTranslatableIfPresent(x.getType()));
});
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.overwrite", sb.toString()));
return CommandResult.empty();
}
// Remove all enchants that cannot co-exist.
currentEnchants.removeIf(enchantmentsToRemove::contains);
// Create the enchantment
currentEnchants.add(Enchantment.of(enchantment, level));
ed.setElements(currentEnchants);
// Offer it to the item.
DataTransactionResult dtr = itemInHand.offer(ed);
if (dtr.isSuccessful()) {
// If successful, we need to put the item in the player's hand for it to actually take effect.
src.setItemInHand(HandTypes.MAIN_HAND, itemInHand);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.success", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
return CommandResult.success();
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.error", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
return CommandResult.empty();
}
Aggregations