Posted February 15, 20223 yr I am trying to make any LivingEntity that collides with a custom projectile ride the player. However after some testing, Projectile$onHitEntity(EntityHitResult entityHitResult) is client-side only, and both the client and the server need to be notified that an entity is riding another. Is there a way to comminate to the server? public class Carrier extends Snowball { private LivingEntity player; public Carrier(Level level, LivingEntity player) { super(level, player); this.player = player; } @Override protected void onHitEntity(EntityHitResult entityHitResult) { //client-side only entityHitResult.getEntity().startRiding(player); } }
February 16, 20223 yr Author Ok, I didn't think about how that variable caused a memory leak. I used getOwner() instead but get the same result. public class Carrier extends Snowball { public Carrier(Level level, LivingEntity player) { super(level, player); } @Override protected void onHitEntity(EntityHitResult entityHitResult) { getOwner().playSound(SoundEvents.BREWING_STAND_BREW, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)); entityHitResult.getEntity().startRiding(getOwner()); } } EDIT: I meant to say server side only in my previous message Edited February 16, 20223 yr by Tablock_
February 16, 20223 yr Author 11 hours ago, diesieben07 said: getOwner can return null. You must handle this. Got it 11 hours ago, diesieben07 said: Yes, that is correct. You want it server side only. Ok, I want an entity that comes in contact with the projectile to ride the player. I do this by calling entityHitResult.getEntity().startRiding(getOwner()) (assume getOwner() is handled). But when I run the game, only the server believes that the entity is riding the player. I know this because the entity still appears in the same location when hit, but its drops spawn above the player's head when killed. I must somehow tell the client that the entity is riding the player too. Edited February 16, 20223 yr by Tablock_
February 18, 20223 yr Author I do not have a git repo, unfortunately. That is a good idea though, I will be creating one in the future. But since I'm running low on time at the moment I will give you it here directly. Note that ID$newItemProperties() just returns new Item.Properties().tab(Main.MOD_TAB). public class EntityCarrier extends Item { public EntityCarrier() { super(ID.newItemProperties()); } @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand interactionHand) { ItemStack itemStack = player.getItemInHand(interactionHand); player.playSound(SoundEvents.BONE_BLOCK_PLACE, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)); if(!level.isClientSide) { Carrier carrier = new Carrier(level, player); carrier.setItem(itemStack); carrier.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); level.addFreshEntity(carrier); } return InteractionResultHolder.consume(itemStack); } } public class Carrier extends Snowball { public Carrier(Level level, LivingEntity player) { super(level, player); } @Override protected void onHitEntity(EntityHitResult entityHitResult) { entityHitResult.getEntity().startRiding(getOwner()); } } And if you need it, this is how I am registering EntityCarrier: public static final RegistryObject<Item> ENTITY_CARRIER = Main.ITEMS.register("entity_carrier", () -> new EntityCarrier()); Do you think this isn't working because I need to create my own entity type? I'm stumped. If you would like anything else please let me know.
February 19, 20223 yr Author I have tried to follow what you have asked of me, but I get this error: Cannot invoke "net.minecraft.client.renderer.entity.EntityRenderer.shouldRender(net.minecraft.world.entity.Entity, net.minecraft.client.renderer.culling.Frustum, double, double, double)" because "entityrenderer" is null I have registered a new entity like so: public static final RegistryObject<EntityType<Carrier>> CARRIER = ENTITIES.register("entity_carrier", () -> EntityType.Builder.<Carrier>of(Carrier::new, MobCategory.MISC).sized(0.25F, 0.25F).clientTrackingRange(4).updateInterval(10).build("entity_carrier")); Please disregard how messy this is. I would like to get it working first. This is my custom entity class: public class Carrier extends ThrowableItemProjectile { public Carrier(EntityType<Carrier> entityType, Level level) { super(entityType, level); } public Carrier(Level level) { super(Main.CARRIER.get(), level); } @Override public Packet<?> getAddEntityPacket() { return NetworkHooks.getEntitySpawningPacket(this); } @Override protected void onHitEntity(EntityHitResult entityHitResult) { entityHitResult.getEntity().startRiding(getOwner()); } @Override protected Item getDefaultItem() { return ID.ENTITY_CARRIER.getItem(); } } And if you need it, my item class: public class EntityCarrier extends Item { public EntityCarrier() { super(ID.newItemProperties()); } @Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand interactionHand) { ItemStack itemStack = player.getItemInHand(interactionHand); player.playSound(SoundEvents.BONE_BLOCK_PLACE, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)); if(!level.isClientSide) { Carrier carrier = new Carrier(level); carrier.setItem(itemStack); carrier.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); level.addFreshEntity(carrier); } return InteractionResultHolder.consume(itemStack); } } 18 hours ago, diesieben07 said: I would recommend keeping a DeferredRegister and its entries in the same class. The DeferredRegister field should just be private. This ensures the proper initialization of things, which you must otherwise achieve in other way uglier ways. My entries will be in the same class and be private. I am trying to get it working first then clean up. No I didn't handle getOwner() yet. I also didn't make a git repo yet, I understand how much easier that will make this. I will try to set it up before you hear back from me. Edited February 19, 20223 yr by Tablock_
February 20, 20223 yr Author That makes sense. I did that and the Carrier entity renders correctly with no error now. However, entities still do not ride the player correctly after being hit with the Carrier projectile. Again, the server knows that the entity is riding the player, but the client is clueless. I created a github repository, here it is: https://github.com/Tablocked/MyFirstMod. Hopefully you can see it with no issues. I set up my entity renderer in a class named Client. I already showed you every other class that you should need. Edited February 20, 20223 yr by Tablock_
February 21, 20223 yr Author 16 hours ago, diesieben07 said: The root of your Git repo should be at your project root (where your build.gradle is). Got it, its changed now. 16 hours ago, diesieben07 said: What the heck is this? This was to load the ID class so that all of those static fields were initialized before I called DeferredRegister$register(IEventBus bus). I removed that now and replaced it with a new method I created: ID$init(). 17 hours ago, diesieben07 said: if (!level.isClientSide && getOwner() != null) { entityHitResult.getEntity().startRiding(getOwner()); if (getOwner() instanceof ServerPlayer ownerPlayer) { ownerPlayer.connection.send(new ClientboundSetPassengersPacket(ownerPlayer)); } } This did it! But I have a couple questions on why this works. Why do I need to check if its server side? Isn't it already server side only? What exactly is ServerPlayer? I see that it extends Player, but what's the difference? Is ClientboundSetPassengersPacket telling the client that the entity is riding the player? Thanks as always.
February 22, 20223 yr Author 22 hours ago, diesieben07 said: If you didn't use that ass-backwards way of registering things, you would not need hacks like this. This is not true, Class$forName has nothing to do with the way I registered things. It has to do with the fact that I did not call DeferredRegister$register in the ID class. I could have also made the DeferredRegister fields private in the ID class. Anyway, thank you for answering all of my questions and giving me all of your help.
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.