Jump to content

[1.16.4] ItemStackHandler/Capability Shared Inventory Problem


dun

Recommended Posts

Hello, I am a complete noob when it comes to Item Capabilities and Handlers, and I've just came across a very perplexing problem(to me)

I'm trying to make wands that cast spells depending on what Wand Focus is in the Wand's inventory, and I've succeeded! 

If you're using a single wand, but if you have multiple wands with different focuses in your inventory, that's a different story:

https://imgur.com/dFZgyx7

Here's the code for the Wand (please excuse the terrible coding):

package com.Polarice3.FireNBlood.items;

import com.Polarice3.FireNBlood.FireNBlood;
import com.Polarice3.FireNBlood.inventory.container.SoulItemContainer;
import com.Polarice3.FireNBlood.items.capability.SoulUsingItemCapability;
import com.Polarice3.FireNBlood.spells.*;
import com.Polarice3.FireNBlood.utils.RegistryHandler;
import com.Polarice3.FireNBlood.items.handler.SoulUsingItemHandler;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.container.SimpleNamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Rarity;
import net.minecraft.item.UseAction;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

import static com.Polarice3.FireNBlood.items.MagicFocusItem.FOCUS;

public class SoulWand extends Item{
    private static final String SOULUSE = "Soul Use";
    private static final String CASTTIME = "Cast Time";
    private static final String CURRENTFOCUS = "Focus";
    private int soulcost;
    private int duration;
    private int cooldown;
    private int cool;
    private SoundEvent castingsound;
    private Spells spell;

    public SoulWand() {
        super(new Properties().group(FireNBlood.TAB).maxStackSize(1).setNoRepair().rarity(Rarity.RARE));
    }

