string-controlled BaseTNTRenderer

This commit is contained in:
Jenny 2025-01-21 18:22:36 +01:00
parent 400942b6f8
commit b6f950b788
Signed by: Jenny
GPG Key ID: 4A98012FB1C39311
10 changed files with 62 additions and 78 deletions

View File

@ -38,7 +38,7 @@ mod_name=Compressed TNT
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved mod_license=All Rights Reserved
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=0.1.0 mod_version=0.2.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View File

@ -52,8 +52,6 @@ public class strongerTNTBlock extends TntBlock {
if (!level.isClientSide) { if (!level.isClientSide) {
int ft = (short) (level.random.nextInt(fuseTime / 4) + fuseTime / 8); int ft = (short) (level.random.nextInt(fuseTime / 4) + fuseTime / 8);
StrongerPrimedTNT primedtnt = new StrongerPrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, pExplosion.getIndirectSourceEntity(), pRadius, ft); StrongerPrimedTNT primedtnt = new StrongerPrimedTNT(level, (double) blockPos.getX() + (double) 0.5F, (double) blockPos.getY(), (double) blockPos.getZ() + (double) 0.5F, pExplosion.getIndirectSourceEntity(), pRadius, ft);
int i = primedtnt.getFuse();
primedtnt.setFuse((short) (level.random.nextInt(i / 4) + i / 8));
level.addFreshEntity(primedtnt); level.addFreshEntity(primedtnt);
} }
} }

View File

@ -15,9 +15,11 @@ public class ClusterPrimedTNT extends basePrimedTNT {
this.setFuse(fuse); this.setFuse(fuse);
this.setPower(power); this.setPower(power);
this.addDeltaMovement(move); this.addDeltaMovement(move);
this.setRenderID("cluster");
} }
public ClusterPrimedTNT(EntityType<ClusterPrimedTNT> entityType, Level level) { public ClusterPrimedTNT(EntityType<ClusterPrimedTNT> entityType, Level level) {
super(entityType, level, null); super(entityType, level, null);
this.setRenderID("cluster");
} }
} }

View File

@ -12,9 +12,23 @@ public class StrongerPrimedTNT extends basePrimedTNT {
this.setPos(pX, pY, pZ); this.setPos(pX, pY, pZ);
this.setFuse(fuse); this.setFuse(fuse);
this.setPower(power); this.setPower(power);
this.setRenderID(evalRenderID());
} }
public StrongerPrimedTNT(EntityType<StrongerPrimedTNT> entityType, Level level) { public StrongerPrimedTNT(EntityType<StrongerPrimedTNT> entityType, Level level) {
super(entityType, level, null); super(entityType, level, null);
this.setRenderID(evalRenderID());
} }
}
public String evalRenderID() {
int a = (int) this.getPower();
return switch ((int) this.getPower()) {
case 8 -> "stronger_8";
case 16 -> "stronger_16";
case 32 -> "stronger_32";
case 64 -> "stronger_64";
case 128 -> "stronger_128";
default -> "default";
};
}
}

View File

