Skip to content

Commit

Permalink
初版实现
Browse files Browse the repository at this point in the history
  • Loading branch information
HeChuQIU committed Sep 13, 2023
1 parent 4d00f35 commit e193cd7
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class BlockEntityRegister {
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITIES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, Mindustry.MODID);
public static final RegistryObject<BlockEntityType<MechanicalDrillBlockEntity>> MECHANICAL_DRILL_BLOCK_ENTITY = BLOCK_ENTITIES.register(MechanicalDrillBlockEntity.NAME, () -> BlockEntityType.Builder.of(MechanicalDrillBlockEntity::new, BlockRegister.MECHANICAL_DRILL.get()).build(null));
public static final RegistryObject<BlockEntityType<PneumaticDrillBlockEntity>> PNEUMATIC_DRILL_BLOCK_ENTITY = BLOCK_ENTITIES.register(PneumaticDrillBlockEntity.NAME, () -> BlockEntityType.Builder.of(PneumaticDrillBlockEntity::new, BlockRegister.PNEUMATIC_DRILL.get()).build(null));
public static final RegistryObject<BlockEntityType<HealthTestBlockEntity>> HEALTH_TEST_BLOCK_ENTITY = BLOCK_ENTITIES.register(HealthTestBlockEntity.NAME, () -> BlockEntityType.Builder.of(HealthTestBlockEntity::new, BlockRegister.HEALTH_TEST.get()).build(null));
}
1 change: 1 addition & 0 deletions src/main/java/com/hechu/mindustry/block/BlockRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class BlockRegister {
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Mindustry.MODID);
public static final RegistryObject<Block> MECHANICAL_DRILL = BLOCKS.register(MechanicalDrillBlock.NAME, MechanicalDrillBlock::new);
public static final RegistryObject<Block> PNEUMATIC_DRILL = BLOCKS.register(PneumaticDrillBlock.NAME, PneumaticDrillBlock::new);
public static final RegistryObject<Block> HEALTH_TEST = BLOCKS.register(HealthTestBlock.NAME, HealthTestBlock::new);
}
54 changes: 54 additions & 0 deletions src/main/java/com/hechu/mindustry/block/HealthTestBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hechu.mindustry.block;

import com.hechu.mindustry.utils.capabilities.MindustryCapabilities;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class HealthTestBlock extends BaseEntityBlock {
public static final String NAME = "health_test";

public HealthTestBlock() {
super(Properties.of().destroyTime(3).strength(3.0F, 3.0F));
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) {
HealthTestBlockEntity healthTestBlockEntity = new HealthTestBlockEntity(pPos, pState);
healthTestBlockEntity.getCapability(MindustryCapabilities.HEALTH_HANDLER, null).ifPresent(healthHandler -> {
healthHandler.setHealth(500);
});
return healthTestBlockEntity;
}

@Override
public void onProjectileHit(Level pLevel, @NotNull BlockState pState, BlockHitResult pHit, @NotNull Projectile pProjectile) {
HealthTestBlockEntity blockEntity = (HealthTestBlockEntity) pLevel.getBlockEntity(pHit.getBlockPos());
if (blockEntity == null)
return;
blockEntity.getCapability(MindustryCapabilities.HEALTH_HANDLER, null).ifPresent(healthHandler -> {
healthHandler.setHealth(healthHandler.getHealth() - 1);
if (healthHandler.getHealth() <= 0) {
pLevel.destroyBlock(pHit.getBlockPos(), true, pProjectile);
}
});
}

@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}
}
81 changes: 81 additions & 0 deletions src/main/java/com/hechu/mindustry/block/HealthTestBlockEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.hechu.mindustry.block;

import com.hechu.mindustry.utils.capabilities.HealthHandler;
import com.hechu.mindustry.utils.capabilities.IHealthHandler;
import com.hechu.mindustry.utils.capabilities.MindustryCapabilities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class HealthTestBlockEntity extends BlockEntity {
public static final String NAME = "health_test";
private final LazyOptional<IHealthHandler> healthHandler = LazyOptional.of(HealthHandler::new);

public HealthTestBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(BlockEntityRegister.HEALTH_TEST_BLOCK_ENTITY.get(), pPos, pBlockState);
}

@Override
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
if (cap == MindustryCapabilities.HEALTH_HANDLER) {
return healthHandler.cast();
}
return super.getCapability(cap, side);
}

