Jump to content

Recommended Posts

Posted

Will return super.getCanSpawnHere() && this.worldObj.canBlockSeeSky(this.getPosition()) && this.getPosition().getY() < 64; work?

 

That will allow the entity to spawn if the block it's in can see the sky and the y coordinate is less than 64. You want it to spawn when the block can't see the sky, so negate the return value of

World#canBlockSeeSky

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Thanks, but they also spawned higher than 64..?

 

Post your new code. I suspect you've negated the y-level check as well as the sky check.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I also lowered the y level to 55, here is the full class:

package com.Egietje.KokkieMod.mobs.grot;

import java.util.UUID;

import javax.annotation.Nullable;

import com.Egietje.KokkieMod.init.KokkieSounds;
import com.Egietje.KokkieMod.mobs.KokkieLoot;
import com.google.common.base.Predicate;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackMelee;
import net.minecraft.entity.ai.EntityAIBeg;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIFollowParent;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILeapAtTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAIPanic;
import net.minecraft.entity.ai.EntityAISit;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAITargetNonTamed;
import net.minecraft.entity.ai.EntityAITempt;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.entity.passive.EntityRabbit;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EntityGrotKokkie extends EntityTameable {
private static final DataParameter<Float> DATA_HEALTH_ID = EntityDataManager
		.<Float>createKey(EntityGrotKokkie.class, DataSerializers.FLOAT);
private float headRotationCourse;
private float headRotationCourseOld;

public EntityGrotKokkie(World worldIn) {
	super(worldIn);
	this.setSize(0.4F, 0.6F);
	this.setTamed(false);
}

protected void initEntityAI() {
	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, new EntityAIAttackMelee(this, 1.0D, true));
	this.tasks.addTask(3, new EntityAIMate(this, 1.0D));
	this.tasks.addTask(4, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(6, new EntityAILookIdle(this));
	this.tasks.addTask(7, new EntityAILeapAtTarget(this, 0.4F));
	this.tasks.addTask(8, new EntityAIFollowParent(this, 1.1D));
	this.tasks.addTask(9, new EntityAITempt(this, 0.9D, Items.CHICKEN, false));
	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true, new Class[0]));
	this.targetTasks.addTask(2,
			new EntityAITargetNonTamed(this, EntityAnimal.class, false, new Predicate<Entity>() {
				public boolean apply(@Nullable Entity p_apply_1_) {
					return p_apply_1_ instanceof EntitySheep || p_apply_1_ instanceof EntityRabbit
							|| p_apply_1_ instanceof EntityChicken;
				}
			}));
}

@Override
protected ResourceLocation getLootTable() {
	return KokkieLoot.ENTITIES_GROT;
}

@Override
public boolean isBreedingItem(@Nullable ItemStack stack) {
	return stack == null ? false : stack.getItem() == Items.CHICKEN;
}

protected void applyEntityAttributes() {
	super.applyEntityAttributes();
	this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
	this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(2.0D);
	this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(0.5D);
}

protected void updateAITasks() {
	this.dataManager.set(DATA_HEALTH_ID, Float.valueOf(this.getHealth()));
}

protected void entityInit() {
	super.entityInit();
	this.dataManager.register(DATA_HEALTH_ID, Float.valueOf(this.getHealth()));
}

protected void playStepSound(BlockPos pos, Block blockIn) {
	this.playSound(SoundEvents.ENTITY_ITEMFRAME_REMOVE_ITEM, 0.15F, 1.0F);
}

public static void func_189788_b(DataFixer p_189788_0_) {
	EntityLiving.func_189752_a(p_189788_0_, "GrotKokkie");
}

protected SoundEvent getAmbientSound() {
	return this.isAngry() ? KokkieSounds.GROT_KOKKIE_ANGRY
			: (this.rand.nextInt(2) == 0 ? KokkieSounds.GROT_KOKKIE_AMBIENCE : KokkieSounds.GROT_KOKKIE_AMBIENCE);
}

protected SoundEvent getHurtSound() {
	return KokkieSounds.GROT_KOKKIE_HURT;
}

protected SoundEvent getDeathSound() {
	return KokkieSounds.GROT_KOKKIE_DEATH;
}

/**
 * Returns the volume for the sounds this mob makes.
 */
