/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Apr 14, 2014, 7:34:56 PM (GMT)]
*/
package vazkii.botania.common.item.equipment.tool.terrasteel;
import java.util.List;
import javax.annotation.Nonnull;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Achievement;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.internal.IManaBurst;
import vazkii.botania.api.mana.BurstProperties;
import vazkii.botania.api.mana.ILensEffect;
import vazkii.botania.api.sound.BotaniaSoundEvents;
import vazkii.botania.common.achievement.ICraftAchievement;
import vazkii.botania.common.achievement.ModAchievements;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.entity.EntityManaBurst;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.equipment.tool.ToolCommons;
import vazkii.botania.common.item.equipment.tool.manasteel.ItemManasteelSword;
import vazkii.botania.common.lib.LibItemNames;
import vazkii.botania.common.network.PacketHandler;
import vazkii.botania.common.network.PacketLeftClick;
public class ItemTerraSword extends ItemManasteelSword implements ILensEffect, ICraftAchievement {
private static final String TAG_ATTACKER_USERNAME = "attackerUsername";
private static final int MANA_PER_DAMAGE = 100;
public ItemTerraSword() {
super(BotaniaAPI.terrasteelToolMaterial, LibItemNames.TERRA_SWORD);
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void leftClick(PlayerInteractEvent.LeftClickEmpty evt) {
if (!evt.getItemStack().isEmpty()
&& evt.getItemStack().getItem() == this) {
PacketHandler.sendToServer(new PacketLeftClick());
}
}
@SubscribeEvent
public void attackEntity(AttackEntityEvent evt) {
if (!evt.getEntityPlayer().world.isRemote) {
trySpawnBurst(evt.getEntityPlayer());
}
}
public void trySpawnBurst(EntityPlayer player) {
if (!player.getHeldItemMainhand().isEmpty()
&& player.getHeldItemMainhand().getItem() == this
&& player.getCooledAttackStrength(0) == 1) {
EntityManaBurst burst = getBurst(player, player.getHeldItemMainhand());
player.world.spawnEntity(burst);
ToolCommons.damageItem(player.getHeldItemMainhand(), 1, player, MANA_PER_DAMAGE);
player.world.playSound(null, player.posX, player.posY, player.posZ, BotaniaSoundEvents.terraBlade, SoundCategory.PLAYERS, 0.4F, 1.4F);
}
}
@Override
public int getManaPerDamage() {
return MANA_PER_DAMAGE;
}
public EntityManaBurst getBurst(EntityPlayer player, ItemStack stack) {
EntityManaBurst burst = new EntityManaBurst(player);
float motionModifier = 7F;
burst.setColor(0x20FF20);
burst.setMana(MANA_PER_DAMAGE);
burst.setStartingMana(MANA_PER_DAMAGE);
burst.setMinManaLoss(40);
burst.setManaLossPerTick(4F);
burst.setGravity(0F);
burst.setMotion(burst.motionX * motionModifier, burst.motionY * motionModifier, burst.motionZ * motionModifier);
ItemStack lens = stack.copy();
ItemNBTHelper.setString(lens, TAG_ATTACKER_USERNAME, player.getName());
burst.setSourceLens(lens);
return burst;
}
@Override
public void apply(ItemStack stack, BurstProperties props) {}
@Override
public boolean collideBurst(IManaBurst burst, RayTraceResult pos, boolean isManaBlock, boolean dead, ItemStack stack) {
return dead;
}
@Override
public void updateBurst(IManaBurst burst, ItemStack stack) {
EntityThrowable entity = (EntityThrowable) burst;
AxisAlignedBB axis = new AxisAlignedBB(entity.posX, entity.posY, entity.posZ, entity.lastTickPosX, entity.lastTickPosY, entity.lastTickPosZ).expand(1, 1, 1);
List<EntityLivingBase> entities = entity.world.getEntitiesWithinAABB(EntityLivingBase.class, axis);
String attacker = ItemNBTHelper.getString(burst.getSourceLens(), TAG_ATTACKER_USERNAME, "");
for(EntityLivingBase living : entities) {
if(living instanceof EntityPlayer && (living.getName().equals(attacker) || FMLCommonHandler.instance().getMinecraftServerInstance() != null && !FMLCommonHandler.instance().getMinecraftServerInstance().isPVPEnabled()))
continue;
if(living.hurtTime == 0) {
int cost = MANA_PER_DAMAGE / 3;
int mana = burst.getMana();
if(mana >= cost) {
burst.setMana(mana - cost);
float damage = 4F + BotaniaAPI.terrasteelToolMaterial.getDamageVsEntity();
if(!burst.isFake() && !entity.world.isRemote) {
EntityPlayer player = living.world.getPlayerEntityByName(attacker);
living.attackEntityFrom(player == null ? DamageSource.MAGIC : DamageSource.causePlayerDamage(player), damage);
entity.setDead();
break;
}
}
}
}
}
@Override
public boolean doParticles(IManaBurst burst, ItemStack stack) {
return true;
}
@Override
public boolean getIsRepairable(ItemStack par1ItemStack, @Nonnull ItemStack par2ItemStack) {
return par2ItemStack.getItem() == ModItems.manaResource && par2ItemStack.getItemDamage() == 4 ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
}
@Override
public Achievement getAchievementOnCraft(ItemStack stack, EntityPlayer player, IInventory matrix) {
return ModAchievements.terrasteelWeaponCraft;
}
}