@Override
public void load(CompoundTag tag) {
setHealth(tag.getInt("health"));
super.load(tag);
}

@Override
protected void saveAdditional(CompoundTag tag) {
tag.putInt("health", getHealth());
super.saveAdditional(tag);
}

@Nullable
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}

@Override
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
handleUpdateTag(pkt.getTag());
}

@Override
public @NotNull CompoundTag getUpdateTag() {
CompoundTag tag = super.getUpdateTag();
tag.putInt("health", health);
return tag;
}

@Override
public void handleUpdateTag(CompoundTag tag) {
health = tag.getInt("health");
}

int health = 0;

public int getHealth() {
return healthHandler.orElseThrow(NullPointerException::new).getHealth();
}

public void setHealth(int health) {
this.health = health;
healthHandler.ifPresent(h -> h.setHealth(health));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class CreativeModeTabRegister {
.displayItems((featureFlags, output) -> {
output.accept(ItemRegister.MECHANICAL_DRILL_ITEM.get());
output.accept(ItemRegister.PNEUMATIC_DRILL_ITEM.get());
output.accept(ItemRegister.HEALTH_TEST_ITEM.get());
}).build()
);
}
11 changes: 11 additions & 0 deletions src/main/java/com/hechu/mindustry/item/HealthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hechu.mindustry.item;

import net.minecraft.world.item.BlockItem;
import net.minecraft.world.level.block.Block;

