package com.yolp900.itsjustacharm.common.items; import com.yolp900.itsjustacharm.api.IJCConstants; import com.yolp900.itsjustacharm.api.ItsJustaCharmAPI; import com.yolp900.itsjustacharm.api.affinities.IAffinity; import com.yolp900.itsjustacharm.api.affinities.IAffinityHolderItem; import com.yolp900.itsjustacharm.common.affinities.ModAffinities; import com.yolp900.itsjustacharm.common.items.base.ModItem; import com.yolp900.itsjustacharm.util.NBTHelper; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; public class ItemAffinityHolder extends ModItem implements IAffinityHolderItem { public ItemAffinityHolder() { super(IJCConstants.Items.AffinityHolder); setMaxStackSize(1); } @Override public int getMaxAmountOfStoredAffinities(@Nonnull ItemStack stack, EntityPlayer player, World world) { return ItsJustaCharmAPI.Affinities.affinities.size(); } @Override public int getCurrentAffinityIndex(@Nonnull ItemStack stack, EntityPlayer player, World world) { return NBTHelper.getInt(stack, IJCConstants.NBT.CURRENT_AFFINITY_INDEX); } @Override public void setCurrentAffinityIndex(@Nonnull ItemStack stack, int index, EntityPlayer player, World world) { NBTHelper.setInt(stack, IJCConstants.NBT.CURRENT_AFFINITY_INDEX, index); } @Override public List<IAffinity> getStoredAffinities(@Nonnull ItemStack stack, EntityPlayer player, World world) { List<IAffinity> storedAffinities = new ArrayList<IAffinity>(getMaxAmountOfStoredAffinities(stack, player, world)); for (int i = 0; i < getMaxAmountOfStoredAffinities(stack, player, world); i++) { IAffinity currAffinity = getStoredAffinity(stack, i, player, world); if (currAffinity != null) { storedAffinities.add(currAffinity); } } return storedAffinities; } @Override public IAffinity getStoredAffinity(@Nonnull ItemStack stack, int index, EntityPlayer player, World world) { int maxAmount = getMaxAmountOfStoredAffinities(stack, player, world); if (index < 0 || index >= maxAmount) { return null; } String affinityName = NBTHelper.getString(stack, IJCConstants.NBT.STORED_AFFINITY(index)); if (affinityName == null || affinityName.equals("")) { return null; } return ItsJustaCharmAPI.Affinities.getAffinityFromName(affinityName); } @Override public boolean storeAffinity(@Nonnull ItemStack stack, @Nonnull IAffinity affinity, EntityPlayer player, World world) { List<IAffinity> storedAffinities = getStoredAffinities(stack, player, world); if (storedAffinities == null || storedAffinities.size() == 0) { NBTHelper.setString(stack, IJCConstants.NBT.STORED_AFFINITY(0), affinity.getName()); } if (storedAffinities != null) { for (IAffinity storedAffinity : storedAffinities) { if (storedAffinity != null && storedAffinity == affinity) { return false; } } for (int i = 0; i < storedAffinities.size(); i++) { if (storedAffinities.get(i) == null) { storeAffinity(stack, affinity, i); return true; } } } return false; } @Override public boolean removeAffinity(@Nonnull ItemStack stack, int index, EntityPlayer player, World world) { removeAffinity(stack, index); return true; } private void storeAffinity(@Nonnull ItemStack stack, @Nonnull IAffinity affinity, int index) { NBTHelper.setString(stack, IJCConstants.NBT.STORED_AFFINITY(index), affinity.getName()); } private void removeAffinity(@Nonnull ItemStack stack, int index) { NBTHelper.setString(stack, IJCConstants.NBT.STORED_AFFINITY(index), ""); } @Override @Nonnull public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (stack == null || !(stack.getItem() instanceof ItemAffinityHolder)) return super.onItemUse(stack, player, world, pos, hand, facing, hitX, hitY, hitZ); IAffinityHolderItem affinityHolder = (IAffinityHolderItem)stack.getItem(); affinityHolder.setCurrentAffinityIndex(stack, 0, player, world); Block block = world.getBlockState(pos).getBlock(); if (block == Blocks.GRASS) { affinityHolder.storeAffinity(stack, ModAffinities.Earth, player, world); } else if (block == Blocks.STONE) { affinityHolder.storeAffinity(stack, ModAffinities.Block, player, world); } else if (block == Blocks.NETHERRACK) { affinityHolder.storeAffinity(stack, ModAffinities.Fire, player, world); } else if (block == Blocks.ICE) { affinityHolder.storeAffinity(stack, ModAffinities.Water, player, world); } else if (block == Blocks.REEDS) { affinityHolder.storeAffinity(stack, ModAffinities.Air, player, world); } return super.onItemUse(stack, player, world, pos, hand, facing, hitX, hitY, hitZ); } }