This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/item/SpellItem.java

106 lines
3.8 KiB
Java

package com.thebrokenrail.sorcerycraft.item;
import com.thebrokenrail.sorcerycraft.SorceryCraft;
import com.thebrokenrail.sorcerycraft.entity.SpellEntity;
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry;
import com.thebrokenrail.sorcerycraft.spell.util.SpellHelper;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpellItem extends Item {
public SpellItem() {
super(new Item.Settings().group(SorceryCraft.ITEM_GROUP).rarity(Rarity.UNCOMMON).maxCount(16));
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
ItemStack itemStack = playerEntity.getStackInHand(hand);
if (!world.isClient()) {
SorceryCraft.playSpellSound(playerEntity);
playerEntity.incrementStat(SorceryCraft.CAST_SPELL_STAT);
SpellEntity entity = new SpellEntity(world, playerEntity);
entity.setItem(itemStack);
entity.setProperties(playerEntity, playerEntity.pitch, playerEntity.yaw, 0.0f, 1.5f, 1.0f);
world.spawnEntity(entity);
}
if (!playerEntity.isCreative()) {
itemStack.decrement(1);
}
return new TypedActionResult<>(ActionResult.SUCCESS, itemStack);
}
@Override
public boolean useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) {
use(user.getEntityWorld(), user, hand);
return true;
}
@Override
public boolean hasEnchantmentGlint(ItemStack stack) {
Map<Identifier, Integer> spells = SpellHelper.getSpells(stack);
return spells.size() > 0 || super.hasEnchantmentGlint(stack);
}
@Override
public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) {
Map<Identifier, Integer> spells = SpellHelper.getSpells(itemStack);
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
tooltip.add(SpellHelper.getTranslatedSpell(entry.getKey(), entry.getValue()));
}
}
@Override
public void appendStacks(ItemGroup group, DefaultedList<ItemStack> stacks) {
if (isIn(group)) {
stacks.add(new ItemStack(this));
Spell[] spells = SpellRegistry.getSpells();
for (Spell value : spells) {
ItemStack item = new ItemStack(this);
Map<Identifier, Integer> spell = new HashMap<>();
spell.put(value.getID(), value.getLevel());
SpellHelper.setSpells(item, spell);
stacks.add(item);
}
}
}
@Override
public boolean isEnchantable(ItemStack stack) {
return false;
}
@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
super.inventoryTick(stack, world, entity, slot, selected);
if (!world.isClient() && entity instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) entity;
Map<Identifier, Integer> itemSpells = SpellHelper.getSpells(player.inventory.getInvStack(slot));
SpellHelper.learnSpells(player, itemSpells);
}
}
}