Skip to content

Commit

Permalink
feat: add Vial Holder block
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Jun 16, 2023
1 parent 288fbd3 commit 4097a81
Show file tree
Hide file tree
Showing 20 changed files with 1,683 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ private void addBlockTranslations() {
addBlock(ModBlocks.YELLOW_BIO_LANTERN, "Yellow Bio-Lantern", "Bioluminescent light source.");
addBlock(ModBlocks.BLUE_BIO_LANTERN, "Blue Bio-Lantern", "Bioluminescent light source.");
addBlock(ModBlocks.TENDON_CHAIN, "Tendon Chain", "Chain made of tendons.");
addBlock(ModBlocks.VIAL_HOLDER, "Vial Holder", "Display and organize your serums.");

addBlock(ModBlocks.PRIMAL_FLESH, "Primal Flesh Block", "Primitive and pure, you better not touch this with your dirty paws.");
addBlock(ModBlocks.PRIMAL_FLESH_SLAB, "Primal Flesh Slab", "Primitive and pure, you better not touch this with your dirty paws.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected void addTables() {
dropSelf(ModBlocks.YELLOW_BIO_LANTERN.get());
dropSelf(ModBlocks.BLUE_BIO_LANTERN.get());
dropSelf(ModBlocks.TENDON_CHAIN.get());
dropSelf(ModBlocks.VIAL_HOLDER.get());

addCustom(ModBlocks.FLESH_DOOR.get(), ModBlockLoot::createFleshDoorTable);
addCustom(ModBlocks.FULL_FLESH_DOOR.get(), ModBlockLoot::createFleshDoorTable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.elenterius.biomancy.block.property.DirectionalSlabType;
import com.github.elenterius.biomancy.block.property.Orientation;
import com.github.elenterius.biomancy.block.property.UserSensitivity;
import com.github.elenterius.biomancy.block.vialholder.VialHolderBlock;
import com.github.elenterius.biomancy.init.ModBlocks;
import com.mojang.math.Vector3f;
import net.minecraft.client.renderer.block.model.ItemTransforms;
Expand All @@ -17,6 +18,7 @@
import net.minecraft.world.level.block.state.BlockState;
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.block.state.properties.Property;
import net.minecraftforge.client.model.generators.*;
import net.minecraftforge.common.data.ExistingFileHelper;
Expand Down Expand Up @@ -74,6 +76,7 @@ protected void registerStatesAndModels() {
bioLantern(ModBlocks.YELLOW_BIO_LANTERN.get());
bioLantern(ModBlocks.BLUE_BIO_LANTERN.get());
tendonChain(ModBlocks.TENDON_CHAIN.get());
vialHolder(ModBlocks.VIAL_HOLDER.get());

geckolibModel(ModBlocks.PRIMORDIAL_CRADLE.get(), PRIMAL_PARTICLE_TEXTURE);
geoBlockItem(ModBlocks.PRIMORDIAL_CRADLE.get(), new Vector3f(16, 16, 16));
Expand All @@ -90,6 +93,31 @@ protected void registerStatesAndModels() {
fleshkinPressurePlate(ModBlocks.FLESHKIN_PRESSURE_PLATE.get());
}

public void vialHolder(VialHolderBlock block) {
ResourceLocation baseModel = blockModel(block);
ModelFile.ExistingModelFile frameModel = models().getExistingFile(extend(baseModel, "_frame"));

DirectionProperty facingProperty = BlockStateProperties.HORIZONTAL_FACING;

MultiPartBlockStateBuilder builder = getMultipartBuilder(block);

facingProperty.getPossibleValues().forEach(direction -> {
int rotY = (((int) direction.toYRot()) + 180) % 360;
builder.part().modelFile(frameModel).rotationY(rotY).addModel().condition(facingProperty, direction).end();
});

for (BooleanProperty vialProperty : VialHolderBlock.getVialProperties()) {
ModelFile.ExistingModelFile vialModel = models().getExistingFile(extend(baseModel, "_" + vialProperty.getName()));

facingProperty.getPossibleValues().forEach(direction -> {
int rotY = (((int) direction.toYRot()) + 180) % 360;
builder.part().modelFile(vialModel).rotationY(rotY).addModel().condition(facingProperty, direction).condition(vialProperty, true).end();
});
}

itemModels().getBuilder(path(block)).parent(frameModel);
}

private void fleshkinPressurePlate(OwnablePressurePlateBlock block) {
String path = path(block);
ResourceLocation baseTexture = blockTexture(block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,11 @@ private void registerBioForgeRecipes(Consumer<FinishedRecipe> consumer) {
.setCategory(ModBioForgeTabs.BLOCKS)
.unlockedBy(Items.SHROOMLIGHT).save(consumer);

BioForgeRecipeBuilder.create(ModItems.VIAL_HOLDER.get())
.addIngredient(ModItems.ELASTIC_FIBERS.get(), 8)
.setCategory(ModBioForgeTabs.MISC)
.unlockedBy(ModItems.ELASTIC_FIBERS.get()).save(consumer);

//////////// WEAPONS ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

BioForgeRecipeBuilder.create(new ItemData(ModItems.LONG_CLAWS.get()))
Expand Down
2 changes: 2 additions & 0 deletions src/generated/resources/assets/biomancy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@
"block.biomancy.bio_lantern_blue.tooltip": "Bioluminescent light source.",
"block.biomancy.tendon_chain": "Tendon Chain",
"block.biomancy.tendon_chain.tooltip": "Chain made of tendons.",
"block.biomancy.vial_holder": "Vial Holder",
"block.biomancy.vial_holder.tooltip": "Holds Vials...",
"block.biomancy.primal_flesh": "Primal Flesh Block",
"block.biomancy.primal_flesh.tooltip": "Primitive and pure, you better not touch this with your dirty paws.",
"block.biomancy.primal_flesh_slab": "Primal Flesh Slab",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package com.github.elenterius.biomancy.block.vialholder;

import com.github.elenterius.biomancy.init.ModBlockEntities;
import com.github.elenterius.biomancy.util.VoxelShapeUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
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.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
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.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
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 java.util.List;

public class VialHolderBlock extends BaseEntityBlock {

public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
public static final BooleanProperty VIAL_0 = BooleanProperty.create("vial_0");
public static final BooleanProperty VIAL_1 = BooleanProperty.create("vial_1");
public static final BooleanProperty VIAL_2 = BooleanProperty.create("vial_2");
public static final BooleanProperty VIAL_3 = BooleanProperty.create("vial_3");
public static final BooleanProperty VIAL_4 = BooleanProperty.create("vial_4");
public static final VoxelShape SHAPE_NORTH = createShape(Direction.NORTH);
public static final VoxelShape SHAPE_SOUTH = createShape(Direction.SOUTH);
public static final VoxelShape SHAPE_WEST = createShape(Direction.WEST);
public static final VoxelShape SHAPE_EAST = createShape(Direction.EAST);
static final BooleanProperty[] VIAL_PROPERTIES = {VIAL_0, VIAL_1, VIAL_2, VIAL_3, VIAL_4};

public VialHolderBlock(Properties properties) {
super(properties);

BlockState defaultState = defaultBlockState();
for (BooleanProperty vialProperty : VIAL_PROPERTIES) {
defaultState = defaultState.setValue(vialProperty, false);
}
registerDefaultState(defaultState.setValue(FACING, Direction.NORTH));
}

public static VoxelShape createShape(Direction direction) {
return VoxelShapeUtil.createYRotatedTowards(direction, 0, 6, 12, 16, 14, 16);
}

public static List<BooleanProperty> getVialProperties() {
return List.of(VIAL_PROPERTIES);
}

public static Direction getFacing(BlockState state) {
return state.getValue(FACING);
}

private static boolean hasVial(BlockState state, int index) {
if (index < 0 || index >= VIAL_PROPERTIES.length) return false;
return state.getValue(VIAL_PROPERTIES[index]);
}

public static int getTintColor(BlockState state, @Nullable BlockAndTintGetter level, @Nullable BlockPos pos, int tintIndex) {
if (level == null || pos == null || !hasVial(state, tintIndex)) return 0xFFFFFFFF;

if (level.getBlockEntity(pos) instanceof VialHolderBlockEntity vialHolder) {
return vialHolder.getVialColor(tintIndex);
}

return 0xFFFFFFFF;
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING).add(VIAL_PROPERTIES);
}

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
Direction clickedFace = context.getClickedFace();

if (clickedFace.getAxis().isHorizontal()) {
return defaultBlockState().setValue(FACING, clickedFace);
}
else {
return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
}
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return ModBlockEntities.VIAL_HOLDER.get().create(pos, state);
}

@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {

if (level.getBlockEntity(pos) instanceof VialHolderBlockEntity vialHolder) {
Direction facing = getFacing(state);
Vec3 hitLocation = hit.getLocation();

float v = (float) facing.getClockWise().getAxis().choose(hitLocation.x, hitLocation.y, hitLocation.z);
v = Math.abs(v - Mth.floor(v));

final int maxIndex = VIAL_PROPERTIES.length - 1;
int index = 0;

float min = 0.5f / 16f;
float max = 15.5f / 16f;
if (v >= max) {
index = maxIndex;
}
else if (v > min) {
v = (v - min) / (max - min);
index = Mth.floor(v * VIAL_PROPERTIES.length);
}

if (facing == Direction.NORTH || facing == Direction.EAST) {
index = maxIndex - index;
}

if (!vialHolder.isValidSlotIndex(index)) return InteractionResult.FAIL;

boolean isVialSlotEmpty = !vialHolder.hasVial(index);
ItemStack stackInHand = player.getItemInHand(hand);
boolean isHandEmpty = stackInHand.isEmpty();

if (isHandEmpty) {
if (isVialSlotEmpty) return InteractionResult.FAIL;

if (!level.isClientSide) vialHolder.extractVial(player, index);

}
else {
if (!isVialSlotEmpty) return InteractionResult.FAIL;

if (!level.isClientSide) {
ItemStack remainder = vialHolder.insertVial(stackInHand, index);
player.setItemInHand(hand, remainder);
}
}

return InteractionResult.sidedSuccess(level.isClientSide);
}

return InteractionResult.PASS;
}

@Override
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
if (!state.is(newState.getBlock())) {
if (level.getBlockEntity(pos) instanceof VialHolderBlockEntity vialHolder) {
vialHolder.dropInventoryContents(level, pos);
}
super.onRemove(state, level, pos, newState, isMoving);
}
}

@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}

@Override
public BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(getFacing(state)));
}

@Override
public BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(getFacing(state)));
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return switch (getFacing(state)) {
case NORTH -> SHAPE_NORTH;
case SOUTH -> SHAPE_SOUTH;
case WEST -> SHAPE_WEST;
case EAST -> SHAPE_EAST;
default -> Shapes.block();
};
}

}

0 comments on commit 4097a81

Please sign in to comment.