User Tools

Site Tools


tutorial:projectiles

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:projectiles [2020/11/16 21:55] – fixing code spxctreofficialtutorial:projectiles [2022/04/29 03:38] – updated a method call ayutac
Line 108: Line 108:
  entity.damage(DamageSource.thrownProjectile(this, this.getOwner()), (float)i); // deals damage  entity.damage(DamageSource.thrownProjectile(this, this.getOwner()), (float)i); // deals damage
  
- if (entity instanceof LivingEntity) { // checks if entity is an instance of LivingEntity (meaning it is not a boat or minecart) + if (entity instanceof LivingEntity livingEntity) { // checks if entity is an instance of LivingEntity (meaning it is not a boat or minecart) 
- ((LivingEntity) entity).addStatusEffect((new StatusEffectInstance(StatusEffects.BLINDNESS, 20 * 3, 0))); // applies a status effect + livingEntity.addStatusEffect((new StatusEffectInstance(StatusEffects.BLINDNESS, 20 * 3, 0))); // applies a status effect 
- ((LivingEntity) entity).addStatusEffect((new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 3, 2))); // applies a status effect + livingEntity.addStatusEffect((new StatusEffectInstance(StatusEffects.SLOWNESS, 20 * 3, 2))); // applies a status effect 
- ((LivingEntity) entity).addStatusEffect((new StatusEffectInstance(StatusEffects.POISON, 20 * 3, 1))); // applies a status effect + livingEntity.addStatusEffect((new StatusEffectInstance(StatusEffects.POISON, 20 * 3, 1))); // applies a status effect 
- entity.playSound(SoundEvents.AMBIENT_CAVE, 2F, 1F); // plays a sound for the entity hit only+ livingEntity.playSound(SoundEvents.AMBIENT_CAVE, 2F, 1F); // plays a sound for the entity hit only
  }  }
  }  }
Line 120: Line 120:
  if (!this.world.isClient) { // checks if the world is client  if (!this.world.isClient) { // checks if the world is client
  this.world.sendEntityStatus(this, (byte)3); // particle?  this.world.sendEntityStatus(this, (byte)3); // particle?
- this.remove(); // kills the projectile+ this.kill(); // kills the projectile
  }  }
  
Line 202: Line 202:
  */  */
  if (!world.isClient) {  if (!world.isClient) {
- SnowballEntity snowballEntity = new SnowballEntity(world, user);+ PackedSnowballEntity snowballEntity = new PackedSnowballEntity(world, user);
  snowballEntity.setItem(itemStack);  snowballEntity.setItem(itemStack);
- snowballEntity.setProperties(user, user.pitch, user.yaw, 0.0F, 1.5F, 0F);+ snowballEntity.setVelocity(user, user.pitch, user.yaw, 0.0F, 1.5F, 0F); 
 +                        /* 
 +                        snowballEntity.setProperties(user, user.getPitch(), user.getYaw(), 0.0F, 1.5F, 1.0F); 
 +                        In 1.17,we will use setProperties instead of setVelocity.                                                         
 +                        */
  world.spawnEntity(snowballEntity); // spawns entity  world.spawnEntity(snowballEntity); // spawns entity
  }  }
Line 217: Line 221:
 } }
 </code> </code>
 +Make sure that the projectile that you are launching with this item is indeed your custom ''ProjectileEntity''. Verify this by checking ''PackedSnowballEntity snowballEntity = new PackedSnowballEntity(world, user);''
 +\\
 Now, we are finished with creating an item for the ''ProjectileEntity''. Keep in mind that if you do not understand how to create an item, refer to the [[https://fabricmc.net/wiki/tutorial:items|"Item" tutorial]]. Now, we are finished with creating an item for the ''ProjectileEntity''. Keep in mind that if you do not understand how to create an item, refer to the [[https://fabricmc.net/wiki/tutorial:items|"Item" tutorial]].
 \\ \\
Line 255: Line 260:
  @Override  @Override
  public void onInitializeClient() {  public void onInitializeClient() {
- EntityRendererRegistry.INSTANCE.register(ProjectileTutorialMod.PackedSnowballEntityType, (dispatcher, context) -> + EntityRendererRegistry.register(ProjectileTutorialMod.PackedSnowballEntityType, (context) -> 
- new FlyingItemEntityRenderer(dispatcher, context.getItemRenderer()));+  new FlyingItemEntityRenderer(context)); 
 + // older versions may have to use 
 + /* EntityRendererRegistry.INSTANCE.register(ProjectileTutorialMod.PackedSnowballEntityType, (context-> 
 + new FlyingItemEntityRenderer(context)); */
  [. . .]  [. . .]
  }  }
Line 271: Line 279:
  byteBuf.writeUuid(e.getUuid());  byteBuf.writeUuid(e.getUuid());
  byteBuf.writeVarInt(e.getEntityId());  byteBuf.writeVarInt(e.getEntityId());
 +                
  PacketBufUtil.writeVec3d(byteBuf, e.getPos());  PacketBufUtil.writeVec3d(byteBuf, e.getPos());
  PacketBufUtil.writeAngle(byteBuf, e.pitch);  PacketBufUtil.writeAngle(byteBuf, e.pitch);
  PacketBufUtil.writeAngle(byteBuf, e.yaw);  PacketBufUtil.writeAngle(byteBuf, e.yaw);
- return ServerSidePacketRegistry.INSTANCE.toPacket(packetID, byteBuf);+                /* 
 +                In 1.17,we use these. 
 +                byteBuf.writeVarInt(e.getId()); 
 +                 
 +                PacketBufUtil.writeVec3d(byteBuf, e.getPos()); 
 +                PacketBufUtil.writeAngle(byteBuf, e.getPitch()); 
 +                PacketBufUtil.writeAngle(byteBuf, e.getYaw());               
 +                */ 
 + 
 + return ServerPlayNetworking.createS2CPacket(packetID, byteBuf);
  }  }
  public static final class PacketBufUtil {  public static final class PacketBufUtil {
Line 399: Line 416:
 @Override @Override
  public void onInitializeClient() {  public void onInitializeClient() {
- EntityRendererRegistry.INSTANCE.register(ProjectileTutorialMod.PackedSnowballEntityType, (dispatcher, context) -> + EntityRendererRegistry.INSTANCE.register(ProjectileTutorialMod.PackedSnowballEntityType, (context) -> 
- new FlyingItemEntityRenderer(dispatcher, context.getItemRenderer()));+ new FlyingItemEntityRenderer(context));
  receiveEntityPacket();  receiveEntityPacket();
  }  }
Line 408: Line 425:
  
 Now, your projectile should be working in-game! Just make sure your textures are in the right place, and your item and projectile should be working. Now, your projectile should be working in-game! Just make sure your textures are in the right place, and your item and projectile should be working.
 +
 +If you would like to try out this projectile, download [[https://github.com/spxctreofficial/fabric-projectile-tutorial/releases/download/v1.0.0/projectile-tutorial-1.0.0.jar|here]].
  
 [INSERT USABLE PICTURE HERE] [INSERT USABLE PICTURE HERE]
tutorial/projectiles.txt · Last modified: 2024/03/08 01:56 by netuserget