public class HealthTest extends BlockItem {
public static final String NAME = "health_test";
public HealthTest(Block pBlock, Properties pProperties) {
super(pBlock, pProperties);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/hechu/mindustry/item/ItemRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class ItemRegister {
() -> new MechanicalDrill(BlockRegister.MECHANICAL_DRILL.get(), new Item.Properties()));
public static final RegistryObject<Item> PNEUMATIC_DRILL_ITEM = ITEMS.register(com.hechu.mindustry.item.PneumaticDrill.NAME,
() -> new PneumaticDrill(BlockRegister.PNEUMATIC_DRILL.get(), new Item.Properties()));
public static final RegistryObject<Item> HEALTH_TEST_ITEM = ITEMS.register(com.hechu.mindustry.item.HealthTest.NAME,
() -> new HealthTest(BlockRegister.HEALTH_TEST.get(), new Item.Properties()));
}
21 changes: 12 additions & 9 deletions src/main/java/com/hechu/mindustry/jade/DrillComponentProvider.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.hechu.mindustry.jade;

import com.hechu.mindustry.block.DrillBlockEntity;
import com.hechu.mindustry.block.HealthTestBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec2;
import snownee.jade.api.BlockAccessor;
Expand All @@ -20,27 +22,28 @@ public enum DrillComponentProvider implements IBlockComponentProvider, IServerDa

@Override
public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig config) {
if (accessor.getBlockEntity() instanceof DrillBlockEntity) {
BlockState miningBlockState = ((DrillBlockEntity) accessor.getBlockEntity()).getMiningBlockState();
BlockEntity blockEntity = accessor.getBlockEntity();
if (blockEntity instanceof DrillBlockEntity drillBlockEntity) {
BlockState miningBlockState = drillBlockEntity.getMiningBlockState();
if (miningBlockState == null)
return;
IElementHelper elements = tooltip.getElementHelper();
IElement icon = elements.item(new ItemStack(miningBlockState.getBlock().asItem()), 0.8f).size(new Vec2(16, 16)).translate(new Vec2(0, -2));
tooltip.add(icon);
tooltip.append(Component.translatable("mindustry.drill_progress", (int) (((DrillBlockEntity) accessor.getBlockEntity()).getProgress() * 100)));
tooltip.add(Component.translatable("mindustry.drill_speed", String.format("%.2f", ((DrillBlockEntity) accessor.getBlockEntity()).getMiningSpeed())));
tooltip.append(Component.translatable("mindustry.drill_progress", (int) (drillBlockEntity.getProgress() * 100)));
tooltip.add(Component.translatable("mindustry.drill_speed", String.format("%.2f", drillBlockEntity.getMiningSpeed())));
}
}

@Override
public ResourceLocation getUid() {
return MindustryPlugin.Drill;
}

@Override
public void appendServerData(CompoundTag data, BlockAccessor accessor) {
DrillBlockEntity drill= (DrillBlockEntity) accessor.getBlockEntity();
data.putFloat("progress", drill.getProgress());
data.putFloat("miningSpeed", drill.getMiningSpeed());
}

@Override
public ResourceLocation getUid() {
return MindustryPlugin.Drill;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hechu.mindustry.jade;

import com.hechu.mindustry.block.DrillBlockEntity;
import com.hechu.mindustry.block.HealthTestBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import snownee.jade.api.BlockAccessor;
import snownee.jade.api.IBlockComponentProvider;
import snownee.jade.api.IServerDataProvider;
import snownee.jade.api.ITooltip;
import snownee.jade.api.config.IPluginConfig;

public enum HealthBlockComponentProvider implements IBlockComponentProvider, IServerDataProvider<BlockAccessor> {
INSTANCE;

@Override
public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig config) {
tooltip.append(Component.translatable("mindustry.block_health", accessor.getServerData().getInt("health")));
}

@Override
public void appendServerData(CompoundTag data, BlockAccessor accessor) {
HealthTestBlockEntity healthBlockEntity = (HealthTestBlockEntity) accessor.getBlockEntity();
data.putInt("health", healthBlockEntity.getHealth());
}

@Override
public ResourceLocation getUid() {
return MindustryPlugin.HealthBlock;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/hechu/mindustry/jade/MindustryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
import com.hechu.mindustry.Mindustry;
import com.hechu.mindustry.block.DrillBlock;
import com.hechu.mindustry.block.DrillBlockEntity;
import com.hechu.mindustry.block.HealthTestBlock;
import com.hechu.mindustry.block.HealthTestBlockEntity;
import net.minecraft.resources.ResourceLocation;
import snownee.jade.api.*;

@WailaPlugin
public class MindustryPlugin implements IWailaPlugin {

public static final ResourceLocation Drill = new ResourceLocation(Mindustry.MODID, "drill");
public static final ResourceLocation HealthBlock = new ResourceLocation(Mindustry.MODID, "health_block");

@Override
public void register(IWailaCommonRegistration registration) {
registration.registerBlockDataProvider(DrillComponentProvider.INSTANCE, DrillBlockEntity.class);
registration.registerBlockDataProvider(HealthBlockComponentProvider.INSTANCE, HealthTestBlockEntity.class);
}

@Override
public void registerClient(IWailaClientRegistration registration) {
registration.registerBlockComponent(DrillComponentProvider.INSTANCE, DrillBlock.class);
registration.registerBlockComponent(HealthBlockComponentProvider.INSTANCE, HealthTestBlock.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hechu.mindustry.utils.capabilities;

public class HealthHandler implements IHealthHandler {
private int health = -1;
@Override
public int getHealth() {
return health;
}

@Override
public void setHealth(int health) {
this.health = health;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hechu.mindustry.utils.capabilities;

import net.minecraftforge.common.capabilities.AutoRegisterCapability;

@AutoRegisterCapability
public interface IHealthHandler {
int getHealth();
void setHealth(int health);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hechu.mindustry.utils.capabilities;

import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.energy.IEnergyStorage;

public class MindustryCapabilities {
public static final Capability<IHealthHandler> HEALTH_HANDLER = CapabilityManager.get(new CapabilityToken<>(){});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "mindustry:block/health_test"
}
}
}
6 changes: 5 additions & 1 deletion src/main/resources/assets/mindustry/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"item.mindustry.mechanical_drill": "机械钻头",
"block.mindustry.pneumatic_drill": "气动钻头",
"item.mindustry.pneumatic_drill": "气动钻头",
"block.mindustry.health_test": "生命测试",
"item.mindustry.health_test": "生命测试",
"config.jade.plugin_mindustry.drill": "不知道干嘛的",
"mindustry.drill_progress": "%d%%已挖掘",
"mindustry.drill_speed":"挖掘速度:%d/秒"
"mindustry.drill_speed": "挖掘速度:%d/秒",
"config.jade.plugin_mindustry.health_block": "不知道干嘛的",
"mindustry.block_health": "生命值:%d"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parent": "minecraft:block/cube_column",
"textures": {
"end": "minecraft:block/target_top",
"side": "minecraft:block/target_side"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "mindustry:block/health_test"
}

0 comments on commit e193cd7

Please sign in to comment.