protected float getSoundVolume() {
	return 0.4F;
}

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

	this.headRotationCourse += (0.0F - this.headRotationCourse) * 0.4F;
}

@SideOnly(Side.CLIENT)
public float getInterestedAngle(float p_70917_1_) {
	return (this.headRotationCourseOld + (this.headRotationCourse - this.headRotationCourseOld) * p_70917_1_)
			* 0.15F * (float) Math.PI;
}

public float getEyeHeight() {
	return this.height;
}

public int getVerticalFaceSpeed() {
	return this.isSitting() ? 20 : super.getVerticalFaceSpeed();
}

public boolean attackEntityAsMob(Entity entityIn) {
	boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this),
			(float) ((int) this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue()));

	if (flag) {
		this.applyEnchantments(this, entityIn);
	}

	return flag;
}

public EntityGrotKokkie createChild(EntityAgeable ageable) {
	EntityGrotKokkie kokkie = new EntityGrotKokkie(this.worldObj);
	return kokkie;
}

public boolean canMateWith(EntityAnimal otherAnimal) {
	if (otherAnimal == this) {
		return false;
	} else if (!(otherAnimal instanceof EntityGrotKokkie)) {
		return false;
	} else {
		EntityGrotKokkie entitywolf = (EntityGrotKokkie) otherAnimal;
		return this.isInLove() && entitywolf.isInLove();
	}
}

public boolean isAngry() {
	return (((Byte) this.dataManager.get(TAMED)).byteValue() & 2) != 0;
}

public void setAngry(boolean angry) {
	byte b0 = ((Byte) this.dataManager.get(TAMED)).byteValue();

	if (angry) {
		this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 | 2)));
	} else {
		this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 & -3)));
	}
}

public boolean shouldAttackEntity(EntityLivingBase p_142018_1_, EntityLivingBase p_142018_2_) {
	if (!(p_142018_1_ instanceof EntityCreeper) && !(p_142018_1_ instanceof EntityGhast)) {
		return p_142018_1_ instanceof EntityPlayer && p_142018_2_ instanceof EntityPlayer
				&& !((EntityPlayer) p_142018_2_).canAttackPlayer((EntityPlayer) p_142018_1_) ? false
						: !(p_142018_1_ instanceof EntityHorse) || !((EntityHorse) p_142018_1_).isTame();
	} else {
		return false;
	}
}

public void setAttackTarget(@Nullable EntityLivingBase entitylivingbaseIn) {
	super.setAttackTarget(entitylivingbaseIn);

	if (entitylivingbaseIn == null) {
		this.setAngry(false);
	} else {
		this.setAngry(true);
	}
}

public void writeEntityToNBT(NBTTagCompound compound) {
	super.writeEntityToNBT(compound);
	compound.setBoolean("Angry", this.isAngry());
}

/**
 * (abstract) Protected helper method to read subclass entity data from NBT.
 */
public void readEntityFromNBT(NBTTagCompound compound) {
	super.readEntityFromNBT(compound);
	this.setAngry(compound.getBoolean("Angry"));
}

public void onLivingUpdate() {
	super.onLivingUpdate();
	if (!this.worldObj.isRemote && this.getAttackTarget() == null && this.isAngry()) {
		this.setAngry(false);
	}
}

@Override
public boolean getCanSpawnHere() {
	return super.getCanSpawnHere() && !this.worldObj.canBlockSeeSky(this.getPosition()) && this.getPosition().getY() < 55;
}
}

Posted

That looks like it should work, you'll need to debug this yourself.

 

Set a breakpoint in

EntityGrotKokkie#getCanSpawnHere

and run Minecraft in debug mode. When the breakpoint is hit, look at the entity's position in the debugger and then step through the method to see the result of each check.

 

For debugging purposes, you may want to split each check onto its own line and assign the result to a local variable.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Ehm, how do I do that?  ???

 

Use your search engine of choice to find documentation/tutorials on how to use the debugger in your IDE.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I mean the breakpoint

 

Again, breakpoints are a feature of your IDE. There is documentation available in your IDE or on the Internet, find it and read it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I have a breakpoint but it doesn't suspend anything...

 

Are you running Minecraft in debug mode? Are the entities being spawned anywhere?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Are you running Minecraft in debug mode? Are the entities being spawned anywhere?

Yes and yes

 

I'm not too sure what's going wrong.

 

