0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-20 01:12:13 +02:00

Add ItemUtils helper for adding enchantments

This commit is contained in:
Alexander01998 2023-10-31 19:43:30 +01:00
parent 068e096fb6
commit 3d7531b04c
2 changed files with 39 additions and 9 deletions

View File

@ -16,6 +16,7 @@ import net.wurstclient.command.CmdException;
import net.wurstclient.command.CmdSyntaxError;
import net.wurstclient.command.Command;
import net.wurstclient.util.ChatUtils;
import net.wurstclient.util.ItemUtils;
public final class EnchantCmd extends Command
{
@ -34,9 +35,7 @@ public final class EnchantCmd extends Command
if(args.length > 1)
throw new CmdSyntaxError();
ItemStack stack = getHeldItem();
enchant(stack);
enchant(getHeldItem(), 127);
ChatUtils.message("Item enchanted.");
}
@ -53,23 +52,26 @@ public final class EnchantCmd extends Command
return stack;
}
private void enchant(ItemStack stack)
private void enchant(ItemStack stack, int level)
{
for(Enchantment enchantment : Registries.ENCHANTMENT)
{
if(enchantment == Enchantments.SILK_TOUCH)
continue;
// Skip curses
if(enchantment.isCursed())
continue;
// Skip Silk Touch so it doesn't remove Fortune
if(enchantment == Enchantments.SILK_TOUCH)
continue;
// Limit Quick Charge to level 5 so it doesn't break
if(enchantment == Enchantments.QUICK_CHARGE)
{
stack.addEnchantment(enchantment, 5);
stack.addEnchantment(enchantment, Math.min(level, 5));
continue;
}
stack.addEnchantment(enchantment, 127);
ItemUtils.addEnchantment(stack, enchantment, level);
}
}

View File

@ -7,10 +7,16 @@
*/
package net.wurstclient.util;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolItem;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
@ -56,4 +62,26 @@ public enum ItemUtils
.get(EntityAttributes.GENERIC_ATTACK_SPEED).stream().findFirst()
.orElseThrow().getValue();
}
/**
* Adds the specified enchantment to the specified item stack. Unlike
* {@link ItemStack#addEnchantment(Enchantment, int)}, this method doesn't
* limit the level to 127.
*/
public static void addEnchantment(ItemStack stack, Enchantment enchantment,
int level)
{
Identifier id = EnchantmentHelper.getEnchantmentId(enchantment);
NbtList nbt = getOrCreateNbtList(stack, ItemStack.ENCHANTMENTS_KEY);
nbt.add(EnchantmentHelper.createNbt(id, level));
}
public static NbtList getOrCreateNbtList(ItemStack stack, String key)
{
NbtCompound nbt = stack.getOrCreateNbt();
if(!nbt.contains(key, NbtElement.LIST_TYPE))
nbt.put(key, new NbtList());
return nbt.getList(key, NbtElement.COMPOUND_TYPE);
}
}