Flower pots

This commit is contained in:
paulevsGitch 2021-07-11 18:16:55 +03:00
parent 13e5e9e088
commit e41fd592c7
14 changed files with 135 additions and 33 deletions

View file

@ -5,6 +5,7 @@ import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty;
import ru.bclib.blocks.BlockProperties; import ru.bclib.blocks.BlockProperties;
import ru.bclib.blocks.properties.StringProperty;
import ru.betterend.registry.EndPortals; import ru.betterend.registry.EndPortals;
public class EndBlockProperties extends BlockProperties { public class EndBlockProperties extends BlockProperties {
@ -12,9 +13,10 @@ public class EndBlockProperties extends BlockProperties {
public static final EnumProperty<PedestalState> PEDESTAL_STATE = EnumProperty.create("state", PedestalState.class); public static final EnumProperty<PedestalState> PEDESTAL_STATE = EnumProperty.create("state", PedestalState.class);
public static final EnumProperty<CactusBottom> CACTUS_BOTTOM = EnumProperty.create("bottom", CactusBottom.class); public static final EnumProperty<CactusBottom> CACTUS_BOTTOM = EnumProperty.create("bottom", CactusBottom.class);
public static final BooleanProperty HAS_ITEM = BooleanProperty.create("has_item");
public static final IntegerProperty PORTAL = IntegerProperty.create("portal", 0, EndPortals.getCount()); public static final IntegerProperty PORTAL = IntegerProperty.create("portal", 0, EndPortals.getCount());
public static final IntegerProperty PLANT_ID = IntegerProperty.create("plant_id", 0, 127); public static final IntegerProperty PLANT_ID = IntegerProperty.create("plant_id", 0, 31);
public static final IntegerProperty SOIL_ID = IntegerProperty.create("soil_id", 0, 10);
public static final BooleanProperty HAS_ITEM = BooleanProperty.create("has_item");
public enum PedestalState implements StringRepresentable { public enum PedestalState implements StringRepresentable {
PEDESTAL_TOP("pedestal_top"), COLUMN_TOP("column_top"), BOTTOM("bottom"), PILLAR("pillar"), COLUMN("column"), DEFAULT("default"); PEDESTAL_TOP("pedestal_top"), COLUMN_TOP("column_top"), BOTTOM("bottom"), PILLAR("pillar"), COLUMN("column"), DEFAULT("default");

View file

@ -15,6 +15,8 @@ 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.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
@ -40,6 +42,8 @@ import ru.bclib.interfaces.IRenderTyped;
import ru.bclib.interfaces.ISpetialItem; import ru.bclib.interfaces.ISpetialItem;
import ru.bclib.util.BlocksHelper; import ru.bclib.util.BlocksHelper;
import ru.bclib.util.JsonFactory; import ru.bclib.util.JsonFactory;
import ru.betterend.BetterEnd;
import ru.betterend.blocks.basis.EndTerrainBlock;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
import ru.betterend.interfaces.PottablePlant; import ru.betterend.interfaces.PottablePlant;
import ru.betterend.registry.EndBlocks; import ru.betterend.registry.EndBlocks;
@ -49,13 +53,12 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IPostInit { 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 static final IntegerProperty PLANT_ID = EndBlockProperties.PLANT_ID;
private static VoxelShape[] plantBoxes; private static final IntegerProperty SOIL_ID = EndBlockProperties.SOIL_ID;
private static Block[] blocks; private static final VoxelShape SHAPE_EMPTY;
private static final VoxelShape SHAPE_FULL;
@Environment(EnvType.CLIENT) private static Block[] plants;
private UnbakedModel source; private static Block[] soils;
public FlowerPotBlock(Block source) { public FlowerPotBlock(Block source) {
super(FabricBlockSettings.copyOf(source)); super(FabricBlockSettings.copyOf(source));
@ -65,28 +68,33 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IP
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
super.createBlockStateDefinition(builder); super.createBlockStateDefinition(builder);
builder.add(PLANT_ID); builder.add(PLANT_ID, SOIL_ID);
} }
@Override @Override
public void postInit() { public void postInit() {
if (this.blocks != null) { if (FlowerPotBlock.plants != null) {
return; return;
} }
List<Block> blocks = Lists.newArrayList(); List<Block> soils = Lists.newArrayList();
List<Block> plants = Lists.newArrayList();
EndBlocks.getModBlocks().forEach(block -> { EndBlocks.getModBlocks().forEach(block -> {
if (block instanceof PottablePlant && block.getStateDefinition().getProperties().isEmpty()) { if (block instanceof PottablePlant && block.getStateDefinition().getProperties().isEmpty()) {
if (!(block instanceof ISpetialItem) || !((ISpetialItem) block).canPlaceOnWater()) { if (!(block instanceof ISpetialItem) || !((ISpetialItem) block).canPlaceOnWater()) {
blocks.add(block); plants.add(block);
} }
} }
else if (block instanceof EndTerrainBlock) {
soils.add(block);
}
}); });
FlowerPotBlock.blocks = blocks.toArray(new Block[] {}); FlowerPotBlock.plants = plants.toArray(new Block[] {});
FlowerPotBlock.plantBoxes = new VoxelShape[FlowerPotBlock.blocks.length]; FlowerPotBlock.soils = soils.toArray(new Block[] {});
for (int i = 0; i < FlowerPotBlock.blocks.length; i++) { if (PLANT_ID.getValue(Integer.toString(FlowerPotBlock.plants.length)).isEmpty()) {
Block block = FlowerPotBlock.blocks[i]; throw new RuntimeException("There are too much plant ID values!");
VoxelShape shape = block.getShape(block.defaultBlockState(), null, BlockPos.ZERO, CollisionContext.empty()); }
plantBoxes[i] = Shapes.or(SHAPE, shape.move(0.25, 0.5, 0.25)); if (SOIL_ID.getValue(Integer.toString(FlowerPotBlock.soils.length)).isEmpty()) {
throw new RuntimeException("There are too much soil ID values!");
} }
} }
@ -96,13 +104,45 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IP
return InteractionResult.CONSUME; return InteractionResult.CONSUME;
} }
ItemStack itemStack = player.getItemInHand(hand); ItemStack itemStack = player.getItemInHand(hand);
int soilID = state.getValue(SOIL_ID);
if (soilID == 0) {
if (!(itemStack.getItem() instanceof BlockItem)) {
return InteractionResult.PASS;
}
Block block = ((BlockItem) itemStack.getItem()).getBlock();
for (int i = 0; i < soils.length; i++) {
if (block == soils[i]) {
BlocksHelper.setWithUpdate(level, pos, state.setValue(SOIL_ID, i + 1));
return InteractionResult.SUCCESS;
}
}
return InteractionResult.PASS;
}
int plantID = state.getValue(PLANT_ID);
if (itemStack.isEmpty()) {
if (plantID > 0 && plantID <= plants.length) {
BlocksHelper.setWithUpdate(level, pos, state.setValue(PLANT_ID, 0));
player.addItem(new ItemStack(plants[plantID - 1]));
return InteractionResult.SUCCESS;
}
if (soilID > 0 && soilID <= soils.length) {
BlocksHelper.setWithUpdate(level, pos, state.setValue(SOIL_ID, 0));
player.addItem(new ItemStack(soils[soilID - 1]));
}
return InteractionResult.PASS;
}
if (!(itemStack.getItem() instanceof BlockItem)) { if (!(itemStack.getItem() instanceof BlockItem)) {
return InteractionResult.PASS; return InteractionResult.PASS;
} }
BlockItem item = (BlockItem) itemStack.getItem(); BlockItem item = (BlockItem) itemStack.getItem();
for (int i = 0; i < blocks.length; i++) { for (int i = 0; i < plants.length; i++) {
if (item.getBlock() == blocks[i]) { if (item.getBlock() == plants[i]) {
if (!((PottablePlant) plants[i]).canPlantOn(soils[soilID - 1])) {
return InteractionResult.PASS;
}
BlocksHelper.setWithUpdate(level, pos, state.setValue(PLANT_ID, i + 1)); BlocksHelper.setWithUpdate(level, pos, state.setValue(PLANT_ID, i + 1));
level.playLocalSound(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, SoundEvents.HOE_TILL, SoundSource.BLOCKS, 1, 1, false);
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} }
} }
@ -127,10 +167,11 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IP
MultiPartBuilder model = MultiPartBuilder.create(stateDefinition); MultiPartBuilder model = MultiPartBuilder.create(stateDefinition);
model.part(new ModelResourceLocation(stateId.getNamespace(), stateId.getPath(), "inventory")).add(); model.part(new ModelResourceLocation(stateId.getNamespace(), stateId.getPath(), "inventory")).add();
Transformation offset = new Transformation(new Vector3f(0, 0.5F, 0), null, null, null); Transformation offset = new Transformation(new Vector3f(0, 7.5F / 16F, 0), null, null, null);
for (int i = 0; i < blocks.length; i++) {
for (int i = 0; i < plants.length; i++) {
final int compareID = i + 1; final int compareID = i + 1;
ResourceLocation modelPath = Registry.BLOCK.getKey(blocks[i]); ResourceLocation modelPath = Registry.BLOCK.getKey(plants[i]);
ResourceLocation objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath() + ".json"); ResourceLocation objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath() + ".json");
if (Minecraft.getInstance().getResourceManager().hasResource(objSource)) { if (Minecraft.getInstance().getResourceManager().hasResource(objSource)) {
objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath()); objSource = new ResourceLocation(modelPath.getNamespace(), "block/potted_" + modelPath.getPath());
@ -152,6 +193,21 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IP
} }
} }
for (int i = 0; i < soils.length; i++) {
ResourceLocation soilLoc = BetterEnd.makeID("flower_pot_soil_" + i);
if (!modelCache.containsKey(soilLoc)) {
String texture = Registry.BLOCK.getKey(soils[i]).getPath() + "_top";
if (texture.contains("rutiscus")) {
texture += "_1";
}
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_FLOWER_POT_SOIL, texture);
UnbakedModel soil = ModelsHelper.fromPattern(pattern);
modelCache.put(soilLoc, soil);
}
final int compareID = i + 1;
model.part(soilLoc).setCondition(state -> state.getValue(SOIL_ID) == compareID).add();
}
UnbakedModel result = model.build(); UnbakedModel result = model.build();
modelCache.put(key, result); modelCache.put(key, result);
return result; return result;
@ -160,16 +216,21 @@ public class FlowerPotBlock extends BaseBlockNotFull implements IRenderTyped, IP
@Override @Override
public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
int id = state.getValue(PLANT_ID); int id = state.getValue(PLANT_ID);
return id > 0 && id <= blocks.length ? plantBoxes[id - 1] : SHAPE; return id > 0 && id <= plants.length ? SHAPE_FULL : SHAPE_EMPTY;
} }
@Override @Override
public VoxelShape getCollisionShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) { public VoxelShape getCollisionShape(BlockState state, BlockGetter view, BlockPos pos, CollisionContext ePos) {
return SHAPE; return SHAPE_EMPTY;
} }
@Override @Override
public BCLRenderLayer getRenderLayer() { public BCLRenderLayer getRenderLayer() {
return BCLRenderLayer.CUTOUT; return BCLRenderLayer.CUTOUT;
} }
static {
SHAPE_EMPTY = Shapes.or(Block.box(4, 1, 4, 12, 8, 12), Block.box(5, 0, 5, 11, 1, 11));
SHAPE_FULL = Shapes.or(SHAPE_EMPTY, Block.box(3, 8, 3, 13, 16, 13));
}
} }

