package com.sproject.guns.common.items;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.EnumAction;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import com.sproject.guns.client.ClientKeys;
import com.sproject.guns.client.ClientTickHandler;
import com.sproject.guns.common.GunAttachment;
import com.sproject.guns.common.GunAttachmentHelper;
import com.sproject.guns.common.entity.EntityBullet;
import com.sproject.guns.common.player.PlayerData;
import com.sproject.guns.common.player.PlayerHandler;
import com.sproject.guns.network.PacketGunAim;
import com.sproject.guns.network.PacketGunReload;
import com.sproject.guns.network.PacketGunTrigger;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class ItemGun extends ItemUnlockable {
public boolean isPrimary;
private static int ammo;
public int clipId;
public float recoil;
private int damage;
private float accuracy;
private float accuracy2;
private float delayFire;
public float delayReload;
private float tickFire;
public float tickReload;
private int tickFireSel;
private static boolean isGunReloading = false;
private static boolean isGunFired = false;
public String soundFire;
private String soundReload = "gunReload";
private String soundEmpty = "gunEmpty";
private int fireMode = 0;
private int lastFireMode;
private EnumFireMode[] fireModeList;
public EnumAloudAttachments aloudAttachments;
private boolean fireModeChanged = false;
public static boolean triggerHeld;
public static boolean lastTriggerHeld = triggerHeld;
public static boolean fireSelHeld;
public static boolean lastFireSelHeld = fireSelHeld;
public static boolean reloadKey;
public static boolean lastReloadKey = !reloadKey;
public static boolean aimKey;
public static boolean lastAimKey = aimKey;
private String texture;
public int levelUnlocked = 0;
public ItemGun(int par1, boolean primary, int ammo, int damage, int clipid, String firesound, EnumFireMode[] fm, EnumAloudAttachments al, float fired, float reloadd, float recoil, float acc) {
super(par1, true);
this.isPrimary = primary;
this.ammo = ammo;
this.clipId = clipid;
this.damage = damage;
this.soundFire = firesound;
this.fireMode = 0;
this.fireModeList = fm;
this.delayFire = fired;
this.delayReload = reloadd;
this.tickReload = reloadd;
this.aloudAttachments = al;
this.recoil = recoil;
this.accuracy = acc;
this.accuracy2 = acc;
this.setMaxDamage(0);
this.setMaxStackSize(1);
this.setHasSubtypes(true);
//this.aimedBow;
}
public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
if(world.isRemote) {
this.onUpdateClient(itemstack, world, entity, i, flag);
} else {
this.onUpdateServer(itemstack, world, entity, i, flag);
}
}
public EnumAction getItemUseAction(ItemStack itemstack)
{
if (itemstack.getItemDamage() > 30)
{
return null;
}
else
{
return EnumAction.bow;
}
}
@SideOnly(Side.CLIENT)
private void onUpdateClient(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
Minecraft mc = FMLClientHandler.instance().getClient();
if(entity instanceof EntityPlayer && ((EntityPlayer)entity).inventory.getCurrentItem() == itemstack && mc.currentScreen == null) {
if(this.isGunFired) {
this.isGunFired = false;
}
lastTriggerHeld = triggerHeld;
triggerHeld = ClientKeys.shootKey.isKeyDown();
lastFireSelHeld = fireSelHeld;
fireSelHeld = ClientKeys.fireSelectKey.isKeyDown();
lastReloadKey = reloadKey;
reloadKey = ClientKeys.reloadKey.isKeyDown();
aimKey = ClientKeys.zoomKey.isKeyDown();
EntityPlayer player = (EntityPlayer)entity;
if(lastAimKey != aimKey) {
PacketDispatcher.sendPacketToServer(PacketGunAim.buildPacket(aimKey));
lastAimKey = aimKey;
}
if(reloadKey && !lastReloadKey && player.inventory.hasItem(this.clipId) && this.getAmmo(itemstack) != this.getAmmoMax(itemstack) && this.tickReload >= this.getReloadDelay(itemstack)) {
PacketDispatcher.sendPacketToServer(PacketGunReload.buildPacket());
this.tickReload = 0.0F;
}
if(this.tickReload >= this.getReloadDelay(itemstack)) {
if(this.fireModeList[this.fireMode] == EnumFireMode.AUTO && triggerHeld && this.tickFire >= this.delayFire) {
this.onTriggerPulled(itemstack);
this.tickFire = 0.0F;
}
if(this.fireModeList[this.fireMode] == EnumFireMode.SEMI && triggerHeld && !lastTriggerHeld) {
this.onTriggerPulled(itemstack);
}
if(this.fireModeList[this.fireMode] == EnumFireMode.BOLT && triggerHeld && !lastTriggerHeld && this.tickFire >= this.delayFire) {
this.onTriggerPulled(itemstack);
this.tickFire = 0.0F;
}
if(fireSelHeld && !lastFireSelHeld) {
this.updateFireMode(itemstack);
}
}
if(this.tickFire < this.delayFire) {
++this.tickFire;
}
if(this.tickReload < this.getReloadDelay(itemstack)) {
this.isGunReloading = true;
++this.tickReload;
} else {
this.isGunReloading = false;
}
if(this.lastFireMode != this.fireMode) {
this.fireModeChanged = true;
this.lastFireMode = this.fireMode;
} else {
this.fireModeChanged = false;
}
}
}
@SideOnly(Side.CLIENT)
private void updateFireMode(ItemStack itemstack) {
if(this.fireModeList.length > 0) {
if(this.fireMode < this.fireModeList.length - 1) {
++this.fireMode;
} else if(this.fireMode == this.fireModeList.length - 1) {
this.fireMode = 0;
}
}
}
private void onUpdateServer(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
if(entity instanceof EntityPlayerMP) {
EntityPlayerMP player = (EntityPlayerMP)entity;
PlayerData data = PlayerHandler.getPlayerData((EntityPlayer)player);
ItemStack itemstack1 = player.inventory.getCurrentItem();
if(itemstack1 != itemstack && (itemstack1 == null || itemstack1.getItem() == null || !(itemstack1.getItem() instanceof ItemGun))) {
data.canShoot = false;
}
if(data.canShoot) {
this.tryToShoot(itemstack, world, player);
}
}
}
private void onTriggerPulled(ItemStack itemstack) {
PacketDispatcher.sendPacketToServer(PacketGunTrigger.buildPacket());
if(this.getAmmo(itemstack) > 0) {
ClientTickHandler.gunRecoil = this.getRecoil(itemstack);
this.isGunFired = true;
}
}
public ItemStack onTriggerPulled(ItemStack itemstack, World world, EntityPlayerMP player, boolean isShooting) {
PlayerData data = PlayerHandler.getPlayerData((EntityPlayer)player);
data.canShoot = isShooting;
if(isShooting) {
data.canShoot = false;
return this.tryToShoot(itemstack, world, player);
} else {
return itemstack;
}
}
public ItemStack tryToShoot(ItemStack itemstack, World world, EntityPlayer entityplayer) {
PlayerData data = PlayerHandler.getPlayerData(entityplayer);
if(entityplayer.isSprinting() == false)
{
if(this.getAmmo(itemstack) > 0) {
int damage1 = GunAttachmentHelper.getFinalGunDamage(itemstack);
world.spawnEntityInWorld(new EntityBullet(world, entityplayer, damage1, this.accuracy, this));
this.playGunSounds(itemstack, world, entityplayer, data);
if(!entityplayer.capabilities.isCreativeMode) {
int lastAmmo = this.getAmmo(itemstack);
this.setAmmo(itemstack, lastAmmo - 1);
}
} else {
world.playSoundAtEntity(entityplayer, "sproject" + this.soundEmpty, 5.0F, 1.0F);
}
}
else
{
entityplayer.addChatMessage("Вы бежите и не можете стрелять.");
}
return itemstack;
}
public void playGunSounds(ItemStack itemstack, World world, EntityPlayer player, PlayerData data) {
if(this.hasAttachment(GunAttachment.supressor, itemstack)) {
if(data.isPlayerInside) {
world.playSoundAtEntity(player, "sproject:" + this.soundFire, 1.3F, 1.8F);
} else {
world.playSoundAtEntity(player, "sproject:" + this.soundFire, 1.0F, 1.8F);
}
} else if(data.isPlayerInside) {
world.playSoundAtEntity(player, "sproject:" + this.soundFire, 1.3F, 1.0F);
} else {
world.playSoundAtEntity(player, "sproject:" + this.soundFire, 1.0F, 1.0F);
}
}
public void onReload(ItemStack itemstack, World world, EntityPlayerMP player) {
if(player.inventory.hasItem(this.clipId) && this.getAmmo(itemstack) != this.getAmmoMax(itemstack) && this.getAmmo(itemstack) >= 0) {
world.playSoundAtEntity(player, "sproject" + this.soundReload, 1.0F, 1.0F);
player.inventory.consumeInventoryItem(this.clipId);
this.setAmmo(itemstack, this.getAmmoMax(itemstack));
}
}
public void onAim(ItemStack itemstack, World world, EntityPlayerMP player, boolean isAiming) {
PlayerData data = PlayerHandler.getPlayerData((EntityPlayer)player);
data.isAiming = isAiming;
if(this.getAttachmentFromSlot(itemstack, 0) != null && this.getAttachmentFromSlot(itemstack, 0).isSight()) {
if(data.isAiming) {
System.out.println("AIMED");
this.accuracy = 2.0F;
} else {
this.accuracy = GunAttachmentHelper.getFinalGunAccuracy(itemstack);
}
} else {
this.accuracy = GunAttachmentHelper.getFinalGunAccuracy(itemstack);
}
}
public static int getAmmo(ItemStack itemstack) {
NBTTagCompound tag = getNBTTagCompound(itemstack);
int par1 = 0;
boolean par2 = false;
int par21;
if(tag.hasKey("gunAmmo")) {
par21 = tag.getInteger("gunAmmo");
} else {
if(hasAttachment(GunAttachment.extendedMag, itemstack)) {
par1 = (int)((double)ammo * 0.25D);
}
tag.setInteger("gunAmmo", ammo + par1);
par21 = tag.getInteger("gunAmmo");
}
return par21;
}
public void setAmmo(ItemStack itemstack, int amount) {
NBTTagCompound tag = this.getNBTTagCompound(itemstack);
if(tag.hasKey("gunAmmo")) {
tag.removeTag("gunAmmo");
}
tag.setInteger("gunAmmo", amount);
}
public int getAmmoMax(ItemStack itemstack) {
return this.hasAttachment(GunAttachment.extendedMag, itemstack)?this.ammo + (int)((double)this.ammo * 0.25D):this.ammo;
}
public float getReloadDelay(ItemStack itemstack) {
return this.hasAttachment(GunAttachment.dualMags, itemstack)?this.delayReload / 2.0F:this.delayReload;
}
public void setFireMode(int par1) {
this.fireMode = par1;
}
@SideOnly(Side.CLIENT)
public static boolean isGunFired() {
return isGunFired;
}
@SideOnly(Side.CLIENT)
public static boolean isGunReloading() {
return isGunReloading;
}
@SideOnly(Side.CLIENT)
public EnumFireMode getFireMode() {
return this.fireModeList[this.fireMode];
}
public int getDamage() {
return this.damage;
}
public float getRecoil(ItemStack itemstack) {
return GunAttachmentHelper.getFinalGunRecoil(itemstack);
}
public float getAccuracy() {
return this.accuracy;
}
public float getAccuracy2() {
return this.accuracy2;
}
public boolean isFireModeChanged() {
return this.fireModeChanged;
}
public boolean getLastTriggerHeld()
{
return this.lastTriggerHeld;
}
public Item setTexture(String texture) {
this.texture = texture;
return this;
}
public static boolean hasAttachment(GunAttachment par1Attach, ItemStack itemstack) {
boolean par1 = false;
if(getAttachmentList(itemstack).length > 0) {
for(int i = 0; i < getAttachmentList(itemstack).length; ++i) {
if(getAttachmentList(itemstack)[i] != null && getAttachmentList(itemstack)[i] == par1Attach) {
par1 = true;
}
}
}
return par1;
}
public GunAttachment getAttachmentFromSlot(ItemStack itemstack, int slot) {
NBTTagCompound tag = this.getNBTTagCompound(itemstack);
if(tag.hasKey("attslot" + slot)) {
int var1 = tag.getInteger("attslot" + slot + ":attachment");
return GunAttachment.attachmentList[var1];
} else {
return null;
}
}
public void setAttachment(ItemStack itemstack, int slot, GunAttachment par2) {
if(this.getAttachmentFromSlot(itemstack, slot) == null) {
NBTTagCompound tag = this.getNBTTagCompound(itemstack);
if(par2 != null) {
tag.setInteger("attslot" + slot, slot);
tag.setInteger("attslot" + slot + ":attachment", par2.getId());
}
}
}
public static GunAttachment[] getAttachmentList(ItemStack itemstack) {
GunAttachment[] list = new GunAttachment[2];
NBTTagCompound tag = getNBTTagCompound(itemstack);
if(tag.hasKey("attslot0")) {
list[0] = GunAttachment.attachmentList[tag.getInteger("attslot0:attachment")];
}
if(tag.hasKey("attslot1")) {
list[1] = GunAttachment.attachmentList[tag.getInteger("attslot1:attachment")];
}
return list;
}
public boolean isAttachmentAloud(GunAttachment par1) {
boolean par2 = false;
for(int i = 0; i < this.aloudAttachments.aloudAttachmentsList.length; ++i) {
if(par1 == this.aloudAttachments.aloudAttachmentsList[i]) {
par2 = true;
}
}
return par2;
}
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
if(ClientKeys.reloadKey.isKeyDown()) {
par3List.add("Item ID: " + super.itemID);
par3List.add("Damage: " + GunAttachmentHelper.getFinalGunDamage(par1ItemStack));
par3List.add("Ammo: " + this.getAmmoMax(par1ItemStack));
par3List.add("Fire Mode: " + this.fireModeList[this.fireMode].getFireModeInfo());
if(this.getAttachmentFromSlot(par1ItemStack, 0) != null) {
par3List.add("Attachment 1: " + this.getAttachmentFromSlot(par1ItemStack, 0).getDisplayName());
} else {
par3List.add("Attachment 1: None");
}
if(this.getAttachmentFromSlot(par1ItemStack, 1) != null) {
par3List.add("Attachment 2: " + this.getAttachmentFromSlot(par1ItemStack, 1).getDisplayName());
} else {
par3List.add("Attachment 2: None");
}
par3List.add("Accuracy: " + GunAttachmentHelper.getFinalGunAccuracy(par1ItemStack));
par3List.add("Recoil: " + this.getRecoil(par1ItemStack));
par3List.add("Reload Delay: " + this.getReloadDelay(par1ItemStack) / 20.0F + "s");
} else {
par3List.add("Press " + ClientKeys.reloadKey.getKeyString() + " for more info");
}
}
@SideOnly(Side.CLIENT)
public boolean isFull3D() {
return true;
}
@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack par1ItemStack) {
return this.getAttachmentFromSlot(par1ItemStack, 0) != null || this.getAttachmentFromSlot(par1ItemStack, 1) != null;
}
@SideOnly(Side.CLIENT)
public EnumRarity getRarity(ItemStack par1ItemStack) {
return this.hasEffect(par1ItemStack)?EnumRarity.rare:EnumRarity.common;
}
public void registerIcons(IconRegister iconRegister) {
if(FMLCommonHandler.instance().getSide() == Side.CLIENT) {
LanguageRegistry.addName(this, this.getUnlocalizedName().replaceAll("item.", "").toUpperCase());
}
super.itemIcon = iconRegister.registerIcon("sproject:" + this.getUnlocalizedName().replace("-", ""));
}
public String getItemDisplayName(ItemStack par1ItemStack) {
String attachments = this.getAttachmentFromSlot(par1ItemStack, 0) != null?" " + this.getAttachmentFromSlot(par1ItemStack, 0).getDisplayName():"";
String attachments1 = this.getAttachmentFromSlot(par1ItemStack, 1) != null?" " + this.getAttachmentFromSlot(par1ItemStack, 1).getDisplayName():"";
String disAtt = "" + attachments + attachments1;
return "" + StatCollector.translateToLocal(this.getUnlocalizedNameInefficiently(par1ItemStack) + ".name") + (this.hasEffect(par1ItemStack)?"" + disAtt:"");
}
public String getDisplayName(ItemStack par1ItemStack) {
return "" + StatCollector.translateToLocal(this.getUnlocalizedNameInefficiently(par1ItemStack) + ".name");
}
@SideOnly(Side.CLIENT)
public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List list) {
int itemId = 0;
list.add(new ItemStack(this, 1, itemId));
int i;
GunAttachment i2;
ItemStack attach;
for(i = 0; i < this.getSightsList().size(); ++i) {
i2 = (GunAttachment)this.getSightsList().get(i);
if(this.isAttachmentAloud(i2)) {
++itemId;
attach = new ItemStack(super.itemID, 1, itemId);
this.setAttachment(attach, 0, i2);
list.add(attach);
}
}
for(i = 0; i < this.getBasicAttachmentList().size(); ++i) {
i2 = (GunAttachment)this.getBasicAttachmentList().get(i);
if(this.isAttachmentAloud(i2)) {
++itemId;
attach = new ItemStack(super.itemID, 1, itemId);
this.setAttachment(attach, 1, i2);
list.add(attach);
}
}
for(i = 0; i < this.getSightsList().size(); ++i) {
for(int var10 = 0; var10 < this.getBasicAttachmentList().size(); ++var10) {
GunAttachment var11 = (GunAttachment)this.getSightsList().get(i);
GunAttachment attach2 = (GunAttachment)this.getBasicAttachmentList().get(var10);
if(this.isAttachmentAloud(var11) && this.isAttachmentAloud(attach2)) {
++itemId;
ItemStack itemstack = new ItemStack(super.itemID, 1, itemId);
this.setAttachment(itemstack, 0, var11);
this.setAttachment(itemstack, 1, attach2);
list.add(itemstack);
}
}
}
}
public ArrayList getSightsList() {
ArrayList list = new ArrayList();
for(int i = 0; i < GunAttachment.attachmentList.length; ++i) {
if(GunAttachment.attachmentList[i] != null && GunAttachment.attachmentList[i].isSight()) {
list.add(GunAttachment.attachmentList[i]);
}
}
return list;
}
public ArrayList getBasicAttachmentList() {
ArrayList list = new ArrayList();
for(int i = 0; i < GunAttachment.attachmentList.length; ++i) {
if(GunAttachment.attachmentList[i] != null && !GunAttachment.attachmentList[i].isSight()) {
list.add(GunAttachment.attachmentList[i]);
}
}
return list;
}
public ItemStack getItemStackFromAttachments(GunAttachment att1, GunAttachment att2) {
for(int i = 0; i < 50; ++i) {
ItemStack itemstack = new ItemStack(this, 1, i);
if(itemstack != null) {
GunAttachment[] list = this.getAttachmentList(itemstack);
for(int i1 = 0; i1 < list.length; ++i1) {
if(list[i1] == att1 && list[i1] == att2) {
return itemstack;
}
}
}
}
return null;
}
public static NBTTagCompound getNBTTagCompound(ItemStack itemstack) {
if(!itemstack.hasTagCompound()) {
itemstack.stackTagCompound = new NBTTagCompound("tag");
}
if(!itemstack.stackTagCompound.hasKey("gun")) {
NBTTagCompound tag = new NBTTagCompound("gun");
itemstack.stackTagCompound.setTag("gun", tag);
}
return (NBTTagCompound)itemstack.stackTagCompound.getTag("gun");
}
/**
* Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
* True if something happen and false if it don't. This is for ITEMS, not BLOCKS
*/
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
{
return false;
}
public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack)
{
return true;
}
/**
* Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
* sword
*/
public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
{
return 0.0F;
}
public int getDamage(ItemStack stack)
{
return 0;
}
/**
* Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
* the damage on the stack.
*/
public boolean hitEntity(ItemStack par1ItemStack, EntityLivingBase par2EntityLivingBase, EntityLivingBase par3EntityLivingBase)
{
return false;
}
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
{
return true;
}
public boolean onBlockStartBreak(ItemStack itemstack, int X, int Y, int Z, EntityPlayer player)
{
return true;
}
public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
{
return true;
}
}