Jump to content

[1.11.2] Custom projectile item not loosing durability (solved)


hhggtg3279

Recommended Posts

Basically I created a custom bow which shoots my custom projectile, however after shooting the projectile the item does not loose durability, I have tried a few things but cant figure it out.

 

IronBow class

 

package top.mod.item;

import javax.annotation.Nullable;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.enchantment.EnchantmentHelper;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.projectile.EntityArrow;

import net.minecraft.init.Enchantments;

import net.minecraft.init.Items;

import net.minecraft.init.SoundEvents;

import net.minecraft.item.EnumAction;

import net.minecraft.item.IItemPropertyGetter;

import net.minecraft.item.Item;

import net.minecraft.item.ItemArrow;

import net.minecraft.item.ItemStack;

import net.minecraft.stats.StatList;

import net.minecraft.util.ActionResult;

import net.minecraft.util.EnumActionResult;

import net.minecraft.util.EnumHand;

import net.minecraft.util.ResourceLocation;

import net.minecraft.util.SoundCategory;

import net.minecraft.world.World;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

public class ModBowIron extends Item

{

public ModBowIron()

{

this.maxStackSize = 1;

this.setMaxDamage(634);

this.setCreativeTab(CreativeTabs.COMBAT);

this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter()

{

@SideOnly(Side.CLIENT)

public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)

{

return entityIn == null ? 0.0F : (entityIn.getActiveItemStack().getItem() != Items.BOW ? 0.0F : (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F);

}

});

this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter()

{

@SideOnly(Side.CLIENT)

public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)

{

return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;

}

});

}

private ItemStack findAmmo(EntityPlayer player)

{

if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))

{

return player.getHeldItem(EnumHand.OFF_HAND);

}

else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))

{

return player.getHeldItem(EnumHand.MAIN_HAND);

}

else

{

for (int i = 0; i < player.inventory.getSizeInventory(); ++i)

{

ItemStack itemstack = player.inventory.getStackInSlot(i);

if (this.isArrow(itemstack))

{

return itemstack;

}

}

return ItemStack.EMPTY;

}

}

protected boolean isArrow(ItemStack stack)

{

return stack.getItem() instanceof ItemArrow;

}

/**

* Called when the player stops using an Item (stops holding the right mouse button).

*/

public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)

{

if (entityLiving instanceof EntityPlayer)

{

EntityPlayer entityplayer = (EntityPlayer)entityLiving;

boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;

ItemStack itemstack = this.findAmmo(entityplayer);

int i = this.getMaxItemUseDuration(stack) - timeLeft;

i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);

if (i < 0) return;

if (!itemstack.isEmpty() || flag)

{

if (itemstack.isEmpty())

{

itemstack = new ItemStack(Items.ARROW);

}

float f = getArrowVelocity(i);

if ((double)f >= 0.1D)

{

boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));

if (!worldIn.isRemote)

{

EntityGrenade entitysnowball = new EntityGrenade(worldIn, entityplayer);

entitysnowball.setHeadingFromThrower(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, 1.5F, 1.0F);

worldIn.spawnEntity(entitysnowball);

worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

if (!flag1 && !entityplayer.capabilities.isCreativeMode)

{

itemstack.shrink(1);

if (itemstack.isEmpty())

{

entityplayer.inventory.deleteStack(itemstack);

}

}

entityplayer.addStat(StatList.getObjectUseStats(this));

}

}

}

}

 

}

/**

* Gets the velocity of the arrow entity from the bow's charge

*/

public static float getArrowVelocity(int charge)

{

float f = (float)charge / 20.0F;

f = (f * f + f * 2.0F) / 3.0F;

if (f > 1.0F)

{

f = 1.0F;

}

return f;

}

/**

* How long it takes to use or consume an item

*/

public int getMaxItemUseDuration(ItemStack stack)

{

return 72000;

}

/**

* returns the action that specifies what animation to play when the items is being used

*/

public EnumAction getItemUseAction(ItemStack stack)

{

return EnumAction.BOW;

}

public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)

{

ItemStack itemstack = playerIn.getHeldItem(handIn);

boolean flag = !this.findAmmo(playerIn).isEmpty();

ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);

if (ret != null) return ret;

if (!playerIn.capabilities.isCreativeMode && !flag)

{

return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);

}

else

{

playerIn.setActiveHand(handIn);

return new ActionResult(EnumActionResult.SUCCESS, itemstack);

}

}

