Advertisement
Guest User

MCreator YourBowItem.JAVA Fixed Code

a guest
Jan 20th, 2024
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1.  
  2. package net.mcreator.yourmodid.item;
  3.  
  4. import net.minecraftforge.registries.ObjectHolder;
  5. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  6.  
  7. import net.minecraft.world.World;
  8. import net.minecraft.util.SoundEvents;
  9. import net.minecraft.util.SoundCategory;
  10. import net.minecraft.util.Hand;
  11. import net.minecraft.util.ActionResultType;
  12. import net.minecraft.util.ActionResult;
  13. import net.minecraft.stats.Stats;
  14. import net.minecraft.item.UseAction;
  15. import net.minecraft.item.Items;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.item.ItemGroup;
  18. import net.minecraft.item.Item;
  19. import net.minecraft.item.ArrowItem;
  20. import net.minecraft.entity.projectile.ArrowEntity;
  21. import net.minecraft.entity.projectile.AbstractArrowEntity;
  22. import net.minecraft.entity.player.PlayerEntity;
  23. import net.minecraft.entity.LivingEntity;
  24. import net.minecraft.entity.EntityType;
  25. import net.minecraft.entity.EntityClassification;
  26. import net.minecraft.enchantment.Enchantments;
  27. import net.minecraft.enchantment.EnchantmentHelper;
  28.  
  29. import net.mcreator.yourmodid.entity.renderer.YourBowRenderer;
  30. import net.mcreator.yourmodid.YourModIDModElements;
  31.  
  32. @YourModIDModElements.ModElement.Tag
  33. public class YourBowItem extends YourModIDModElements.ModElement {
  34. @ObjectHolder("your_mod_id:your_bow")
  35. public static final Item block = null;
  36. public static final EntityType arrow = (EntityType.Builder.<ArrowEntity>create(ArrowEntity::new, EntityClassification.MISC).size(0.5f, 0.5f))
  37. .build("projectile_your_bow").setRegistryName("projectile_your_bow");
  38.  
  39. public YourBowItem(YourModIDModElements instance) {
  40. super(instance, 87);
  41. FMLJavaModLoadingContext.get().getModEventBus().register(new YourBowRenderer.ModelRegisterHandler());
  42. }
  43.  
  44. @Override
  45. public void initElements() {
  46. elements.items.add(() -> new ItemRanged());
  47. elements.entities.add(() -> arrow);
  48. }
  49.  
  50. public static class ItemRanged extends Item {
  51. public ItemRanged() {
  52. super(new Item.Properties().group(ItemGroup.COMBAT).maxDamage("bow durability"));
  53. setRegistryName("your_bow");
  54. }
  55.  
  56. @Override
  57. public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity entity, Hand hand) {
  58. entity.setActiveHand(hand);
  59. return new ActionResult(ActionResultType.SUCCESS, entity.getHeldItem(hand));
  60. }
  61.  
  62. @Override
  63. public UseAction getUseAction(ItemStack itemstack) {
  64. return UseAction.BOW;
  65. }
  66.  
  67. @Override
  68. public int getUseDuration(ItemStack itemstack) {
  69. return 72000;
  70. }
  71.  
  72. @Override
  73. public void onPlayerStoppedUsing(ItemStack stack, World worldIn, LivingEntity entityLiving, int timeLeft) {
  74. if (entityLiving instanceof PlayerEntity) {
  75. PlayerEntity playerentity = (PlayerEntity) entityLiving;
  76. boolean flag = playerentity.abilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, stack) > 0;
  77. ItemStack itemstack = playerentity.findAmmo(stack);
  78. int i = this.getUseDuration(stack) - timeLeft;
  79. i = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(stack, worldIn, playerentity, i, !itemstack.isEmpty() || flag);
  80. if (i < 0)
  81. return;
  82. if (!itemstack.isEmpty() || flag) {
  83. if (itemstack.isEmpty()) {
  84. itemstack = new ItemStack(Items.ARROW);
  85. }
  86. float f = getArrowVelocity(i);
  87. if (!((double) f < 0.1D)) {
  88. boolean flag1 = playerentity.abilities.isCreativeMode || (itemstack.getItem() instanceof ArrowItem
  89. && ((ArrowItem) itemstack.getItem()).isInfinite(itemstack, stack, playerentity));
  90. if (!worldIn.isRemote) {
  91. ArrowItem arrowitem = (ArrowItem) (itemstack.getItem() instanceof ArrowItem ? itemstack.getItem() : Items.ARROW);
  92. AbstractArrowEntity abstractarrowentity = arrowitem.createArrow(worldIn, itemstack, playerentity);
  93. abstractarrowentity = customArrow(abstractarrowentity);
  94. abstractarrowentity.func_234612_a_(playerentity, playerentity.rotationPitch, playerentity.rotationYaw, 0.0F, f * 3.0F,
  95. 1.0F);
  96. if (f == 1.0F) {
  97. abstractarrowentity.setIsCritical(true);
  98. }
  99. int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, stack);
  100. if (j > 0) {
  101. abstractarrowentity.setDamage(abstractarrowentity.getDamage() + (double) j * 0.5D + 0.5D);
  102. }
  103. int k = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, stack);
  104. if (k > 0) {
  105. abstractarrowentity.setKnockbackStrength(k);
  106. }
  107. if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, stack) > 0) {
  108. abstractarrowentity.setFire(100);
  109. }
  110. stack.damageItem(1, playerentity, (p_220009_1_) -> {
  111. p_220009_1_.sendBreakAnimation(playerentity.getActiveHand());
  112. });
  113. if (flag1 || playerentity.abilities.isCreativeMode
  114. && (itemstack.getItem() == Items.SPECTRAL_ARROW || itemstack.getItem() == Items.TIPPED_ARROW)) {
  115. abstractarrowentity.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
  116. }
  117. worldIn.addEntity(abstractarrowentity);
  118. }
  119. worldIn.playSound((PlayerEntity) null, playerentity.getPosX(), playerentity.getPosY(), playerentity.getPosZ(),
  120. SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (random.nextFloat() * 2F + 3F) + 2 * 12.0F);
  121. if (!flag1 && !playerentity.abilities.isCreativeMode) {
  122. itemstack.shrink(1);
  123. if (itemstack.isEmpty()) {
  124. playerentity.inventory.deleteStack(itemstack);
  125. }
  126. }
  127. playerentity.addStat(Stats.ITEM_USED.get(this));
  128. }
  129. }
  130. }
  131. }
  132.  
  133. /**
  134. * Gets the velocity of the arrow entity from the bow's charge
  135. */
  136. public static float getArrowVelocity(int charge) {
  137. float f = (float) charge / 20.0F;
  138. f = (f * f + f * 2.0F) / 3.0F;
  139. if (f > 1.0F) {
  140. f = 1.0F;
  141. }
  142. return f;
  143. }
  144.  
  145. /**
  146. * Get the predicate to match ammunition when searching the player's inventory, not their main/offhand
  147. */
  148. public AbstractArrowEntity customArrow(AbstractArrowEntity arrow) {
  149. return arrow;
  150. }
  151.  
  152. public int func_230305_d_() {
  153. return 15;
  154. }
  155. }
  156. }
  157.  
  158.  
Tags: #MCreator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement