ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/items/ItemTeleStaff.java

117 lines
4.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("ItemTeleStaff.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
package ellpeck.actuallyadditions.items;
2015-07-07 21:59:57 +02:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
2015-07-07 21:59:57 +02:00
import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.WorldUtil;
2015-07-07 21:59:57 +02:00
import net.minecraft.client.renderer.texture.IIconRegister;
2015-07-07 23:41:29 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
2015-07-07 23:41:29 +02:00
import net.minecraft.nbt.NBTTagCompound;
2015-07-07 21:59:57 +02:00
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
2015-07-08 18:57:40 +02:00
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
public class ItemTeleStaff extends ItemEnergy{
public ItemTeleStaff(){
super(500000, 10000);
}
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.epic;
}
@Override
public String getName(){
return "itemTeleStaff";
}
2015-07-07 21:59:57 +02:00
@Override
public IIcon getIcon(ItemStack stack, int pass){
return this.itemIcon;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconReg){
2015-10-02 16:48:01 +02:00
this.itemIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getName());
2015-07-07 21:59:57 +02:00
}
2015-07-07 23:41:29 +02:00
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){
int time = this.getWaitTime(stack);
if(time > 0){
this.setWaitTime(stack, time-1);
}
}
private void setWaitTime(ItemStack stack, int time){
NBTTagCompound compound = stack.getTagCompound();
if(compound == null) compound = new NBTTagCompound();
compound.setInteger("waitTime", time);
stack.setTagCompound(compound);
2015-07-07 23:41:29 +02:00
}
private int getWaitTime(ItemStack stack){
NBTTagCompound compound = stack.getTagCompound();
2015-10-02 16:48:01 +02:00
if(compound == null){
return 0;
}
else{
return compound.getInteger("waitTime");
}
2015-07-07 23:41:29 +02:00
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){
2015-07-07 23:41:29 +02:00
if(!world.isRemote){
if(this.getWaitTime(stack) <= 0){
2015-08-15 20:41:45 +02:00
MovingObjectPosition pos = WorldUtil.getNearestPositionWithAir(world, player, ConfigIntValues.TELE_STAFF_REACH.getValue());
2015-07-08 11:49:38 +02:00
if(pos != null && (pos.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK || player.rotationPitch >= -5)){
2015-07-07 23:41:29 +02:00
int side = pos.sideHit;
if(side != -1){
ForgeDirection forgeSide = ForgeDirection.getOrientation(side);
if(forgeSide != ForgeDirection.UNKNOWN){
double x = pos.hitVec.xCoord-(side == 4 ? 0.5 : 0)+(side == 5 ? 0.5 : 0);
double y = pos.hitVec.yCoord-(side == 0 ? 2.0 : 0)+(side == 1 ? 0.5 : 0);
double z = pos.hitVec.zCoord-(side == 2 ? 0.5 : 0)+(side == 3 ? 0.5 : 0);
2015-08-15 20:41:45 +02:00
int use = ConfigIntValues.TELE_STAFF_ENERGY_USE.getValue()+(int)(ConfigIntValues.TELE_STAFF_ENERGY_USE.getValue()*pos.hitVec.distanceTo(Vec3.createVectorHelper(player.posX, player.posY+(player.getEyeHeight()-player.getDefaultEyeHeight()), player.posZ)));
2015-07-07 23:41:29 +02:00
if(this.getEnergyStored(stack) >= use){
((EntityPlayerMP)player).playerNetServerHandler.setPlayerLocation(x, y, z, player.rotationYaw, player.rotationPitch);
2015-07-08 11:49:38 +02:00
player.mountEntity(null);
2015-07-07 23:41:29 +02:00
world.playSoundAtEntity(player, "mob.endermen.portal", 1.0F, 1.0F);
2015-07-08 11:49:38 +02:00
if(!player.capabilities.isCreativeMode){
this.extractEnergy(stack, use, false);
2015-08-15 20:41:45 +02:00
this.setWaitTime(stack, ConfigIntValues.TELE_STAFF_WAIT_TIME.getValue());
2015-07-08 11:49:38 +02:00
}
2015-07-07 23:41:29 +02:00
}
}
}
}
}
}
2015-07-07 23:41:29 +02:00
player.swingItem();
return stack;
}
}