Is the breakpoint definitely configured to suspend? If you set a breakpoint in the

EntityGrotKokkie

constructor, is it hit?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

FYI you know there's both a client thread and a server thread and that this method is only called on the server and therefore the client thread isn't paused, right?

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.

Posted

I'll look around for that option in Eclipse, but the last time I did it, it definitely didn't pause the client thread.  Mildly annoying, but workable.

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.

Posted

Are those the Y vals where it tried to spawn, or where it actually spawned? What did your method return when it had those Y values? What were the sky values?

 

Find a cave coordinate in your test world. Use the debugger to pause at the point where spawn coords are chosen, and then set the spawn coords to your cave's x, y, z. Then step through the program to see what happens and why. Be sure that your player entity is far enough away to allow creature spawning.

 

Also: Get help with the debugger. It is an essential tool for most non-crash bugs. Find somebody who can visit you (or whom you can visit) to spend an hour or two getting a feel for the basics (setting breakpoints, examining variables, stepping into / over / out of methods, conditional breakpoints, and poking values into variables).

 

You're not a complete Jedi modder until you can wield the debugger.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted

I also lowered the y level to 55, here is the full class:

package com.Egietje.KokkieMod.mobs.grot;

import java.util.UUID;

import javax.annotation.Nullable;

import com.Egietje.KokkieMod.init.KokkieSounds;
import com.Egietje.KokkieMod.mobs.KokkieLoot;
import com.google.common.base.Predicate;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackMelee;
import net.minecraft.entity.ai.EntityAIBeg;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIFollowParent;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILeapAtTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAIPanic;
import net.minecraft.entity.ai.EntityAISit;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAITargetNonTamed;
import net.minecraft.entity.ai.EntityAITempt;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.entity.passive.EntityRabbit;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EntityGrotKokkie extends EntityTameable {
private static final DataParameter<Float> DATA_HEALTH_ID = EntityDataManager
		.<Float>createKey(EntityGrotKokkie.class, DataSerializers.FLOAT);
private float headRotationCourse;
private float headRotationCourseOld;

public EntityGrotKokkie(World worldIn) {
	super(worldIn);
	this.setSize(0.4F, 0.6F);
	this.setTamed(false);
}

protected void initEntityAI() {
	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, new EntityAIAttackMelee(this, 1.0D, true));
	this.tasks.addTask(3, new EntityAIMate(this, 1.0D));
	this.tasks.addTask(4, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(6, new EntityAILookIdle(this));
	this.tasks.addTask(7, new EntityAILeapAtTarget(this, 0.4F));
	this.tasks.addTask(8, new EntityAIFollowParent(this, 1.1D));
	this.tasks.addTask(9, new EntityAITempt(this, 0.9D, Items.CHICKEN, false));
	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true, new Class[0]));
	this.targetTasks.addTask(2,
			new EntityAITargetNonTamed(this, EntityAnimal.class, false, new Predicate<Entity>() {
				public boolean apply(@Nullable Entity p_apply_1_) {
					return p_apply_1_ instanceof EntitySheep || p_apply_1_ instanceof EntityRabbit
							|| p_apply_1_ instanceof EntityChicken;
				}
			}));
}

@Override
protected ResourceLocation getLootTable() {
	return KokkieLoot.ENTITIES_GROT;
}

@Override
public boolean isBreedingItem(@Nullable ItemStack stack) {
	return stack == null ? false : stack.getItem() == Items.CHICKEN;
}

protected void applyEntityAttributes() {
	super.applyEntityAttributes();
	this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
	this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(2.0D);
	this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(0.5D);
}

protected void updateAITasks() {
	this.dataManager.set(DATA_HEALTH_ID, Float.valueOf(this.getHealth()));
}

protected void entityInit() {
	super.entityInit();
	this.dataManager.register(DATA_HEALTH_ID, Float.valueOf(this.getHealth()));
}

protected void playStepSound(BlockPos pos, Block blockIn) {
	this.playSound(SoundEvents.ENTITY_ITEMFRAME_REMOVE_ITEM, 0.15F, 1.0F);
}

public static void func_189788_b(DataFixer p_189788_0_) {
	EntityLiving.func_189752_a(p_189788_0_, "GrotKokkie");
}

