Skip to content

Commit

Permalink
#feat: 完善机械钻头
Browse files Browse the repository at this point in the history
  • Loading branch information
HeChuQIU committed Jun 26, 2023
1 parent f728184 commit 130fc84
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/hechu/mindustry/block/MechanicalDrill.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.hechu.mindustry.block;

import net.minecraft.core.BlockPos;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
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.material.Material;
import org.jetbrains.annotations.NotNull;
Expand All @@ -27,4 +32,14 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState sta
return RenderShape.ENTITYBLOCK_ANIMATED;
// return RenderShape.MODEL;
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return (level1, blockPos, blockState, t) -> {
if (t instanceof MechanicalDrillBlockEntity) {
((MechanicalDrillBlockEntity) t).tick();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.hechu.mindustry.block;

import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import software.bernie.geckolib.animatable.GeoBlockEntity;
Expand Down Expand Up @@ -35,4 +43,66 @@ public void registerControllers(AnimatableManager.ControllerRegistrar controller
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}

public float progress = 0;
private long tickCounter = 0;

public void tick() {
if (level == null)
return;
tickCounter++;
BlockPos miningBlockPos = getBlockPos().below();
BlockState miningBlockState = level.getBlockState(miningBlockPos);
boolean isMining = miningBlockState.getTags().anyMatch(tag ->
tag.equals(BlockTags.COPPER_ORES) ||
tag.equals(BlockTags.COAL_ORES) ||
tag.equals(BlockTags.SAND));
if (level.isClientSide) {
LocalPlayer localPlayer = Minecraft.getInstance().player;
if (isMining) {
progress += 0.4 / 20;
if (localPlayer != null)
level.destroyBlockProgress(localPlayer.getId(), miningBlockPos, (int) (progress * 10));
if (progress >= 1) {
level.addDestroyBlockEffect(miningBlockPos, miningBlockState);
progress = 0;
}
} else {
if (localPlayer != null)
level.destroyBlockProgress(localPlayer.getId(), miningBlockPos, 10);
}
return;
}
if (isMining) {
progress += 0.4 / 20;
/*PlayerList playerList = level.getServer() == null ? null : level.getServer().getPlayerList();
if (playerList != null && playerList.getPlayerCount() > 0) {
for (ServerPlayer player : playerList.getPlayers()) {
level.destroyBlockProgress(player.getId(), miningBlockPos, (int) Math.ceil(counter * 10));
}
}*/
if (progress >= 1) {
for (ItemStack drop : Block.getDrops(miningBlockState, (ServerLevel) level, miningBlockPos, null)) {
Block.popResource(level, getBlockPos().above(), drop);
}
level.playSound(null, getBlockPos(), SoundEvents.STONE_BREAK, SoundSource.BLOCKS, 1f, 1f);
progress = 0;
}
} else {
progress = 0;
}
/*if (counter++ % 100 == 0) {
BlockState belowBlockState = level.getBlockState(getBlockPos().below());
if (belowBlockState.getTags().anyMatch(tag -> tag.equals(BlockTags.COPPER_ORES))) {
level.removeBlock(getBlockPos().below(), true);
for (ItemStack drop : Block.getDrops(belowBlockState, (ServerLevel) level, getBlockPos().below(), null)) {
Block.popResource(level, getBlockPos().below(), drop);
}
}
}*/
}

public float getProgress() {
return progress;
}
}
21 changes: 5 additions & 16 deletions src/main/resources/assets/mindustry/geo/mechanical_drill.geo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{
"description": {
"identifier": "geometry.unknown",
"texture_width": 256,
"texture_height": 128,
"texture_width": 64,
"texture_height": 32,
"visible_bounds_width": 2,
"visible_bounds_height": 3.5,
"visible_bounds_offset": [0, 1.25, 0]
Expand All @@ -15,23 +15,12 @@
"name": "mechanical_drill",
"pivot": [0, 12.0625, 0],
"cubes": [
{
"origin": [-8, 0, -8],
"size": [16, 16, 16],
"uv": {
"north": {"uv": [64, 64], "uv_size": [64, 64]},
"east": {"uv": [0, 64], "uv_size": [64, 64]},
"south": {"uv": [192, 64], "uv_size": [64, 64]},
"west": {"uv": [128, 64], "uv_size": [64, 64]},
"up": {"uv": [128, 64], "uv_size": [-64, -64]},
"down": {"uv": [192, 64], "uv_size": [-64, -64]}
}
},
{"origin": [-8, 0, -8], "size": [16, 16, 16], "uv": [0, 0]},
{
"origin": [-8, 16.125, -8],
"size": [16, 0, 16],
"uv": {
"up": {"uv": [64, 64], "uv_size": [-64, -64]}
"up": {"uv": [0, 0], "uv_size": [16, 16]}
}
}
]
Expand All @@ -45,7 +34,7 @@
"origin": [-8, 16.0625, -8],
"size": [16, 0, 16],
"uv": {
"up": {"uv": [256, 64], "uv_size": [-64, -64]}
"up": {"uv": [48, 0], "uv_size": [16, 16]}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"credit": "Made with Blockbench",
"parent": "builtin/entity",
"texture_size": [
256,
128
64,
32
],
"display": {
"thirdperson_righthand": {
Expand Down

0 comments on commit 130fc84

Please sign in to comment.