Jump to content

Recommended Posts

Posted (edited)

Hi all,

When I try and spawn my custom entity GhastKing nothing happens in game but the console throws this error 

Failed to handle packet net.minecraft.network.protocol.game.ServerboundUseItemOnPacket@ac2e660, suppressing error
java.lang.IllegalArgumentException: Duplicate id value for 16!

I've tried manually assigning an ID number on registration among other things. See below my GhastKingEntity.java class and my ModEntityTypes.java class

 

All help and advice is appreciated, thanks in advance.


 

package com.stardust.mymod.entity.custom;

import java.util.EnumSet;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import java.util.Random;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Difficulty;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.FlyingMob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.control.MoveControl;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.monster.Enemy;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.LargeFireball;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;

public class GhastKingEntity extends FlyingMob implements Enemy, IAnimatable {

private static EntityDataAccessor<Boolean> DATA_IS_CHARGINGG = SynchedEntityData.defineId(GhastKingEntity.class, EntityDataSerializers.BOOLEAN);


private int explosionPower = 1;

public GhastKingEntity(EntityType<? extends GhastKingEntity> entityType, Level world) {
super(entityType, world);
this.xpReward = 5;
this.moveControl = new GhastKingEntity.GhastKingEntityMoveControl(this);
this.getAttribute(Attributes.MAX_HEALTH);
this.getAttribute(Attributes.ATTACK_DAMAGE);
this.getAttribute(Attributes.ATTACK_SPEED);
this.getAttribute(Attributes.FLYING_SPEED);
this.getAttribute(Attributes.FOLLOW_RANGE);
this.entityData.define(DATA_IS_CHARGINGG, false);
}

public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20.0D).add(Attributes.FOLLOW_RANGE, 5.0D).add(Attributes.ATTACK_DAMAGE, 3.0D).add(Attributes.ATTACK_SPEED, 4.0D).add(Attributes.FLYING_SPEED, 1.0D);


}




protected void registerGoals() {
this.goalSelector.addGoal(5, new GhastKingEntity.RandomFloatAroundGoal(this));
this.goalSelector.addGoal(7, new GhastKingEntity.GhastKingEntityLookGoal(this));
this.goalSelector.addGoal(7, new GhastKingEntity.GhastKingEntityShootFireballGoal(this));
this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, (p_32755_) -> {
return Math.abs(p_32755_.getY() - this.getY()) <= 4.0D;
}));
}

public boolean isCharging() {
return this.entityData.get(DATA_IS_CHARGINGG);
}

public void setCharging(boolean charging) {
this.entityData.set(DATA_IS_CHARGINGG, charging);
}

public int getExplosionPower() {
return this.explosionPower;
}

protected boolean shouldDespawnInPeaceful() {
return true;
}

private static boolean isReflectedFireball(DamageSource damageSource) {
return damageSource.getDirectEntity() instanceof LargeFireball && damageSource.getEntity() instanceof Player;
}

public boolean isInvulnerableTo(@NotNull DamageSource damageSource) {
return !isReflectedFireball(damageSource) && super.isInvulnerableTo(damageSource);
}

public boolean hurt(DamageSource damageSource, float p_32731_) {
if (isReflectedFireball(damageSource)) {
super.hurt(damageSource, 1000.0F);
return true;
} else {
return !this.isInvulnerableTo(damageSource) && super.hurt(damageSource, p_32731_);
}
}


@Override
protected void defineSynchedData() {
super.defineSynchedData();

this.entityData.define(DATA_IS_CHARGINGG, false);
}

public SoundSource getSoundSource() {
return SoundSource.HOSTILE;
}

protected SoundEvent getAmbientSound() {
return SoundEvents.GHAST_AMBIENT;
}

protected SoundEvent getHurtSound(DamageSource damageSource) {
return SoundEvents.GHAST_HURT;
}

protected SoundEvent getDeathSound() {
return SoundEvents.GHAST_DEATH;
}

protected float getSoundVolume() {
return 5.0F;
}

public static boolean checkGhastKingEntitySpawnRules(EntityType<GhastKingEntity> p_218985_, LevelAccessor p_218986_, MobSpawnType p_218987_, BlockPos p_218988_, RandomSource p_218989_) {
return p_218986_.getDifficulty() != Difficulty.PEACEFUL && p_218989_.nextInt(20) == 0 && checkMobSpawnRules(p_218985_, p_218986_, p_218987_, p_218988_, p_218989_);
}

public int getMaxSpawnClusterSize() {
return 1;
}

