Skip to content

Commit

Permalink
#feat: 完成了传送带的模型、方块状态,缺失渲染功能以及实际物流功能。目前传送带纹理与实际方向不匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
HeChuQIU committed Jan 23, 2024
1 parent b29ccdc commit 5313ef8
Show file tree
Hide file tree
Showing 30 changed files with 881 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/hechu/mindustry/Mindustry.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.hechu.mindustry;

import com.hechu.mindustry.client.model.MissileBulletModel;
import com.hechu.mindustry.client.renderer.blockentity.MechanicalDrillBlockEntityRenderer;
import com.hechu.mindustry.client.renderer.blockentity.PneumaticDrillBlockEntityRenderer;
import com.hechu.mindustry.client.renderer.blockentity.PowerNodeRenderer;
import com.hechu.mindustry.client.renderer.blockentity.TurretRenderer;
import com.hechu.mindustry.client.renderer.blockentity.*;
import com.hechu.mindustry.client.renderer.entity.BasicBulletRender;
import com.hechu.mindustry.client.renderer.entity.MissileBulletRender;
import com.hechu.mindustry.config.CommonConfig;
Expand All @@ -15,11 +12,13 @@
import com.hechu.mindustry.world.entity.turrets.Duo;
import com.hechu.mindustry.world.entity.turrets.DuoRenderer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.RenderTypeHelper;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.loading.FMLPaths;
import software.bernie.geckolib.GeckoLib;

