Jump to content

[1.16.4] Checking an ItemEntity is in water.


AzizD

Recommended Posts

    @SubscribeEvent
    public static void crimsonIngotCraft(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        if(entity instanceof ItemEntity){
            ItemEntity itemEntity = (ItemEntity) event.getEntity();
            if(itemEntity.getItem().getItem() == itemInit.RED_PEARL.get()){
                System.out.println("Working!!");
                if(itemEntity.isInWater()){
                    System.out.println("Not working :(");
                    itemEntity.remove();
                  	//FIXME Continue.
                }
            }
        }
    }

Everything is working except the

if(itemEntity.isInWater()){
                    System.out.println("Not working :(");
                    itemEntity.remove();
                }

this part. I wanna create a recipe that you throw an item into water and water transforms into another block.

Link to comment
Share on other sites

Well of course. That would only be true if the entity spawned already in the water.

(Try throwing your item while under water).

 

If you want to check for it being in water some time later you need to replace the entity with a custom entity and override the relevant method (there should already be one for when an entity is in water, or enters water, but Update would also work).

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

    @SubscribeEvent
    public static void crimsonIngotCraft(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        World world = entity.world;
        double posX = entity.getPosX();
        double posY = entity.getPosY();
        double posZ = entity.getPosZ();
        if(entity instanceof ItemEntity && !(entity instanceof SpecialItemEntity)){
            ItemEntity itemEntity = (ItemEntity) event.getEntity();
            if(itemEntity.getItem().getItem() == itemInit.RED_PEARL.get()){
                SpecialItemEntity ie = new SpecialItemEntity(entity.world,entity.getPosX(),entity.getPosY(),entity.getPosZ(),((ItemEntity) entity).getItem());
                event.setCanceled(true);
                SpecialItemEntity specialItemEntity = new SpecialItemEntity(world,posX,posY,posZ,((ItemEntity) entity).getItem().getStack());
            }
        }
    }

I changed code into this and SpecialItemEntity clas is:

package com.azizd.thunderbird.entities;

import com.azizd.thunderbird.init.blockInit;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class SpecialItemEntity extends ItemEntity {
    public SpecialItemEntity(World worldIn, double x, double y, double z, ItemStack stack) {
        super(worldIn, x, y, z, stack);
    }

    @Override
    public void tick() {
        BlockPos pos= new BlockPos(getPosX(),getPosY(),getPosZ());
        if(inWater){
            this.setDead();
            this.world.setBlockState(pos,blockInit.BLOCK_OF_RED_PEARL.get().getDefaultState());
        }
    }
}

The item deletes when i press the q button.

  • Like 1
Link to comment
Share on other sites

You are canceling the event but you are not spawning your custom entity in the world. Also if you just need to change your item into something else when that item touches water i think you could just override the onEntityItemUpdate method inside your item class

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Link to comment
Share on other sites

It is kinda working right now.

This is EventHandler part

public static void crimsonIngotCraft(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        World world = entity.world;
        double posX = entity.getPosX();
        double posY = entity.getPosY();
        double posZ = entity.getPosZ();
        if(entity instanceof ItemEntity && !(entity instanceof SpecialItemEntity)){
            ItemEntity itemEntity = (ItemEntity) event.getEntity();
            if(itemEntity.getItem().getItem() == itemInit.RED_PEARL.get()){
                event.setCanceled(true);
                SpecialItemEntity specialItemEntity = new SpecialItemEntity(world,posX,posY,posZ);
                specialItemEntity.setItem(itemEntity.getItem());
                world.addEntity(specialItemEntity);
            }
        }
    }

 

and this is the special item entity class
 

package com.azizd.thunderbird.entities;

import com.azizd.thunderbird.init.itemInit;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.extensions.IForgeEntity;

public class SpecialItemEntity extends ItemEntity implements IForgeEntity {
    public SpecialItemEntity(World worldIn, double x, double y, double z) {
        super(worldIn, x, y, z);
    }

    @Override
    public void tick() {
        ItemEntity entity = new ItemEntity(getEntityWorld(),this.getPosX(),this.getPosY(),this.getPosZ());
        if(this.inWater){
            this.setDead();
            entity.setItem(new ItemStack(itemInit.CRIMSON_INGOT.get()));
            this.world.addEntity(entity);
        }
        ItemStack stack = this.getItem();

        if (stack != null && stack.getItem() != null && stack.getItem().onEntityItemUpdate(this.getItem(),entity)) return;
        if(entity == null) this.setDead();
        else{
            super.tick();
            this.setDefaultPickupDelay();
            this.prevPosX = this.getPosX();
            this.prevPosY = this.getPosY();
            this.prevPosZ = this.getPosZ();
            this.setMotion(0,-0.098,0);
        }
    }
}

 

The item converting part is complete but i can't pickup the item i threw and motion part is bad. When i  throw a whole stack it gives 1 single item rather than giving a stack.

Link to comment
Share on other sites

3 hours ago, AzizD said:

ItemEntity entity = new ItemEntity(getEntityWorld(),this.getPosX(),this.getPosY(),this.getPosZ());

