Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Entity Sinking into ground


Recommended Posts

My Entity's hitbox (using F3 + B) is above ground and in place, but the actual entity is still in the ground (like halfway). When I get on it, I'm am on top of the hitbox

Here is a picture:

5CJNcMZ.png

 

Code:

 

Entity:

 

package com.apmods.swbf2.entity;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.apmods.swbf2.item.ItemManager;

public class EntityIFT extends Entity implements IEntityAdditionalSpawnData, IInventory
{
public double speed;
public double accelaration;
public double maxSpeed;


public String owner;

public String letter = "X";


private ItemStack[] invItems = new ItemStack[18];

public EntityIFT(World world)
{
	super(world);
	this.preventEntitySpawning = true;
	this.setSize(3F, 2f);
	this.stepHeight = 1.1F;
	this.accelaration = 0.1D;
	this.maxSpeed = 0.5D;
}
public EntityIFT(World world, String l)
{
	this(world);
	this.dataWatcher.updateObject(27, l);
}

public AxisAlignedBB getBoundingBox(){
	return this.getEntityBoundingBox();
}

/**
 * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
 * prevent them from trampling crops
 */
protected boolean canTriggerWalking()
{
	return false;
}

protected void entityInit()
{
	this.dataWatcher.addObject(17, new Integer(0));
	this.dataWatcher.addObject(18, new Integer(1));
	this.dataWatcher.addObject(19, new Float(0.0F));
	this.dataWatcher.addObject(20, new Float(this.speed));
	this.dataWatcher.addObject(21, new Float(this.rotationYaw));
	this.dataWatcher.addObject(22, new Float(this.motionX));
	this.dataWatcher.addObject(23, new Float(this.motionY));
	this.dataWatcher.addObject(24, new Float(this.motionZ));
	this.dataWatcher.addObject(26, new Float(this.speed));
	this.dataWatcher.addObject(27, "T");
}



/**
 * Returns true if this entity should push and be pushed by other entities when colliding.
 */
public boolean canBePushed()
{
	return true;
}

public EntityIFT(World world, int x, int y, int z, String owner, String l)
{
	this(world, l);
	this.setPosition(x, y + (double)this.height, z);
	this.motionX = 0.0D;
	this.motionY = 0.0D;
	this.motionZ = 0.0D;
	this.prevPosX = x;
	this.prevPosY = y;
	this.prevPosZ = z;
	this.owner = owner;
}

/**
 * Returns the Y offset from the entity's position for any entity riding this one.
 */
public double getMountedYOffset()
{
	return (double)this.height;
}

/**
 * Called when the entity is attacked.
 */
public boolean attackEntityFrom(DamageSource damageSource, float damage)
{
	if (this.isEntityInvulnerable(damageSource))
	{
		return false;
	}
	else if (!this.worldObj.isRemote && !this.isDead)
	{
		this.setForwardDirection(-this.getForwardDirection());
		this.setTimeSinceHit(10);
		this.setDamageTaken(this.getDamageTaken() + damage * 6.0F);
		this.setBeenAttacked();
		boolean isCreativeMode = damageSource.getEntity() instanceof EntityPlayer && ((EntityPlayer)damageSource.getEntity()).capabilities.isCreativeMode;

		if(!isCreativeMode)
		{
			if(damageSource.getEntity() instanceof EntityPlayer)
			{
				EntityPlayer player = (EntityPlayer) damageSource.getEntity();
				if(!player.getName().equalsIgnoreCase(owner))
				{
					player.addChatMessage(new ChatComponentText("Your name isn't " + owner + "!"));
					return false;
				}
			}
		}

		if (isCreativeMode || this.getDamageTaken() > 40.0F)
		{
			if (this.riddenByEntity != null)
			{
				this.riddenByEntity.mountEntity(this);
			}

			if (!isCreativeMode)
			{
				if(this.getLetter().equals("T")){
					this.dropItem(ItemManager.iftt, 1);
				}
				else{
					this.dropItem(ItemManager.iftx, 1);
				}



				for (int i = 0; i < this.getSizeInventory(); ++i)
				{
					ItemStack itemstack = this.getStackInSlot(i);

					if (itemstack != null)
					{
						float f = this.rand.nextFloat() * 0.8F + 0.1F;
						float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
						float f2 = this.rand.nextFloat() * 0.8F + 0.1F;

						while (itemstack.stackSize > 0)
						{
							int j = this.rand.nextInt(21) + 10;

							if (j > itemstack.stackSize)
							{
								j = itemstack.stackSize;
							}

							itemstack.stackSize -= j;
							EntityItem entityitem = new EntityItem(this.worldObj, this.posX + (double)f, this.posY + (double)f1, this.posZ + (double)f2, new ItemStack(itemstack.getItem(), j, itemstack.getItemDamage()));
							float f3 = 0.05F;
							entityitem.motionX = (double)((float)this.rand.nextGaussian() * f3);
							entityitem.motionY = (double)((float)this.rand.nextGaussian() * f3 + 0.2F);
							entityitem.motionZ = (double)((float)this.rand.nextGaussian() * f3);
							this.worldObj.spawnEntityInWorld(entityitem);
						}
					}
				}
			}

			this.setDead();
		}

		return true;
	}
	else
	{
		return true;
	}
}

/**
 * Setups the entity to do the hurt animation. Only used by packets in multiplayer.
 */
@SideOnly(Side.CLIENT)
public void performHurtAnimation()
{
	this.setForwardDirection(-this.getForwardDirection());
	this.setTimeSinceHit(10);
	this.setDamageTaken(this.getDamageTaken() * 11.0F);
}

/**
 * Returns true if other Entities should be prevented from moving through this Entity.
 */
public boolean canBeCollidedWith()
{
	return !this.isDead;
}

public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int par9)
{
	this.setPositionAndRotation(x, y + 0.026d, z, yaw, pitch);
}

