Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
public interface IVariantEntity {
RandomSource random = RandomSource.create();

default ResourceLocation GetTextureLocation() {
return new ResourceLocation(BlueLib.MODID, "textures/entity/" + this.GetEntityName() + "/" + this.GetVariantName() + ".png");
default ResourceLocation getTextureLocation(String pModId, String pPath) {
return new ResourceLocation(pModId, pPath);
}

String GetVariantName();
String getVariantName();

String GetEntityName();
String getEntityName();

ResourceLocation GetVariantResource();

default String ReturnRandomVariant(String pDefaultVariant) {
List<String> entityVariantNames = List.of(GetEntityName());
List<String> spawnableVariants = entityVariantNames.stream()
default String getRandomVariant(List<String> pVariantNamesList, String pDefaultVariant) {
List<String> spawnableVariants = pVariantNamesList.stream()
.toList();
if (!spawnableVariants.isEmpty()) {
return spawnableVariants.get(this.random.nextInt(spawnableVariants.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
public class VariantKeys extends VariantKeysBase {

public VariantKeys(
String variantName,
String entityName
String pVariantName,
String pEntityName
) {
AddParameter("VariantName", variantName);
AddParameter("EntityName", entityName);
addParameter("VariantName", pVariantName);
addParameter("EntityName", pEntityName);
}

public String GetVariantName() {
return (String) GetParameter("VariantName");
public String getVariantName() {
return (String) getParameter("VariantName");
}

public String GetEntityType() {
return (String) GetParameter("EntityName");
public String getEntityName() {
return (String) getParameter("EntityName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
public abstract class VariantKeysBase {
private final Map<String, Object> parameters = new HashMap<>();

protected void AddParameter(String pKey, Object pValue) {
protected void addParameter(String pKey, Object pValue) {
parameters.put(pKey, pValue);
}

public Object GetParameter(String pKey) {
public Object getParameter(String pKey) {
return parameters.get(pKey);
}

public void RemoveParameter(String pKey) {
public void removeParameter(String pKey) {
parameters.remove(pKey);
}

public Map<String, Object> GetAllParameters() {
public Map<String, Object> getAllParameters() {
return new HashMap<>(parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class VariantRegistry implements IVariantEntity {
private static final Gson gson = new Gson();
private final List<VariantKeys> variants = new ArrayList<>();

private void LoadVariantsFromJson() {
public final void loadVariantsFromJson(ResourceLocation pJSONLocation) {
/**
* if (Objects.equals(this.returnEntity(), "") || this.returnEntity() == null) {
* throw new EntityNotDefined("Entity name is not defined. The method returnEntity() returned an empty string.");
Expand All @@ -33,11 +33,11 @@ private void LoadVariantsFromJson() {
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();

try {
Optional<Resource> resource = resourceManager.getResource(GetVariantResource());
Optional<Resource> resource = resourceManager.getResource(pJSONLocation);
if (resource.isPresent()) {
InputStream inputStream = resource.get().open();
JsonObject jsonObject = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
ParseVariants(jsonObject);
parseVariants(jsonObject);
} else {
throw new ResourceNotFound("JSON File not Found. Please check the ResourceLocation thoroughly.");
}
Expand All @@ -46,41 +46,36 @@ private void LoadVariantsFromJson() {
}
}

private void ParseVariants(JsonObject jsonObject) {
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
private void parseVariants(JsonObject pJsonObject) {
for (Map.Entry<String, JsonElement> entry : pJsonObject.entrySet()) {
String type = entry.getKey();
JsonArray textureArray = entry.getValue().getAsJsonArray();

for (JsonElement element : textureArray) {
VariantKeys dragonVariants = GetEntityVariant(element, type);
VariantKeys dragonVariants = getEntityVariant(element, type);
variants.add(dragonVariants);
}
}
}

private static VariantKeys GetEntityVariant(JsonElement element, String type) {
JsonObject textureObject = element.getAsJsonObject();
private static VariantKeys getEntityVariant(JsonElement pElement, String pType) {
JsonObject textureObject = pElement.getAsJsonObject();
String variantName = textureObject.get("VariantName").getAsString();

return new VariantKeys(variantName, type);
return new VariantKeys(variantName, pType);
}

public List<VariantKeys> GetVariants() {
public List<VariantKeys> getVariants() {
return variants;
}

@Override
public ResourceLocation GetVariantResource() {
return new ResourceLocation(BlueLib.MODID, "bluelib/entity/variant/EntityName.json");
}

@Override
public String GetVariantName() {
public String getVariantName() {
return "EntityName";
}

@Override
public String GetEntityName() {
public String getEntityName() {
return "EntityName";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package software.bluelib.exception;

public class EntityNotDefined extends RuntimeException {
public EntityNotDefined(String message) {
super(message);
public EntityNotDefined(String pMessage) {
super(pMessage);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package software.bluelib.exception;

public class ResourceNotFound extends RuntimeException {
public ResourceNotFound(String message) {
super(message);
public ResourceNotFound(String pMessage) {
super(pMessage);
}
}
8 changes: 7 additions & 1 deletion TestMods/TestNeoForge/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ run
runs
run-data

repo
repo
/src/main/resources/assets/bluetest/animations/dragon.animation.json
/src/main/resources/assets/bluetest/geo/dragon.geo.json
/src/main/resources/assets/bluetest/variant/entity/dragon.json
/src/main/resources/assets/bluetest/textures/entity/dragon/purple.png
/src/main/resources/assets/bluetest/textures/entity/dragon/red.png
/src/main/resources/assets/bluetest/textures/entity/dragon/blue.png
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import software.bluetest.event.ClientEvents;
import software.bluetest.init.ModEntities;

@Mod(BlueTest.MODID)
public class BlueTest
Expand All @@ -12,6 +14,9 @@ public class BlueTest
public BlueTest(IEventBus pModEventBus)
{
//NeoForge.EVENT_BUS.register(this);
ModEntities.REGISTRY.register(pModEventBus);

pModEventBus.addListener(ClientEvents::registerAttributes);
pModEventBus.addListener(ClientEvents::registerRenderers);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,113 @@
package software.bluetest.entity.dragon;

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.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.server.level.ServerLevel;
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.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil;
import software.bluelib.BlueLib;
import software.bluelib.entity.variant.IVariantEntity;
import software.bluelib.entity.variant.VariantKeys;
import software.bluelib.entity.variant.VariantRegistry;
import software.bluetest.BlueTest;

public class DragonEntity extends PathfinderMob implements IVariantEntity, GeoEntity {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
import java.util.List;
import java.util.stream.Collectors;

public class DragonEntity extends TamableAnimal implements IVariantEntity, GeoEntity {
private final String entityName = "dragon";
private final VariantRegistry texturesLoader = new VariantRegistry();
private List<String> variantNames;

public DragonEntity(EntityType<? extends PathfinderMob> pEntityType, Level pLevel) {
public static final EntityDataAccessor<String> VARIANT = SynchedEntityData.defineId(DragonEntity.class, EntityDataSerializers.STRING);

public DragonEntity(EntityType<? extends TamableAnimal> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
texturesLoader.loadVariantsFromJson(getJSONLocation());
this.variantNames = List.of(getEntityName());
List<String> variants = getDragonVariants(getEntityName());
if (variants != null && !variants.isEmpty()) {
this.variantNames = variants;
}
}

@Override
public String GetEntityName() {
return "";
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(VARIANT, "");
}

public ResourceLocation GetTextureLocation() {
return null;
@Override
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putString("Variant", getVariantName());
}

@Override
public ResourceLocation GetVariantResource() {
return null;
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
this.setVariantName(pCompound.getString("Variant"));
}

// When Entity is Spawned
@Override
public void onAddedToWorld() {
super.onAddedToWorld();
if (!this.level().isClientSide) {
if (getVariantName().isEmpty()) {
this.setVariantName(getRandomVariant(variantNames, "blue"));
}
}
}

@Override
public String getEntityName() {
return entityName;
}

// Set the Variant
public void setVariantName(String pName) {
this.entityData.set(VARIANT, pName);
}

@Override
public String getVariantName() {
return this.entityData.get(VARIANT);
}

public List<String> getDragonVariants(String variantType) {
return texturesLoader.getVariants().stream()
.filter(variant -> variantType.equals(variant.getEntityName()))
.map(VariantKeys::getVariantName)
.collect(Collectors.toList());
}

public ResourceLocation getJSONLocation() {
return new ResourceLocation(BlueTest.MODID, "variant/entity/dragon.json");
}

// Just to get the Entity Working

private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

public static AttributeSupplier.Builder createAttributes() {
return Mob.createMobAttributes()
.add(Attributes.MOVEMENT_SPEED, 0.3)
.add(Attributes.MAX_HEALTH, 10)
.add(Attributes.ARMOR, 0)
.add(Attributes.ATTACK_DAMAGE, 3)
.add(Attributes.FOLLOW_RANGE, 16)
.add(Attributes.FLYING_SPEED, 0.3);
}

@Override
Expand All @@ -40,4 +118,10 @@ public void registerControllers(AnimatableManager.ControllerRegistrar pControlle
public AnimatableInstanceCache getAnimatableInstanceCache() {
return cache;
}

@Nullable
@Override
public AgeableMob getBreedOffspring(@NotNull ServerLevel pLevel, @NotNull AgeableMob pOtherParent) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ResourceLocation getModelResource(DragonEntity pObject) {
// Get the Texture Location
@Override
public ResourceLocation getTextureResource(DragonEntity pObject) {
return pObject.GetTextureLocation();
return pObject.getTextureLocation(BlueTest.MODID, "textures/entity/" + pObject.getEntityName() + "/" + pObject.getVariantName() + ".png");
}

// Get the Animation Location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package software.bluetest.event;

import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;
import software.bluetest.entity.dragon.DragonEntity;
import software.bluetest.entity.dragon.DragonRender;
import software.bluetest.init.ModEntities;

public class ClientEvents {
@SubscribeEvent
public static void registerRenderers(final EntityRenderersEvent.RegisterRenderers pEvent) {
// Register the renderer for all the Entities
pEvent.registerEntityRenderer(ModEntities.DRAGON.get(), DragonRender::new);
}

// Register the Attributes
@SubscribeEvent
public static void registerAttributes(EntityAttributeCreationEvent pEvent) {
pEvent.put(ModEntities.DRAGON.get(), DragonEntity.createAttributes().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ModEntities {

// List of Entities
public static final DeferredHolder<EntityType<?>, EntityType<DragonEntity>> DRAGON = register(
"test",
"dragon",
EntityType.Builder.of(DragonEntity::new, MobCategory.AMBIENT)
.setShouldReceiveVelocityUpdates(true)
.setTrackingRange(64)
Expand Down