Last active
August 7, 2023 13:59
-
-
Save TheYule/386b73020c68d0fe4813c54ac47d0721 to your computer and use it in GitHub Desktop.
Custom arrow for Minecraft Forge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "replace": false, | |
| "values": [ | |
| "modid goes gere:custom_arrow_item" | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Mod.EventBusSubscriber(modid = "modid goes here", bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) | |
| public class ClientEvents { | |
| @SubscribeEvent | |
| public static void onRegisterEntityRenderers(EntityRenderersEvent.RegisterRenderers event) { | |
| // Register the arrow renderer | |
| event.registerEntityRenderer(EntityInit.CUSTOM_ARROW_ENTITY.get(), CustomArrowEntityRenderer::new); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Make sure to register this as an item | |
| public class CustomArrowEntity extends AbstractArrow { | |
| public CustomArrowEntity(EntityType<CustomArrowEntity> type, Level world) { | |
| super(type, world); | |
| } | |
| public CustomArrowEntity(EntityType<CustomArrowEntity> type, double x, double y, double z, Level world) { | |
| super(type, x, y, z, world); | |
| } | |
| public CustomArrowEntity(EntityType<CustomArrowEntity> type, LivingEntity shooter, Level world) { | |
| super(type, shooter, world); | |
| } | |
| @Override | |
| protected @NotNull ItemStack getPickupItem() { | |
| return new ItemStack(ItemInit.CUSTOM_ARROW.get()); | |
| } | |
| protected void onHitEntity(@NotNull EntityHitResult ray) { | |
| Entity entity = ray.getEntity(); | |
| // Do something if the arrow hits an enemy | |
| super.onHitEntity(ray); | |
| } | |
| @Override | |
| protected void tickDespawn() { | |
| // You could check how many ticks have gone by and despawn the arrow, or do something else. Here it just despawns immediately | |
| this.discard(); | |
| } | |
| @Override | |
| public @NotNull Packet<?> getAddEntityPacket() { | |
| return NetworkHooks.getEntitySpawningPacket(this); | |
| } | |
| // Adapted from skeleton entity | |
| public static void shoot(LivingEntity shooter, LivingEntity target, Level level) { | |
| CustomArrowEntity arrow = new CustomArrowEntity(EntityInit.CUSTOM_ARROW_ENTITY.get(), shooter, level); | |
| double vx = target.getX() - shooter.getX(); | |
| double vz = target.getZ() - shooter.getZ(); | |
| double v = Math.sqrt(vx * vx + vz * vz); | |
| double vy = target.getY((double) 2 / 3) - arrow.getY() + v * .2; | |
| arrow.shoot(vx, vy, vz, 1.5f, 2); | |
| level.addFreshEntity(arrow); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @OnlyIn(Dist.CLIENT) | |
| public class CustomArrowEntityRenderer extends ArrowRenderer<CustomArrowEntity> { | |
| // Put your custom texture here | |
| public static final ResourceLocation TEXTURE = new ResourceLocation("modid goes here, "textures/entity/custom_arrow.png"); | |
| public CustomArrowEntityRenderer(EntityRendererProvider.Context manager) { | |
| super(manager); | |
| } | |
| public @NotNull ResourceLocation getTextureLocation(@NotNull CustomArrowEntity arrow) { | |
| // You can have the texture change based on the entity's health, age, etc. Here I just have it always return the same texture | |
| return TEXTURE; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Make sure to register this as an item | |
| public class CustomArrowItem extends ArrowItem { | |
| public CustomArrowItem(Properties properties) { | |
| super(properties); | |
| } | |
| @Override | |
| public @NotNull AbstractArrow createArrow(@NotNull Level world, @NotNull ItemStack ammoStack, @NotNull LivingEntity shooter) { | |
| return new CustomArrowEntity(EntityInit.CUSTOM_ARROW_ENTITY.get(), shooter, world); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class EntityInit { | |
| public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, "modid goes here"); | |
| public static final RegistryObject<EntityType<CustomArrowEntity>> CUSTOM_ARROW_ENTITY = ENTITY_TYPES.register("custom_arrow", () -> EntityType.Builder.of((EntityType.EntityFactory<CustomArrowEntity>) CustomArrowEntity::new, MobCategory.MISC).sized(.5F, .5F).build("modid goes here:custom_arrow")); | |
| } |
Author
Author
Make sure to put the arrows.json file in resources/data/minecraft/tags/items.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry it's a bit complicated just for an arrow...