Skip to content

Commit

Permalink
铁砧上升动画
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuRuoLing committed Apr 17, 2024
1 parent 72f7ece commit a6002c2
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 13 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/AnvilCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModDispenserBehavior;
import dev.dubhe.anvilcraft.init.ModEntities;
import dev.dubhe.anvilcraft.init.ModEvents;
import dev.dubhe.anvilcraft.init.ModItemGroups;
import dev.dubhe.anvilcraft.init.ModItems;
Expand Down Expand Up @@ -40,6 +41,7 @@ public static void init() {
// common
ModEvents.register();
ModBlocks.register();
ModEntities.register();
ModItems.register();
ModItemGroups.register();
ModBlockEntities.register();
Expand Down
34 changes: 24 additions & 10 deletions common/src/main/java/dev/dubhe/anvilcraft/block/MagnetBlock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.entity.AscendingBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
Expand Down Expand Up @@ -77,31 +80,42 @@ public void neighborChanged(
}
}

private void attract(BlockState state, @NotNull Level level, @NotNull BlockPos magnet) {
private void attract(BlockState state, @NotNull Level level, @NotNull BlockPos magnetPos) {
if (level.isClientSide()) return;
if (!(state.getBlock() instanceof MagnetBlock) || state.getValue(LIT)) return;
if (level.getBlockState(magnet.below()).is(BlockTags.ANVIL)) return;
if (level.getBlockState(magnetPos.below()).is(BlockTags.ANVIL)) return;
int distance = AnvilCraft.config.magnetAttractsDistance;
BlockPos anvil = magnet;
BlockPos currentPos = magnetPos;
checkAnvil:
for (int i = 0; i < distance; i++) {
anvil = anvil.below();
BlockState state1 = level.getBlockState(anvil);
currentPos = currentPos.below();
BlockState state1 = level.getBlockState(currentPos);
if (state1.is(BlockTags.ANVIL)) {
level.setBlockAndUpdate(magnet.below(), state1);
level.setBlockAndUpdate(anvil, Blocks.AIR.defaultBlockState());
//level.setBlock(currentPos, state1.getFluidState().createLegacyBlock(), 3);
AscendingBlockEntity.ascend(level, currentPos, state1);
break;
}
List<FallingBlockEntity> entities = level.getEntitiesOfClass(FallingBlockEntity.class, new AABB(anvil));

List<AscendingBlockEntity> ascendingBlockEntities = level.getEntitiesOfClass(
AscendingBlockEntity.class,
new AABB(currentPos)
);
if (!ascendingBlockEntities.isEmpty()) return;

List<FallingBlockEntity> entities = level.getEntitiesOfClass(
FallingBlockEntity.class,
new AABB(currentPos)
);
for (FallingBlockEntity entity : entities) {
if (entity instanceof AscendingBlockEntity) continue;
BlockState state2 = entity.getBlockState();
if (state2.is(BlockTags.ANVIL)) {
level.setBlockAndUpdate(magnet.below(), state2);
entity.remove(Entity.RemovalReason.DISCARDED);
AscendingBlockEntity.ascend(level, currentPos, state2);
break checkAnvil;
}
}
if (!level.isEmptyBlock(anvil)) return;
if (!level.isEmptyBlock(currentPos)) return;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.dubhe.anvilcraft.entity;

import dev.dubhe.anvilcraft.init.ModEntities;
import dev.dubhe.anvilcraft.mixin.accessor.FallingBlockEntityAccessor;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.Vec3;

public class AscendingBlockEntity extends FallingBlockEntity {
public AscendingBlockEntity(EntityType<? extends AscendingBlockEntity> entityType, Level level) {
super(entityType, level);
}

private AscendingBlockEntity(Level level, double x, double y, double z, BlockState state) {
this(ModEntities.ASCENDING_BLOCK_ENTITY.get(), level);
((FallingBlockEntityAccessor) this).setBlockState(state);
this.blocksBuilding = true;
this.setPos(x, y, z);
this.setDeltaMovement(Vec3.ZERO);
this.xo = x;
this.yo = y;
this.zo = z;
this.setStartPos(this.blockPosition());
}

@Override
public void tick() {
if (this.getBlockState().isAir()) {
System.out.println("this.getBlockState() = " + this.getBlockState());
this.discard();
return;
}
if (!this.isNoGravity()) {
this.setDeltaMovement(this.getDeltaMovement().add(0.0, 0.4, 0.0));
}
this.move(MoverType.SELF, this.getDeltaMovement());
if (this.level().isClientSide) return;
BlockPos current = this.blockPosition();
BlockPos up = current.above();
if (!this.level().getBlockState(up).isAir()) {
this.level().setBlockAndUpdate(current, this.getBlockState());
this.discard();
}
this.setDeltaMovement(this.getDeltaMovement().scale(0.98));
}


public static AscendingBlockEntity ascend(Level level, BlockPos pos, BlockState blockState) {

Check warning on line 53 in common/src/main/java/dev/dubhe/anvilcraft/entity/AscendingBlockEntity.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle] common/src/main/java/dev/dubhe/anvilcraft/entity/AscendingBlockEntity.java#L53 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck>

Missing a Javadoc comment.
Raw output
/home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/entity/AscendingBlockEntity.java:53:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
AscendingBlockEntity ascendingBlockEntity = new AscendingBlockEntity(
level,
pos.getX() + 0.5,
pos.getY(),
pos.getZ() + 0.5,
blockState.hasProperty(BlockStateProperties.WATERLOGGED)
? blockState.setValue(BlockStateProperties.WATERLOGGED, false)
: blockState
);
level.setBlock(pos, blockState.getFluidState().createLegacyBlock(), 3);
level.addFreshEntity(ascendingBlockEntity);
return ascendingBlockEntity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dev.dubhe.anvilcraft.entity.render;

import com.mojang.blaze3d.vertex.PoseStack;
import dev.dubhe.anvilcraft.entity.AscendingBlockEntity;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
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.texture.OverlayTexture;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class AscendingBlockRenderer extends EntityRenderer<AscendingBlockEntity> {
private final BlockRenderDispatcher dispatcher;

public AscendingBlockRenderer(EntityRendererProvider.Context context) {

Check warning on line 26 in common/src/main/java/dev/dubhe/anvilcraft/entity/render/AscendingBlockRenderer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle] common/src/main/java/dev/dubhe/anvilcraft/entity/render/AscendingBlockRenderer.java#L26 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck>

Missing a Javadoc comment.
Raw output
/home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/entity/render/AscendingBlockRenderer.java:26:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
super(context);
this.shadowRadius = 0.5F;
this.dispatcher = context.getBlockRenderDispatcher();
}

@Override
public void render(
AscendingBlockEntity entity,
float entityYaw,
float partialTicks,
PoseStack poseStack,
MultiBufferSource buffer,
int packedLight
) {
BlockState blockState = entity.getBlockState();
if (blockState.getRenderShape() == RenderShape.MODEL) {
Level level = entity.level();
if (blockState != level.getBlockState(entity.blockPosition())
&& blockState.getRenderShape() != RenderShape.INVISIBLE
) {
poseStack.pushPose();
BlockPos blockPos = BlockPos.containing(entity.getX(), entity.getBoundingBox().maxY, entity.getZ());
poseStack.translate(-0.5, 0.0, -0.5);
this.dispatcher
.getModelRenderer()
.tesselateBlock(
level,
this.dispatcher.getBlockModel(blockState),
blockState,
blockPos,
poseStack,
buffer.getBuffer(ItemBlockRenderTypes.getMovingBlockRenderType(blockState)),
false,
RandomSource.create(),
blockState.getSeed(entity.getStartPos()),
OverlayTexture.NO_OVERLAY);
poseStack.popPose();
super.render(entity, entityYaw, partialTicks, poseStack, buffer, packedLight);
}
}
}

@Override
public @NotNull ResourceLocation getTextureLocation(@NotNull AscendingBlockEntity entity) {
return TextureAtlas.LOCATION_BLOCKS;
}
}
18 changes: 18 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/init/ModEntities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.dubhe.anvilcraft.init;

import com.tterrag.registrate.util.entry.EntityEntry;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.entity.AscendingBlockEntity;
import dev.dubhe.anvilcraft.entity.render.AscendingBlockRenderer;
import net.minecraft.world.entity.MobCategory;

public class ModEntities {
public static final EntityEntry<? extends AscendingBlockEntity> ASCENDING_BLOCK_ENTITY = AnvilCraft.REGISTRATE
.entity("ascending_block", AscendingBlockEntity::new, MobCategory.MISC)
.renderer(() -> AscendingBlockRenderer::new)
.register();

public static void register() {
// intentionally empty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.block.MagnetBlock;
import dev.dubhe.anvilcraft.entity.AscendingBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockTags;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -90,8 +91,7 @@ public void onPlace(
else return;
}
if (!level.isEmptyBlock(magnet.below())) return;
level.setBlockAndUpdate(magnet.below(), state);
level.setBlockAndUpdate(anvil, Blocks.AIR.defaultBlockState());
AscendingBlockEntity.ascend(level, anvil, state);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.dubhe.anvilcraft.mixin.accessor;

import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(FallingBlockEntity.class)

Check warning on line 8 in common/src/main/java/dev/dubhe/anvilcraft/mixin/accessor/FallingBlockEntityAccessor.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle] common/src/main/java/dev/dubhe/anvilcraft/mixin/accessor/FallingBlockEntityAccessor.java#L8 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck>

Missing a Javadoc comment.
Raw output
/home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/mixin/accessor/FallingBlockEntityAccessor.java:8:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
public interface FallingBlockEntityAccessor {
@Accessor("blockState")
void setBlockState(BlockState state);
}
3 changes: 2 additions & 1 deletion common/src/main/resources/anvilcraft-common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"DispenseItemWaterBottleBehaviorMixin",
"ItemEntityMixin",
"SolidBucketItemMixin",
"accessor.BaseSpawnerAccessor"
"accessor.BaseSpawnerAccessor",
"accessor.FallingBlockEntityAccessor"
],
"client": [
"LevelRendererMixin"
Expand Down

0 comments on commit a6002c2

Please sign in to comment.