@ -13,10 +13,13 @@ import javax.annotation.Nullable;
public abstract class basePrimedTNT extends Entity implements TraceableEntity { public abstract class basePrimedTNT extends Entity implements TraceableEntity {
private static final EntityDataAccessor<Integer> DATA_FUSE_ID = SynchedEntityData.defineId(basePrimedTNT.class, EntityDataSerializers.INT); private static final EntityDataAccessor<Integer> DATA_FUSE_ID = SynchedEntityData.defineId(basePrimedTNT.class, EntityDataSerializers.INT);
private static final EntityDataAccessor<Float> DATA_POWER_ID = SynchedEntityData.defineId(basePrimedTNT.class, EntityDataSerializers.FLOAT); private static final EntityDataAccessor<Float> DATA_POWER_ID = SynchedEntityData.defineId(basePrimedTNT.class, EntityDataSerializers.FLOAT);
private static final EntityDataAccessor<String> DATA_RENDER_ID = SynchedEntityData.defineId(basePrimedTNT.class, EntityDataSerializers.STRING);
@Nullable @Nullable
private LivingEntity owner; private LivingEntity owner;
private String renderID = "default";
public basePrimedTNT(EntityType<? extends basePrimedTNT> pEntityType, Level pLevel, @Nullable LivingEntity owner) { public basePrimedTNT(EntityType<? extends basePrimedTNT> pEntityType, Level pLevel, @Nullable LivingEntity owner) {
super(pEntityType, pLevel); super(pEntityType, pLevel);
double d0 = pLevel.random.nextDouble() * (double)((float)Math.PI * 2F); double d0 = pLevel.random.nextDouble() * (double)((float)Math.PI * 2F);
@ -29,7 +32,7 @@ public abstract class basePrimedTNT extends Entity implements TraceableEntity {
this.level().explode(this, this.getX(), this.getY(0.0625D), this.getZ(), this.getPower(), Level.ExplosionInteraction.TNT); this.level().explode(this, this.getX(), this.getY(0.0625D), this.getZ(), this.getPower(), Level.ExplosionInteraction.TNT);
} }
public int getFuse() { public int getFuse() {
return this.entityData.get(DATA_FUSE_ID); return this.entityData.get(DATA_FUSE_ID);
} }
@ -79,20 +82,22 @@ public abstract class basePrimedTNT extends Entity implements TraceableEntity {
return Entity.MovementEmission.NONE; return Entity.MovementEmission.NONE;
} }
protected void defineSynchedData() { protected void defineSynchedData() {
this.entityData.define(DATA_FUSE_ID, 80); this.entityData.define(DATA_FUSE_ID, 80);
this.entityData.define(DATA_POWER_ID, 4.0f); this.entityData.define(DATA_POWER_ID, 4.0f);
this.entityData.define(DATA_RENDER_ID, "default");
} }
protected void addAdditionalSaveData(CompoundTag pCompound) { protected void addAdditionalSaveData(CompoundTag pCompound) {
pCompound.putShort("Fuse", (short)this.getFuse()); pCompound.putShort("Fuse", (short)this.getFuse());
pCompound.putFloat("Power", (short)this.getPower()); pCompound.putFloat("Power", (short)this.getPower());
pCompound.putString("RenderID", getRenderID());
} }
protected void readAdditionalSaveData(CompoundTag pCompound) { protected void readAdditionalSaveData(CompoundTag pCompound) {
this.setFuse(pCompound.getShort("Fuse")); this.setFuse(pCompound.getShort("Fuse"));
this.setPower(pCompound.getFloat("Power")); this.setPower(pCompound.getFloat("Power"));
this.setRenderID(pCompound.getString("RenderID"));
} }
@Nullable @Nullable
@ -103,4 +108,12 @@ public abstract class basePrimedTNT extends Entity implements TraceableEntity {
public void setOwner(LivingEntity owner) { public void setOwner(LivingEntity owner) {
this.owner = owner; this.owner = owner;
} }
public void setRenderID(String renderID) {
this.entityData.set(DATA_RENDER_ID, renderID);
}
public String getRenderID() {
return this.entityData.get(DATA_RENDER_ID);
}
} }

View File

@ -1,5 +1,6 @@
package com.jenny.compressedtnt.entities.client; package com.jenny.compressedtnt.entities.client;
import com.jenny.compressedtnt.blocks.blocks;
import com.jenny.compressedtnt.entities.basePrimedTNT; import com.jenny.compressedtnt.entities.basePrimedTNT;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis; import com.mojang.math.Axis;
@ -12,19 +13,19 @@ import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;
public class BaseTNTRenderer extends EntityRenderer<basePrimedTNT> { public class BaseTNTRenderer extends EntityRenderer<basePrimedTNT> {
private BlockRenderDispatcher blockRenderer; private final BlockRenderDispatcher blockRenderer;
private final Block block;
public BaseTNTRenderer(EntityRendererProvider.Context pContext, Block block) { public BaseTNTRenderer(EntityRendererProvider.Context pContext) {
super(pContext); super(pContext);
this.shadowRadius = 0.5F; this.shadowRadius = 0.5F;
this.blockRenderer = pContext.getBlockRenderDispatcher(); this.blockRenderer = pContext.getBlockRenderDispatcher();
this.block = block;
} }
public void render(basePrimedTNT pEntity, float pEntityYaw, float pPartialTicks, PoseStack pPoseStack, MultiBufferSource pBuffer, int pPackedLight) { public void render(basePrimedTNT pEntity, float pEntityYaw, float pPartialTicks, PoseStack pPoseStack, @NotNull MultiBufferSource pBuffer, int pPackedLight) {
pPoseStack.pushPose(); pPoseStack.pushPose();
pPoseStack.translate(0.0F, 0.5F, 0.0F); pPoseStack.translate(0.0F, 0.5F, 0.0F);
int i = pEntity.getFuse(); int i = pEntity.getFuse();
@ -36,6 +37,19 @@ public class BaseTNTRenderer extends EntityRenderer<basePrimedTNT> {
float f1 = 1.0F + f * 0.3F; float f1 = 1.0F + f * 0.3F;
pPoseStack.scale(f1, f1, f1); pPoseStack.scale(f1, f1, f1);
} }
String renderID = pEntity.getRenderID();
if (renderID == null) {
renderID = "";
}
Block block = switch (renderID) {
case "stronger_8" -> blocks.TNT_8.get();
case "stronger_16" -> blocks.TNT_16.get();
case "stronger_32" -> blocks.TNT_32.get();
case "stronger_64" -> blocks.TNT_64.get();
case "stronger_128" -> blocks.TNT_128.get();
case "homing" -> blocks.TNT_HOMING.get();
default -> Blocks.NETHER_PORTAL; // placeholder for debugging
};
pPoseStack.mulPose(Axis.YP.rotationDegrees(-90.0F)); pPoseStack.mulPose(Axis.YP.rotationDegrees(-90.0F));
pPoseStack.translate(-0.5F, -0.5F, 0.5F); pPoseStack.translate(-0.5F, -0.5F, 0.5F);
@ -45,7 +59,8 @@ public class BaseTNTRenderer extends EntityRenderer<basePrimedTNT> {
super.render(pEntity, pEntityYaw, pPartialTicks, pPoseStack, pBuffer, pPackedLight); super.render(pEntity, pEntityYaw, pPartialTicks, pPoseStack, pBuffer, pPackedLight);
} }
public ResourceLocation getTextureLocation(basePrimedTNT pEntity) { @NotNull
public ResourceLocation getTextureLocation(@NotNull basePrimedTNT pEntity) {
return TextureAtlas.LOCATION_BLOCKS; return TextureAtlas.LOCATION_BLOCKS;
} }
} }

View File

@ -1,6 +1,5 @@
package com.jenny.compressedtnt.entities.client; package com.jenny.compressedtnt.entities.client;
import com.jenny.compressedtnt.blocks.blocks;
import com.jenny.compressedtnt.entities.ClusterPrimedTNT; import com.jenny.compressedtnt.entities.ClusterPrimedTNT;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis; import com.mojang.math.Axis;
@ -12,6 +11,7 @@ import net.minecraft.client.renderer.entity.TntMinecartRenderer;
import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class clusterTNTRenderer extends EntityRenderer<ClusterPrimedTNT> { public class clusterTNTRenderer extends EntityRenderer<ClusterPrimedTNT> {
@ -42,7 +42,7 @@ public class clusterTNTRenderer extends EntityRenderer<ClusterPrimedTNT> {
pPoseStack.mulPose(Axis.YP.rotationDegrees(-90.0F)); pPoseStack.mulPose(Axis.YP.rotationDegrees(-90.0F));
pPoseStack.translate(-0.5F, -0.5F, 0.5F); pPoseStack.translate(-0.5F, -0.5F, 0.5F);
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F)); pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
TntMinecartRenderer.renderWhiteSolidBlock(this.blockRenderer, blocks.TNT_CLUSTER_2.get().defaultBlockState(), pPoseStack, pBuffer, pPackedLight, i / 5 % 2 == 0); TntMinecartRenderer.renderWhiteSolidBlock(this.blockRenderer, Blocks.TNT.defaultBlockState(), pPoseStack, pBuffer, pPackedLight, i / 5 % 2 == 0);
pPoseStack.popPose(); pPoseStack.popPose();
super.render(pEntity, pEntityYaw, pPartialTicks, pPoseStack, pBuffer, pPackedLight); super.render(pEntity, pEntityYaw, pPartialTicks, pPoseStack, pBuffer, pPackedLight);
} }