/**
 * Called to update the entity's position/logic.
 */
public void onUpdate()
{
	super.onUpdate();



	if (this.getTimeSinceHit() > 0)
	{
		this.setTimeSinceHit(this.getTimeSinceHit() - 1);
	}

	if (this.getDamageTaken() > 0.0F)
	{
		this.setDamageTaken(this.getDamageTaken() - 1.0F);
	}

	this.prevPosX = this.posX;
	this.prevPosY = this.posY;
	this.prevPosZ = this.posZ;

	if(this.worldObj.isRemote)
	{
		this.speed = this.dataWatcher.getWatchableObjectFloat(20);
		this.rotationYaw = this.dataWatcher.getWatchableObjectFloat(21);
		this.motionX = this.dataWatcher.getWatchableObjectFloat(22);
		this.motionY = this.dataWatcher.getWatchableObjectFloat(23);
		this.motionZ = this.dataWatcher.getWatchableObjectFloat(24);
		this.speed = this.dataWatcher.getWatchableObjectFloat(26);
	}

	double cos = Math.cos(this.rotationYaw * Math.PI / 180.0f);
	double sin = Math.sin(this.rotationYaw * Math.PI / 180.0f);

	this.motionX = -sin * speed;
	this.motionZ = cos * speed;
	if(!this.onGround){
		this.motionY = -0.53;
	}

	if(this.riddenByEntity != null)
	{
		if(this.riddenByEntity instanceof EntityLivingBase)
		{
			EntityLivingBase entity = (EntityLivingBase) this.riddenByEntity;

			if(this.worldObj.isRemote)
			{	
			}

			if(!this.isInWater())
			{
				if(this.speed > 0.1)
				{
					this.worldObj.spawnParticle(EnumParticleTypes.CLOUD, posX + (rand.nextDouble() - 0.5D) * (double)width, (posY + rand.nextDouble() * (double)height) - 0.25D, posZ + (rand.nextDouble() - 0.5D) * (double)width, 0, 0, 0);
				}
			}

				if(!this.isInWater())
				{			
					this.speed += entity.moveForward * this.accelaration;

					if(speed > maxSpeed)
					{
						speed = maxSpeed;
					}
					if(speed < -maxSpeed)
					{
						speed = -maxSpeed;
					}
				}
				else
				{
					this.speed *= 0.8500;
				}



			if(entity.moveForward <= 0)
			{
				this.speed *= 0.8500;
			}

			this.rotationYaw = MathHelper.wrapAngleTo180_float(entity.getRotationYawHead());
		}
	}
	else
	{
		this.speed *= 0.8500;
	}


	this.setRotation(this.rotationYaw, this.rotationPitch);
	this.moveEntity(motionX, motionY, motionZ);

	if(!this.worldObj.isRemote)
	{
		this.dataWatcher.updateObject(20, new Float(this.speed));
		this.dataWatcher.updateObject(21, new Float(this.rotationYaw));
		this.dataWatcher.updateObject(22, new Float(this.motionX));
		this.dataWatcher.updateObject(23, new Float(this.motionY));
		this.dataWatcher.updateObject(24, new Float(this.motionZ));
		this.dataWatcher.updateObject(26, new Float(this.speed));
	}
}

