Change models loading (yes, WIP)

This commit is contained in:
Aleksey 2021-05-18 17:59:01 +03:00
parent 9b67bc5719
commit 52fc329a0a
8 changed files with 98 additions and 17 deletions

View file

@ -7,6 +7,7 @@ import com.google.common.collect.Maps;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags; import net.fabricmc.fabric.api.tool.attribute.v1.FabricToolTags;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -18,6 +19,7 @@ import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.betterend.blocks.basis.EndLanternBlock; import ru.betterend.blocks.basis.EndLanternBlock;
import ru.betterend.client.render.ERenderLayer; import ru.betterend.client.render.ERenderLayer;
import ru.betterend.interfaces.IRenderTypeable; import ru.betterend.interfaces.IRenderTypeable;
@ -66,6 +68,17 @@ public class BulbVineLanternBlock extends EndLanternBlock implements IRenderType
return Patterns.createJson(Patterns.BLOCK_BULB_LANTERN_FLOOR, map); return Patterns.createJson(Patterns.BLOCK_BULB_LANTERN_FLOOR, map);
} }
} }
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
Map<String, String> textures = Maps.newHashMap();
textures.put("%glow%", getGlowTexture());
textures.put("%metal%", getMetalTexture(resourceLocation));
Optional<String> pattern = blockState.getValue(IS_FLOOR) ?
Patterns.createJson(Patterns.BLOCK_BULB_LANTERN_FLOOR, textures) :
Patterns.createJson(Patterns.BLOCK_BULB_LANTERN_CEIL, textures);
return pattern.map(BlockModel::fromString).orElse(null);
}
protected String getMetalTexture(ResourceLocation blockId) { protected String getMetalTexture(ResourceLocation blockId) {
String name = blockId.getPath(); String name = blockId.getPath();

View file

@ -14,16 +14,12 @@ public class BulbVineLanternColoredBlock extends BulbVineLanternBlock implements
@Override @Override
public BlockColor getProvider() { public BlockColor getProvider() {
return (state, world, pos, tintIndex) -> { return (state, world, pos, tintIndex) -> getColor();
return getColor();
};
} }
@Override @Override
public ItemColor getItemProvider() { public ItemColor getItemProvider() {
return (stack, tintIndex) -> { return (stack, tintIndex) -> getColor();
return getColor();
};
} }
private int getColor() { private int getColor() {

View file

@ -15,6 +15,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
import ru.betterend.registry.EndBlockEntities; import ru.betterend.registry.EndBlockEntities;
@ -59,7 +60,6 @@ public class EndChestBlock extends ChestBlock implements BlockModelProvider {
@Override @Override
public BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) { public BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent); ResourceLocation parentId = Registry.BLOCK.getKey(parent);
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath()); return ModelsHelper.createBlockEmpty(parentId);
return pattern.map(BlockModel::fromString).orElse(null);
} }
} }

View file

@ -1,8 +1,11 @@
package ru.betterend.blocks.basis; package ru.betterend.blocks.basis;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelAccessor;
@ -19,6 +22,9 @@ import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.material.Fluids;
import ru.betterend.blocks.BlockProperties; import ru.betterend.blocks.BlockProperties;
import ru.betterend.client.models.ModelsHelper;
import java.util.Map;
public class EndLanternBlock extends BlockBaseNotFull implements SimpleWaterloggedBlock, LiquidBlockContainer { public class EndLanternBlock extends BlockBaseNotFull implements SimpleWaterloggedBlock, LiquidBlockContainer {
public static final BooleanProperty IS_FLOOR = BlockProperties.IS_FLOOR; public static final BooleanProperty IS_FLOOR = BlockProperties.IS_FLOOR;
@ -116,4 +122,13 @@ public class EndLanternBlock extends BlockBaseNotFull implements SimpleWaterlogg
public FluidState getFluidState(BlockState state) { public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState(); return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState();
} }
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
String floor = blockState.getValue(IS_FLOOR) ? "_floor" : "";
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + floor);
registerBlockModel(resourceLocation, modelId, blockState, modelCache);
return ModelsHelper.createBlockSimple(modelId);
}
} }

