Advertisement
Waltini

Bow Mcreator Error

Oct 30th, 2022 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1.  
  2. package net.mcreator.projectsmithery.item;
  3.  
  4. import net.minecraftforge.registries.ObjectHolder;
  5. import net.minecraftforge.registries.ForgeRegistries;
  6. import net.minecraftforge.fml.network.NetworkHooks;
  7. import net.minecraftforge.fml.network.FMLPlayMessages;
  8. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  9. import net.minecraftforge.api.distmarker.OnlyIn;
  10. import net.minecraftforge.api.distmarker.Dist;
  11.  
  12. import net.minecraft.world.World;
  13. import net.minecraft.util.math.MathHelper;
  14. import net.minecraft.util.SoundCategory;
  15. import net.minecraft.util.ResourceLocation;
  16. import net.minecraft.util.Hand;
  17. import net.minecraft.util.ActionResultType;
  18. import net.minecraft.util.ActionResult;
  19. import net.minecraft.network.IPacket;
  20. import net.minecraft.item.UseAction;
  21. import net.minecraft.item.ShootableItem;
  22. import net.minecraft.item.Items;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.item.ItemGroup;
  25. import net.minecraft.item.Item;
  26. import net.minecraft.item.BowItem;
  27. import net.minecraft.entity.projectile.AbstractArrowEntity;
  28. import net.minecraft.entity.player.ServerPlayerEntity;
  29. import net.minecraft.entity.player.PlayerEntity;
  30. import net.minecraft.entity.LivingEntity;
  31. import net.minecraft.entity.IRendersAsItem;
  32. import net.minecraft.entity.EntityType;
  33. import net.minecraft.entity.EntityClassification;
  34. import net.minecraft.entity.Entity;
  35.  
  36. import net.mcreator.projectsmithery.entity.renderer.ObsidianLongbowRenderer;
  37. import net.mcreator.projectsmithery.ProjectSmitheryModElements;
  38.  
  39. import java.util.Random;
  40.  
  41. @ProjectSmitheryModElements.ModElement.Tag
  42. public class ObsidianLongbowItem extends ProjectSmitheryModElements.ModElement {
  43. @ObjectHolder("project_smithery:obsidian_longbow")
  44. public static final Item block = null;
  45. public static final EntityType arrow = (EntityType.Builder.<ArrowCustomEntity>create(ArrowCustomEntity::new, EntityClassification.MISC)
  46. .setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(ArrowCustomEntity::new)
  47. .size(0.5f, 0.5f)).build("projectile_obsidian_longbow").setRegistryName("projectile_obsidian_longbow");
  48.  
  49. public ObsidianLongbowItem(ProjectSmitheryModElements instance) {
  50. super(instance, 43);
  51. FMLJavaModLoadingContext.get().getModEventBus().register(new ObsidianLongbowRenderer.ModelRegisterHandler());
  52. }
  53.  
  54. @Override
  55. public void initElements() {
  56. elements.items.add(() -> new ItemRanged());
  57. elements.entities.add(() -> arrow);
  58. }
  59.  
  60. public static class ItemRanged extends Item {
  61. public ItemRanged() {
  62. super(new Item.Properties().group(ItemGroup.COMBAT).maxDamage(100));
  63. setRegistryName("obsidian_longbow");
  64. }
  65.  
  66. @Override
  67. public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity entity, Hand hand) {
  68. entity.setActiveHand(hand);
  69. return new ActionResult(ActionResultType.SUCCESS, entity.getHeldItem(hand));
  70. }
  71.  
  72. @Override
  73. public UseAction getUseAction(ItemStack itemstack) {
  74. return UseAction.BOW;
  75. }
  76.  
  77. @Override
  78. public int getUseDuration(ItemStack itemstack) {
  79. return 72000;
  80. }
  81.  
  82. @Override
  83. public void onPlayerStoppedUsing(ItemStack itemstack, World world, LivingEntity entityLiving, int timeLeft) {
  84. if (!world.isRemote && entityLiving instanceof ServerPlayerEntity) {
  85. ServerPlayerEntity entity = (ServerPlayerEntity) entityLiving;
  86. double x = entity.getPosX();
  87. double y = entity.getPosY();
  88. double z = entity.getPosZ();
  89. boolean flag = entity.abilities.isCreativeMode;
  90. ItemStack stack = ShootableItem.getHeldAmmo(entity, e -> e.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem());
  91. int h = this.getUseDuration(itemstack) - timeLeft;
  92. h = net.minecraftforge.event.ForgeEventFactory.onArrowLoose(itemstack, world, entity, h, flag || stack != ItemStack.EMPTY);
  93. if (h < 0)
  94. return;
  95. if (stack == ItemStack.EMPTY) {
  96. for (int i = 0; i < entity.inventory.mainInventory.size(); i++) {
  97. ItemStack teststack = entity.inventory.mainInventory.get(i);
  98. if (teststack != null && teststack.getItem() == new ItemStack(Items.ARROW, (int) (1)).getItem()) {
  99. stack = teststack;
  100. break;
  101. }
  102. }
  103. }
  104. if (flag || stack != ItemStack.EMPTY) {
  105. float j = getArrowVelocity(h);
  106. if (!((double) j < 0.1D)) {
  107. ArrowCustomEntity entityarrow = shoot(world, entity, random, 1f, 4, 0);
  108. itemstack.damageItem(1, entity, e -> e.sendBreakAnimation(entity.getActiveHand()));
  109. if (entity.abilities.isCreativeMode) {
  110. entityarrow.pickupStatus = AbstractArrowEntity.PickupStatus.CREATIVE_ONLY;
  111. } else {
  112. if (new ItemStack(Items.ARROW, (int) (1)).isDamageable()) {
  113. if (stack.attemptDamageItem(1, random, entity)) {
  114. stack.shrink(1);
  115. stack.setDamage(0);
  116. if (stack.isEmpty())
  117. entity.inventory.deleteStack(stack);
  118. }
  119. } else {
  120. stack.shrink(1);
  121. if (stack.isEmpty())
  122. entity.inventory.deleteStack(stack);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. @OnlyIn(value = Dist.CLIENT, _interface = IRendersAsItem.class)
  132. public static class ArrowCustomEntity extends AbstractArrowEntity implements IRendersAsItem {
  133. public ArrowCustomEntity(FMLPlayMessages.SpawnEntity packet, World world) {
  134. super(arrow, world);
  135. }
  136.  
  137. public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, World world) {
  138. super(type, world);
  139. }
  140.  
  141. public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, double x, double y, double z, World world) {
  142. super(type, x, y, z, world);
  143. }
  144.  
  145. public ArrowCustomEntity(EntityType<? extends ArrowCustomEntity> type, LivingEntity entity, World world) {
  146. super(type, entity, world);
  147. }
  148.  
  149. @Override
  150. public IPacket<?> createSpawnPacket() {
  151. return NetworkHooks.getEntitySpawningPacket(this);
  152. }
  153.  
  154. @Override
  155. @OnlyIn(Dist.CLIENT)
  156. public ItemStack getItem() {
  157. return new ItemStack(Items.ARROW);
  158. }
  159.  
  160. @Override
  161. protected ItemStack getArrowStack() {
  162. return ItemStack.EMPTY;
  163. }
  164.  
  165. @Override
  166. protected void arrowHit(LivingEntity entity) {
  167. super.arrowHit(entity);
  168. entity.setArrowCountInEntity(entity.getArrowCountInEntity() - 1);
  169. }
  170.  
  171. @Override
  172. public void tick() {
  173. super.tick();
  174. double x = this.getPosX();
  175. double y = this.getPosY();
  176. double z = this.getPosZ();
  177. World world = this.world;
  178. Entity entity = this.func_234616_v_();
  179. Entity immediatesourceentity = this;
  180. if (this.inGround)
  181. this.remove();
  182. }
  183. }
  184.  
  185. public static ArrowCustomEntity shoot(World world, LivingEntity entity, Random random, float power, double damage, int knockback) {
  186. ArrowCustomEntity entityarrow = new ArrowCustomEntity(arrow, entity, world);
  187. entityarrow.shoot(entity.getLook(1).x, entity.getLook(1).y, entity.getLook(1).z, power * 2, 0);
  188. entityarrow.setSilent(true);
  189. entityarrow.setIsCritical(false);
  190. entityarrow.setDamage(damage);
  191. entityarrow.setKnockbackStrength(knockback);
  192. world.addEntity(entityarrow);
  193. double x = entity.getPosX();
  194. double y = entity.getPosY();
  195. double z = entity.getPosZ();
  196. world.playSound((PlayerEntity) null, (double) x, (double) y, (double) z,
  197. (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.arrow.shoot")),
  198. SoundCategory.PLAYERS, 1, 1f / (random.nextFloat() * 0.5f + 1) + (power / 2));
  199. return entityarrow;
  200. }
  201.  
  202. public static ArrowCustomEntity shoot(LivingEntity entity, LivingEntity target) {
  203. ArrowCustomEntity entityarrow = new ArrowCustomEntity(arrow, entity, entity.world);
  204. double d0 = target.getPosY() + (double) target.getEyeHeight() - 1.1;
  205. double d1 = target.getPosX() - entity.getPosX();
  206. double d3 = target.getPosZ() - entity.getPosZ();
  207. entityarrow.shoot(d1, d0 - entityarrow.getPosY() + (double) MathHelper.sqrt(d1 * d1 + d3 * d3) * 0.2F, d3, 1f * 2, 12.0F);
  208. entityarrow.setSilent(true);
  209. entityarrow.setDamage(5);
  210. entityarrow.setKnockbackStrength(5);
  211. entityarrow.setIsCritical(false);
  212. entity.world.addEntity(entityarrow);
  213. double x = entity.getPosX();
  214. double y = entity.getPosY();
  215. double z = entity.getPosZ();
  216. entity.world.playSound((PlayerEntity) null, (double) x, (double) y, (double) z,
  217. (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.arrow.shoot")),
  218. SoundCategory.PLAYERS, 1, 1f / (new Random().nextFloat() * 0.5f + 1));
  219. return entityarrow;
  220. }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement