[Change] SurvivesOn-Tooltips can now show different prefixes

This commit is contained in:
Frank 2023-06-13 16:46:47 +02:00
parent 8013404726
commit e0622a1c11
4 changed files with 35 additions and 7 deletions

View file

@ -0,0 +1,10 @@
package org.betterx.bclib.interfaces;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Internal
public interface SurvivesOn {
boolean isSurvivable(BlockState state);
}

View file

@ -15,9 +15,13 @@ import com.google.common.collect.Lists;
import java.util.List;
public interface SurvivesOnSpecialGround {
public interface SurvivesOnSpecialGround extends SurvivesOn {
String getSurvivableBlocksString();
default String prefixComponent() {
return "tooltip.bclib.place_on";
}
@Environment(EnvType.CLIENT)
static List<String> splitLines(String input) {
final int MAX_LEN = 45;
@ -38,14 +42,15 @@ public interface SurvivesOnSpecialGround {
}
@Environment(EnvType.CLIENT)
static void appendHoverText(List<Component> list, String description) {
static void appendHoverText(SurvivesOnSpecialGround surv, List<Component> list) {
if (!Configs.CLIENT_CONFIG.survivesOnHint()) return;
final int MAX_LINES = 7;
final String description = surv.getSurvivableBlocksString();
List<String> lines = splitLines(description);
if (lines.size() == 1) {
list.add(Component.translatable("tooltip.bclib.place_on", lines.get(0)).withStyle(ChatFormatting.GREEN));
list.add(Component.translatable(surv.prefixComponent(), lines.get(0)).withStyle(ChatFormatting.GREEN));
} else if (lines.size() > 1) {
list.add(Component.translatable("tooltip.bclib.place_on", "").withStyle(ChatFormatting.GREEN));
list.add(Component.translatable(surv.prefixComponent(), "").withStyle(ChatFormatting.GREEN));
for (int i = 0; i < Math.min(lines.size(), MAX_LINES); i++) {
String line = lines.get(i);
if (i == MAX_LINES - 1 && i < lines.size() - 1) line += " ...";
@ -54,8 +59,6 @@ public interface SurvivesOnSpecialGround {
}
}
boolean isSurvivable(BlockState state);
default boolean canSurviveOnTop(LevelReader world, BlockPos pos) {
return isSurvivable(world.getBlockState(pos.below()));
}

View file

@ -0,0 +1,15 @@
package org.betterx.bclib.interfaces;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import java.util.List;
public interface SurvivesOnWater extends SurvivesOnBlocks {
List<Block> BLOCKS = List.of(Blocks.WATER);
@Override
default List<Block> getSurvivableBlocks() {
return BLOCKS;
}
}

View file

@ -27,7 +27,7 @@ public class BlockMixin {
CallbackInfo ci
) {
if (this instanceof SurvivesOnSpecialGround surv) {
SurvivesOnSpecialGround.appendHoverText(list, surv.getSurvivableBlocksString());
SurvivesOnSpecialGround.appendHoverText(surv, list);
}
}
}