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

193 lines
7.3 KiB
Java
Raw Normal View History

2015-11-03 10:34:11 -05:00
package WayofTime.bloodmagic.item;
import WayofTime.bloodmagic.ConfigHandler;
import WayofTime.bloodmagic.apibutnotreally.BloodMagicAPI;
import WayofTime.bloodmagic.apibutnotreally.Constants;
import WayofTime.bloodmagic.apibutnotreally.event.SacrificeKnifeUsedEvent;
import WayofTime.bloodmagic.apibutnotreally.util.helper.NBTHelper;
import WayofTime.bloodmagic.apibutnotreally.util.helper.PlayerHelper;
import WayofTime.bloodmagic.apibutnotreally.util.helper.PlayerSacrificeHelper;
import WayofTime.bloodmagic.client.IMeshProvider;
import WayofTime.bloodmagic.core.RegistrarBloodMagicItems;
import WayofTime.bloodmagic.item.types.ISubItem;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.tile.TileAltar;
import WayofTime.bloodmagic.util.helper.TextHelper;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
2017-08-15 20:21:54 -07:00
import net.minecraft.client.util.ITooltipFlag;
2015-11-07 11:51:41 -05:00
import net.minecraft.entity.Entity;
2016-03-18 16:50:33 -04:00
import net.minecraft.entity.EntityLivingBase;
2015-11-07 11:51:41 -05:00
import net.minecraft.entity.player.EntityPlayer;
2016-03-18 16:50:33 -04:00
import net.minecraft.init.SoundEvents;
2015-11-07 11:51:41 -05:00
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
2016-03-18 16:50:33 -04:00
import net.minecraft.util.math.RayTraceResult;
2015-11-07 11:51:41 -05:00
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
2017-08-15 21:30:48 -07:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class ItemSacrificialDagger extends ItemEnum<ItemSacrificialDagger.DaggerType> implements IMeshProvider {
2015-11-07 11:51:41 -05:00
2017-08-15 21:30:48 -07:00
public ItemSacrificialDagger() {
super(DaggerType.class, "sacrificial_dagger");
2015-11-07 11:51:41 -05:00
setHasSubtypes(true);
setMaxStackSize(1);
setFull3D();
}
@Override
2017-08-15 21:30:48 -07:00
public void addInformation(ItemStack stack, World world, List<String> list, ITooltipFlag flag) {
2017-01-02 01:18:29 -08:00
list.addAll(Arrays.asList(TextHelper.cutLongString(TextHelper.localizeEffect("tooltip.bloodmagic.sacrificialDagger.desc"))));
if (stack.getItemDamage() == 1)
2017-01-02 01:18:29 -08:00
list.add(TextHelper.localizeEffect("tooltip.bloodmagic.sacrificialDagger.creative"));
2015-11-07 11:51:41 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {
2017-01-01 22:26:42 -08:00
if (entityLiving instanceof EntityPlayer && !entityLiving.getEntityWorld().isRemote)
2016-03-18 16:50:33 -04:00
PlayerSacrificeHelper.sacrificePlayerHealth((EntityPlayer) entityLiving);
2015-11-07 11:51:41 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public int getMaxItemUseDuration(ItemStack stack) {
2015-11-07 11:51:41 -05:00
return 72000;
}
@Override
2017-08-15 21:30:48 -07:00
public EnumAction getItemUseAction(ItemStack stack) {
2015-11-07 11:51:41 -05:00
return EnumAction.BOW;
}
@Override
2017-08-15 21:30:48 -07:00
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
2017-01-01 22:26:42 -08:00
ItemStack stack = player.getHeldItem(hand);
if (PlayerHelper.isFakePlayer(player))
2017-01-01 22:26:42 -08:00
return super.onItemRightClick(world, player, hand);
2017-08-15 21:30:48 -07:00
if (this.canUseForSacrifice(stack)) {
2016-03-18 16:50:33 -04:00
player.setActiveHand(hand);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
2015-11-07 11:51:41 -05:00
}
int lpAdded = ConfigHandler.values.sacrificialDaggerConversion * 2;
2016-04-24 10:06:28 -07:00
RayTraceResult rayTrace = rayTrace(world, player, false);
2017-08-15 21:30:48 -07:00
if (rayTrace != null && rayTrace.typeOfHit == RayTraceResult.Type.BLOCK) {
2016-04-24 10:06:28 -07:00
TileEntity tile = world.getTileEntity(rayTrace.getBlockPos());
if (tile != null && tile instanceof TileAltar && stack.getItemDamage() == 1)
lpAdded = ((TileAltar) tile).getCapacity();
}
2017-08-15 21:30:48 -07:00
if (!player.capabilities.isCreativeMode) {
SacrificeKnifeUsedEvent evt = new SacrificeKnifeUsedEvent(player, true, true, 2, lpAdded);
if (MinecraftForge.EVENT_BUS.post(evt))
2017-01-01 22:26:42 -08:00
return super.onItemRightClick(world, player, hand);
2015-11-07 11:51:41 -05:00
2017-08-15 21:30:48 -07:00
if (evt.shouldDrainHealth) {
player.hurtResistantTime = 0;
2017-08-15 20:21:54 -07:00
player.attackEntityFrom(BloodMagicAPI.damageSource, 0.001F);
player.setHealth(Math.max(player.getHealth() - 2, 0.0001f));
2017-08-15 21:30:48 -07:00
if (player.getHealth() <= 0.001f) {
2017-08-15 20:21:54 -07:00
player.onDeath(BloodMagicAPI.damageSource);
player.setHealth(0);
}
// player.attackEntityFrom(BloodMagicAPI.getDamageSource(), 2.0F);
2015-11-07 11:51:41 -05:00
}
if (!evt.shouldFillAltar)
2017-01-01 22:26:42 -08:00
return super.onItemRightClick(world, player, hand);
lpAdded = evt.lpAdded;
2015-11-07 11:51:41 -05:00
}
double posX = player.posX;
double posY = player.posY;
double posZ = player.posZ;
2016-04-24 10:06:28 -07:00
world.playSound(null, posX, posY, posZ, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
2015-11-07 11:51:41 -05:00
for (int l = 0; l < 8; ++l)
world.spawnParticle(EnumParticleTypes.REDSTONE, posX + Math.random() - Math.random(), posY + Math.random() - Math.random(), posZ + Math.random() - Math.random(), 0, 0, 0);
2015-11-07 11:51:41 -05:00
if (!world.isRemote && PlayerHelper.isFakePlayer(player))
2017-01-01 22:26:42 -08:00
return super.onItemRightClick(world, player, hand);
2015-11-07 11:51:41 -05:00
// TODO - Check if SoulFray is active
PlayerSacrificeHelper.findAndFillAltar(world, player, lpAdded, false);
2015-11-07 11:51:41 -05:00
2017-01-01 22:26:42 -08:00
return super.onItemRightClick(world, player, hand);
2015-11-07 11:51:41 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {
if (!world.isRemote && entity instanceof EntityPlayer)
2015-11-07 11:51:41 -05:00
this.setUseForSacrifice(stack, this.isPlayerPreparedForSacrifice(world, (EntityPlayer) entity));
}
2017-08-15 21:30:48 -07:00
public boolean isPlayerPreparedForSacrifice(World world, EntityPlayer player) {
2015-11-07 11:51:41 -05:00
return !world.isRemote && (PlayerSacrificeHelper.getPlayerIncense(player) > 0);
}
2017-08-15 21:30:48 -07:00
public boolean canUseForSacrifice(ItemStack stack) {
stack = NBTHelper.checkNBT(stack);
return stack.getTagCompound().getBoolean(Constants.NBT.SACRIFICE);
2015-11-07 11:51:41 -05:00
}
2017-08-15 21:30:48 -07:00
public void setUseForSacrifice(ItemStack stack, boolean sacrifice) {
stack = NBTHelper.checkNBT(stack);
stack.getTagCompound().setBoolean(Constants.NBT.SACRIFICE, sacrifice);
2015-11-07 11:51:41 -05:00
}
@Override
@SideOnly(Side.CLIENT)
2017-08-15 21:30:48 -07:00
public ItemMeshDefinition getMeshDefinition() {
2017-08-16 16:39:57 -07:00
return stack -> {
String variant = "type=normal";
if (stack.getItemDamage() != 0)
variant = "type=creative";
2017-08-16 16:39:57 -07:00
if (canUseForSacrifice(stack))
variant = "type=ceremonial";
2017-08-16 16:39:57 -07:00
return new ModelResourceLocation(getRegistryName(), variant);
};
}
@Override
2017-08-15 21:30:48 -07:00
public List<String> getVariants() {
List<String> variants = new ArrayList<String>();
variants.add("type=normal");
variants.add("type=creative");
variants.add("type=ceremonial");
return variants;
}
public enum DaggerType implements ISubItem {
NORMAL,
2018-02-14 23:38:57 -08:00
CREATIVE,;
@Nonnull
@Override
public String getInternalName() {
return name().toLowerCase(Locale.ROOT);
}
@Nonnull
@Override
public ItemStack getStack(int count) {
return new ItemStack(RegistrarBloodMagicItems.SACRIFICIAL_DAGGER, count, ordinal());
}
2015-11-07 11:51:41 -05:00
}
}