public void updateRiderPosition()
{
	if (this.riddenByEntity != null)
	{
		double dZ = Math.cos((double)this.rotationYaw * Math.PI / 180.0D) * -0.1D;
		double dX = -Math.sin((double)this.rotationYaw * Math.PI / 180.0D) * -0.1D;
		this.riddenByEntity.setPosition(this.posX + dX, this.posY + this.getMountedYOffset() + this.riddenByEntity.getYOffset(), this.posZ + dZ);
	}
}

/**
 * (abstract) Protected helper method to write subclass entity data to NBT.
 */
public void writeEntityToNBT(NBTTagCompound nbt)
{
	nbt.setString("owner", owner);

	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.invItems.length; ++i)
	{
		if (this.invItems[i] != null)
		{
			NBTTagCompound slotTag = new NBTTagCompound();
			slotTag.setByte("Slot", (byte)i);
			this.invItems[i].writeToNBT(slotTag);
			nbttaglist.appendTag(slotTag);
		}
	}
	nbt.setString("letter", this.dataWatcher.getWatchableObjectString(27));
	nbt.setTag("Items", nbttaglist);
}

/**
 * (abstract) Protected helper method to read subclass entity data from NBT.
 */
public void readEntityFromNBT(NBTTagCompound nbt) 
{
	this.owner = nbt.getString("owner");

	NBTTagList nbttaglist = nbt.getTagList("Items", 10);
	this.invItems = new ItemStack[this.getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); ++i)
	{
		NBTTagCompound slotTag = nbttaglist.getCompoundTagAt(i);
		int j = slotTag.getByte("Slot") & 255;

		if (j >= 0 && j < this.invItems.length)
		{
			this.invItems[j] = ItemStack.loadItemStackFromNBT(slotTag);
		}
	}
	this.dataWatcher.updateObject(27, nbt.getString("letter"));
}

@SideOnly(Side.CLIENT)
public float getShadowSize()
{
	return 0.0F;
}

/**
 * First layer of player interaction
 */
public boolean interactFirst(EntityPlayer player)
{
	if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityPlayer && this.riddenByEntity != player)
	{
		return true;
	}
	else
	{
			if(player.isSneaking())
			{
				player.displayGUIChest(this);
			}
			else
			{
				if(!this.worldObj.isRemote)
				{
					player.mountEntity(this);
				}
			}

		return true;
	}
}

/**
 * Takes in the distance the entity has fallen this tick and whether its on the ground to update the fall distance
 * and deal fall damage if landing on the ground.  Args: distanceFallenThisTick, onGround
 */
protected void updateFallState(double distanceFallenThisTick, boolean onGround)
{
	this.fallDistance = 0.0F;
}

/**
 * Sets the damage taken from the last hit.
 */
public void setDamageTaken(float damage)
{
	this.dataWatcher.updateObject(19, Float.valueOf(damage));
}

/**
 * Gets the damage taken from the last hit.
 */
public float getDamageTaken()
{
	return this.dataWatcher.getWatchableObjectFloat(19);
}

/**
 * Sets the time to count down from since the last time entity was hit.
 */
public void setTimeSinceHit(int time)
{
	this.dataWatcher.updateObject(17, Integer.valueOf(time));
}

/**
 * Gets the time since the last hit.
 */
public int getTimeSinceHit()
{
	return this.dataWatcher.getWatchableObjectInt(17);
}

/**
 * Sets the forward direction of the entity.
 */
public void setForwardDirection(int direction)
{
	this.dataWatcher.updateObject(18, Integer.valueOf(direction));
}

/**
 * Gets the forward direction of the entity.
 */
public int getForwardDirection()
{
	return this.dataWatcher.getWatchableObjectInt(18);
}

/**
 * Returns the stack in slot i
 */
public ItemStack getStackInSlot(int slot)
{
	return this.invItems[slot];
}

/**
 * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
 * new stack.
 */