public void addAdditionalSaveData(CompoundTag compoundTag) {
super.addAdditionalSaveData(compoundTag);
compoundTag.putByte("ExplosionPower", (byte)this.explosionPower);
}

public void readAdditionalSaveData(CompoundTag compoundTag) {
super.readAdditionalSaveData(compoundTag);
if (compoundTag.contains("ExplosionPower", 99)) {
this.explosionPower = compoundTag.getByte("ExplosionPower");
}

}

protected float getStandingEyeHeight(Pose p_32741_, EntityDimensions p_32742_) {
return 2.6F;
}

@Override
public void registerControllers(AnimationData data) {
}

@Override
public AnimationFactory getFactory() {
return null;
}

static class GhastKingEntityLookGoal extends Goal {
private final GhastKingEntity GhastKingEntity;

public GhastKingEntityLookGoal(GhastKingEntity ghastKingEntity) {
this.GhastKingEntity = ghastKingEntity;
this.setFlags(EnumSet.of(Goal.Flag.LOOK));
}

public boolean canUse() {
return true;
}

public boolean requiresUpdateEveryTick() {
return true;
}

public void tick() {
if (this.GhastKingEntity.getTarget() == null) {
Vec3 vec3 = this.GhastKingEntity.getDeltaMovement();
this.GhastKingEntity.setYRot(-((float)Mth.atan2(vec3.x, vec3.z)) * (180F / (float)Math.PI));
this.GhastKingEntity.yBodyRot = this.GhastKingEntity.getYRot();
} else {
LivingEntity livingentity = this.GhastKingEntity.getTarget();
double d0 = 64.0D;
if (livingentity.distanceToSqr(this.GhastKingEntity) < 4096.0D) {
double d1 = livingentity.getX() - this.GhastKingEntity.getX();
double d2 = livingentity.getZ() - this.GhastKingEntity.getZ();
this.GhastKingEntity.setYRot(-((float)Mth.atan2(d1, d2)) * (180F / (float)Math.PI));
this.GhastKingEntity.yBodyRot = this.GhastKingEntity.getYRot();
}
}

}
}

static class GhastKingEntityMoveControl extends MoveControl {
private final GhastKingEntity GhastKingEntity;
private int floatDuration;

public GhastKingEntityMoveControl(GhastKingEntity ghastKingEntity) {
super(ghastKingEntity);
this.GhastKingEntity = ghastKingEntity;
}

public void tick() {
if (this.operation == MoveControl.Operation.MOVE_TO) {
if (this.floatDuration-- <= 0) {
this.floatDuration += this.GhastKingEntity.getRandom().nextInt(5) + 2;
Vec3 vec3 = new Vec3(this.wantedX - this.GhastKingEntity.getX(), this.wantedY - this.GhastKingEntity.getY(), this.wantedZ - this.GhastKingEntity.getZ());
double d0 = vec3.length();
vec3 = vec3.normalize();
if (this.canReach(vec3, Mth.ceil(d0))) {
this.GhastKingEntity.setDeltaMovement(this.GhastKingEntity.getDeltaMovement().add(vec3.scale(0.1D)));
} else {
this.operation = MoveControl.Operation.WAIT;
}
}

}
}

private boolean canReach(Vec3 p_32771_, int p_32772_) {
AABB aabb = this.GhastKingEntity.getBoundingBox();

for(int i = 1; i < p_32772_; ++i) {
aabb = aabb.move(p_32771_);
if (!this.GhastKingEntity.level.noCollision(this.GhastKingEntity, aabb)) {
return false;
}
}

return true;
}
}