protected SoundEvent getAmbientSound() {
	return this.isAngry() ? KokkieSounds.GROT_KOKKIE_ANGRY
			: (this.rand.nextInt(2) == 0 ? KokkieSounds.GROT_KOKKIE_AMBIENCE : KokkieSounds.GROT_KOKKIE_AMBIENCE);
}

protected SoundEvent getHurtSound() {
	return KokkieSounds.GROT_KOKKIE_HURT;
}

protected SoundEvent getDeathSound() {
	return KokkieSounds.GROT_KOKKIE_DEATH;
}

/**
 * Returns the volume for the sounds this mob makes.
 */
protected float getSoundVolume() {
	return 0.4F;
}

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

	this.headRotationCourse += (0.0F - this.headRotationCourse) * 0.4F;
}

@SideOnly(Side.CLIENT)
public float getInterestedAngle(float p_70917_1_) {
	return (this.headRotationCourseOld + (this.headRotationCourse - this.headRotationCourseOld) * p_70917_1_)
			* 0.15F * (float) Math.PI;
}

public float getEyeHeight() {
	return this.height;
}

public int getVerticalFaceSpeed() {
	return this.isSitting() ? 20 : super.getVerticalFaceSpeed();
}

public boolean attackEntityAsMob(Entity entityIn) {
	boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this),
			(float) ((int) this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue()));

	if (flag) {
		this.applyEnchantments(this, entityIn);
	}

	return flag;
}

public EntityGrotKokkie createChild(EntityAgeable ageable) {
	EntityGrotKokkie kokkie = new EntityGrotKokkie(this.worldObj);
	return kokkie;
}

public boolean canMateWith(EntityAnimal otherAnimal) {
	if (otherAnimal == this) {
		return false;
	} else if (!(otherAnimal instanceof EntityGrotKokkie)) {
		return false;
	} else {
		EntityGrotKokkie entitywolf = (EntityGrotKokkie) otherAnimal;
		return this.isInLove() && entitywolf.isInLove();
	}
}

public boolean isAngry() {
	return (((Byte) this.dataManager.get(TAMED)).byteValue() & 2) != 0;
}

public void setAngry(boolean angry) {
	byte b0 = ((Byte) this.dataManager.get(TAMED)).byteValue();

	if (angry) {
		this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 | 2)));
	} else {
		this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 & -3)));
	}
}

public boolean shouldAttackEntity(EntityLivingBase p_142018_1_, EntityLivingBase p_142018_2_) {
	if (!(p_142018_1_ instanceof EntityCreeper) && !(p_142018_1_ instanceof EntityGhast)) {
		return p_142018_1_ instanceof EntityPlayer && p_142018_2_ instanceof EntityPlayer
				&& !((EntityPlayer) p_142018_2_).canAttackPlayer((EntityPlayer) p_142018_1_) ? false
						: !(p_142018_1_ instanceof EntityHorse) || !((EntityHorse) p_142018_1_).isTame();
	} else {
		return false;
	}
}

public void setAttackTarget(@Nullable EntityLivingBase entitylivingbaseIn) {
	super.setAttackTarget(entitylivingbaseIn);

	if (entitylivingbaseIn == null) {
		this.setAngry(false);
	} else {
		this.setAngry(true);
	}
}

public void writeEntityToNBT(NBTTagCompound compound) {
	super.writeEntityToNBT(compound);
	compound.setBoolean("Angry", this.isAngry());
}

/**
 * (abstract) Protected helper method to read subclass entity data from NBT.
 */
public void readEntityFromNBT(NBTTagCompound compound) {
	super.readEntityFromNBT(compound);
	this.setAngry(compound.getBoolean("Angry"));
}

public void onLivingUpdate() {
	super.onLivingUpdate();
	if (!this.worldObj.isRemote && this.getAttackTarget() == null && this.isAngry()) {
		this.setAngry(false);
	}
}

@Override
public boolean getCanSpawnHere() {
	return super.getCanSpawnHere() && !this.worldObj.canBlockSeeSky(this.getPosition()) && this.getPosition().getY() < 55;
}
}

 

It might be worth mentioning that EntityAnimal#getCanSpawnHere (which you invoke through the super call) requires that the light level of the checked spawn block be greater than 8, which in a cave doesn't really happen (unless it chooses a spot next to lava or a torch in a mineshaft), and that the entity must spawn on a grass block (which never happens in a cave).

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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