|
@@ -1,12 +1,11 @@
|
|
package me.km.items;
|
|
package me.km.items;
|
|
|
|
|
|
import me.km.api.Utils;
|
|
import me.km.api.Utils;
|
|
-import me.km.capabilities.GunLoadProvider;
|
|
|
|
-import me.km.capabilities.IGunLoad;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
+import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.stats.StatList;
|
|
import net.minecraft.stats.StatList;
|
|
import net.minecraft.util.ActionResult;
|
|
import net.minecraft.util.ActionResult;
|
|
import net.minecraft.util.DamageSource;
|
|
import net.minecraft.util.DamageSource;
|
|
@@ -15,6 +14,7 @@ import net.minecraft.util.EnumHand;
|
|
import net.minecraft.util.SoundCategory;
|
|
import net.minecraft.util.SoundCategory;
|
|
import net.minecraft.util.SoundEvent;
|
|
import net.minecraft.util.SoundEvent;
|
|
import net.minecraft.world.World;
|
|
import net.minecraft.world.World;
|
|
|
|
+import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
|
|
|
|
public class ItemGun extends ItemWeapon
|
|
public class ItemGun extends ItemWeapon
|
|
{
|
|
{
|
|
@@ -67,19 +67,51 @@ public class ItemGun extends ItemWeapon
|
|
this.soundReload = soundReload;
|
|
this.soundReload = soundReload;
|
|
this.soundCrit = soundCrit;
|
|
this.soundCrit = soundCrit;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
|
|
|
|
+ {
|
|
|
|
+ getTagCompound(stack);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static NBTTagCompound getTagCompound(ItemStack stack)
|
|
|
|
+ {
|
|
|
|
+ NBTTagCompound com = stack.getTagCompound();
|
|
|
|
+ if(com == null)
|
|
|
|
+ {
|
|
|
|
+ com = new NBTTagCompound();
|
|
|
|
+ com.setInteger("load", 0);
|
|
|
|
+ }
|
|
|
|
+ else if(!com.hasKey("load"))
|
|
|
|
+ {
|
|
|
|
+ com.setInteger("load", 0);
|
|
|
|
+ }
|
|
|
|
+ stack.setTagCompound(com);
|
|
|
|
+ return com;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static int getLoad(ItemStack stack)
|
|
|
|
+ {
|
|
|
|
+ return getTagCompound(stack).getInteger("load");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void setLoad(ItemStack stack, int load)
|
|
|
|
+ {
|
|
|
|
+ getTagCompound(stack).setInteger("load", load);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft)
|
|
public void onPlayerStoppedUsing(ItemStack stack, World w, EntityLivingBase liv, int timeLeft)
|
|
{
|
|
{
|
|
if(liv instanceof EntityPlayer)
|
|
if(liv instanceof EntityPlayer)
|
|
{
|
|
{
|
|
EntityPlayer p = (EntityPlayer) liv;
|
|
EntityPlayer p = (EntityPlayer) liv;
|
|
- IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
|
|
|
|
- int load = gunLoad.getCurrentLoad();
|
|
|
|
|
|
+ int load = getLoad(stack);
|
|
boolean creative = p.capabilities.isCreativeMode;
|
|
boolean creative = p.capabilities.isCreativeMode;
|
|
-
|
|
|
|
if(load > 0 || creative)
|
|
if(load > 0 || creative)
|
|
{
|
|
{
|
|
|
|
+ p.getCooldownTracker().setCooldown(this, 10);
|
|
if (!w.isRemote)
|
|
if (!w.isRemote)
|
|
{
|
|
{
|
|
stack.damageItem(1, p);
|
|
stack.damageItem(1, p);
|
|
@@ -93,16 +125,15 @@ public class ItemGun extends ItemWeapon
|
|
if(target != null)
|
|
if(target != null)
|
|
{
|
|
{
|
|
target.attackEntityFrom(DamageSource.causeMobDamage(p), damage);
|
|
target.attackEntityFrom(DamageSource.causeMobDamage(p), damage);
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- playSound(p, w, soundShot);
|
|
|
|
|
|
|
|
if(!creative)
|
|
if(!creative)
|
|
{
|
|
{
|
|
- gunLoad.setCurrentLoad(load - 1);
|
|
|
|
|
|
+ setLoad(stack, load - 1);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ playSound(p, w, soundShot);
|
|
p.addStat(StatList.getObjectUseStats(this));
|
|
p.addStat(StatList.getObjectUseStats(this));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -154,26 +185,25 @@ public class ItemGun extends ItemWeapon
|
|
{
|
|
{
|
|
if(p.capabilities.isCreativeMode)
|
|
if(p.capabilities.isCreativeMode)
|
|
{
|
|
{
|
|
- p.getHeldItem(hand).getCapability(GunLoadProvider.GUN_LOAD_CAP, null).setCurrentLoad(maxLoad);
|
|
|
|
|
|
+ setLoad(p.getHeldItem(hand), maxLoad);
|
|
playSound(p, w, soundReload);
|
|
playSound(p, w, soundReload);
|
|
return new ActionResult(EnumActionResult.FAIL, p.getHeldItem(hand));
|
|
return new ActionResult(EnumActionResult.FAIL, p.getHeldItem(hand));
|
|
}
|
|
}
|
|
- ItemStack stack = p.getHeldItem(hand);
|
|
|
|
- IGunLoad gunLoad = stack.getCapability(GunLoadProvider.GUN_LOAD_CAP, null);
|
|
|
|
- int load = gunLoad.getCurrentLoad();
|
|
|
|
|
|
+ ItemStack stack = p.getHeldItem(hand);
|
|
|
|
+ int load = getLoad(stack);
|
|
if(load <= 0)
|
|
if(load <= 0)
|
|
{
|
|
{
|
|
ItemStack ammoStack = findAmmo(p);
|
|
ItemStack ammoStack = findAmmo(p);
|
|
if(!ammoStack.isEmpty())
|
|
if(!ammoStack.isEmpty())
|
|
{
|
|
{
|
|
int newLoad = Math.min(ammoStack.getCount(), maxLoad);
|
|
int newLoad = Math.min(ammoStack.getCount(), maxLoad);
|
|
- gunLoad.setCurrentLoad(newLoad);
|
|
|
|
|
|
+ setLoad(stack, newLoad);
|
|
|
|
+ p.getCooldownTracker().setCooldown(this, cooldown * newLoad);
|
|
ammoStack.shrink(newLoad);
|
|
ammoStack.shrink(newLoad);
|
|
if(ammoStack.isEmpty())
|
|
if(ammoStack.isEmpty())
|
|
{
|
|
{
|
|
p.inventory.deleteStack(ammoStack);
|
|
p.inventory.deleteStack(ammoStack);
|
|
}
|
|
}
|
|
- p.getCooldownTracker().setCooldown(this, cooldown * newLoad);
|
|
|
|
playSound(p, w, soundReload);
|
|
playSound(p, w, soundReload);
|
|
}
|
|
}
|
|
return new ActionResult(EnumActionResult.FAIL, stack);
|
|
return new ActionResult(EnumActionResult.FAIL, stack);
|