Jump to content

Recommended Posts

Posted

Hello! I was trying to create a mob which has two different textures: "red" and "black". It should be chosen randomly when the mob spawns and then never change. I had to find some way to save data about the variant of the mob when player go out from the game. I thought that using read/addAdditionalSaveData can help, but nothing has changed, it still doesn't remember what color each mob had. Did I something wrong? Here are methods in MyEntity class, related to the problem:

private final static EntityDataAccessor<String> DATA_VARIANT_NAME = SynchedEntityData.defineId(MyEntity.class, EntityDataSerializers.STRING);

//Entity variant is set in constructor
public MyEntity(EntityType<? extends Animal> pEntityType, Level pLevel) {
    super(pEntityType, pLevel);
    setMyEntityVariant(RandomMyEntityVariant());
}

@Override
protected void defineSynchedData() {
    super.defineSynchedData();
    this.entityData.define(DATA_VARIANT_NAME, RandomMyEntityVariant().getSerializedName());
}

public static enum MyEntityVariant implements StringRepresentable{
    RED("red"),
    BLACK("black");

    private final String name;

    private MyEntityVariant(String name){
        this.name = name;
    }

    @Override
    public String getSerializedName() {
        return name;
    }

    public static MyEntityVariant byName(String pName) {
        if (Objects.equals(pName, "black")){
            return BLACK;
        }
        else return RED;
    }
}

private MyEntityVariant RandomMyEntityVariant(){
    switch (this.random.nextIntBetweenInclusive(1,2)){
        case 1:
            return MyEntityVariant.BLACK;
        default:
            return MyEntityVariant.RED;
    }
}

public MyEntityVariant getMyEntityVariant(){
    return MyEntityVariant.byName(this.entityData.get(DATA_VARIANT_NAME));
}

public void setMyEntityVariant(MyEntityVariant variant){
    this.entityData.set(DATA_VARIANT_NAME,variant.getSerializedName());
}

@Override
public void addAdditionalSaveData(CompoundTag pCompound) {
    super.addAdditionalSaveData(pCompound);
    pCompound.putString("MyEntityVariant",this.getVariant().getSerializedName());
}

@Override
public void readAdditionalSaveData(CompoundTag pCompound) {
    super.readAdditionalSaveData(pCompound);
    this.setMyEntityVariant(MyEntityVariant.byName(pCompound.getString("MyEntityVariant")));
}

Maybe, I should call SetMyEntityVariant somewhere in another place?

Posted

You could store your variant as a plain string, then implement IEntityAdditionalSpawnData, and write and read your string to the client there. Then just read and write your string inside read/addAdditionalSaveData

  • Like 1
Posted
9 hours ago, Xoroshio said:

You could store your variant as a plain string, then implement IEntityAdditionalSpawnData, and write and read your string to the client there. Then just read and write your string inside read/addAdditionalSaveData

Thank you! So I mustn't to use EntityData to store the variant? Also, when implementing IEntityAdditionalSpawnData I must override two additional methods (write/readSpawnData). Should I write and read my string in two places (write/readSpawnData and read/addAdditionalSaveData)?

Posted
55 minutes ago, Xoroshio said:

no you dont need EntityData, and yes, read and write your string in the read/writeSpawn data

Something still goes wrong. I've changed my code: added new field Variant, which also is set randomly in constructor. Here are my write/readSpawnData:

@Override
public void writeSpawnData(FriendlyByteBuf friendlyByteBuf) {
    System.out.print("WRITING");
    friendlyByteBuf.writeEnum(this.getVariant());
}

@Override
public void readSpawnData(FriendlyByteBuf friendlyByteBuf) {
    System.out.print("READING");
    friendlyByteBuf.readEnum(MyEntityType.class);
}

But in my log WRITING or READING never appears. So when these methods should be called?

Posted

I should be working, I’m not sure what’s going on. Have you registered the entity, and are you spawning it in the world?

Also, you need to do variant = friendlyByteBuff.readEnum(MyEntityType.class);

currently you are just reading the data and not doing anything with it, but that’s a minor problem compared to the methods not firing. The read and write spawn data methods are called automatically by the games client packet manager.

Posted (edited)

Ok here is some code that I think might do the job

 

public class MyEntity extends Animal {

// 0 : black

// 1 : red
private int colorId;

public MyEntity(EntityType<? extends Animal> pEntityType, Level pLevel) {

super(pEntityType, pLevel);

colourId = pLevel.random().nextInt(0,1);

}

@Override public void addAdditionalSaveData(CompoundTag pCompound) { super.addAdditionalSaveData(pCompound); pCompound.putInt("MyEntityVariant",colourId);

}

@Override public void readAdditionalSaveData(CompoundTag pCompound) { super.readAdditionalSaveData(pCompound);

colorId = pCompound.getInt(“MyEntityVarient”);

}

public Packet<ClientGamePacketListener> getAddEntityPacket() {
      return new ClientboundAddEntityPacket(this, colourId);
   }

   public void recreateFromPacket(ClientboundAddEntityPacket packet) {
      super.recreateFromPacket(packet);
      colorId = packet.getData();
   }

}

Edited by Xoroshio

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

    • I tried do download the essential mod to my mod pack but i didnt work. I paly on 1.21 and it should work. I use neoforge for my modding. The weird things is my friend somehow added the mod to his modpack and many others that I somehow can´t. Is there anything i can do? 
    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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