public ItemStack decrStackSize(int slot, int amount)
{
	if (this.invItems[slot] != null)
	{
		ItemStack itemstack;

		if (this.invItems[slot].stackSize <= amount)
		{
			itemstack = this.invItems[slot];
			this.invItems[slot] = null;
			return itemstack;
		}
		else
		{
			itemstack = this.invItems[slot].splitStack(amount);

			if (this.invItems[slot].stackSize == 0)
			{
				this.invItems[slot] = null;
			}

			return itemstack;
		}
	}
	else
	{
		return null;
	}
}

/**
 * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
 * like when you close a workbench GUI.
 */
public ItemStack getStackInSlotOnClosing(int slot)
{
	if (this.invItems[slot] != null)
	{
		ItemStack itemstack = this.invItems[slot];
		this.invItems[slot] = null;
		return itemstack;
	}
	else
	{
		return null;
	}
}

/**
 * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
 */
public void setInventorySlotContents(int slot, ItemStack stack)
{
	this.invItems[slot] = stack;

	if (stack != null && stack.stackSize > this.getInventoryStackLimit())
	{
		stack.stackSize = this.getInventoryStackLimit();
	}
}

/**
 * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
 * hasn't changed and skip it.
 */
public void markDirty() {}

/**
 * Do not make give this method the name canInteractWith because it clashes with Container
 */
public boolean isUseableByPlayer(EntityPlayer player)
{
	return this.isDead ? false : player.getDistanceSqToEntity(this) <= 64.0D;
}

public void openInventory(EntityPlayer p) {}

public void closeInventory(EntityPlayer p) {}

/**
 * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
 */
public boolean isItemValidForSlot(int slot, ItemStack stack)
{	
	return true;
}

/**
 * Returns the name of the inventory
 */
public String getName()
{
	return "IFT-" + this.dataWatcher.getWatchableObjectString(27);
}

/**
 * Returns the maximum stack size for a inventory slot.
 */
public int getInventoryStackLimit()
{
	return 64;
}

@Override
public int getSizeInventory() 
{
	return invItems.length;
}

@Override
public void writeSpawnData(ByteBuf buffer) {
	// TODO Auto-generated method stub

}

@Override
public void readSpawnData(ByteBuf additionalData) {
	// TODO Auto-generated method stub

}

public String getLetter(){
	return this.dataWatcher.getWatchableObjectString(27);
}

//	public double getYOffset(){
//		return this.height* 2;
//	}
@Override
public int getField(int id) {
	// TODO Auto-generated method stub
	return 0;
}
@Override
public void setField(int id, int value) {
	// TODO Auto-generated method stub

}
@Override
public int getFieldCount() {
	// TODO Auto-generated method stub
	return 0;
}
@Override
public void clear() {
	// TODO Auto-generated method stub

}

}

 

 

Render"

 

package com.apmods.swbf2.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import com.apmods.swbf2.entity.EntityIFT;
import com.apmods.swbf2.main.Battlefront;

public class RenderIFT extends Render {

private static String letter;
public static ResourceLocation IFT_texture = new ResourceLocation(Battlefront.MODID + ":textures/entity/IFT-T" + ".png");
public static ModelIFT modelIFT;	

public RenderIFT()
    {
    	super(Minecraft.getMinecraft().getRenderManager());
        this.modelIFT = new ModelIFT();
    }

@Override
public void doRender(Entity _entity, double posX, double posY, double posZ, float var8, float var9) {
	EntityIFT entity = (EntityIFT) _entity;
	GL11.glPushMatrix();
	GL11.glDisable(GL11.GL_CULL_FACE);
	GL11.glTranslatef((float)posX, (float)posY, (float)posZ);
//		if(entity.riddenByEntity != null){
		GL11.glRotatef(-90.0F - var8, 0.0F, 1.0F, 0.0F);
        float f2 = (float)entity.getTimeSinceHit() - var9;
	        float f3 = entity.getDamageTaken() - var9;

	        if (f3 < 0.0F)
	        {
	            f3 = 0.0F;
	        }

	        if (f2 > 0.0F)
	        {
	            GL11.glRotatef(MathHelper.sin(f2) * f2 * f3 / 10.0F * (float)entity.getForwardDirection(), 1.0F, 0.0F, 0.0F);
	        }
//			}
//		else{
//			 float f2 = (float)entity.getTimeSinceHit() - var9;
//			 float f3 = entity.getDamageTaken() - var9;
//				
//		        if (f3 < 0.0F)
//		        {
//		            f3 = 0.0F;
//		        }
//			if (f2 > 0.0F)
//	        {
//	            GL11.glRotatef(MathHelper.sin(f2) * f2 * f3 / 10.0F * (float)entity.getForwardDirection(), 1.0F, 0.0F, 0.0F);
//	        }
//		}
	this.bindEntityTexture(entity);

	this.modelIFT.render(entity, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0625F);
	GL11.glEnable(GL11.GL_CULL_FACE);
	GL11.glPopMatrix();
}

@Override
protected ResourceLocation getEntityTexture(Entity var1) {
	return this.getEntityTexture((EntityIFT)var1);
}
protected ResourceLocation getEntityTexture(EntityIFT var1) {
	return new ResourceLocation(Battlefront.MODID + ":textures/entity/IFT-" + var1.getLetter() + ".png");
}
}

 

 

