Merge branch 'main' into subbuild

This commit is contained in:
Frank 2021-07-22 23:01:43 +02:00
commit bd77ea46b9
6 changed files with 133 additions and 184 deletions

View file

@ -0,0 +1,62 @@
package ru.bclib.api;
import com.google.common.collect.Lists;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import ru.bclib.blocks.BaseChestBlock;
import ru.bclib.blocks.BaseSignBlock;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.client.render.BaseChestBlockEntityRenderer;
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
import ru.bclib.interfaces.PostInitable;
import ru.bclib.interfaces.RenderLayerProvider;
import java.util.List;
import java.util.function.Consumer;
public class PostInitAPI {
private static List<Consumer<Void>> postInitFunctions = Lists.newArrayList();
public static void register(Consumer<Void> function) {
postInitFunctions.add(function);
}
public static void postInit(boolean isClient) {
if (postInitFunctions == null) {
return;
}
postInitFunctions.forEach(function -> function.accept(null));
Registry.BLOCK.forEach(block -> {
processBlockCommon(block);
if (isClient) {
processBlockClient(block);
}
});
postInitFunctions = null;
}
@Environment(EnvType.CLIENT)
private static void processBlockClient(Block block) {
if (block instanceof RenderLayerProvider) {
BCLRenderLayer layer = ((RenderLayerProvider) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.cutout());
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, RenderType.translucent());
}
if (block instanceof BaseChestBlock) {
BaseChestBlockEntityRenderer.registerRenderLayer(block);
}
else if (block instanceof BaseSignBlock) {
BaseSignBlockEntityRenderer.registerRenderLayer(block);
}
}
private static void processBlockCommon(Block block) {
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
}
}

View file

@ -5,27 +5,12 @@ import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.LadderBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.bclib.client.models.BasePatterns;
import ru.bclib.client.models.ModelsHelper;
@ -33,108 +18,17 @@ import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.interfaces.RenderLayerProvider;
import ru.bclib.util.BlocksHelper;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@SuppressWarnings("deprecation")
public class BaseLadderBlock extends BaseBlockNotFull implements RenderLayerProvider, BlockModelProvider {
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
protected static final VoxelShape EAST_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 3.0D, 16.0D, 16.0D);
protected static final VoxelShape WEST_SHAPE = Block.box(13.0D, 0.0D, 0.0D, 16.0D, 16.0D, 16.0D);
protected static final VoxelShape SOUTH_SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 16.0D, 3.0D);
protected static final VoxelShape NORTH_SHAPE = Block.box(0.0D, 0.0D, 13.0D, 16.0D, 16.0D, 16.0D);
public class BaseLadderBlock extends LadderBlock implements RenderLayerProvider, BlockModelProvider {
public BaseLadderBlock(Block block) {
super(FabricBlockSettings.copyOf(block).noOcclusion());
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateManager) {
stateManager.add(FACING);
stateManager.add(WATERLOGGED);
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return switch (state.getValue(FACING)) {
case SOUTH -> SOUTH_SHAPE;
case WEST -> WEST_SHAPE;
case EAST -> EAST_SHAPE;
default -> NORTH_SHAPE;
};
}
private boolean canPlaceOn(BlockGetter world, BlockPos pos, Direction side) {
BlockState blockState = world.getBlockState(pos);
return !blockState.isSignalSource() && blockState.isFaceSturdy(world, pos, side);
}
@Override
public boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) {
Direction direction = state.getValue(FACING);
return canPlaceOn(world, pos.relative(direction.getOpposite()), direction);
}
@Override
public BlockState updateShape(BlockState state, Direction facing, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
if (facing.getOpposite() == state.getValue(FACING) && !state.canSurvive(world, pos)) {
return Blocks.AIR.defaultBlockState();
}
else {
if (state.getValue(WATERLOGGED)) {
world.getLiquidTicks().scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
}
return super.updateShape(state, facing, neighborState, world, pos, neighborPos);
}
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext ctx) {
BlockState blockState;
if (!ctx.replacingClickedOnBlock()) {
blockState = ctx.getLevel().getBlockState(ctx.getClickedPos().relative(ctx.getClickedFace().getOpposite()));
if (blockState.getBlock() == this && blockState.getValue(FACING) == ctx.getClickedFace()) {
return null;
}
}
blockState = defaultBlockState();
LevelReader worldView = ctx.getLevel();
BlockPos blockPos = ctx.getClickedPos();
FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos());
Direction[] directions = ctx.getNearestLookingDirections();
for (Direction direction : directions) {
if (direction.getAxis().isHorizontal()) {
blockState = blockState.setValue(FACING, direction.getOpposite());
if (blockState.canSurvive(worldView, blockPos)) {
return blockState.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
}
}
return null;
}
@Override
public BlockState rotate(BlockState state, Rotation rotation) {
return BlocksHelper.rotateHorizontal(state, rotation, FACING);
}
@Override
public BlockState mirror(BlockState state, Mirror mirror) {
return BlocksHelper.mirrorHorizontal(state, mirror, FACING);
}
@Override
public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT;
@ -160,4 +54,10 @@ public class BaseLadderBlock extends BaseBlockNotFull implements RenderLayerProv
registerBlockModel(stateId, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true);
}
@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return Collections.singletonList(new ItemStack(this));
}
}

View file

@ -5,6 +5,7 @@ import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.Registry;
import ru.bclib.api.ModIntegrationAPI;
import ru.bclib.api.PostInitAPI;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.PostInitable;
import ru.bclib.interfaces.RenderLayerProvider;
@ -15,23 +16,6 @@ public class BCLibClient implements ClientModInitializer {
public void onInitializeClient() {
ModIntegrationAPI.registerAll();
BaseBlockEntityRenders.register();
registerRenderLayers();
Registry.BLOCK.forEach(block -> {
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
});
}
private void registerRenderLayers() {
RenderType cutout = RenderType.cutout();
RenderType translucent = RenderType.translucent();
Registry.BLOCK.forEach(block -> {
if (block instanceof RenderLayerProvider) {
BCLRenderLayer layer = ((RenderLayerProvider) block).getRenderLayer();
if (layer == BCLRenderLayer.CUTOUT) BlockRenderLayerMap.INSTANCE.putBlock(block, cutout);
else if (layer == BCLRenderLayer.TRANSLUCENT) BlockRenderLayerMap.INSTANCE.putBlock(block, translucent);
}
});
PostInitAPI.postInit(true);
}
}

View file

@ -0,0 +1,5 @@
package ru.bclib.interfaces;
public interface TileEntityRenderProvider {
}

View file

@ -3,16 +3,13 @@ package ru.bclib.server;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.minecraft.core.Registry;
import ru.bclib.api.ModIntegrationAPI;
import ru.bclib.api.PostInitAPI;
import ru.bclib.interfaces.PostInitable;
public class BCLibServer implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
ModIntegrationAPI.registerAll();
Registry.BLOCK.forEach(block -> {
if (block instanceof PostInitable) {
((PostInitable) block).postInit();
}
});
PostInitAPI.postInit(false);
}
}

View file

