Skip to content

Commit

Permalink
Created a Mixin
Browse files Browse the repository at this point in the history
Lowered the Files the Mod Owners need to use to create the Variant Loader
  • Loading branch information
MeAlam1 committed Sep 27, 2024
1 parent 8003e54 commit f123d99
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 162 deletions.
4 changes: 4 additions & 0 deletions NeoForge/src/main/java/software/bluelib/BlueLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.MixinEnvironment;
import software.bluelib.example.event.ClientEvents;
import software.bluelib.example.init.ModEntities;
import software.bluelib.utils.logging.BaseLogLevel;
Expand Down Expand Up @@ -72,6 +75,7 @@ public class BlueLib {
*/
public BlueLib(IEventBus pModEventBus, ModContainer pModContainer) {
pModEventBus.register(this);
MixinBootstrap.init();
if (isDeveloperMode()) {
ModEntities.REGISTRY.register(pModEventBus);
if (FMLEnvironment.dist.isClient()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package software.bluelib.example.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.server.level.ServerLevel;
import net.minecraft.world.DifficultyInstance;
Expand All @@ -20,6 +18,7 @@
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil;
import software.bluelib.interfaces.variant.IVariantAccessor;
import software.bluelib.interfaces.variant.IVariantEntity;
import software.bluelib.utils.logging.BaseLogger;
import software.bluelib.utils.variant.ParameterUtils;
Expand All @@ -39,8 +38,6 @@
* <li>{@link #addAdditionalSaveData(CompoundTag)} - Adds custom data to the entity's NBT for saving.</li>
* <li>{@link #readAdditionalSaveData(CompoundTag)} - Reads custom data from the entity's NBT for loading.</li>
* <li>{@link #finalizeSpawn(ServerLevelAccessor, DifficultyInstance, MobSpawnType, SpawnGroupData)} - Finalizes the spawning process and sets up parameters.</li>
* <li>{@link #setVariantName(String)} - Sets the variant name of the dragon.</li>
* <li>{@link #getVariantName()} - Retrieves the current variant name of the dragon.</li>
* </ul>
* </p>
*
Expand Down Expand Up @@ -80,56 +77,12 @@ public DragonEntity(EntityType<? extends TamableAnimal> pEntityType, Level pLeve
super(pEntityType, pLevel);
}

/**
* Defines the synchronized data for this dragon entity, including the variant.
* <p>
* This method initializes the {@link EntityDataAccessor} to handle the variant data.
* </p>
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
protected void defineSynchedData(SynchedEntityData.@NotNull Builder pBuilder) {
super.defineSynchedData(pBuilder);
pBuilder.define(VARIANT, "normal");
}

/**
* Adds custom data to the entity's NBT tag for saving.
* <p>
* This method stores the variant name in the NBT data so it can be restored when loading the entity.
* </p>
*
* @param pCompound {@link CompoundTag} - The NBT tag to which data should be added.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putString("Variant", getVariantName());
public void setVariantName(String pVariantName) {
((IVariantAccessor) this).setEntityVariantName(pVariantName);
}

/**
* Reads custom data from the entity's NBT tag for loading.
* <p>
* This method retrieves the variant name from the NBT data and sets it for the entity.
* </p>
*
* @param pCompound {@link CompoundTag} - The NBT tag from which data should be read.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
this.setVariantName(pCompound.getString("Variant"));
public String getVariantName() {
return ((IVariantAccessor) this).getEntityVariantName();
}

/**
Expand All @@ -151,7 +104,7 @@ public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
@Override
public SpawnGroupData finalizeSpawn(@NotNull ServerLevelAccessor pLevel, @NotNull DifficultyInstance pDifficulty, @NotNull MobSpawnType pReason, @Nullable SpawnGroupData pSpawnData) {
if (getVariantName() == null || getVariantName().isEmpty()) {
this.setVariantName(getRandomVariant(getEntityVariants(entityName), "normal"));
setVariantName(getRandomVariant(getEntityVariants(entityName), "normal"));
ParameterUtils.ParameterBuilder.forVariant(entityName,this.getVariantName())
.withParameter("customParameter")
.withParameter("int")
Expand All @@ -163,31 +116,9 @@ public SpawnGroupData finalizeSpawn(@NotNull ServerLevelAccessor pLevel, @NotNul
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData);
}

/**
* Sets the variant name for the dragon entity.
*
* @param pName {@link String} - The name of the variant to set.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
public void setVariantName(String pName) {
this.entityData.set(VARIANT, pName);
}

/**
* Retrieves the current variant name of the dragon entity.
*
* @return {@link String} - The current variant name.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
public String getVariantName() {
return this.entityData.get(VARIANT);
}



/**
* All Code below this Fragment is not Library Related!!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil;
import software.bluelib.interfaces.variant.IVariantAccessor;
import software.bluelib.interfaces.variant.IVariantEntity;
import software.bluelib.utils.variant.ParameterUtils;

Expand Down Expand Up @@ -48,16 +49,6 @@
* @since 1.0.0
*/
public class RexEntity extends TamableAnimal implements IVariantEntity, GeoEntity {
/**
* Entity data accessor for the variant of the Rex.
* <p>
* This is used to store and retrieve the variant data for synchronization between server and client.
* </p>
* @Co-author MeAlam, Dan
* @since 1.0.0
*/
public static final EntityDataAccessor<String> VARIANT = SynchedEntityData.defineId(RexEntity.class, EntityDataSerializers.STRING);

/**
* The name of the entity.
* @Co-author MeAlam, Dan
Expand All @@ -79,56 +70,12 @@ public RexEntity(EntityType<? extends TamableAnimal> pEntityType, Level pLevel)
super(pEntityType, pLevel);
}

/**
* Defines the synchronized data for this Rex entity, including the variant.
* <p>
* This method initializes the {@link EntityDataAccessor} to handle the variant data.
* </p>
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
protected void defineSynchedData(SynchedEntityData.@NotNull Builder pBuilder) {
super.defineSynchedData(pBuilder);
pBuilder.define(VARIANT, "normal");
}

/**
* Adds custom data to the entity's NBT tag for saving.
* <p>
* This method stores the variant name in the NBT data so it can be restored when loading the entity.
* </p>
*
* @param pCompound {@link CompoundTag} - The NBT tag to which data should be added.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.addAdditionalSaveData(pCompound);
pCompound.putString("Variant", getVariantName());
public void setVariantName(String pVariantName) {
((IVariantAccessor) this).setEntityVariantName(pVariantName);
}

/**
* Reads custom data from the entity's NBT tag for loading.
* <p>
* This method retrieves the variant name from the NBT data and sets it for the entity.
* </p>
*
* @param pCompound {@link CompoundTag} - The NBT tag from which data should be read.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
@Override
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
super.readAdditionalSaveData(pCompound);
this.setVariantName(pCompound.getString("Variant"));
public String getVariantName() {
return ((IVariantAccessor) this).getEntityVariantName();
}

/**
Expand Down Expand Up @@ -161,32 +108,6 @@ public SpawnGroupData finalizeSpawn(@NotNull ServerLevelAccessor pLevel, @NotNul
return super.finalizeSpawn(pLevel, pDifficulty, pReason, pSpawnData);
}

/**
* Sets the variant name for the Rex entity.
*
* @param pName {@link String} - The name of the variant to set.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
public void setVariantName(String pName) {
this.entityData.set(VARIANT, pName);
}

/**
* Retrieves the current variant name of the Rex entity.
*
* @return {@link String} - The current variant name.
*
* @since 1.0.0
* @author MeAlam
* @Co-author Dan
*/
public String getVariantName() {
return this.entityData.get(VARIANT);
}

/**
* All Code below this Fragment is not Library Related!!!
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package software.bluelib.interfaces.variant;

public interface IVariantAccessor {
void setEntityVariantName(String pVariantName);
String getEntityVariantName();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package software.bluelib.mixin.variant;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import software.bluelib.interfaces.variant.IVariantAccessor;

@Mixin(LivingEntity.class)
public class LivingEntityMixin implements IVariantAccessor {

@Unique
private static final EntityDataAccessor<String> bluelib$VARIANT =
SynchedEntityData.defineId(LivingEntity.class, EntityDataSerializers.STRING);

@Inject(method = "defineSynchedData", at = @At("HEAD"))
protected void defineSynchedData(SynchedEntityData.@NotNull Builder pBuilder, CallbackInfo pCi) {
pBuilder.define(bluelib$VARIANT, "normal");
}

@Inject(method = "addAdditionalSaveData", at = @At("HEAD"))
public void addAdditionalSaveData(@NotNull CompoundTag pCompound, CallbackInfo pCi) {
pCompound.putString("Variant", bluelib$getVariantName());
}

@Inject(method = "readAdditionalSaveData", at = @At("HEAD"))
public void readAdditionalSaveData(@NotNull CompoundTag pCompound, CallbackInfo pCi) {
bluelib$setVariantName(pCompound.getString("Variant"));
}

@Unique
public void bluelib$setVariantName(String pName) {
((Entity) (Object) this).getEntityData().set(bluelib$VARIANT, pName);
}

@Unique
public String bluelib$getVariantName() {
return ((Entity) (Object) this).getEntityData().get(bluelib$VARIANT);
}

@Override
public void setEntityVariantName(String pVariantName) {
bluelib$setVariantName(pVariantName);
}

@Override
public String getEntityVariantName() {
return bluelib$getVariantName();
}
}

5 changes: 4 additions & 1 deletion NeoForge/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ description='''${mod_description}'''
type="required"
versionRange="${minecraft_version_range}"
ordering="NONE"
side="BOTH"
side="BOTH"

[[mixins]]
config = "bluelib.mixins.json"
14 changes: 14 additions & 0 deletions NeoForge/src/main/resources/bluelib.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8.0",
"package": "software.bluelib.mixin",
"refmap": "bluelib.refmap.json",
"mixins": [
"variant.LivingEntityMixin"
],
"client": [],
"server": [],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit f123d99

Please sign in to comment.