Skip to content

Commit

Permalink
Jade将判断物体是否具有生命值能力以显示生命值
Browse files Browse the repository at this point in the history
  • Loading branch information
HeChuQIU committed Sep 20, 2023
1 parent 609b0fa commit a9d1118
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.hechu.mindustry.jade;

import com.hechu.mindustry.world.level.block.entity.HealthTestBlockEntity;
import com.hechu.mindustry.utils.capabilities.MindustryCapabilities;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.level.block.entity.BlockEntity;
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;
import snownee.jade.api.ui.BoxStyle;
import snownee.jade.api.ui.IBoxStyle;
import snownee.jade.api.ui.IElement;
import snownee.jade.api.ui.IProgressStyle;
import snownee.jade.impl.ui.ProgressElement;
import snownee.jade.impl.ui.ProgressStyle;

Expand All @@ -22,28 +19,35 @@ public enum HealthBlockComponentProvider implements IBlockComponentProvider, ISe

@Override
public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig config) {
float health = accessor.getServerData().getFloat("health");
float maxHealth = accessor.getServerData().getFloat("maxHealth");
BlockEntity blockEntity = accessor.getBlockEntity();

ProgressStyle progressStyle = new ProgressStyle();
progressStyle.color = 0xff0000|0xff<<24;
progressStyle.color2 = 0x000000|0x88<<24;
progressStyle.glowText = true;
if (blockEntity.getCapability(MindustryCapabilities.HEALTH_HANDLER).isPresent()) {
float health = accessor.getServerData().getFloat("health");
float maxHealth = accessor.getServerData().getFloat("maxHealth");

BoxStyle boxStyle = new BoxStyle();
ProgressStyle progressStyle = new ProgressStyle();
progressStyle.color = 0xff0000|0xff<<24;
progressStyle.color2 = 0x000000|0x88<<24;
progressStyle.glowText = true;

ProgressElement progressElement = new ProgressElement(health / maxHealth,
Component.translatable("mindustry.block_health", health, maxHealth),
progressStyle, boxStyle, true);
BoxStyle boxStyle = new BoxStyle();

ProgressElement progressElement = new ProgressElement(health / maxHealth,
Component.translatable("mindustry.block_health", health, maxHealth),
progressStyle, boxStyle, true);
// progressElement.size(new Vec2(100f,progressElement.getSize().y));
tooltip.add(progressElement);
tooltip.add(progressElement);
}
}

@Override
public void appendServerData(CompoundTag data, BlockAccessor accessor) {
HealthTestBlockEntity healthBlockEntity = (HealthTestBlockEntity) accessor.getBlockEntity();
data.putFloat("health", healthBlockEntity.getHealth());
data.putFloat("maxHealth", healthBlockEntity.getMaxHealth());
BlockEntity blockEntity = accessor.getBlockEntity();

blockEntity.getCapability(MindustryCapabilities.HEALTH_HANDLER).ifPresent(healthHandler -> {
data.putFloat("health", healthHandler.getHealth());
data.putFloat("maxHealth", healthHandler.getMaxHealth());
});
}

@Override
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/hechu/mindustry/jade/MindustryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.hechu.mindustry.Mindustry;
import com.hechu.mindustry.world.level.block.DrillBlock;
import com.hechu.mindustry.world.level.block.entity.DrillBlockEntity;
import com.hechu.mindustry.world.level.block.HealthTestBlock;
import com.hechu.mindustry.world.level.block.entity.HealthTestBlockEntity;
import net.minecraft.resources.ResourceLocation;
import snownee.jade.api.*;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import snownee.jade.api.IWailaClientRegistration;
import snownee.jade.api.IWailaCommonRegistration;
import snownee.jade.api.IWailaPlugin;
import snownee.jade.api.WailaPlugin;

@WailaPlugin
public class MindustryPlugin implements IWailaPlugin {
Expand All @@ -17,13 +20,13 @@ public class MindustryPlugin implements IWailaPlugin {
@Override
public void register(IWailaCommonRegistration registration) {
registration.registerBlockDataProvider(DrillComponentProvider.INSTANCE, DrillBlockEntity.class);
registration.registerBlockDataProvider(HealthBlockComponentProvider.INSTANCE, HealthTestBlockEntity.class);
registration.registerBlockDataProvider(HealthBlockComponentProvider.INSTANCE, BlockEntity.class);
}

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

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.hechu.mindustry.world.level.block.entity.turrets;

import com.hechu.mindustry.utils.capabilities.HealthHandler;
import com.hechu.mindustry.utils.capabilities.IHealthHandler;
import com.hechu.mindustry.utils.capabilities.MindustryCapabilities;
import com.hechu.mindustry.world.level.block.entity.BlockEntityRegister;
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.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.monster.Enemy;
import net.minecraft.world.entity.projectile.Arrow;
Expand All @@ -18,6 +21,8 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -33,6 +38,16 @@ public TurretBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(BlockEntityRegister.TURRET_BLOCK_ENTITY.get(), pPos, pBlockState);
}

private final LazyOptional<IHealthHandler> healthHandler = LazyOptional.of(HealthHandler::new);

@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);
}

public LivingEntity target = null;
public double targetPosX;
public double targetPosY;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hechu.mindustry.world.level.block.turrets;

import com.hechu.mindustry.utils.capabilities.MindustryCapabilities;
import com.hechu.mindustry.world.level.block.entity.BlockEntityRegister;
import com.hechu.mindustry.world.level.block.entity.turrets.TurretBlockEntity;
import net.minecraft.core.BlockPos;
Expand All @@ -24,7 +25,12 @@ public TurretBlock() {
@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) {
return new TurretBlockEntity(pPos, pState);
TurretBlockEntity turretBlockEntity = new TurretBlockEntity(pPos, pState);
turretBlockEntity.getCapability(MindustryCapabilities.HEALTH_HANDLER, null).ifPresent(healthHandler -> {
healthHandler.setMaxHealth(100);
healthHandler.setHealth(100);
});
return turretBlockEntity;
}

@Nullable
Expand Down

0 comments on commit a9d1118

Please sign in to comment.