/**

* Return the enchantability factor of the item, most of the time is based on material.

*/

public int getItemEnchantability()

{

return 1;

}

}

 

 

If needed it is on pastebin too

http://pastebin.com/AyqKYj19

Link to comment
Share on other sites

Updated code:

http://pastebin.com/GQPe0ufd

 

 

 

package top.mod.item;

import javax.annotation.Nullable;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.enchantment.EnchantmentHelper;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.projectile.EntityArrow;

import net.minecraft.init.Enchantments;

import net.minecraft.init.Items;

import net.minecraft.init.SoundEvents;

import net.minecraft.item.EnumAction;

import net.minecraft.item.IItemPropertyGetter;

import net.minecraft.item.Item;

import net.minecraft.item.ItemArrow;

import net.minecraft.item.ItemStack;

import net.minecraft.stats.StatList;

import net.minecraft.util.ActionResult;

import net.minecraft.util.EnumActionResult;

import net.minecraft.util.EnumHand;

import net.minecraft.util.ResourceLocation;

import net.minecraft.util.SoundCategory;

import net.minecraft.world.World;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

public class ModBowIron extends Item

{

public ModBowIron()

{

this.maxStackSize = 1;

this.setMaxDamage(634);

this.setCreativeTab(CreativeTabs.COMBAT);

this.addPropertyOverride(new ResourceLocation("pull"), new IItemPropertyGetter()

{

@SideOnly(Side.CLIENT)

public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)

{

return entityIn == null ? 0.0F : (entityIn.getActiveItemStack().getItem() != Items.BOW ? 0.0F : (float)(stack.getMaxItemUseDuration() - entityIn.getItemInUseCount()) / 20.0F);

}

});

this.addPropertyOverride(new ResourceLocation("pulling"), new IItemPropertyGetter()

{

@SideOnly(Side.CLIENT)

public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)

{

return entityIn != null && entityIn.isHandActive() && entityIn.getActiveItemStack() == stack ? 1.0F : 0.0F;

}

});

}

private ItemStack findAmmo(EntityPlayer player)

{

if (this.isArrow(player.getHeldItem(EnumHand.OFF_HAND)))

{

return player.getHeldItem(EnumHand.OFF_HAND);

}

else if (this.isArrow(player.getHeldItem(EnumHand.MAIN_HAND)))

{

return player.getHeldItem(EnumHand.MAIN_HAND);

}

else

{

for (int i = 0; i < player.inventory.getSizeInventory(); ++i)

{

ItemStack itemstack = player.inventory.getStackInSlot(i);

if (this.isArrow(itemstack))

{

return itemstack;

}

}

return ItemStack.EMPTY;

}

}

protected boolean isArrow(ItemStack stack)

{

return stack.getItem() instanceof ItemArrow;

}

/**

* Called when the player stops using an Item (stops holding the right mouse button).

*/

public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)

{

if (entityLiving instanceof EntityPlayer)

{

EntityPlayer entityplayer = (EntityPlayer)entityLiving;

boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;

ItemStack itemstack = this.findAmmo(entityplayer);

int i = this.getMaxItemUseDuration(stack) - timeLeft;

i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, entityplayer, i, !itemstack.isEmpty() || flag);

if (i < 0) return;

if (!itemstack.isEmpty() || flag)

{

if (itemstack.isEmpty())

{

itemstack = new ItemStack(Items.ARROW);

itemstack.damageItem(1, entityplayer);

}

float f = getArrowVelocity(i);

if ((double)f >= 0.1D)

{

boolean flag1 = entityplayer.capabilities.isCreativeMode || (itemstack.getItem() instanceof ItemArrow && ((ItemArrow) itemstack.getItem()).isInfinite(itemstack, stack, entityplayer));

if (!worldIn.isRemote)

{

EntityGrenade entitysnowball = new EntityGrenade(worldIn, entityplayer);

entitysnowball.setHeadingFromThrower(entityplayer, entityplayer.rotationPitch, entityplayer.rotationYaw, 0.0F, 1.5F, 1.0F);

worldIn.spawnEntity(entitysnowball);

worldIn.playSound((EntityPlayer)null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

if (!flag1 && !entityplayer.capabilities.isCreativeMode)

{

itemstack.shrink(1);

 

if (itemstack.isEmpty())

{

entityplayer.inventory.deleteStack(itemstack);

}

}

entityplayer.addStat(StatList.getObjectUseStats(this));

}

}

}

}

 

}

