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
  On 8/31/2024 at 9:39 PM, 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

Expand  

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
  On 9/1/2024 at 8:07 AM, Xoroshio said:

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

Expand  

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

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

×
×
  • Create New...

Important Information

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