Changing models loading (WIP)

This commit is contained in:
Aleksey 2021-05-22 23:22:02 +03:00
parent 9f98e6f3f8
commit 2c546e0038
8 changed files with 315 additions and 221 deletions

View file

@ -30,7 +30,7 @@ public class BlockProperties {
public static final IntegerProperty SIZE = IntegerProperty.create("size", 0, 7); public static final IntegerProperty SIZE = IntegerProperty.create("size", 0, 7);
public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3); public static final IntegerProperty AGE = IntegerProperty.create("age", 0, 3);
public static enum TripleShape implements StringRepresentable { public enum TripleShape implements StringRepresentable {
TOP("top", 0), TOP("top", 0),
MIDDLE("middle", 1), MIDDLE("middle", 1),
BOTTOM("bottom", 2); BOTTOM("bottom", 2);
@ -62,7 +62,7 @@ public class BlockProperties {
} }
} }
public static enum PedestalState implements StringRepresentable { public enum PedestalState implements StringRepresentable {
PEDESTAL_TOP("pedestal_top"), PEDESTAL_TOP("pedestal_top"),
COLUMN_TOP("column_top"), COLUMN_TOP("column_top"),
BOTTOM("bottom"), BOTTOM("bottom"),
@ -87,7 +87,7 @@ public class BlockProperties {
} }
} }
public static enum HydraluxShape implements StringRepresentable { public enum HydraluxShape implements StringRepresentable {
FLOWER_BIG_BOTTOM("flower_big_bottom", true), FLOWER_BIG_BOTTOM("flower_big_bottom", true),
FLOWER_BIG_TOP("flower_big_top", true), FLOWER_BIG_TOP("flower_big_top", true),
FLOWER_SMALL_BOTTOM("flower_small_bottom", true), FLOWER_SMALL_BOTTOM("flower_small_bottom", true),
@ -118,7 +118,7 @@ public class BlockProperties {
} }
} }
public static enum PentaShape implements StringRepresentable { public enum PentaShape implements StringRepresentable {
BOTTOM("bottom"), BOTTOM("bottom"),
PRE_BOTTOM("pre_bottom"), PRE_BOTTOM("pre_bottom"),
MIDDLE("middle"), MIDDLE("middle"),
@ -142,7 +142,7 @@ public class BlockProperties {
} }
} }
public static enum LumecornShape implements StringRepresentable { public enum LumecornShape implements StringRepresentable {
LIGHT_TOP("light_top", 15), LIGHT_TOP("light_top", 15),
LIGHT_TOP_MIDDLE("light_top_middle", 15), LIGHT_TOP_MIDDLE("light_top_middle", 15),
LIGHT_MIDDLE("light_middle", 15), LIGHT_MIDDLE("light_middle", 15),
@ -174,7 +174,7 @@ public class BlockProperties {
} }
} }
public static enum CactusBottom implements StringRepresentable { public enum CactusBottom implements StringRepresentable {
EMPTY("empty"), EMPTY("empty"),
SAND("sand"), SAND("sand"),
MOSS("moss"); MOSS("moss");

View file

@ -8,6 +8,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import ru.betterend.blocks.basis.EndTerrainBlock;
import ru.betterend.registry.EndParticles; import ru.betterend.registry.EndParticles;
public class ShadowGrassBlock extends EndTerrainBlock { public class ShadowGrassBlock extends EndTerrainBlock {

View file

@ -1,4 +1,4 @@
package ru.betterend.blocks; package ru.betterend.blocks.basis;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -8,8 +8,11 @@ import java.util.Optional;
import com.google.common.collect.Maps; 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.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;
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.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper; import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.item.enchantment.Enchantments;
@ -21,7 +24,8 @@ import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
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 ru.betterend.blocks.basis.BlockBaseNotFull; import org.jetbrains.annotations.Nullable;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
public class EndPathBlock extends BlockBaseNotFull { public class EndPathBlock extends BlockBaseNotFull {
@ -63,4 +67,24 @@ public class EndPathBlock extends BlockBaseNotFull {
return Patterns.createJson(Patterns.BLOCK_PATH, map); return Patterns.createJson(Patterns.BLOCK_PATH, map);
} }
@Override
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
String name = resourceLocation.getPath();
Map<String, String> textures = Maps.newHashMap();
textures.put("%top%", name + "_top");
textures.put("%side%", name.replace("_path", "") + "_side");
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_PATH, textures);
return ModelsHelper.fromPattern(pattern);
}
@Override
public UnbakedModel getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(), "block/" + resourceLocation.getPath());
return ModelsHelper.createRandomTopModel(modelId);
}
} }