View file

@ -1,12 +1,12 @@
package ru.betterend.blocks.basis; package ru.betterend.blocks.basis;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import ru.bclib.api.TagAPI; import ru.bclib.api.TagAPI;
import ru.bclib.blocks.BasePlantBlock; import ru.bclib.blocks.BasePlantBlock;
import ru.betterend.interfaces.PottablePlant; import ru.betterend.interfaces.PottablePlant;
public class EndPlantBlock extends BasePlantBlock implements PottablePlant { public class EndPlantBlock extends BasePlantBlock implements PottablePlant {
public EndPlantBlock() { public EndPlantBlock() {
this(false); this(false);
} }
@ -31,4 +31,9 @@ public class EndPlantBlock extends BasePlantBlock implements PottablePlant {
protected boolean isTerrain(BlockState state) { protected boolean isTerrain(BlockState state) {
return state.is(TagAPI.END_GROUND); return state.is(TagAPI.END_GROUND);
} }
@Override
public boolean canPlantOn(Block block) {
return isTerrain(block.defaultBlockState());
}
} }

View file

@ -75,6 +75,7 @@ public class Patterns {
public final static ResourceLocation BLOCK_TOP_SIDE_BOTTOM = BetterEnd.makeID("patterns/block/top_side_bottom.json"); public final static ResourceLocation BLOCK_TOP_SIDE_BOTTOM = BetterEnd.makeID("patterns/block/top_side_bottom.json");
public final static ResourceLocation BLOCK_PATH = BetterEnd.makeID("patterns/block/path.json"); public final static ResourceLocation BLOCK_PATH = BetterEnd.makeID("patterns/block/path.json");
public final static ResourceLocation BLOCK_FLOWER_POT = BetterEnd.makeID("patterns/block/flower_pot.json"); public final static ResourceLocation BLOCK_FLOWER_POT = BetterEnd.makeID("patterns/block/flower_pot.json");
public final static ResourceLocation BLOCK_FLOWER_POT_SOIL = BetterEnd.makeID("patterns/block/flower_pot_soil.json");
//Item Models //Item Models
public final static ResourceLocation ITEM_WALL = BetterEnd.makeID("patterns/item/pattern_wall.json"); public final static ResourceLocation ITEM_WALL = BetterEnd.makeID("patterns/item/pattern_wall.json");

View file

@ -1,3 +1,7 @@
package ru.betterend.interfaces; package ru.betterend.interfaces;
public interface PottablePlant {} import net.minecraft.world.level.block.Block;
public interface PottablePlant {
boolean canPlantOn(Block block);
}

View file

@ -11,7 +11,7 @@
"from": [ 4, 7, 4 ], "from": [ 4, 7, 4 ],
"to": [ 12, 8, 12 ], "to": [ 12, 8, 12 ],
"faces": { "faces": {
"down": { "uv": [ 4, 4, 12, 12 ], "texture": "#texture" }, "down": { "uv": [ 8, 0, 16, 8 ], "texture": "#texture" },
"up": { "uv": [ 8, 0, 16, 8 ], "texture": "#texture" }, "up": { "uv": [ 8, 0, 16, 8 ], "texture": "#texture" },
"north": { "uv": [ 0, 0, 8, 1 ], "texture": "#texture" }, "north": { "uv": [ 0, 0, 8, 1 ], "texture": "#texture" },
"south": { "uv": [ 0, 0, 8, 1 ], "texture": "#texture" }, "south": { "uv": [ 0, 0, 8, 1 ], "texture": "#texture" },
@ -25,6 +25,7 @@
"to": [ 11, 1, 11 ], "to": [ 11, 1, 11 ],
"faces": { "faces": {
"down": { "uv": [ 9, 9, 15, 15 ], "texture": "#texture", "cullface": "down" }, "down": { "uv": [ 9, 9, 15, 15 ], "texture": "#texture", "cullface": "down" },
"up": { "uv": [ 9, 9, 15, 15 ], "texture": "#texture" },
"north": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" }, "north": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" },
"south": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" }, "south": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" },
"west": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" }, "west": { "uv": [ 1, 7, 7, 8 ], "texture": "#texture" },
@ -49,10 +50,21 @@
"from": [ 4.5, 6, 4.5 ], "from": [ 4.5, 6, 4.5 ],
"to": [ 11.5, 7, 11.5 ], "to": [ 11.5, 7, 11.5 ],
"faces": { "faces": {
"north": { "uv": [ 1, 1, 8, 2 ], "texture": "#texture" }, "north": { "uv": [ 0.5, 1, 7.5, 2 ], "texture": "#texture" },
"south": { "uv": [ 1, 1, 8, 2 ], "texture": "#texture" }, "south": { "uv": [ 0.5, 1, 7.5, 2 ], "texture": "#texture" },
"west": { "uv": [ 1, 1, 8, 2 ], "texture": "#texture" }, "west": { "uv": [ 0.5, 1, 7.5, 2 ], "texture": "#texture" },
"east": { "uv": [ 1, 1, 8, 2 ], "texture": "#texture" } "east": { "uv": [ 0.5, 1, 7.5, 2 ], "texture": "#texture" }
}
},
{
"__comment": "Box14",
"from": [ 11, 8, 11 ],
"to": [ 5, 1, 5 ],
"faces": {
"north": { "uv": [ 1, 0, 7, 7 ], "texture": "#texture" },
"south": { "uv": [ 1, 0, 7, 7 ], "texture": "#texture" },
"west": { "uv": [ 1, 0, 7, 7 ], "texture": "#texture" },
"east": { "uv": [ 1, 0, 7, 7 ], "texture": "#texture" }
} }
} }
] ]

View file

@ -0,0 +1,17 @@
{
"__comment": "Designed by Paulevs with Cubik Studio - https://cubik.studio",
"textures": {
"particle": "betterend:block/%texture%",
"texture": "betterend:block/%texture%"
},
"elements": [
{
"__comment": "PlaneY1",
"from": [ 5, 7.5, 5 ],
"to": [ 11, 7.501, 11 ],
"faces": {
"up": { "uv": [ 5, 5, 11, 11 ], "texture": "#texture" }
}
}
]
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 263 B

After

Width:  |  Height:  |  Size: 217 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 B

After

Width:  |  Height:  |  Size: 236 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 298 B

After

Width:  |  Height:  |  Size: 252 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 263 B

After

Width:  |  Height:  |  Size: 217 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 270 B

After

Width:  |  Height:  |  Size: 226 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 538 B

After

Width:  |  Height:  |  Size: 252 B

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 263 B

After

Width:  |  Height:  |  Size: 217 B

Before After
Before After