@ -1,6 +1,6 @@
package ru.bclib.util;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
@ -9,28 +9,34 @@ import net.minecraft.resources.ResourceLocation;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class TranslationHelper {
public static void printMissingNames(String modID) {
List<String> missingNamesEn = Lists.newArrayList();
List<String> missingNamesRu = Lists.newArrayList();
/**
* Print English translation file lines. Translation is "auto-beautified" text (example "strange_thing" -> "Strange Thing").
* @param modID {@link String} mod ID string.
*/
public static void printMissingEnNames(String modID) {
printMissingNames(modID, "en_us");
}
/**
* Prints translation file lines for specified language.
* @param modID {@link String} mod ID string;
* @param languageCode {@link String} language code (example "en_us", "ru_ru").
*/
public static void printMissingNames(String modID, String languageCode) {
Set<String> missingNames = Sets.newHashSet();
Gson gson = new Gson();
InputStream streamEn = TranslationHelper.class.getResourceAsStream("/assets/" + modID + "/lang/en_us.json");
InputStream streamRu = TranslationHelper.class.getResourceAsStream("/assets/" + modID + "/lang/ru_ru.json");
JsonObject translationEn = gson.fromJson(new InputStreamReader(streamEn), JsonObject.class);
JsonObject translationRu = gson.fromJson(new InputStreamReader(streamRu), JsonObject.class);
InputStream inputStream = TranslationHelper.class.getResourceAsStream("/assets/" + modID + "/lang/" + languageCode + ".json");
JsonObject translation = gson.fromJson(new InputStreamReader(inputStream), JsonObject.class);
Registry.BLOCK.forEach(block -> {
if (Registry.BLOCK.getKey(block).getNamespace().equals(modID)) {
String name = block.getName().getString();
if (!translationEn.has(name)) {
missingNamesEn.add(name);
}
if (!translationRu.has(name)) {
missingNamesRu.add(name);
if (!translation.has(name)) {
missingNames.add(name);
}
}
});
@ -38,11 +44,8 @@ public class TranslationHelper {
Registry.ITEM.forEach(item -> {
if (Registry.ITEM.getKey(item).getNamespace().equals(modID)) {
String name = item.getDescription().getString();
if (!translationEn.has(name)) {
missingNamesEn.add(name);
}
if (!translationRu.has(name)) {
missingNamesRu.add(name);
if (!translation.has(name)) {
missingNames.add(name);
}
}
});
@ -51,11 +54,8 @@ public class TranslationHelper {
ResourceLocation id = BuiltinRegistries.BIOME.getKey(biome);
if (id.getNamespace().equals(modID)) {
String name = "biome." + modID + "." + id.getPath();
if (!translationEn.has(name)) {
missingNamesEn.add(name);
}
if (!translationRu.has(name)) {
missingNamesRu.add(name);
if (!translation.has(name)) {
missingNames.add(name);
}
}
});
@ -64,44 +64,45 @@ public class TranslationHelper {
ResourceLocation id = Registry.ENTITY_TYPE.getKey(entity);
if (id.getNamespace().equals(modID)) {
String name = "entity." + modID + "." + id.getPath();
if (!translationEn.has(name)) {
missingNamesEn.add(name);
}
if (!translationRu.has(name)) {
missingNamesRu.add(name);
if (!translation.has(name)) {
missingNames.add(name);
}
}
});
if (!missingNamesEn.isEmpty() || !missingNamesRu.isEmpty()) {
if (!missingNames.isEmpty()) {
System.out.println("========================================");
System.out.println(" MISSING NAMES LIST");
if (!missingNamesEn.isEmpty()) {
Collections.sort(missingNamesEn);
System.out.println("========================================");
System.out.println(" ENGLISH");
System.out.println("========================================");
missingNamesEn.forEach((name) -> {
System.out.println(" \"" + name + "\": \"" + fastTranslateEn(name) + "\",");
});
}
if (!missingNamesRu.isEmpty()) {
Collections.sort(missingNamesRu);
System.out.println("========================================");
System.out.println(" RUSSIAN");
System.out.println("========================================");
missingNamesRu.forEach((name) -> {
System.out.println(" \"" + name + "\": \"\",");
});
if (!missingNames.isEmpty()) {
if (languageCode.equals("en_us")) {
System.out.println("========================================");
System.out.println(" AUTO ENGLISH BEAUTIFICATION");
System.out.println("========================================");
missingNames.stream().sorted().forEach(name -> {
System.out.println(" \"" + name + "\": \"" + fastTranslateEn(name) + "\",");
});
}
else {
System.out.println("========================================");
System.out.println(" TEMPLATE: [" + languageCode + "]");
System.out.println("========================================");
missingNames.stream().sorted().forEach(name -> {
System.out.println(" \"" + name + "\": \"\",");
});
}
}
System.out.println("========================================");
}
}
/**
* Simple fast text beautification (example "strange_thing" -> "Strange Thing").
* @param text {@link String} to process;
* @return {@link String} result.
*/
public static String fastTranslateEn(String text) {
String[] words = text.substring(text.lastIndexOf('.') + 1).split("_");
StringBuilder builder = new StringBuilder();