BloodMagic/src/main/java/WayofTime/bloodmagic/item/ItemSigilToggleable.java

96 lines
3.9 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.item;
2016-03-18 18:54:31 +00:00
import WayofTime.bloodmagic.core.data.Binding;
import WayofTime.bloodmagic.util.Constants;
import WayofTime.bloodmagic.iface.IActivatable;
import WayofTime.bloodmagic.iface.ISigil;
import WayofTime.bloodmagic.util.helper.NBTHelper;
import WayofTime.bloodmagic.util.helper.NetworkHelper;
import WayofTime.bloodmagic.util.helper.PlayerHelper;
2015-12-28 00:38:12 +00:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
2016-03-18 18:54:31 +00:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
2015-12-28 00:38:12 +00:00
import net.minecraft.util.EnumFacing;
2016-03-18 18:54:31 +00:00
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
2015-12-28 00:38:12 +00:00
import net.minecraft.world.World;
/**
* Base class for all toggleable sigils.
*/
2017-08-16 04:30:48 +00:00
public class ItemSigilToggleable extends ItemSigil implements IActivatable {
2017-08-16 04:30:48 +00:00
public ItemSigilToggleable(int lpUsed) {
super(lpUsed);
}
@Override
2017-08-16 04:30:48 +00:00
public boolean getActivated(ItemStack stack) {
2017-01-02 05:43:34 +00:00
return !stack.isEmpty() && NBTHelper.checkNBT(stack).getTagCompound().getBoolean(Constants.NBT.ACTIVATED);
}
@Override
2017-08-16 04:30:48 +00:00
public ItemStack setActivatedState(ItemStack stack, boolean activated) {
if (!stack.isEmpty()) {
NBTHelper.checkNBT(stack).getTagCompound().setBoolean(Constants.NBT.ACTIVATED, activated);
return stack;
}
2017-01-02 05:43:34 +00:00
return stack;
2015-12-28 00:38:12 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
2017-01-02 05:43:34 +00:00
ItemStack stack = player.getHeldItem(hand);
if (stack.getItem() instanceof ISigil.Holding)
stack = ((Holding) stack.getItem()).getHeldItem(stack, player);
if (PlayerHelper.isFakePlayer(player))
return ActionResult.newResult(EnumActionResult.FAIL, stack);
2017-08-16 04:30:48 +00:00
if (!world.isRemote && !isUnusable(stack)) {
2015-12-31 18:50:38 +00:00
if (player.isSneaking())
setActivatedState(stack, !getActivated(stack));
if (getActivated(stack) && NetworkHelper.getSoulNetwork(player).syphonAndDamage(player, getLpUsed()))
2017-01-02 05:43:34 +00:00
return super.onItemRightClick(world, player, hand);
2015-12-28 00:38:12 +00:00
}
2017-01-02 05:43:34 +00:00
return super.onItemRightClick(world, player, hand);
2015-12-28 00:38:12 +00:00
}
@Override
2017-08-16 04:30:48 +00:00
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
ItemStack stack = player.getHeldItem(hand);
if (stack.getItem() instanceof ISigil.Holding)
stack = ((Holding) stack.getItem()).getHeldItem(stack, player);
Binding binding = getBinding(stack);
if (binding == null || player.isSneaking()) // Make sure Sigils are bound before handling. Also ignores while toggling state
return EnumActionResult.PASS;
return (NetworkHelper.getSoulNetwork(binding).syphonAndDamage(player, getLpUsed()) && onSigilUse(player.getHeldItem(hand), player, world, pos, side, hitX, hitY, hitZ)) ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
2015-12-28 00:38:12 +00:00
}
2017-08-16 04:30:48 +00:00
public boolean onSigilUse(ItemStack itemStack, EntityPlayer player, World world, BlockPos blockPos, EnumFacing side, float hitX, float hitY, float hitZ) {
2015-12-28 00:38:12 +00:00
return false;
}
@Override
2017-08-16 04:30:48 +00:00
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
if (!worldIn.isRemote && entityIn instanceof EntityPlayerMP && getActivated(stack)) {
if (entityIn.ticksExisted % 100 == 0) {
if (!NetworkHelper.getSoulNetwork(getBinding(stack)).syphonAndDamage((EntityPlayer) entityIn, getLpUsed())) {
setActivatedState(stack, false);
2015-12-28 00:38:12 +00:00
}
}
onSigilUpdate(stack, worldIn, (EntityPlayer) entityIn, itemSlot, isSelected);
}
}
2017-08-16 04:30:48 +00:00
public void onSigilUpdate(ItemStack stack, World world, EntityPlayer player, int itemSlot, boolean isSelected) {
}
2015-12-28 00:38:12 +00:00
}