Skip to content

Commit

Permalink
custom entity variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaupenjoe committed Dec 8, 2024
1 parent b3cb70f commit 780b569
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/entity/GeckoVariant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.kaupenjoe.tutorialmod.entity;

import java.util.Arrays;
import java.util.Comparator;

public enum GeckoVariant {
BLUE(0),
GREEN(1),
PINK(2),
BROWN(3);

private static final GeckoVariant[] BY_ID = Arrays.stream(values()).sorted(
Comparator.comparingInt(GeckoVariant::getId)).toArray(GeckoVariant[]::new);
private final int id;

GeckoVariant(int id) {
this.id = id;
}

public int getId() {
return id;
}

public static GeckoVariant byId(int id) {
return BY_ID[id % BY_ID.length];
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package net.kaupenjoe.tutorialmod.entity.client;

import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import net.kaupenjoe.tutorialmod.TutorialMod;
import net.kaupenjoe.tutorialmod.entity.GeckoVariant;
import net.kaupenjoe.tutorialmod.entity.custom.GeckoEntity;
import net.minecraft.Util;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.MobRenderer;
import net.minecraft.resources.ResourceLocation;

import java.util.Map;

public class GeckoRenderer extends MobRenderer<GeckoEntity, GeckoModel<GeckoEntity>> {
private static final Map<GeckoVariant, ResourceLocation> LOCATION_BY_VARIANT =
Util.make(Maps.newEnumMap(GeckoVariant.class), map -> {
map.put(GeckoVariant.BLUE,
ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "textures/entity/gecko/gecko_blue.png"));
map.put(GeckoVariant.GREEN,
ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "textures/entity/gecko/gecko_green.png"));
map.put(GeckoVariant.PINK,
ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "textures/entity/gecko/gecko_pink.png"));
map.put(GeckoVariant.BROWN,
ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "textures/entity/gecko/gecko_brown.png"));
});

public GeckoRenderer(EntityRendererProvider.Context context) {
super(context, new GeckoModel<>(context.bakeLayer(GeckoModel.LAYER_LOCATION)), 0.25f);
}

@Override
public ResourceLocation getTextureLocation(GeckoEntity entity) {
return ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "textures/entity/gecko/gecko_blue.png");
return LOCATION_BY_VARIANT.get(entity.getVariant());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package net.kaupenjoe.tutorialmod.entity.custom;

import net.kaupenjoe.tutorialmod.entity.GeckoVariant;
import net.kaupenjoe.tutorialmod.entity.ModEntities;
import net.kaupenjoe.tutorialmod.item.ModItems;
import net.minecraft.Util;
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.server.level.ServerLevel;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.AnimationState;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.*;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.ServerLevelAccessor;
import org.jetbrains.annotations.Nullable;

public class GeckoEntity extends Animal {
public final AnimationState idleAnimationState = new AnimationState();
private int idleAnimationTimeout = 0;

private static final EntityDataAccessor<Integer> VARIANT =
SynchedEntityData.defineId(GeckoEntity.class, EntityDataSerializers.INT);

public GeckoEntity(EntityType<? extends Animal> entityType, Level level) {
super(entityType, level);
}
Expand Down Expand Up @@ -53,7 +62,10 @@ public boolean isFood(ItemStack stack) {
@Nullable
@Override
public AgeableMob getBreedOffspring(ServerLevel level, AgeableMob otherParent) {
return ModEntities.GECKO.get().create(level);
GeckoVariant variant = Util.getRandom(GeckoVariant.values(), this.random);
GeckoEntity baby = ModEntities.GECKO.get().create(level);
baby.setVariant(variant);
return baby;
}

private void setupAnimationStates() {
Expand All @@ -73,4 +85,43 @@ public void tick() {
this.setupAnimationStates();
}
}

/* VARIANT */
@Override
protected void defineSynchedData(SynchedEntityData.Builder builder) {
super.defineSynchedData(builder);
builder.define(VARIANT, 0);
}

private int getTypeVariant() {
return this.entityData.get(VARIANT);
}

public GeckoVariant getVariant() {
return GeckoVariant.byId(this.getTypeVariant() & 255);
}

private void setVariant(GeckoVariant variant) {
this.entityData.set(VARIANT, variant.getId() & 255);
}

@Override
public void addAdditionalSaveData(CompoundTag compound) {
super.addAdditionalSaveData(compound);
compound.putInt("Variant", this.getTypeVariant());
}

@Override
public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);
this.entityData.set(VARIANT, compound.getInt("Variant"));
}

@Override
public SpawnGroupData finalizeSpawn(ServerLevelAccessor level, DifficultyInstance difficulty,
MobSpawnType spawnType, @Nullable SpawnGroupData spawnGroupData) {
GeckoVariant variant = Util.getRandom(GeckoVariant.values(), this.random);
this.setVariant(variant);
return super.finalizeSpawn(level, difficulty, spawnType, spawnGroupData);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 780b569

Please sign in to comment.