More pots (WIP)

This commit is contained in:
paulevsGitch 2021-07-11 13:24:04 +03:00
parent 287e25bbcf
commit 13e5e9e088
13 changed files with 145 additions and 30 deletions

View file

@ -3,66 +3,63 @@ package ru.betterend.blocks;
import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.math.Matrix4f;
import com.mojang.math.Quaternion;
import com.mojang.math.Transformation;
import com.mojang.math.Vector3f;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
import ru.bclib.blocks.BaseBlockNotFull;
import ru.bclib.client.models.ModelsHelper;
import ru.bclib.client.models.ModelsHelper.MultiPartBuilder;
import ru.bclib.client.models.PatternsHelper;
import ru.bclib.client.render.BCLRenderLayer;
import ru.bclib.interfaces.IPostInit;
import ru.bclib.interfaces.IRenderTyped;
import ru.bclib.interfaces.ISpetialItem;
import ru.bclib.util.BlocksHelper;
import ru.bclib.util.JsonFactory;
import ru.bclib.util.MHelper;
import ru.betterend.BetterEnd;
import ru.betterend.client.models.Patterns;
import ru.betterend.interfaces.PottablePlant;
import ru.betterend.registry.EndBlocks;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped {
private static final VoxelShape SHAPE = Block.box(4, 4, 4, 12, 12, 12);
public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IPostInit {
private static final VoxelShape SHAPE = Block.box(4, 0, 4, 12, 8, 12);
private static final IntegerProperty PLANT_ID = EndBlockProperties.PLANT_ID;
private final ResourceLocation[] blocks;
private static VoxelShape[] plantBoxes;
private static Block[] blocks;
@Environment(EnvType.CLIENT)
private UnbakedModel source;
public FlowerPotBlock(Block source) {
super(FabricBlockSettings.copyOf(source));
List<ResourceLocation> blocks = Lists.newArrayList();
EndBlocks.getModBlocks().forEach(block -> {
if (block instanceof PottablePlant && block.getStateDefinition().getProperties().isEmpty()) {
blocks.add(Registry.BLOCK.getKey(block));
}
});
this.blocks = blocks.toArray(new ResourceLocation[] {});
this.registerDefaultState(this.defaultBlockState().setValue(PLANT_ID, 0));
}
@Override
@ -71,6 +68,54 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped {
builder.add(PLANT_ID);
}
@Override
public void postInit() {
if (this.blocks != null) {
return;
}
List<Block> blocks = Lists.newArrayList();
EndBlocks.getModBlocks().forEach(block -> {
if (block instanceof PottablePlant && block.getStateDefinition().getProperties().isEmpty()) {
if (!(block instanceof ISpetialItem) || !((ISpetialItem) block).canPlaceOnWater()) {
blocks.add(block);
}
}
});
FlowerPotBlock.blocks = blocks.toArray(new Block[] {});
FlowerPotBlock.plantBoxes = new VoxelShape[FlowerPotBlock.blocks.length];
for (int i = 0; i < FlowerPotBlock.blocks.length; i++) {
Block block = FlowerPotBlock.blocks[i];
VoxelShape shape = block.getShape(block.defaultBlockState(), null, BlockPos.ZERO, CollisionContext.empty());
plantBoxes[i] = Shapes.or(SHAPE, shape.move(0.25, 0.5, 0.25));
}
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.isClientSide) {
return InteractionResult.CONSUME;
}
ItemStack itemStack = player.getItemInHand(hand);
if (!(itemStack.getItem() instanceof BlockItem)) {
return InteractionResult.PASS;
}
BlockItem item = (BlockItem) itemStack.getItem();
for (int i = 0; i < blocks.length; i++) {
if (item.getBlock() == blocks[i]) {
BlocksHelper.setWithUpdate(level, pos, state.setValue(PLANT_ID, i + 1));
return InteractionResult.SUCCESS;
}
}
return InteractionResult.PASS;
}
@Override
@Environment(EnvType.CLIENT)
public BlockModel getItemModel(ResourceLocation blockId) {
Optional<String> pattern = PatternsHelper.createJson(Patterns.BLOCK_FLOWER_POT, blockId);
return ModelsHelper.fromPattern(pattern);
}
@Override
@Environment(EnvType.CLIENT)
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
@ -81,11 +126,11 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped {
}
MultiPartBuilder model = MultiPartBuilder.create(stateDefinition);
model.part(new ResourceLocation("block/flower_pot")).add();
model.part(new ModelResourceLocation(stateId.getNamespace(), stateId.getPath(), "inventory")).add();
Transformation offset = new Transformation(new Vector3f(0, 0.5F, 0), null, null, null);
for (int i = 0; i < blocks.length; i++) {
final int compareID = i + 1;
ResourceLocation modelPath = blocks[i];
ResourceLocation modelPath = Registry.BLOCK.getKey(blocks[i]);
ResourceLocation objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath() + ".json");
if (Minecraft.getInstance().getResourceManager().hasResource(objSource)) {
objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath());
@ -114,7 +159,8 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped {
@Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPE;
int id = state.getValue(PLANT_ID);
return id > 0 && id <= blocks.length ? plantBoxes[id - 1] : SHAPE;
}
@Override