View File

@ -1,59 +0,0 @@
package com.jenny.compressedtnt.entities.client;
import com.jenny.compressedtnt.blocks.blocks;
import com.jenny.compressedtnt.entities.StrongerPrimedTNT;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.TntMinecartRenderer;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.NotNull;
public class strongerTNTRenderer extends EntityRenderer<StrongerPrimedTNT> {
private final BlockRenderDispatcher blockRenderer;
public strongerTNTRenderer(EntityRendererProvider.Context pContext) {
super(pContext);
this.shadowRadius = 0.5F;
this.blockRenderer = pContext.getBlockRenderDispatcher();
}
public void render(StrongerPrimedTNT pEntity, float pEntityYaw, float pPartialTicks, PoseStack pPoseStack, @NotNull MultiBufferSource pBuffer, int pPackedLight) {
pPoseStack.pushPose();
pPoseStack.translate(0.0F, 0.5F, 0.0F);
int i = pEntity.getFuse();
if ((float)i - pPartialTicks + 1.0F < 10.0F) {
float f = 1.0F - ((float)i - pPartialTicks + 1.0F) / 10.0F;
f = Mth.clamp(f, 0.0F, 1.0F);
f *= f;
f *= f;
float f1 = 1.0F + f * 0.3F;
pPoseStack.scale(f1, f1, f1);
}
Block block = switch ((int) pEntity.getPower()) {
case 8 -> blocks.TNT_8.get();
case 16 -> blocks.TNT_16.get();
case 32 -> blocks.TNT_32.get();
case 64 -> blocks.TNT_64.get();
case 128 -> blocks.TNT_128.get();
default -> blocks.TNT_HOMING.get();
};
pPoseStack.mulPose(Axis.YP.rotationDegrees(-90.0F));
pPoseStack.translate(-0.5F, -0.5F, 0.5F);
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
TntMinecartRenderer.renderWhiteSolidBlock(this.blockRenderer, block.defaultBlockState(), pPoseStack, pBuffer, pPackedLight, i / 5 % 2 == 0);
pPoseStack.popPose();
super.render(pEntity, pEntityYaw, pPartialTicks, pPoseStack, pBuffer, pPackedLight);
}
public @NotNull ResourceLocation getTextureLocation(@NotNull StrongerPrimedTNT pEntity) {
return TextureAtlas.LOCATION_BLOCKS;
}
}