View file

@ -1,4 +1,4 @@
package ru.betterend.blocks; package ru.betterend.blocks.basis;
import java.util.*; import java.util.*;
@ -6,9 +6,12 @@ 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.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.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
@ -30,7 +33,10 @@ import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import ru.betterend.blocks.BlockSounds;
import ru.betterend.blocks.basis.BlockBase; import ru.betterend.blocks.basis.BlockBase;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
public class EndTerrainBlock extends BlockBase { public class EndTerrainBlock extends BlockBase {
@ -100,4 +106,25 @@ public class EndTerrainBlock extends BlockBase {
return Patterns.createJson(Patterns.BLOCK_TOP_SIDE_BOTTOM, map); return Patterns.createJson(Patterns.BLOCK_TOP_SIDE_BOTTOM, map);
} }
@Override
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
String name = resourceLocation.getPath();
Map<String, String> textures = Maps.newHashMap();
textures.put("%top%", "betterend:block/" + name + "_top");
textures.put("%side%", "betterend:block/" + name + "_side");
textures.put("%bottom%", "minecraft:block/end_stone");
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_PATH, textures);
return ModelsHelper.fromPattern(pattern);
}
@Override
public UnbakedModel getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(), "block/" + resourceLocation.getPath());
return ModelsHelper.createRandomTopModel(modelId);
}
} }

View file