Why are you creating a new ItemEntity every tick?

  

3 hours ago, AzizD said:

if (stack != null && stack.getItem() != null

Neither of these will ever be true.

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

No, you're creating a new entity, diddling some of its values, and then throwing it in the garbage when the method returns.

On 1/4/2021 at 4:21 PM, AzizD said:

if(entity == null)

This is also never false because you set entity = new ItemEntity already.

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

Finally i fixed all the problems. Now it works exactly what i want.

 

This is my EventHandler Method 

@SubscribeEvent
    public static void crimsonIngotCraft(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        World world = entity.world;
        double posX = entity.getPosX();
        double posY = entity.getPosY();
        double posZ = entity.getPosZ();
        if(entity instanceof ItemEntity && !(entity instanceof SpecialItemEntity)){
            ItemEntity itemEntity = (ItemEntity) event.getEntity();
            if(itemEntity.getItem().getItem() == itemInit.RED_PEARL.get()){
                event.setCanceled(true);
                SpecialItemEntity specialItemEntity = new SpecialItemEntity(world,posX,posY,posZ);
                specialItemEntity.setItem(itemEntity.getItem().getStack());
                specialItemEntity.setPickupDelay(50);
                specialItemEntity.setMotion(itemEntity.getMotion());
                world.addEntity(specialItemEntity);
            }
        }
    }

 

And this is my SpecialItemEntity class

package com.azizd.thunderbird.entities;

import com.azizd.thunderbird.init.itemInit;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.extensions.IForgeEntity;

public class SpecialItemEntity extends ItemEntity implements IForgeEntity {
    public SpecialItemEntity(World worldIn, double x, double y, double z) {
        super(worldIn, x, y, z);
    }
    ItemEntity entity = new ItemEntity(getEntityWorld(),this.getPosX(),this.getPosY(),this.getPosZ());

    @Override
    public void tick() {
        if(this.inWater){
            this.setDead();
            int count = this.getItem().getCount();
            entity.setPosition(this.getPosX(),this.getPosY(),this.getPosZ());
            entity.setItem(new ItemStack(itemInit.CRIMSON_INGOT.get()));
            entity.getItem().setCount(count);
            this.world.addEntity(entity);
        }
        else{
            super.tick();
        }
    }
}


Thanks for the helps.

Link to comment
Share on other sites

3 hours ago, AzizD said:

ItemEntity entity = new ItemEntity(getEntityWorld(),this.getPosX(),this.getPosY(),this.getPosZ());

Why is this a class property and not a local variable?

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

17 minutes ago, AzizD said:

This is not the issue.

I didn't say it was. I was asking why you did it because its stupid.

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

  • 2 weeks later...

Finally i fixed the all problems. I added this condition and thats all.

world.chunkExists(specialItemEntity.chunkCoordX,specialItemEntity.chunkCoordZ)

 

Final code is :

 

EventHandler class

package com.azizd.thunderbird.events;

import com.azizd.thunderbird.Thunderbird;
import com.azizd.thunderbird.entities.SpecialItemEntity;
import com.azizd.thunderbird.init.itemInit;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = Thunderbird.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class EventHandler {
    public static void register(){}

    @SubscribeEvent
    public static void crimsonIngotCraft(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        World world = entity.world;
        double posX = entity.getPosX();
        double posY = entity.getPosY();
        double posZ = entity.getPosZ();
        SpecialItemEntity specialItemEntity = new SpecialItemEntity(world, posX, posY, posZ);

        if(entity instanceof ItemEntity && !(entity instanceof SpecialItemEntity)){
            ItemEntity itemEntity = (ItemEntity) event.getEntity();
            ItemStack item = itemEntity.getItem();
            if(item.getItem() == itemInit.RED_PEARL.get()){
                if(world.chunkExists(specialItemEntity.chunkCoordX,specialItemEntity.chunkCoordZ)) {
                    event.setCanceled(true);
                    specialItemEntity.setItem(itemEntity.getItem().getStack());
                    specialItemEntity.setPickupDelay(30);
                    specialItemEntity.setMotion(itemEntity.getMotion());
                    world.addEntity(specialItemEntity);
                }
            }
        }
    }
}

 

SpecialItemEntity class

package com.azizd.thunderbird.entities;

import com.azizd.thunderbird.init.itemInit;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class SpecialItemEntity extends ItemEntity {
    private ItemEntity entity = new ItemEntity(this.world,this.getPosX(),this.getPosY(),this.getPosZ());

    public SpecialItemEntity(World worldIn, double x, double y, double z) {
        super(worldIn, x, y, z);
    }

    @Override
    public void tick() {
        if(this.inWater && !this.world.isRemote){
            this.setDead();
            int count = this.getItem().getCount();
            entity.setPosition(this.getPosX(),this.getPosY(),this.getPosZ());
            entity.setItem(new ItemStack(itemInit.CRIMSON_INGOT.get()));
            entity.getItem().setCount(count);
            this.world.addEntity(entity);
        }
        else{
            super.tick();
        }
    }
}

 

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.