    @Override
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        LivingEntity livingEntity = (LivingEntity) entityIn;
        if (stack.getTag() == null){
            CompoundNBT compound = stack.getOrCreateTag();
            compound.putInt(SOULUSE, SoulUse(livingEntity));
            compound.putInt(CASTTIME, CastTime(livingEntity));
        }
        if (getFocus(stack) != ItemStack.EMPTY){
            this.ChangeFocus(stack);
            stack.getTag().putString(CURRENTFOCUS, FOCUS);
        } else {
            this.setSpellConditions(null);
            this.setSpell(null);
        }
        stack.getTag().putInt(SOULUSE, SoulUse(livingEntity));
        stack.getTag().putInt(CASTTIME, CastTime(livingEntity));
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
    }

    public boolean SoulDiscount(LivingEntity entityLiving){
        return entityLiving.getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == RegistryHandler.DARKROBE.get()
                || entityLiving.getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == RegistryHandler.NECROROBE.get();
    }

    public boolean SoulCostUp(LivingEntity entityLiving){
        return entityLiving.isPotionActive(RegistryHandler.SUMMONDOWN.get());
    }

    public boolean ReduceCastTime(LivingEntity entityLiving){
        return entityLiving.getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == RegistryHandler.DARKHELM.get()
                || entityLiving.getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == RegistryHandler.NECROHELM.get();
    }

    public int SoulUse(LivingEntity entityLiving){
        if (SoulCostUp(entityLiving)){
            int amp = Objects.requireNonNull(entityLiving.getActivePotionEffect(RegistryHandler.SUMMONDOWN.get())).getAmplifier() + 2;
            return SoulCost() * amp;
        } else if (SoulDiscount(entityLiving)){
            return SoulCost()/2;
        } else {
            return SoulCost();
        }
    }

    public int CastTime(LivingEntity entityLiving){
        if (ReduceCastTime(entityLiving)){
            return CastDuration()/2;
        } else {
            return CastDuration();
        }
    }

    public void onUse(World worldIn, LivingEntity livingEntityIn, ItemStack stack, int count) {
        if (!worldIn.isRemote) {
            SoundEvent soundevent = this.CastingSound();
            int CastTime = stack.getUseDuration() - count;
            if (CastTime == 1) {
                if (soundevent != null) {
                    worldIn.playSound(null, livingEntityIn.getPosX(), livingEntityIn.getPosY(), livingEntityIn.getPosZ(), soundevent, SoundCategory.PLAYERS, 0.5F, 1.0F);
                } else {
                    worldIn.playSound(null, livingEntityIn.getPosX(), livingEntityIn.getPosY(), livingEntityIn.getPosZ(), SoundEvents.ENTITY_EVOKER_PREPARE_ATTACK, SoundCategory.PLAYERS, 0.5F, 1.0F);
                }
            }
            if (this.getSpell() instanceof ChargingSpells){
                ++this.cool;
                if (this.cool > Cooldown()){
                    this.cool = 0;
                    this.MagicResults(stack, worldIn, livingEntityIn);
                }
            }
        }
    }

    public int getUseDuration(ItemStack stack) {
        if (stack.getTag() != null) {
            return stack.getTag().getInt(CASTTIME);
        } else {
            return this.CastDuration();
        }
    }

    @Nonnull
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.BOW;
    }

    @Nonnull
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        super.onItemUseFinish(stack, worldIn, entityLiving);
        if (!(getSpell() instanceof ChargingSpells)){
            this.MagicResults(stack, worldIn, entityLiving);
        }
        if (this.cool > 0){
            cool = 0;
        }
        return stack;
    }

    @Nonnull
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        if (!playerIn.isCrouching()) {
            if (this.getSpell() != null) {
                playerIn.setActiveHand(handIn);
                for (int i = 0; i < playerIn.world.rand.nextInt(35) + 10; ++i) {
                    double d = worldIn.rand.nextGaussian() * 0.2D;
                    playerIn.world.addParticle(ParticleTypes.ENTITY_EFFECT, playerIn.getPosX(), playerIn.getPosYEye(), playerIn.getPosZ(), d, d, d);
                }
            }
            return ActionResult.resultConsume(itemstack);
        } else {
            if (!worldIn.isRemote) {
                SimpleNamedContainerProvider provider = new SimpleNamedContainerProvider(
                        (id, inventory, player) -> new SoulItemContainer(id, inventory, SoulUsingItemHandler.get(itemstack), itemstack, handIn), getDisplayName(itemstack));
                NetworkHooks.openGui((ServerPlayerEntity) playerIn, provider, (buffer) -> buffer.writeBoolean(handIn == Hand.MAIN_HAND));
            }
            return ActionResult.resultPass(itemstack);
        }

    }

    public void ChangeFocus(ItemStack itemStack){
        if (getFocus(itemStack).getItem() == RegistryHandler.VEXINGFOCUS.get()) {
            this.setSpellConditions(new VexSpell());
            this.setSpell(new VexSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.BITINGFOCUS.get()) {
            this.setSpellConditions(new FangSpell());
            this.setSpell(new FangSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.ROARINGFOCUS.get()) {
            this.setSpellConditions(new RoarSpell());
            this.setSpell(new RoarSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.NECROTURGYFOCUS.get()) {
            this.setSpellConditions(new ZombieSpell());
            this.setSpell(new ZombieSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.OSSEOUSFOCUS.get()) {
            this.setSpellConditions(new SkeletonSpell());
            this.setSpell(new SkeletonSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.CRIPPLINGFOCUS.get()) {
            this.setSpellConditions(new CrippleSpell());
            this.setSpell(new CrippleSpell());
        } else
        if (getFocus(itemStack).getItem() == RegistryHandler.SPIDERLINGFOCUS.get()) {
            this.setSpellConditions(new SpiderlingSpell());
            this.setSpell(new SpiderlingSpell());
        }
    }

    public void setSpellConditions(Spells spell){
        if (spell != null) {
            this.soulcost = spell.SoulCost();
            this.duration = spell.CastDuration();
            this.castingsound = spell.CastingSound();
            if (spell instanceof ChargingSpells){
                this.cooldown = ((ChargingSpells) spell).Cooldown();
            }
        } else {
            this.soulcost = 0;
            this.duration = 0;
            this.cooldown = 0;
            this.castingsound = SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE;
        }
    }

    public void setSpell(Spells spell) {
        this.spell = spell;
    }

    public Spells getSpell(){
        return this.spell;
    }

    public int SoulCost() {
        return soulcost;
    }

    public int CastDuration() {
        return duration;
    }

    public int Cooldown() {
        return cooldown;
    }

    public SoundEvent CastingSound() {
        return castingsound;
    }

    public static ItemStack getFocus(ItemStack itemstack) {
        SoulUsingItemHandler handler = SoulUsingItemHandler.get(itemstack);
        return handler.getSlot();
    }

    public void MagicResults(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        ItemStack foundStack = ItemStack.EMPTY;
        PlayerEntity playerEntity = (PlayerEntity) entityLiving;
        for (int i = 0; i <= 9; i++) {
            ItemStack itemStack = playerEntity.inventory.getStackInSlot(i);
            if (!itemStack.isEmpty() && itemStack.getItem() == RegistryHandler.GOLDTOTEM.get()) {
                foundStack = itemStack;
                break;
            }
        }
        if (this.getSpell() != null && !foundStack.isEmpty() && GoldTotemItem.currentSouls(foundStack) >= SoulUse(entityLiving)) {
            GoldTotemItem.decreaseSouls(foundStack, SoulUse(entityLiving));
            this.getSpell().WandResult(worldIn, entityLiving);
        } else {
            worldIn.playSound(null, entityLiving.getPosX(), entityLiving.getPosY(), entityLiving.getPosZ(), SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.NEUTRAL, 1.0F, 1.0F);
            for(int i = 0; i < entityLiving.world.rand.nextInt(35) + 10; ++i) {
                double d = worldIn.rand.nextGaussian() * 0.2D;
                entityLiving.world.addParticle(ParticleTypes.CLOUD, entityLiving.getPosX(), entityLiving.getPosYEye(), entityLiving.getPosZ(), d, d, d);
            }
        }
    }

    @Override
    public CompoundNBT getShareTag(ItemStack stack) {
        CompoundNBT result = new CompoundNBT();
        CompoundNBT tag = super.getShareTag(stack);
        CompoundNBT cap = SoulUsingItemHandler.get(stack).serializeNBT();
        if (tag != null) {
            result.put("tag", tag);
        }
        if (cap != null) {
            result.put("cap", cap);
        }
        return result;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
        assert nbt != null;
        stack.setTag(nbt.getCompound("tag"));
        SoulUsingItemHandler.get(stack).deserializeNBT(nbt.getCompound("cap"));
    }

    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
        return super.shouldCauseReequipAnimation(oldStack, newStack, slotChanged) && slotChanged;
    }

    @Override
    @Nullable
    public ICapabilityProvider initCapabilities(@Nonnull ItemStack stack, @Nullable CompoundNBT nbt) {
        return new SoulUsingItemCapability(stack);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        super.addInformation(stack, worldIn, tooltip, flagIn);
        if (stack.getTag() != null) {
            int SoulUse = stack.getTag().getInt(SOULUSE);
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.cost", SoulUse));
        } else {
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.cost", SoulCost()));
        }
        if (getFocus(stack) != ItemStack.EMPTY){
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.focus", getFocus(stack).getItem().getName()));
        } else {
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.focus", "Empty"));
        }
    }
}

What's weird is that the Wands show the Focus Tooltip text correctly, but when debugging, they seemed to tag the newly added wand's focus instead. It does that whenever the other wand is anywhere in the player's inventory.

I made the ChangeFocus function every tick as when I tried to make it so that it's called everytime the Container menu is closed, it would take the next Right Click for it to properly update.

The ItemStackHandler:

package com.Polarice3.FireNBlood.items.handler;

import com.Polarice3.FireNBlood.items.MagicFocusItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;

public class SoulUsingItemHandler extends ItemStackHandler {
    private final ItemStack itemStack;
    private int slot;

    public SoulUsingItemHandler(ItemStack itemStack) {
        this.itemStack = itemStack;
    }

    public ItemStack extractItem() {
        return extractItem(slot, 1, false);
    }

    public ItemStack insertItem(ItemStack insert) {
        return insertItem(slot, insert, false);
    }

    public ItemStack getSlot() {
        return getStackInSlot(slot);
    }

    @Override
    public boolean isItemValid(int slot, @Nonnull ItemStack stack)
    {
        return stack.getItem() instanceof MagicFocusItem;
    }

    @Override
    public int getSlotLimit(int slot) {
        return 1;
    }

    public NonNullList<ItemStack> getContents(){
        return stacks;
    }

    @Override
    public CompoundNBT serializeNBT() {
        CompoundNBT nbt = super.serializeNBT();
        ListNBT nbtTagList = new ListNBT();
        nbt.put("Items", nbtTagList);
        nbt.putInt("slot", slot);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        super.deserializeNBT(nbt);
        ListNBT tagList = nbt.getList("Items", Constants.NBT.TAG_COMPOUND);
        for (int i = 0; i < tagList.size(); i++)
        {
            CompoundNBT itemTags = tagList.getCompound(i);
            if (nbt.contains("slot")) {
                slot = nbt.getInt("slot");
                stacks.set(slot, ItemStack.read(itemTags));
            }
        }
        onLoad();

    }

    @Override
    protected void onContentsChanged(int slot) {
        CompoundNBT nbt = itemStack.getOrCreateTag();
        nbt.putBoolean("firenblood-dirty", !nbt.getBoolean("firenblood-dirty"));
    }

    public static SoulUsingItemHandler orNull(ItemStack stack) {
        return (SoulUsingItemHandler) stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(null);
    }

    public static LazyOptional<SoulUsingItemHandler> getOptional(ItemStack stack) {
        LazyOptional<IItemHandler> itemHandlerOpt = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
        if (itemHandlerOpt.isPresent()) {
            IItemHandler handler = itemHandlerOpt.orElse(null);
            if (handler instanceof SoulUsingItemHandler)
                return LazyOptional.of(() -> (SoulUsingItemHandler) handler);
        }
        return LazyOptional.empty();
    }

    public static SoulUsingItemHandler get(ItemStack stack) {
        IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
                .orElseThrow(() -> new IllegalArgumentException("ItemStack is missing item capability"));
        return (SoulUsingItemHandler) handler;
    }
}

And the Capability:

package com.Polarice3.FireNBlood.items.capability;

import com.Polarice3.FireNBlood.items.handler.SoulUsingItemHandler;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class SoulUsingItemCapability implements ICapabilitySerializable<INBT> {
    private final ItemStack stack;
    private final LazyOptional<IItemHandler> holder = LazyOptional.of(this::getHandler);
    private SoulUsingItemHandler handler;

    public SoulUsingItemCapability(ItemStack stack) {
        this.stack = stack;
    }

    @Nonnull
    private SoulUsingItemHandler getHandler() {
        if (handler == null) {
            handler = new SoulUsingItemHandler(stack);
        }
        return handler;
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? holder.cast() : LazyOptional.empty();
    }

    public INBT serializeNBT() {
        return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(getHandler(), null);
    }

    public void deserializeNBT(INBT nbt) {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(getHandler(), null, nbt);
    }
}

The ItemHandler and Capability are cobbled together from what I can find on the Internet relating to Items holding inventory without any mention of TileEntities.

Any ideas on how I can make each wand use their own Focus rather than copying the last one? Or how to improve my codes?

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

You cannot store these here. There is only one instance of your item class. All dynamic things must be stored in the ItemStack, either via NBT or via a capability.

 

It worked! Each wand does a different spell! I made a new class to cast spells based on Integers to help. The new code looks messy currently, but now it works!

package com.Polarice3.FireNBlood.items;

import com.Polarice3.FireNBlood.FireNBlood;
import com.Polarice3.FireNBlood.inventory.container.SoulItemContainer;
import com.Polarice3.FireNBlood.items.capability.SoulUsingItemCapability;
import com.Polarice3.FireNBlood.spells.*;
import com.Polarice3.FireNBlood.utils.RegistryHandler;
import com.Polarice3.FireNBlood.items.handler.SoulUsingItemHandler;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.inventory.container.SimpleNamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Rarity;
import net.minecraft.item.UseAction;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

import static com.Polarice3.FireNBlood.items.MagicFocusItem.FOCUS;

public class SoulWand extends Item{
    private static final String SOULUSE = "Soul Use";
    private static final String CASTTIME = "Cast Time";
    private static final String CURRENTFOCUS = "Focus";
    private static final String SOULCOST = "Soul Cost";
    private static final String DURATION = "Duration";
    private static final String COOLDOWN = "Cooldown";
    private static final String CASTSOUND = "Cast Sound";
    private static final String SPELL = "Spell";
    private static final String COOL = "Cool";

    public SoulWand() {
        super(new Properties().group(FireNBlood.TAB).maxStackSize(1).setNoRepair().rarity(Rarity.RARE));
    }

    @Override
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        if (entityIn instanceof LivingEntity) {
            LivingEntity livingEntity = (LivingEntity) entityIn;
            if (stack.getTag() == null) {
                CompoundNBT compound = stack.getOrCreateTag();
                compound.putInt(SOULUSE, SoulUse(livingEntity, stack));
                compound.putInt(SOULCOST, 0);
                compound.putInt(CASTTIME, CastTime(livingEntity, stack));
                compound.putInt(COOL, 0);
            }
            if (getFocus(stack) != ItemStack.EMPTY && getFocus(stack).getTag() != null) {
                stack.getTag().putString(CURRENTFOCUS, FOCUS);
                this.ChangeFocus(stack);
                stack.getTag().putString(CURRENTFOCUS, FOCUS);
            } else {
                stack.getTag().putString(CURRENTFOCUS, "none");
                this.setSpellConditions(null, stack);
            }
            stack.getTag().putInt(SOULUSE, SoulUse(livingEntity, stack));
            stack.getTag().putInt(CASTTIME, CastTime(livingEntity, stack));
        }
        super.inventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
    }

    public boolean SoulDiscount(LivingEntity entityLiving){
        return entityLiving.getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == RegistryHandler.DARKROBE.get()
                || entityLiving.getItemStackFromSlot(EquipmentSlotType.CHEST).getItem() == RegistryHandler.NECROROBE.get();
    }

    public boolean SoulCostUp(LivingEntity entityLiving){
        return entityLiving.isPotionActive(RegistryHandler.SUMMONDOWN.get());
    }

    public boolean ReduceCastTime(LivingEntity entityLiving){
        return entityLiving.getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == RegistryHandler.DARKHELM.get()
                || entityLiving.getItemStackFromSlot(EquipmentSlotType.HEAD).getItem() == RegistryHandler.NECROHELM.get();
    }

    public int SoulUse(LivingEntity entityLiving, ItemStack stack){
        if (SoulCostUp(entityLiving)){
            int amp = Objects.requireNonNull(entityLiving.getActivePotionEffect(RegistryHandler.SUMMONDOWN.get())).getAmplifier() + 2;
            return SoulCost(stack) * amp;
        } else if (SoulDiscount(entityLiving)){
            return SoulCost(stack)/2;
        } else {
            return SoulCost(stack);
        }
    }

    public int CastTime(LivingEntity entityLiving, ItemStack stack){
        if (ReduceCastTime(entityLiving)){
            return CastDuration(stack)/2;
        } else {
            return CastDuration(stack);
        }
    }

    public void onUse(World worldIn, LivingEntity livingEntityIn, ItemStack stack, int count) {
        if (!worldIn.isRemote) {
            SoundEvent soundevent = this.CastingSound(stack);
            int CastTime = stack.getUseDuration() - count;
            if (CastTime == 1) {
                if (soundevent != null) {
                    worldIn.playSound(null, livingEntityIn.getPosX(), livingEntityIn.getPosY(), livingEntityIn.getPosZ(), soundevent, SoundCategory.PLAYERS, 0.5F, 1.0F);
                } else {
                    worldIn.playSound(null, livingEntityIn.getPosX(), livingEntityIn.getPosY(), livingEntityIn.getPosZ(), SoundEvents.ENTITY_EVOKER_PREPARE_ATTACK, SoundCategory.PLAYERS, 0.5F, 1.0F);
                }
            }
            if (this.getSpell(stack) instanceof ChargingSpells){
                assert stack.getTag() != null;
                stack.getTag().putInt(COOL, stack.getTag().getInt(COOL) + 1);
                if (stack.getTag().getInt(COOL) > Cooldown(stack)){
                    stack.getTag().putInt(COOL, 0);
                    this.MagicResults(stack, worldIn, livingEntityIn);
                }
            }
        }
    }

    public int getUseDuration(ItemStack stack) {
        if (stack.getTag() != null) {
            return stack.getTag().getInt(CASTTIME);
        } else {
            return this.CastDuration(stack);
        }
    }

    @Nonnull
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.BOW;
    }

    @Nonnull
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        super.onItemUseFinish(stack, worldIn, entityLiving);
        if (!(this.getSpell(stack) instanceof ChargingSpells)){
            this.MagicResults(stack, worldIn, entityLiving);
        }
        if (stack.getTag().getInt(COOL) > 0){
            stack.getTag().putInt(COOL, 0);
        }
        return stack;
    }

    @Nonnull
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        if (!playerIn.isCrouching()) {
            if (this.getSpell(itemstack) != null) {
                playerIn.setActiveHand(handIn);
                for (int i = 0; i < playerIn.world.rand.nextInt(35) + 10; ++i) {
                    double d = worldIn.rand.nextGaussian() * 0.2D;
                    playerIn.world.addParticle(ParticleTypes.ENTITY_EFFECT, playerIn.getPosX(), playerIn.getPosYEye(), playerIn.getPosZ(), d, d, d);
                }
            }
            return ActionResult.resultConsume(itemstack);
        } else {
            if (!worldIn.isRemote) {
                SimpleNamedContainerProvider provider = new SimpleNamedContainerProvider(
                        (id, inventory, player) -> new SoulItemContainer(id, inventory, SoulUsingItemHandler.get(itemstack), itemstack, handIn), getDisplayName(itemstack));
                NetworkHooks.openGui((ServerPlayerEntity) playerIn, provider, (buffer) -> buffer.writeBoolean(handIn == Hand.MAIN_HAND));
            }
            return ActionResult.resultPass(itemstack);
        }

    }

    public void ChangeFocus(ItemStack itemStack){
        if (getFocus(itemStack).getTag().getString(FOCUS).contains("vexing")) {
            this.setSpellConditions(new VexSpell(), itemStack);
            this.setSpell(0, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("biting")) {
            this.setSpellConditions(new FangSpell(), itemStack);
            this.setSpell(1, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("roaring")) {
            this.setSpellConditions(new RoarSpell(), itemStack);
            this.setSpell(2, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("necroturgy")) {
            this.setSpellConditions(new ZombieSpell(), itemStack);
            this.setSpell(3, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("osseous")) {
            this.setSpellConditions(new SkeletonSpell(), itemStack);
            this.setSpell(4, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("crippling")) {
            this.setSpellConditions(new CrippleSpell(), itemStack);
            this.setSpell(5, itemStack);
        } else if (getFocus(itemStack).getTag().getString(FOCUS).contains("spiderling")) {
            this.setSpellConditions(new SpiderlingSpell(), itemStack);
            this.setSpell(6, itemStack);
        }
    }

    public void setSpellConditions(Spells spell, ItemStack stack){
        assert stack.getTag() != null;
        if (spell != null) {
            stack.getTag().putInt(SOULCOST, spell.SoulCost());
            stack.getTag().putInt(DURATION, spell.CastDuration());
            if (spell instanceof ChargingSpells){
                stack.getTag().putInt(COOLDOWN, ((ChargingSpells) spell).Cooldown());
            }
        } else {
            stack.getTag().putInt(SOULCOST, 0);
            stack.getTag().putInt(DURATION, 0);
            stack.getTag().putInt(COOLDOWN, 0);
        }
    }

    public void setSpell(int spellint, ItemStack stack) {
        assert stack.getTag() != null;
        stack.getTag().putInt(SPELL, spellint);
    }

    public Spells getSpell(ItemStack stack){
        assert stack.getTag() != null;
        return new CastSpells(stack.getTag().getInt(SPELL)).getSpell();
    }

    public int SoulCost(ItemStack itemStack) {
        if (itemStack.getTag() == null){
            return 0;
        } else {
            return itemStack.getTag().getInt(SOULCOST);
        }
    }

    public int CastDuration(ItemStack itemStack) {
        assert itemStack.getTag() != null;
        return itemStack.getTag().getInt(DURATION);
    }

    public int Cooldown(ItemStack itemStack) {
        assert itemStack.getTag() != null;
        return itemStack.getTag().getInt(COOLDOWN);
    }

    public SoundEvent CastingSound(ItemStack stack) {
        return this.getSpell(stack).CastingSound();
    }

    public static ItemStack getFocus(ItemStack itemstack) {
        SoulUsingItemHandler handler = SoulUsingItemHandler.get(itemstack);
        return handler.getSlot();
    }

    public void MagicResults(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        ItemStack foundStack = ItemStack.EMPTY;
        PlayerEntity playerEntity = (PlayerEntity) entityLiving;
        for (int i = 0; i <= 9; i++) {
            ItemStack itemStack = playerEntity.inventory.getStackInSlot(i);
            if (!itemStack.isEmpty() && itemStack.getItem() == RegistryHandler.GOLDTOTEM.get()) {
                foundStack = itemStack;
                break;
            }
        }
        if (this.getSpell(stack) != null && !foundStack.isEmpty() && GoldTotemItem.currentSouls(foundStack) >= SoulUse(entityLiving, stack)) {
            GoldTotemItem.decreaseSouls(foundStack, SoulUse(entityLiving, stack));
            assert stack.getTag() != null;
            this.getSpell(stack).WandResult(worldIn, entityLiving);
        } else {
            worldIn.playSound(null, entityLiving.getPosX(), entityLiving.getPosY(), entityLiving.getPosZ(), SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.NEUTRAL, 1.0F, 1.0F);
            for(int i = 0; i < entityLiving.world.rand.nextInt(35) + 10; ++i) {
                double d = worldIn.rand.nextGaussian() * 0.2D;
                entityLiving.world.addParticle(ParticleTypes.CLOUD, entityLiving.getPosX(), entityLiving.getPosYEye(), entityLiving.getPosZ(), d, d, d);
            }
        }
    }

    @Override
    public CompoundNBT getShareTag(ItemStack stack) {
        CompoundNBT result = new CompoundNBT();
        CompoundNBT tag = super.getShareTag(stack);
        CompoundNBT cap = SoulUsingItemHandler.get(stack).serializeNBT();
        if (tag != null) {
            result.put("tag", tag);
        }
        if (cap != null) {
            result.put("cap", cap);
        }
        return result;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
        assert nbt != null;
        stack.setTag(nbt.getCompound("tag"));
        SoulUsingItemHandler.get(stack).deserializeNBT(nbt.getCompound("cap"));
    }

    @Override
    public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
        return super.shouldCauseReequipAnimation(oldStack, newStack, slotChanged) && slotChanged;
    }

    @Override
    @Nullable
    public ICapabilityProvider initCapabilities(@Nonnull ItemStack stack, @Nullable CompoundNBT nbt) {
        return new SoulUsingItemCapability(stack);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        super.addInformation(stack, worldIn, tooltip, flagIn);
        if (stack.getTag() != null) {
            int SoulUse = stack.getTag().getInt(SOULUSE);
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.cost", SoulUse));
        } else {
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.cost", SoulCost(stack)));
        }
        if (getFocus(stack) != ItemStack.EMPTY){
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.focus", getFocus(stack).getItem().getName()));
        } else {
            tooltip.add(new TranslationTextComponent("info.firenblood.soulitems.focus", "Empty"));
        }
    }
}

Also, I mistakenly labelled this post 1.16.4, when I've already updated my mod to 1.16.5!

Edited by dun
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.