View file

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
@ -38,7 +39,9 @@ import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.betterend.blocks.entities.ESignBlockEntity; import ru.betterend.blocks.entities.ESignBlockEntity;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.interfaces.ISpetialItem; import ru.betterend.interfaces.ISpetialItem;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
@ -79,14 +82,15 @@ public class EndSignBlock extends SignBlock implements BlockModelProvider, ISpet
@Override @Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) { public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
if (placer != null && placer instanceof Player) { if (placer instanceof Player) {
ESignBlockEntity sign = (ESignBlockEntity) world.getBlockEntity(pos); ESignBlockEntity sign = (ESignBlockEntity) world.getBlockEntity(pos);
if (!world.isClientSide) { if (sign != null) {
sign.setAllowedPlayerEditor((Player) placer); if (!world.isClientSide) {
((ServerPlayer) placer).connection.send(new ClientboundOpenSignEditorPacket(pos)); sign.setAllowedPlayerEditor((Player) placer);
} ((ServerPlayer) placer).connection.send(new ClientboundOpenSignEditorPacket(pos));
else { } else {
sign.setEditable(true); sign.setEditable(true);
}
} }
} }
} }
@ -128,8 +132,7 @@ public class EndSignBlock extends SignBlock implements BlockModelProvider, ISpet
BlockPos blockPos = ctx.getClickedPos(); BlockPos blockPos = ctx.getClickedPos();
Direction[] directions = ctx.getNearestLookingDirections(); Direction[] directions = ctx.getNearestLookingDirections();
for (int i = 0; i < directions.length; ++i) { for (Direction direction : directions) {
Direction direction = directions[i];
if (direction.getAxis().isHorizontal()) { if (direction.getAxis().isHorizontal()) {
Direction dir = direction.getOpposite(); Direction dir = direction.getOpposite();
int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15; int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15;
@ -153,6 +156,12 @@ public class EndSignBlock extends SignBlock implements BlockModelProvider, ISpet
return Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath()); return Patterns.createJson(Patterns.BLOCK_EMPTY, parentId.getPath());
} }
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent);
return ModelsHelper.createBlockEmpty(parentId);
}
@Override @Override
public BlockState rotate(BlockState state, Rotation rotation) { public BlockState rotate(BlockState state, Rotation rotation) {
return (BlockState) state.setValue(ROTATION, rotation.rotate((Integer) state.getValue(ROTATION), 16)); return (BlockState) state.setValue(ROTATION, rotation.rotate((Integer) state.getValue(ROTATION), 16));

View file

@ -2,17 +2,27 @@ package ru.betterend.blocks.basis;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import com.google.common.collect.Lists;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SlabBlock; import net.minecraft.world.level.block.SlabBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.SlabType;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
public class EndSlabBlock extends SlabBlock implements BlockModelProvider { public class EndSlabBlock extends SlabBlock implements BlockModelProvider {
@ -35,4 +45,24 @@ public class EndSlabBlock extends SlabBlock implements BlockModelProvider {
return Patterns.createJson(Patterns.BLOCK_SLAB, parentId.getPath(), blockId.getPath()); return Patterns.createJson(Patterns.BLOCK_SLAB, parentId.getPath(), blockId.getPath());
} }
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
ResourceLocation parentId = Registry.BLOCK.getKey(parent);
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_SLAB, parentId.getPath(), blockId.getPath());
return pattern.map(BlockModel::fromString).orElse(null);
}
@Override
public MultiVariant getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
SlabType type = blockState.getValue(TYPE);
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + "_" + type);
registerBlockModel(resourceLocation, modelId, blockState, modelCache);
if (type == SlabType.TOP) {
BlockModelRotation rotation = BlockModelRotation.by(180, 0);
Variant variant = new Variant(modelId, rotation.getRotation(), true, 1);
return new MultiVariant(Lists.newArrayList(variant));
}
return ModelsHelper.createBlockSimple(modelId);
}
} }

View file

@ -5,15 +5,18 @@ import java.util.Optional;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.color.block.BlockColor; import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor; import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.betterend.blocks.AuroraCrystalBlock; import ru.betterend.blocks.AuroraCrystalBlock;
import ru.betterend.interfaces.IColorProvider; import ru.betterend.interfaces.IColorProvider;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
@ -70,4 +73,12 @@ public class StoneLanternBlock extends EndLanternBlock implements IColorProvider
return Patterns.createJson(Patterns.BLOCK_STONE_LANTERN_FLOOR, texture, texture); return Patterns.createJson(Patterns.BLOCK_STONE_LANTERN_FLOOR, texture, texture);
} }
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
String blockName = resourceLocation.getPath();
Optional<String> pattern = blockState.getValue(IS_FLOOR) ?
Patterns.createJson(Patterns.BLOCK_STONE_LANTERN_FLOOR, blockName, blockName) :
Patterns.createJson(Patterns.BLOCK_STONE_LANTERN_CEIL, blockName, blockName);
return pattern.map(BlockModel::fromString).orElse(null);
}
} }

View file

@ -10,7 +10,9 @@ import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant; import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.client.resources.model.BlockModelRotation; import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -25,6 +27,11 @@ public class ModelsHelper {
return pattern.map(BlockModel::fromString).orElse(null); return pattern.map(BlockModel::fromString).orElse(null);
} }
public static BlockModel createBlockEmpty(ResourceLocation resourceLocation) {
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_EMPTY, resourceLocation.getPath());
return pattern.map(BlockModel::fromString).orElse(null);
}
public static MultiVariant createBlockSimple(ResourceLocation resourceLocation) { public static MultiVariant createBlockSimple(ResourceLocation resourceLocation) {
Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1); Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1);
return new MultiVariant(Lists.newArrayList(variant)); return new MultiVariant(Lists.newArrayList(variant));