@ -1,14 +1,23 @@
package ru.betterend.blocks.basis; package ru.betterend.blocks.basis;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mojang.math.Transformation;
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.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;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
@ -23,9 +32,10 @@ import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;
import ru.betterend.blocks.BlockProperties; import ru.betterend.blocks.BlockProperties;
import ru.betterend.blocks.BlockProperties.TripleShape; import ru.betterend.blocks.BlockProperties.TripleShape;
import ru.betterend.blocks.EndTerrainBlock; import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
public class TripleTerrainBlock extends EndTerrainBlock { public class TripleTerrainBlock extends EndTerrainBlock {
@ -48,19 +58,6 @@ public class TripleTerrainBlock extends EndTerrainBlock {
return this.defaultBlockState().setValue(SHAPE, shape); return this.defaultBlockState().setValue(SHAPE, shape);
} }
@Override
public Optional<String> getModelString(String block) {
String name = Registry.BLOCK.getKey(this).getPath();
if (block.endsWith("_middle")) {
return Patterns.createJson(Patterns.BLOCK_BASE, name + "_top", name + "_top");
}
Map<String, String> map = Maps.newHashMap();
map.put("%top%", "betterend:block/" + name + "_top");
map.put("%side%", "betterend:block/" + name + "_side");
map.put("%bottom%", "minecraft:block/end_stone");
return Patterns.createJson(Patterns.BLOCK_TOP_SIDE_BOTTOM, map);
}
@Override @Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
TripleShape shape = state.getValue(SHAPE); TripleShape shape = state.getValue(SHAPE);
@ -75,9 +72,7 @@ public class TripleTerrainBlock extends EndTerrainBlock {
TripleShape shape = state.getValue(SHAPE); TripleShape shape = state.getValue(SHAPE);
if (shape == TripleShape.BOTTOM) { if (shape == TripleShape.BOTTOM) {
super.randomTick(state, world, pos, random); super.randomTick(state, world, pos, random);
return; } else if (random.nextInt(16) == 0) {
}
else if (random.nextInt(16) == 0) {
boolean bottom = canStayBottom(world, pos); boolean bottom = canStayBottom(world, pos);
if (shape == TripleShape.TOP) { if (shape == TripleShape.TOP) {
if (!bottom) { if (!bottom) {
@ -88,11 +83,9 @@ public class TripleTerrainBlock extends EndTerrainBlock {
boolean top = canStay(state, world, pos) || isMiddle(world.getBlockState(pos.above())); boolean top = canStay(state, world, pos) || isMiddle(world.getBlockState(pos.above()));
if (!top && !bottom) { if (!top && !bottom) {
world.setBlockAndUpdate(pos, Blocks.END_STONE.defaultBlockState()); world.setBlockAndUpdate(pos, Blocks.END_STONE.defaultBlockState());
} } else if (top && !bottom) {
else if (top && !bottom) {
world.setBlockAndUpdate(pos, state.setValue(SHAPE, TripleShape.BOTTOM)); world.setBlockAndUpdate(pos, state.setValue(SHAPE, TripleShape.BOTTOM));
} } else if (!top) {
else if (!top && bottom) {
world.setBlockAndUpdate(pos, state.setValue(SHAPE, TripleShape.TOP)); world.setBlockAndUpdate(pos, state.setValue(SHAPE, TripleShape.TOP));
} }
} }
@ -104,16 +97,72 @@ public class TripleTerrainBlock extends EndTerrainBlock {
BlockState blockState = world.getBlockState(blockPos); BlockState blockState = world.getBlockState(blockPos);
if (isMiddle(blockState)) { if (isMiddle(blockState)) {
return true; return true;
} } else if (blockState.getFluidState().getAmount() == 8) {
else if (blockState.getFluidState().getAmount() == 8) {
return false; return false;
} } else {
else {
return !blockState.isFaceSturdy(world, blockPos, Direction.UP); return !blockState.isFaceSturdy(world, blockPos, Direction.UP);
} }
} }
protected boolean isMiddle(BlockState state) { @Override
return state.is(this) && state.getValue(SHAPE) == TripleShape.MIDDLE; public Optional<String> getModelString(String block) {
String name = Registry.BLOCK.getKey(this).getPath();
if (block.endsWith("_middle")) {
return Patterns.createJson(Patterns.BLOCK_BASE, name + "_top", name + "_top");
}
Map<String, String> map = Maps.newHashMap();
map.put("%top%", "betterend:block/" + name + "_top");
map.put("%side%", "betterend:block/" + name + "_side");
map.put("%bottom%", "minecraft:block/end_stone");
return Patterns.createJson(Patterns.BLOCK_TOP_SIDE_BOTTOM, map);
}
@Override
public BlockModel getModel(ResourceLocation blockId) {
return getBlockModel(blockId, defaultBlockState());
}
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation resourceLocation, BlockState blockState) {
String name = resourceLocation.getPath();
Optional<String> pattern;
if (isMiddle(blockState)) {
pattern = Patterns.createBlockSimple(name + "_top");
} else {
Map<String, String> textures = Maps.newHashMap();
textures.put("%top%", "betterend:block/" + name + "_top");
textures.put("%side%", "betterend:block/" + name + "_side");
textures.put("%bottom%", "minecraft:block/end_stone");
pattern = Patterns.createJson(Patterns.BLOCK_TOP_SIDE_BOTTOM, textures);
}
return ModelsHelper.fromPattern(pattern);
}
@Override
public UnbakedModel getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
boolean isMiddle = isMiddle(blockState);
String middle = isMiddle ? "_middle" : "";
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + middle);
registerBlockModel(resourceLocation, modelId, blockState, modelCache);
if (isMiddle) {
List<Variant> variants = Lists.newArrayList();
for (BlockModelRotation rotation : BlockModelRotation.values()) {
variants.add(new Variant(modelId, rotation.getRotation(), false, 1));
}
return new MultiVariant(variants);
} else if (blockState.getValue(SHAPE) == TripleShape.TOP) {
return new MultiVariant(Lists.newArrayList(
new Variant(modelId, BlockModelRotation.X180_Y0.getRotation(), false, 1),
new Variant(modelId, BlockModelRotation.X180_Y90.getRotation(), false, 1),
new Variant(modelId, BlockModelRotation.X180_Y180.getRotation(), false, 1),
new Variant(modelId, BlockModelRotation.X180_Y270.getRotation(), false, 1)
));
}
return ModelsHelper.createRandomTopModel(modelId);
}
protected boolean isMiddle(BlockState blockState) {
return blockState.is(this) && blockState.getValue(SHAPE) == TripleShape.MIDDLE;
} }
} }

View file

@ -69,6 +69,15 @@ public class ModelsHelper {
return createMultiVariant(resourceLocation, rotation.getRotation(), false); return createMultiVariant(resourceLocation, rotation.getRotation(), false);
} }
public static MultiVariant createRandomTopModel(ResourceLocation resourceLocation) {
return new MultiVariant(Lists.newArrayList(
new Variant(resourceLocation, Transformation.identity(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y90.getRotation(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y180.getRotation(), false, 1),
new Variant(resourceLocation, BlockModelRotation.X0_Y270.getRotation(), false, 1)
));
}
public static class MultiPartBuilder { public static class MultiPartBuilder {
private final static MultiPartBuilder BUILDER = new MultiPartBuilder(); private final static MultiPartBuilder BUILDER = new MultiPartBuilder();

View file

@ -11,23 +11,7 @@ import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.MaterialColor; import net.minecraft.world.level.material.MaterialColor;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.blocks.*; import ru.betterend.blocks.*;
import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.blocks.basis.*;
import ru.betterend.blocks.basis.EndCropBlock;
import ru.betterend.blocks.basis.EndFurnaceBlock;
import ru.betterend.blocks.basis.EndLeavesBlock;
import ru.betterend.blocks.basis.EndOreBlock;
import ru.betterend.blocks.basis.EndPillarBlock;
import ru.betterend.blocks.basis.EndSlabBlock;
import ru.betterend.blocks.basis.EndStairsBlock;
import ru.betterend.blocks.basis.EndUnderwaterWallPlantBlock;
import ru.betterend.blocks.basis.EndWallPlantBlock;
import ru.betterend.blocks.basis.FurBlock;
import ru.betterend.blocks.basis.SimpleLeavesBlock;
import ru.betterend.blocks.basis.StalactiteBlock;
import ru.betterend.blocks.basis.StoneLanternBlock;
import ru.betterend.blocks.basis.TripleTerrainBlock;
import ru.betterend.blocks.basis.VineBlock;
import ru.betterend.blocks.basis.WallMushroomBlock;
import ru.betterend.blocks.complex.ColoredMaterial; import ru.betterend.blocks.complex.ColoredMaterial;
import ru.betterend.blocks.complex.CrystalSubblocksMaterial; import ru.betterend.blocks.complex.CrystalSubblocksMaterial;
import ru.betterend.blocks.complex.MetalMaterial; import ru.betterend.blocks.complex.MetalMaterial;

View file

@ -27,7 +27,7 @@ import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.levelgen.surfacebuilders.SurfaceBuilderConfiguration; import net.minecraft.world.level.levelgen.surfacebuilders.SurfaceBuilderConfiguration;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndTerrainBlock; import ru.betterend.blocks.basis.EndTerrainBlock;
import ru.betterend.blocks.basis.PedestalBlock; import ru.betterend.blocks.basis.PedestalBlock;
import ru.betterend.blocks.basis.SimpleLeavesBlock; import ru.betterend.blocks.basis.SimpleLeavesBlock;
import ru.betterend.blocks.basis.VineBlock; import ru.betterend.blocks.basis.VineBlock;