/**

* Gets the velocity of the arrow entity from the bow's charge

*/

public static float getArrowVelocity(int charge)

{

float f = (float)charge / 20.0F;

f = (f * f + f * 2.0F) / 3.0F;

if (f > 1.0F)

{

f = 1.0F;

}

return f;

}

/**

* How long it takes to use or consume an item

*/

public int getMaxItemUseDuration(ItemStack stack)

{

return 72000;

}

/**

* returns the action that specifies what animation to play when the items is being used

*/

public EnumAction getItemUseAction(ItemStack stack)

{

return EnumAction.BOW;

}

public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)

{

ItemStack itemstack = playerIn.getHeldItem(handIn);

boolean flag = !this.findAmmo(playerIn).isEmpty();

ActionResult<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, worldIn, playerIn, handIn, flag);

if (ret != null) return ret;

if (!playerIn.capabilities.isCreativeMode && !flag)

{

return flag ? new ActionResult(EnumActionResult.PASS, itemstack) : new ActionResult(EnumActionResult.FAIL, itemstack);

}

else

{

playerIn.setActiveHand(handIn);

return new ActionResult(EnumActionResult.SUCCESS, itemstack);

}

}

/**

* Return the enchantability factor of the item, most of the time is based on material.

*/

public int getItemEnchantability()

{

return 1;

}

}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • If you are using AMD/ATI, get the latest drivers from their website - do not update via system
    • So I don't have any other mods and I try to install my first mod. At first I tried turning on Forge but it didn't work for me. I entered into these logs and this is what debug I found.   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0. 9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   -------------------------------------------------------------   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.428] [main/DEBUG] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Found launch services [fmlclientdev,forgeclient,minecraft,forgegametestserverdev,fmlserveruserdev,fmlclient,fmldatauserdev,forgeserverdev,forgeserveruserdev,forgeclientdev,forgeclientuserdev,forgeserver,forgedatadev,fmlserver,fmlclientuserdev,fmlserverdev,forgedatauserdev,testharness,forgegametestserveruserdev] [05maj2024 19:29:16.448] [main/DEBUG] [cpw.mods.modlauncher.NameMappingServiceHandler/MODLAUNCHER]: Found naming services : [srgtomcp] [05maj2024 19:29:16.468] [main/DEBUG] [cpw.mods.modlauncher.LaunchPluginHandler/MODLAUNCHER]: Found launch plugins: [mixin,eventbus,slf4jfixer,object_holder_definalize,runtime_enum_extender,capability_token_subclass,accesstransformer,runtimedistcleaner] [05maj2024 19:29:16.485] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Discovering transformation services [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path GAMEDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path MODSDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft\mods [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path CONFIGDIR is C:\Users\Tadeusz\AppData\Roaming\.minecraft\config [05maj2024 19:29:16.495] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path FMLCONFIG is C:\Users\Tadeusz\AppData\Roaming\.minecraft\config\fml.toml [05maj2024 19:29:16.550] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Found additional transformation services from discovery services:  [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   Can you help me with this?
    • So I don't have any other mods and I try to install my first mod. At first I tried turning on Forge but it didn't work for me. I entered into these logs and this is what debug I found.   [05maj2024 19:29:16.383] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, kod00, --version, 1.20.1-forge-47.2.30, --gameDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Tadeusz\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 0b6cb7d0d9794ea3995565e2c9e6961f, --accessToken, ????????, --clientId, ZmVhZDQ5MDEtODA2ZC00OTZhLTg1NWEtNDAzYzhmZGVhYzAx, --xuid, 2535448611541717, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Tadeusz\AppData\Roaming\.minecraft\quickPlay\java\1714930153126.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.30, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [05maj2024 19:29:16.393] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0. 9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [05maj2024 19:29:16.557] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [05maj2024 19:29:16.668] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6   Can you help me with this?  
    • Thank you so, so much for your help! I have implemented the code from the example you gave me and it has been excepted, the only downside currently being that the game crashes on launch, saying:  Caused by: java.lang.IllegalStateException: Cannot register new entries to DeferredRegister after RegisterEvent has been fired.  (Three times) and  Caused by: java.lang.ExceptionInInitializerError (Two times) With further scanning I have found this error: 2024-05-05T18:41:24.287+0100 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] I am not exactly sure what to do with these errors but I am sure I can resolve them. Once again, thank you for your help! It is much appreciated. 😁
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.