2015-12-29 19:32:35 +00:00
|
|
|
package WayofTime.bloodmagic.item;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
|
|
import WayofTime.bloodmagic.api.ItemStackWrapper;
|
|
|
|
import WayofTime.bloodmagic.api.event.BoundToolEvent;
|
|
|
|
import WayofTime.bloodmagic.api.util.helper.BindableHelper;
|
|
|
|
import WayofTime.bloodmagic.util.helper.TextHelper;
|
|
|
|
import com.google.common.collect.Multiset;
|
|
|
|
import lombok.Getter;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.EnumAction;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.BlockPos;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.StatCollector;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
@Getter
|
2015-12-30 20:34:40 +00:00
|
|
|
public class ItemBoundTool extends ItemBindable
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
private Set<Block> effectiveBlocks;
|
|
|
|
protected final String tooltipBase;
|
|
|
|
private final String name;
|
|
|
|
|
|
|
|
public boolean beingHeldDown;
|
|
|
|
private int heldDownCount;
|
|
|
|
|
|
|
|
public final int chargeTime = 30;
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public ItemBoundTool(String name, int lpUsed, Set<Block> effectiveBlocks)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
super();
|
|
|
|
setUnlocalizedName(Constants.Mod.MODID + ".bound." + name);
|
|
|
|
setHasSubtypes(true);
|
|
|
|
setLPUsed(lpUsed);
|
|
|
|
setFull3D();
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
this.tooltipBase = "tooltip.BloodMagic.bound." + name + ".";
|
|
|
|
this.effectiveBlocks = effectiveBlocks;
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public ItemBoundTool(String name, int lpUsed)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
this(name, lpUsed, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public float getStrVsBlock(ItemStack stack, Block block)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return this.effectiveBlocks.contains(block) ? 8.0F : 1.0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
|
|
|
|
{
|
|
|
|
if (entityIn instanceof EntityPlayer && getActivated(stack) && isSelected && isBeingHeldDown() && stack == ((EntityPlayer) entityIn).getCurrentEquippedItem())
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
EntityPlayer player = (EntityPlayer) entityIn;
|
|
|
|
heldDownCount = Math.min(player.getItemInUseDuration(), chargeTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
BindableHelper.checkAndSetItemOwner(stack, player);
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
// if (!world.isRemote)
|
2015-12-29 19:32:35 +00:00
|
|
|
{
|
2015-12-31 18:50:38 +00:00
|
|
|
if (player.isSneaking())
|
|
|
|
setActivated(stack, !getActivated(stack));
|
2015-12-30 20:34:40 +00:00
|
|
|
// if (getActivated(stack) && ItemBindable.syphonBatteries(stack,
|
|
|
|
// player, getLPUsed()))
|
|
|
|
// return stack;
|
2015-12-30 22:24:40 +00:00
|
|
|
// if (getActivated(stack) && ItemBindable.syphonNetwork(stack, player, getLPUsed()))
|
|
|
|
// return stack;
|
2015-12-29 19:32:35 +00:00
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
if (!player.isSneaking() && getActivated(stack))
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
BoundToolEvent.Charge event = new BoundToolEvent.Charge(player, stack);
|
2015-12-31 18:50:38 +00:00
|
|
|
if (MinecraftForge.EVENT_BUS.post(event))
|
|
|
|
return event.result;
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
player.setItemInUse(stack, this.getMaxItemUseDuration(stack));
|
|
|
|
beingHeldDown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos blockPos, EnumFacing side, float hitX, float hitY, float hitZ)
|
|
|
|
{
|
2015-12-30 22:24:40 +00:00
|
|
|
if (BindableHelper.checkAndSetItemOwner(stack, player) && ItemBindable.syphonNetwork(stack, player, getLPUsed()))
|
2015-12-29 19:32:35 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
this.onItemRightClick(stack, world, player);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityPlayer playerIn, int timeLeft)
|
|
|
|
{
|
|
|
|
if (!playerIn.isSneaking() && getActivated(stack))
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
int i = this.getMaxItemUseDuration(stack) - timeLeft;
|
|
|
|
BoundToolEvent.Release event = new BoundToolEvent.Release(playerIn, stack, i);
|
2015-12-31 18:50:38 +00:00
|
|
|
if (MinecraftForge.EVENT_BUS.post(event))
|
|
|
|
return;
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
i = event.charge;
|
|
|
|
|
|
|
|
onBoundRelease(stack, worldIn, playerIn, Math.min(i, chargeTime));
|
|
|
|
beingHeldDown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
protected void onBoundRelease(ItemStack stack, World world, EntityPlayer player, int charge)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityPlayer playerIn)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public int getMaxItemUseDuration(ItemStack stack)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return 72000;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public EnumAction getItemUseAction(ItemStack stack)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return EnumAction.BOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public int getItemEnchantability()
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2015-12-30 20:34:40 +00:00
|
|
|
public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
if (StatCollector.canTranslate(tooltipBase + "desc"))
|
|
|
|
tooltip.add(TextHelper.localizeEffect(tooltipBase + "desc"));
|
2015-12-31 18:50:38 +00:00
|
|
|
if (getActivated(stack))
|
|
|
|
tooltip.add(TextHelper.localize("tooltip.BloodMagic.activated"));
|
|
|
|
else
|
|
|
|
tooltip.add(TextHelper.localize("tooltip.BloodMagic.deactivated"));
|
2015-12-29 19:32:35 +00:00
|
|
|
|
|
|
|
super.addInformation(stack, player, tooltip, advanced);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public boolean showDurabilityBar(ItemStack stack)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return getActivated(stack) && beingHeldDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-30 20:34:40 +00:00
|
|
|
public double getDurabilityForDisplay(ItemStack stack)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return ((double) -Math.min(getHeldDownCount(), chargeTime) / chargeTime) + 1;
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
protected static void dropStacks(Multiset<ItemStackWrapper> drops, World world, BlockPos posToDrop)
|
|
|
|
{
|
|
|
|
for (Multiset.Entry<ItemStackWrapper> entry : drops.entrySet())
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
int count = entry.getCount();
|
|
|
|
ItemStackWrapper stack = entry.getElement();
|
|
|
|
int maxStackSize = stack.item.getItemStackLimit(stack.toStack(1));
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
while (count >= maxStackSize)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
world.spawnEntityInWorld(new EntityItem(world, posToDrop.getX(), posToDrop.getY(), posToDrop.getZ(), stack.toStack(maxStackSize)));
|
|
|
|
count -= maxStackSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
world.spawnEntityInWorld(new EntityItem(world, posToDrop.getX(), posToDrop.getY(), posToDrop.getZ(), stack.toStack(count)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public boolean getActivated(ItemStack stack)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
return stack.getItemDamage() > 0;
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public ItemStack setActivated(ItemStack stack, boolean activated)
|
|
|
|
{
|
2015-12-29 19:32:35 +00:00
|
|
|
stack.setItemDamage(activated ? 1 : 0);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|