Jump to content

Entity Health Not Updating


someRandomKid

Recommended Posts

[1.15.2]

 

I am going to have a tough time describing this, so here is a video.

 

https://youtu.be/XEwj8rv0jdo

 

If you saw the video, you saw the after the car go damaged and then healed by my custom item, the car takes way more damage then it should when it hits the cactus. 

I am sure that it is because I am not updating the entity health on server side, so how am I supposed to update the entity's health (on server side)?

 

and here is my code (which heals the car)

 @SubscribeEvent
    public void keyInputEvent(InputEvent.KeyInputEvent event) {
        Minecraft minecraft = Minecraft.getInstance();
        PlayerEntity player = minecraft.player;
        if(player == null)
            return;
        LivingEntity car = (LivingEntity)player.getRidingEntity();
        if (car != null) {
            if (car.getName().getString().equals("Car")) {
                if (event.getKey() == GLFW.GLFW_KEY_R && event.getAction() == GLFW.GLFW_PRESS) {
                    if(player.getHeldItemMainhand().getItem() == RegistryHandler.GAS_CAN.get()){
                        ((Car)car).Refule();
                        car.heal(1);
                        player.getHeldItemMainhand().shrink(1);
                    }
                }
                if(event.getKey() == GLFW.GLFW_KEY_R && event.getAction() == GLFW.GLFW_PRESS){
                    if(player.getHeldItemMainhand().getItem() == RegistryHandler.REPAIR_KIT.get() && car.getHealth() != car.getMaxHealth()){
                        car.heal(6);
                        player.inventory.mainInventory.get(player.inventory.currentItem).shrink(1);
                    }
                }
            }
        }
    }

 

Edited by someRandomKid
Link to comment
Share on other sites

16 hours ago, someRandomKid said:

if (car.getName().getString().equals("Car")) {

Don't compare strings. Use instanceof. You already use a cast later down.

 

16 hours ago, someRandomKid said:

if (event.getKey() == GLFW.GLFW_KEY_R && event.getAction() == GLFW.GLFW_PRESS) {

16 hours ago, someRandomKid said:

if(event.getKey() == GLFW.GLFW_KEY_R && event.getAction() == GLFW.GLFW_PRESS){

Why are you performing this particular check twice?

 

16 hours ago, someRandomKid said:

if(player == null) return;

16 hours ago, someRandomKid said:

if (car != null) {

Use an early return here like you did with a null player.

 

16 hours ago, someRandomKid said:

((Car)car).Refule();

"Refuel."

 

And finally:
Why are you bothering with R keypresses? Why not use the systems Minecraft already has in place (right clicking with a stack)? Then you don't need this key press handler at all and can put this logic inside your item (where it belongs) and don't need to deal with custom packets.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Item has methods for onRightClick and onEntityInteract (you'll have to poke around for exact method names). Just override them and put your functionality there.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

here is code to override for those who are wondering.

 

 @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        Entity entity = playerIn.getRidingEntity();
        if(entity instanceof Car){
            Car car = (Car)entity;
            car.Refuel();
            car.heal(1);
            playerIn.inventory.mainInventory.get(playerIn.inventory.currentItem).shrink(1);
        }
        return ActionResult.resultPass(playerIn.getHeldItem(handIn));
    }

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.