View File

@ -1,7 +1,7 @@
package com.jenny.compressedtnt.entities; package com.jenny.compressedtnt.entities;
import com.jenny.compressedtnt.blocks.blocks; import com.jenny.compressedtnt.entities.client.BaseTNTRenderer;
import com.jenny.compressedtnt.entities.client.*; import com.jenny.compressedtnt.entities.client.clusterTNTRenderer;
import net.minecraft.client.renderer.entity.EntityRenderers; import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory; import net.minecraft.world.entity.MobCategory;
@ -33,9 +33,9 @@ public class entities {
} }
public static void registerRenderers () { public static void registerRenderers () {
EntityRenderers.register(TNT_STRONGER.get(), strongerTNTRenderer::new); EntityRenderers.register(TNT_STRONGER.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_HOMING.get(), BaseTNTRenderer::new);
EntityRenderers.register(TNT_CLUSTER.get(), clusterTNTRenderer::new); EntityRenderers.register(TNT_CLUSTER.get(), clusterTNTRenderer::new);
EntityRenderers.register(TNT_HOMING.get(), pContext -> new BaseTNTRenderer(pContext, blocks.TNT_HOMING.get()));
} }

View File

@ -14,13 +14,14 @@ public class homingPrimedTNT extends basePrimedTNT {
Entity target; Entity target;
public homingPrimedTNT (Level pLevel, double pX, double pY, double pZ, @Nullable LivingEntity pOwner, float power, int fuse, float speed) { public homingPrimedTNT (Level pLevel, double pX, double pY, double pZ, @Nullable LivingEntity pOwner, float power, int fuse, float speed) {
this(entities.TNT_HOMING.get(), pLevel); super(entities.TNT_HOMING.get(), pLevel, pOwner);
this.setPos(pX, pY, pZ); this.setPos(pX, pY, pZ);
this.setOwner(pOwner); this.setOwner(pOwner);
this.speed = speed; this.speed = speed;
this.target = null; this.target = null;
this.setPower(power); this.setPower(power);
this.setFuse(fuse); this.setFuse(fuse);
this.setRenderID("homing");
} }
public homingPrimedTNT(EntityType<homingPrimedTNT> entityType, Level level) { public homingPrimedTNT(EntityType<homingPrimedTNT> entityType, Level level) {