static class GhastKingEntityShootFireballGoal extends Goal {
private final GhastKingEntity GhastKingEntity;
public int chargeTime;

public GhastKingEntityShootFireballGoal(GhastKingEntity p_32776_) {
this.GhastKingEntity = p_32776_;
}

public boolean canUse() {
return this.GhastKingEntity.getTarget() != null;
}

public void start() {
this.chargeTime = 0;
}

public void stop() {
this.GhastKingEntity.setCharging(false);
}

public boolean requiresUpdateEveryTick() {
return true;
}

public void tick() {
LivingEntity livingentity = this.GhastKingEntity.getTarget();
if (livingentity != null) {
double d0 = 64.0D;
if (livingentity.distanceToSqr(this.GhastKingEntity) < 4096.0D && this.GhastKingEntity.hasLineOfSight(livingentity)) {
Level level = this.GhastKingEntity.level;
++this.chargeTime;
if (this.chargeTime == 10 && !this.GhastKingEntity.isSilent()) {
level.levelEvent((Player)null, 1015, this.GhastKingEntity.blockPosition(), 0);
}

if (this.chargeTime == 20) {
double d1 = 4.0D;
Vec3 vec3 = this.GhastKingEntity.getViewVector(1.0F);
double d2 = livingentity.getX() - (this.GhastKingEntity.getX() + vec3.x * 4.0D);
double d3 = livingentity.getY(0.5D) - (0.5D + this.GhastKingEntity.getY(0.5D));
double d4 = livingentity.getZ() - (this.GhastKingEntity.getZ() + vec3.z * 4.0D);
if (!this.GhastKingEntity.isSilent()) {
level.levelEvent((Player)null, 1016, this.GhastKingEntity.blockPosition(), 0);
}

LargeFireball largefireball = new LargeFireball(level, this.GhastKingEntity, d2, d3, d4, this.GhastKingEntity.getExplosionPower());
largefireball.setPos(this.GhastKingEntity.getX() + vec3.x * 4.0D, this.GhastKingEntity.getY(0.5D) + 0.5D, largefireball.getZ() + vec3.z * 4.0D);
level.addFreshEntity(largefireball);
this.chargeTime = -40;
}
} else if (this.chargeTime > 0) {
--this.chargeTime;
}

this.GhastKingEntity.setCharging(this.chargeTime > 10);
}
}
}

static class RandomFloatAroundGoal extends Goal {
private final GhastKingEntity GhastKingEntity;

public RandomFloatAroundGoal(GhastKingEntity p_32783_) {
this.GhastKingEntity = p_32783_;
this.setFlags(EnumSet.of(Goal.Flag.MOVE));
}

public boolean canUse() {
MoveControl movecontrol = this.GhastKingEntity.getMoveControl();
if (!movecontrol.hasWanted()) {
return true;
} else {
double d0 = movecontrol.getWantedX() - this.GhastKingEntity.getX();
double d1 = movecontrol.getWantedY() - this.GhastKingEntity.getY();
double d2 = movecontrol.getWantedZ() - this.GhastKingEntity.getZ();
double d3 = d0 * d0 + d1 * d1 + d2 * d2;
return d3 < 1.0D || d3 > 3600.0D;
}
}

public boolean canContinueToUse() {
return false;
}

public void start() {
RandomSource randomsource = this.GhastKingEntity.getRandom();
double d0 = this.GhastKingEntity.getX() + (double)((randomsource.nextFloat() * 2.0F - 1.0F) * 16.0F);
double d1 = this.GhastKingEntity.getY() + (double)((randomsource.nextFloat() * 2.0F - 1.0F) * 16.0F);
double d2 = this.GhastKingEntity.getZ() + (double)((randomsource.nextFloat() * 2.0F - 1.0F) * 16.0F);
this.GhastKingEntity.getMoveControl().setWantedPosition(d0, d1, d2, 1.0D);
}
}
}

 

////////////////////////////////////////////////////////////////////////

 

package com.stardust.mymod.entity;

import com.stardust.mymod.Main;
import com.stardust.mymod.entity.custom.GhastKingEntity;
import com.stardust.mymod.items.TexEntity;
import com.stardust.mymod.items.TexEntityRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.ThrownItemRenderer;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.MobType;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class ModEntityTypes {
public static final DeferredRegister<EntityType<?>> ENTITIES = DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, Main.MODID);

public static final int TEX_ENTITY_ID = 743;
public static final int GHAST_KING_ID = 229;

public static final RegistryObject<EntityType<TexEntity>> TEX_ENTITY = ENTITIES.register("tex_entity",
() -> EntityType.Builder.<TexEntity>of(TexEntity::new, MobCategory.MISC)
.sized(0.25F, 0.25F)
.setTrackingRange(4)
.setUpdateInterval(10)
.build(Integer.toString(TEX_ENTITY_ID)));
public static final RegistryObject<EntityType<GhastKingEntity>> GHAST_KING = ENTITIES.register("ghast_king", () -> EntityType.Builder.of(GhastKingEntity::new, MobCategory.MONSTER)
.sized(1.0F, 1.0F) // Change these values based on the size of your GhastKingEntity
.setTrackingRange(8)
.setUpdateInterval(3)
.setShouldReceiveVelocityUpdates(true)
.build(Integer.toString(GHAST_KING_ID))
);







}

 

Edited by StarDuster01
  • StarDuster01 changed the title to [1.19.2] Strange Duplicate id value error for spawning custom Entity
  • 2 weeks later...
Posted

You have registered the SynchedEntityData variable DATA_IS_CHARGINGG twice. And your mob should work fine and be more code efficient if you don't bother getting the attributes in the constructor.

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.