Expand Down Expand Up @@ -57,12 +56,12 @@ public static void onRegisterLayers(EntityRenderersEvent.RegisterLayerDefinition

@SubscribeEvent
public static void onRegisterRenderers(EntityRenderersEvent.RegisterRenderers event) {

event.registerBlockEntityRenderer(BlockEntityModule.MECHANICAL_DRILL_BLOCK_ENTITY.get(), context -> new MechanicalDrillBlockEntityRenderer());
event.registerBlockEntityRenderer(BlockEntityModule.PNEUMATIC_DRILL_BLOCK_ENTITY.get(), context -> new PneumaticDrillBlockEntityRenderer());
event.registerBlockEntityRenderer(BlockEntityModule.SWARMER_TURRET_BLOCK_ENTITY.get(), TurretRenderer::new);
event.registerBlockEntityRenderer(BlockEntityModule.SPECTRE_TURRET_BLOCK_ENTITY.get(), TurretRenderer::new);
event.registerBlockEntityRenderer(BlockEntityModule.POWER_NODE_BLOCK_ENTITY.get(), pContext -> new PowerNodeRenderer());
event.registerBlockEntityRenderer(BlockEntityModule.CONVEYOR_BLOCK_ENTITY.get(), pContext -> new ConveyorBlockEntityRenderer());

event.registerEntityRenderer(EntityModule.DUO.get(), DuoRenderer::new);
event.registerEntityRenderer(EntityModule.MISSILE_BULLET.get(), MissileBulletRender::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.hechu.mindustry.client.renderer.blockentity;

import com.hechu.mindustry.world.level.block.entity.distribution.ConveyorBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

@OnlyIn(Dist.CLIENT)
public class ConveyorBlockEntityRenderer implements BlockEntityRenderer<ConveyorBlockEntity> {
@Override
public void render(@NotNull ConveyorBlockEntity blockEntity, float partialTick, @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight, int packedOverlay) {
List<ItemStack> items = new ArrayList<>();
for (int i = 0; i < ConveyorBlockEntity.MAX_ITEMS; i++) {
ItemStack stack = blockEntity.getItemHandler().getStackInSlot(i);
items.add(stack);
}

ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();

for (int i = 0; i < ConveyorBlockEntity.MAX_ITEMS; i++) {
poseStack.pushPose();
int c = ConveyorBlockEntity.MAX_ITEMS / 2 + 1;
float offset = (i - c) * 1F / ConveyorBlockEntity.MAX_ITEMS;
var diff = blockEntity.getBlockPos().getCenter().subtract(Vec3.atLowerCornerOf(blockEntity.getBlockPos()));
poseStack.translate(diff.x, diff.y, diff.z);
poseStack.translate(0f, 0f, offset);
// poseStack.scale(0.5f, 0.5f, 0.5f);
itemRenderer.renderStatic(items.get(i), ItemDisplayContext.GROUND, packedLight, packedOverlay, poseStack, buffer, Minecraft.getInstance().level, 0);
poseStack.popPose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
* 请转到 {@link com.hechu.mindustry.Mindustry.ClientModEvents}
*/
@OnlyIn(Dist.CLIENT)
public class RendererRegister {
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.data.PackOutput;
import net.minecraft.data.models.BlockModelGenerators;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.generators.BlockModelProvider;
import net.minecraftforge.common.data.ExistingFileHelper;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/hechu/mindustry/kiwi/BlockEntityModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hechu.mindustry.world.level.block.entity.HealthTestBlockEntity;
import com.hechu.mindustry.world.level.block.entity.MechanicalDrillBlockEntity;
import com.hechu.mindustry.world.level.block.entity.PneumaticDrillBlockEntity;
import com.hechu.mindustry.world.level.block.entity.distribution.ConveyorBlockEntity;
import com.hechu.mindustry.world.level.block.entity.multiblock.KilnBlockEntity;
import com.hechu.mindustry.world.level.block.entity.multiblock.TestTurretMultiblockEntity;
import com.hechu.mindustry.world.level.block.entity.turrets.SpectreTurretBlockEntity;
Expand All @@ -23,4 +24,5 @@ public class BlockEntityModule extends AbstractModule {
public static final KiwiGO<BlockEntityType<PowerNodeBlockEntity>> POWER_NODE_BLOCK_ENTITY = blockEntity(PowerNodeBlockEntity::new, null, BlockModule.POWER_NODE);
public static final KiwiGO<BlockEntityType<SwarmerTurretBlockEntity>> SWARMER_TURRET_BLOCK_ENTITY = blockEntity(SwarmerTurretBlockEntity::new, null, MutilBlockModule.SWARMER_TURRET);
public static final KiwiGO<BlockEntityType<SpectreTurretBlockEntity>> SPECTRE_TURRET_BLOCK_ENTITY = blockEntity(SpectreTurretBlockEntity::new, null, MutilBlockModule.SPECTRE_TURRET);
public static final KiwiGO<BlockEntityType<ConveyorBlockEntity>> CONVEYOR_BLOCK_ENTITY = blockEntity(ConveyorBlockEntity::new, null, BlockModule.CONVEYOR);
}
4 changes: 4 additions & 0 deletions src/main/java/com/hechu/mindustry/kiwi/BlockModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hechu.mindustry.MindustryConstants;
import com.hechu.mindustry.world.level.block.Equipment.PowerNodeBlock;
import com.hechu.mindustry.world.level.block.distribution.ConveyorBlock;
import com.hechu.mindustry.world.level.block.ore.*;
import net.minecraft.world.level.block.Block;
import snownee.kiwi.AbstractModule;
Expand All @@ -18,6 +19,9 @@
@KiwiModule(value = "block")
@KiwiModule.Category(value = MindustryConstants.MOD_ID + ":tab_materials")
public class BlockModule extends AbstractModule {
@KiwiModule.Category(value = MindustryConstants.MOD_ID + ":tab_main")
public static final KiwiGO<Block> CONVEYOR = go(ConveyorBlock::new);

@KiwiModule.Category(value = MindustryConstants.MOD_ID + ":tab_main")
public static final KiwiGO<Block> POWER_NODE = go(PowerNodeBlock::new);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.hechu.mindustry.world.level.block.distribution;

import com.hechu.mindustry.world.level.block.entity.distribution.ConveyorBlockEntity;
import com.hechu.mindustry.world.level.block.entity.turrets.TurretBlockEntityBase;
import com.hechu.mindustry.world.level.block.state.properties.ConveyorShape;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ConveyorBlock extends BaseEntityBlock {
public static final EnumProperty<ConveyorShape> SHAPE = EnumProperty.create("shape", ConveyorShape.class);
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
protected static final VoxelShape FLAT_AABB = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D);
protected static final VoxelShape HALF_BLOCK_AABB = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D);

public ConveyorBlock() {
super(Properties.of().strength(0.7F).sound(SoundType.METAL));
this.registerDefaultState(this.stateDefinition.any().setValue(SHAPE, ConveyorShape.NORTH_SOUTH).setValue(WATERLOGGED, Boolean.FALSE));
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(SHAPE, WATERLOGGED);
super.createBlockStateDefinition(builder);
}

public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return switch (pState.getValue(SHAPE)) {
case DESCENDING_EAST, DESCENDING_NORTH, DESCENDING_SOUTH, DESCENDING_WEST, ASCENDING_EAST, ASCENDING_NORTH, ASCENDING_SOUTH, ASCENDING_WEST ->
HALF_BLOCK_AABB;
default -> FLAT_AABB;
};
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level, @NotNull BlockState state, @NotNull BlockEntityType<T> type) {
return (pLevel1, pPos, pState1, pBlockEntity) -> {
if (pBlockEntity instanceof ConveyorBlockEntity conveyorBlockEntity) {
if (pLevel1.isClientSide) {
conveyorBlockEntity.clientTick();
} else {
conveyorBlockEntity.serverTick();
}
}
};
}

protected void updateState(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock) {
// if (pBlock.defaultBlockState().isSignalSource() && (new RailState(pLevel, pPos, pState)).getConnections() == 3) {
// this.updateDir(pLevel, pPos, pState, false);
// }
}

// protected BlockState updateDir(Level pLevel, BlockPos pPos, BlockState pState, boolean pAlwaysPlace) {
// if (pLevel.isClientSide) {
// return pState;
// } else {
// RailShape railshape = pState.getValue(SHAPE);
// return (new RailState(pLevel, pPos, pState)).place(pLevel.hasNeighborSignal(pPos), pAlwaysPlace, railshape).getState();
// }
// }

public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) {
if (!pLevel.isClientSide && pLevel.getBlockState(pPos).is(this)) {
var shape = pState.getValue(SHAPE);
if (shouldBeRemoved(pPos, pLevel, shape)) {
dropResources(pState, pLevel, pPos);
pLevel.removeBlock(pPos, pIsMoving);
} else {
this.updateState(pState, pLevel, pPos, pBlock);
}
}
}

private static boolean shouldBeRemoved(BlockPos pPos, Level pLevel, ConveyorShape pShape) {
return false;
// if (!canSupportRigidBlock(pLevel, pPos.below())) {
// return true;
// } else {
// switch (pShape) {
// case ASCENDING_EAST:
// return !canSupportRigidBlock(pLevel, pPos.east());
// case ASCENDING_WEST:
// return !canSupportRigidBlock(pLevel, pPos.west());
// case ASCENDING_NORTH:
// return !canSupportRigidBlock(pLevel, pPos.north());
// case ASCENDING_SOUTH:
// return !canSupportRigidBlock(pLevel, pPos.south());
// default:
// return false;
// }
// }
}

public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (!pIsMoving) {
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
// if (pState.getValue(SHAPE).isAscending()) {
// pLevel.updateNeighborsAt(pPos.above(), this);
// }
// if (this.isStraight) {
// pLevel.updateNeighborsAt(pPos, this);
// pLevel.updateNeighborsAt(pPos.below(), this);
// }
}
}

public BlockState getStateForPlacement(BlockPlaceContext pContext) {
FluidState fluidstate = pContext.getLevel().getFluidState(pContext.getClickedPos());
boolean flag = fluidstate.getType() == Fluids.WATER;
BlockState blockstate = super.defaultBlockState();
Direction direction = pContext.getHorizontalDirection();
boolean flag1 = direction == Direction.EAST || direction == Direction.WEST;
return blockstate.setValue(SHAPE, flag1 ? ConveyorShape.EAST_WEST : ConveyorShape.NORTH_SOUTH).setValue(WATERLOGGED, Boolean.valueOf(flag));
}

/**
* Update the provided state given the provided neighbor direction and neighbor state, returning a new state.
* For example, fences make their connections to the passed in state if possible, and wet concrete powder immediately
* returns its solidified counterpart.
* Note that this method should ideally consider only the specific direction passed in.
*/
public @NotNull BlockState updateShape(BlockState pState, @NotNull Direction pDirection,
@NotNull BlockState pNeighborState, @NotNull LevelAccessor pLevel,
@NotNull BlockPos pNeighborPos, @NotNull BlockPos pCurrentPos) {
if (pState.getValue(WATERLOGGED)) {
pLevel.scheduleTick(pCurrentPos, Fluids.WATER, Fluids.WATER.getTickDelay(pLevel));
}

return super.updateShape(pState, pDirection, pNeighborState, pLevel, pCurrentPos, pNeighborPos);
}


@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new ConveyorBlockEntity(pPos, pState);
}

/**
* The type of render function called. MODEL for mixed tesr and static model, MODELBLOCK_ANIMATED for TESR-only,
* LIQUID for vanilla liquids, INVISIBLE to skip all rendering
*
* @param state
* @deprecated call via {@link BlockStateBase#getRenderShape}
* whenever possible. Implementing/overriding is fine.
*/
@Override
public RenderShape getRenderShape(@NotNull BlockState state) {
return RenderShape.MODEL;
}
}
Loading

0 comments on commit 5313ef8

Please sign in to comment.