Thanks

Creator of the MyFit, MagiCraft, Tesseract gun, and Papa's Wingeria mod.

Link to post
Share on other sites

By default, if you render a model from it's origin point, its sunken into the ground a little bit. If you change

(float)posY

in

GL11.glTranslatef()

to

(float)posY - 1.5F

, it should work alright.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to post
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.

Guest
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.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • i currently try to find out how the world gen works, at the moment im working on a structure but i have some problems with the generation. i use this  as an example, i created everything for the generation, but the structure is not generating this is my structure this is the structure registry this is my world load event and this my biome loading event thanks for replies
    • I'm currently trying to make a new cauldron-type block, and I'm having difficulty implementing blockstates. Here's the relevant part of the alchemical cauldron's code:   public class AlchemicalCauldronBlock extends Block { public static final IntegerProperty LEVEL = IntegerProperty.create("level", 1, 3); private static final VoxelShape INSIDE = box(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D); protected static final VoxelShape SHAPE = VoxelShapes.join(VoxelShapes.block(), VoxelShapes.or(box(0.0D, 0.0D, 4.0D, 16.0D, 3.0D, 12.0D), box(4.0D, 0.0D, 0.0D, 12.0D, 3.0D, 16.0D), box(2.0D, 0.0D, 2.0D, 14.0D, 3.0D, 14.0D), INSIDE), IBooleanFunction.ONLY_FIRST); public AlchemicalCauldronBlock() { super(Properties.of(Material.METAL).sound(SoundType.METAL).harvestLevel(3).strength(2F,2F)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0))); } ... When I try to run this with either of the two attempts at registerDefaultState uncommented, I get this error: https://pastebin.com/vPf3UryC. For convenience, I believe this part is most relevant: How can I avoid this? My registry classes look like this, if it helps: public class registry { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, AlchemyPlus.MODID); public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, AlchemyPlus.MODID); public static void register() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); BLOCKS.register(modEventBus); ITEMS.register(modEventBus); registerItems.register(); registerBlocks.register(); } } public class registerBlocks { public static final RegistryObject<Block> ALCHEMICAL_CAULDRON = register("alchemical_cauldron", AlchemicalCauldronBlock::new); static void register() {} private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) { return registry.BLOCKS.register(name, block); } private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) { RegistryObject<T> ret = registerNoItem(name, block); registry.ITEMS.register(name, () -> new BlockItem(ret.get(), new Item.Properties().tab(ItemGroup.TAB_BREWING))); return ret; } } Full GitHub repository available here.   (Other minor problem(?):  I copied the methods getShape and getInteractionShape from net.minecraft.block.CauldronBlock and I'm getting these weird warnings. What's up with that? Do I need to worry about it?)  
    • Personally I use IForgeRegistryEntry#getRegistryName and then ResourceLocation#getNameSpace to get the id of the object.
    • you can get a block form a ResourceLocation, take a look at NBTUtil#readBlockState, do somthing like that
    • I am running forge version 1.16.5 36.1.31 on a server with a lot of mods and when a player refreshes the server list, it sends a long error in the server console. Additionally, the server list displays "Can't connect to server", despite refreshing and actually being able to connect to the server. here's the links to the logs: https://gist.github.com/WaffleTraits/83885b1539d5711fabff73a91924e665 https://gist.github.com/WaffleTraits/5ae78e6eaa330422bbc9f7226faf60ce Here's what's displayed in console when a player refreshes server listing: https://hastebin.com/ativexeyoy.properties server is running on ubuntu 20.04 and is using java 8 for this forge instance.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.