-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
257 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/hechu/mindustry/block/HealthTestBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
src/main/java/com/hechu/mindustry/block/HealthTestBlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/hechu/mindustry/jade/HealthBlockComponentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/hechu/mindustry/utils/capabilities/HealthHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/hechu/mindustry/utils/capabilities/IHealthHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/hechu/mindustry/utils/capabilities/MindustryCapabilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(){}); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/resources/assets/mindustry/blockstates/health_test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"variants": { | ||
"": { | ||
"model": "mindustry:block/health_test" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/resources/assets/mindustry/models/block/health_test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/resources/assets/mindustry/models/item/health_test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"parent": "mindustry:block/health_test" | ||
} |