2020-03-01 18:19:59 +00:00
|
|
|
package com.thebrokenrail.sorcerycraft.item;
|
|
|
|
|
|
|
|
import com.thebrokenrail.sorcerycraft.SorceryCraft;
|
|
|
|
import com.thebrokenrail.sorcerycraft.entity.SpellEntity;
|
2020-03-08 17:45:15 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
|
2020-03-21 02:05:32 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry;
|
|
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellHelper;
|
2020-03-01 18:19:59 +00:00
|
|
|
import net.minecraft.client.item.TooltipContext;
|
|
|
|
import net.minecraft.entity.Entity;
|
2020-03-21 02:05:32 +00:00
|
|
|
import net.minecraft.entity.LivingEntity;
|
2020-03-01 18:19:59 +00:00
|
|
|
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;
|
2020-03-02 22:11:37 +00:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.Hand;
|
2020-03-03 23:08:22 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-03-02 22:11:37 +00:00
|
|
|
import net.minecraft.util.Rarity;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
2020-03-18 00:42:02 +00:00
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
2020-03-01 18:19:59 +00:00
|
|
|
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
|
2020-03-02 22:11:37 +00:00
|
|
|
public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
|
2020-03-01 18:19:59 +00:00
|
|
|
ItemStack itemStack = playerEntity.getStackInHand(hand);
|
|
|
|
|
2020-03-04 01:22:40 +00:00
|
|
|
if (!world.isClient()) {
|
2020-03-11 21:34:52 +00:00
|
|
|
SorceryCraft.playSpellSound(playerEntity);
|
|
|
|
|
2020-03-18 00:42:02 +00:00
|
|
|
playerEntity.incrementStat(SorceryCraft.CAST_SPELL_STAT);
|
2020-03-10 21:32:00 +00:00
|
|
|
|
2020-03-04 01:22:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-04 01:22:40 +00:00
|
|
|
if (!playerEntity.isCreative()) {
|
|
|
|
itemStack.decrement(1);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
2020-03-04 01:22:40 +00:00
|
|
|
|
|
|
|
return new TypedActionResult<>(ActionResult.SUCCESS, itemStack);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 02:05:32 +00:00
|
|
|
@Override
|
|
|
|
public boolean useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) {
|
|
|
|
use(user.getEntityWorld(), user, hand);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-01 18:19:59 +00:00
|
|
|
@Override
|
|
|
|
public boolean hasEnchantmentGlint(ItemStack stack) {
|
2020-03-21 02:05:32 +00:00
|
|
|
Map<Identifier, Integer> spells = SpellHelper.getSpells(stack);
|
2020-03-07 20:12:46 +00:00
|
|
|
return spells.size() > 0 || super.hasEnchantmentGlint(stack);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) {
|
2020-03-21 02:05:32 +00:00
|
|
|
Map<Identifier, Integer> spells = SpellHelper.getSpells(itemStack);
|
2020-03-03 23:08:22 +00:00
|
|
|
for (Map.Entry<Identifier, Integer> entry : spells.entrySet()) {
|
2020-03-21 02:05:32 +00:00
|
|
|
tooltip.add(SpellHelper.getTranslatedSpell(entry.getKey(), entry.getValue()));
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2020-03-03 23:08:22 +00:00
|
|
|
Map<Identifier, Integer> spell = new HashMap<>();
|
2020-03-01 18:19:59 +00:00
|
|
|
spell.put(value.getID(), value.getLevel());
|
2020-03-21 02:05:32 +00:00
|
|
|
SpellHelper.setSpells(item, spell);
|
2020-03-01 18:19:59 +00:00
|
|
|
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;
|
2020-03-21 02:05:32 +00:00
|
|
|
Map<Identifier, Integer> itemSpells = SpellHelper.getSpells(player.inventory.getInvStack(slot));
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-21 02:05:32 +00:00
|
|
|
SpellHelper.learnSpells(player, itemSpells);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|