Skip to content

Commit

Permalink
Implements automatic card and jewel identifier when table is powered …
Browse files Browse the repository at this point in the history
…by redstone and has energy stored inside it.

- Cards are random
- Jewels are identified based on their weight.
  • Loading branch information
BONNe committed Dec 30, 2024
1 parent 60b196e commit a33e324
Show file tree
Hide file tree
Showing 10 changed files with 1,214 additions and 352 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod_name=More Vault Tables Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GNU GPL 3.0
# The mod version. See https://semver.org/
mod_version=1.2
mod_version=1.3
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

import lv.id.bonne.vaulthunters.morevaulttables.block.entity.CardSelectorTableTileEntity;
Expand All @@ -22,6 +21,8 @@
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.pathfinder.PathComputationType;
Expand All @@ -39,7 +40,8 @@ public class CardSelectorTableBlock extends HorizontalDirectionalBlock implement
/**
* Instantiates a new Card selector table block.
*/
public CardSelectorTableBlock(Properties properties, VoxelShape shape) {
public CardSelectorTableBlock(Properties properties, VoxelShape shape)
{
super(properties);
this.registerDefaultState(this.getStateDefinition().any().
setValue(FACING, Direction.NORTH));
Expand All @@ -49,6 +51,7 @@ public CardSelectorTableBlock(Properties properties, VoxelShape shape) {

/**
* Create block state definition
*
* @param builder The definition builder.
*/
@Override
Expand All @@ -60,6 +63,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt

/**
* This method allows to rotate block opposite to player.
*
* @param context The placement context.
* @return The new block state.
*/
Expand All @@ -73,6 +77,7 @@ public BlockState getStateForPlacement(@NotNull BlockPlaceContext context)

/**
* This method returns the shape of current table.
*
* @param state The block state.
* @param level The level where block is located.
* @param pos The position of the block.
Expand All @@ -92,6 +97,7 @@ public VoxelShape getShape(@NotNull BlockState state,

/**
* The interaction that happens when player click on block.
*
* @param state The block state.
* @param level The level where block is located.
* @param pos The position of the block.
Expand Down Expand Up @@ -119,7 +125,9 @@ else if (player instanceof ServerPlayer serverPlayer)

if (tile instanceof CardSelectorTableTileEntity vaultCardApplicationStationTile)
{
NetworkHooks.openGui(serverPlayer, vaultCardApplicationStationTile, buffer -> buffer.writeBlockPos(pos));
NetworkHooks.openGui(serverPlayer,
vaultCardApplicationStationTile,
buffer -> buffer.writeBlockPos(pos));
return InteractionResult.SUCCESS;
}
else
Expand All @@ -136,6 +144,7 @@ else if (player instanceof ServerPlayer serverPlayer)

/**
* This method indicates if entities can path find over this block.
*
* @param state The block state.
* @param level Level where block is located.
* @param pos Position of the block.
Expand All @@ -151,14 +160,19 @@ public boolean isPathfindable(BlockState state, BlockGetter level, BlockPos pos,

/**
* This method drops all items from container when block is broken.
*
* @param state The BlockState.
* @param level Level where block is broken.
* @param pos Position of broken block.
* @param newState New block state.
* @param isMoving Boolean if block is moving.
*/
@Override
public void onRemove(BlockState state, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean isMoving)
public void onRemove(BlockState state,
@NotNull Level level,
@NotNull BlockPos pos,
BlockState newState,
boolean isMoving)
{
if (!state.is(newState.getBlock()))
{
Expand Down Expand Up @@ -203,6 +217,7 @@ public void onRemove(BlockState state, @NotNull Level level, @NotNull BlockPos p

/**
* This method creates a new block entity.
*
* @param pos Position for block.
* @param state Block state.
* @return New block entity.
Expand All @@ -214,6 +229,53 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState sta
}


@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level,
@NotNull BlockState state,
@NotNull BlockEntityType<T> type)
{
return createTickerHelper(type,
MoreVaultTablesReferences.CARD_SELECTOR_TABLE_TILE_ENTITY,
(world, pos, blockState, tileEntity) -> tileEntity.tick());
}


@Override
public boolean canConnectRedstone(BlockState state, BlockGetter level, BlockPos pos, @Nullable Direction direction)
{
return true;
}


@Override
public void neighborChanged(@NotNull BlockState state,
@NotNull Level level,
@NotNull BlockPos pos,
@NotNull Block block,
@NotNull BlockPos fromPos,
boolean moving)
{
super.neighborChanged(state, level, pos, block, fromPos, moving);

if (level.getBlockEntity(pos) instanceof CardSelectorTableTileEntity tileEntity)
{
tileEntity.setRedstonePowered(level.hasNeighborSignal(pos));
}
}


/**
* This method creates requested tick block.
*/
@Nullable
public static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(
BlockEntityType<A> type,
BlockEntityType<E> expectedType,
BlockEntityTicker<? super E> ticker)
{
return type == expectedType ? (BlockEntityTicker<A>) ticker : null;
}

/**
* The constant SHAPE.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

import lv.id.bonne.vaulthunters.morevaulttables.block.entity.JewelSelectorTableTileEntity;
Expand All @@ -14,14 +13,18 @@
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
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.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.pathfinder.PathComputationType;
Expand All @@ -38,8 +41,12 @@ public class JewelSelectorTableBlock extends HorizontalDirectionalBlock implemen
{
/**
* Instantiates a new Jewel selector table block.
*
* @param properties the properties
* @param shape the shape
*/
public JewelSelectorTableBlock(Properties properties, VoxelShape shape) {
public JewelSelectorTableBlock(Properties properties, VoxelShape shape)
{
super(properties);
this.registerDefaultState(this.getStateDefinition().any().
setValue(FACING, Direction.NORTH));
Expand All @@ -49,6 +56,7 @@ public JewelSelectorTableBlock(Properties properties, VoxelShape shape) {

/**
* Create block state definition
*
* @param builder The definition builder.
*/
@Override
Expand All @@ -60,6 +68,7 @@ protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockSt

/**
* This method allows to rotate block opposite to player.
*
* @param context The placement context.
* @return The new block state.
*/
Expand All @@ -73,6 +82,7 @@ public BlockState getStateForPlacement(@NotNull BlockPlaceContext context)

/**
* This method returns the shape of current table.
*
* @param state The block state.
* @param level The level where block is located.
* @param pos The position of the block.
Expand All @@ -92,6 +102,7 @@ public VoxelShape getShape(@NotNull BlockState state,

/**
* The interaction that happens when player click on block.
*
* @param state The block state.
* @param level The level where block is located.
* @param pos The position of the block.
Expand Down Expand Up @@ -119,7 +130,9 @@ else if (player instanceof ServerPlayer serverPlayer)

if (tile instanceof JewelSelectorTableTileEntity vaultJewelApplicationStationTile)
{
NetworkHooks.openGui(serverPlayer, vaultJewelApplicationStationTile, buffer -> buffer.writeBlockPos(pos));
NetworkHooks.openGui(serverPlayer,
vaultJewelApplicationStationTile,
buffer -> buffer.writeBlockPos(pos));
return InteractionResult.SUCCESS;
}
else
Expand All @@ -136,6 +149,7 @@ else if (player instanceof ServerPlayer serverPlayer)

/**
* This method indicates if entities can path find over this block.
*
* @param state The block state.
* @param level Level where block is located.
* @param pos Position of the block.
Expand All @@ -151,14 +165,19 @@ public boolean isPathfindable(BlockState state, BlockGetter level, BlockPos pos,

/**
* This method drops all items from container when block is broken.
*
* @param state The BlockState.
* @param level Level where block is broken.
* @param pos Position of broken block.
* @param newState New block state.
* @param isMoving Boolean if block is moving.
*/
@Override
public void onRemove(BlockState state, @NotNull Level level, @NotNull BlockPos pos, BlockState newState, boolean isMoving)
public void onRemove(BlockState state,
@NotNull Level level,
@NotNull BlockPos pos,
BlockState newState,
boolean isMoving)
{
if (!state.is(newState.getBlock()))
{
Expand Down Expand Up @@ -203,6 +222,7 @@ public void onRemove(BlockState state, @NotNull Level level, @NotNull BlockPos p

/**
* This method creates a new block entity.
*
* @param pos Position for block.
* @param state Block state.
* @return New block entity.
Expand All @@ -214,6 +234,89 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState sta
}


/**
* Triggers when block is placed by.
* @param level World where it is placed.
* @param pos Placement position
* @param state Placement state
* @param placer The placer object
* @param stack The items stack
*/
@Override
public void setPlacedBy(@NotNull Level level,
@NotNull BlockPos pos,
@NotNull BlockState state,
@Nullable LivingEntity placer,
@NotNull ItemStack stack)
{
super.setPlacedBy(level, pos, state, placer, stack);

if (placer instanceof Player player)
{
BlockEntity blockEntity = level.getBlockEntity(pos);

if (blockEntity instanceof JewelSelectorTableTileEntity tileEntity)
{
tileEntity.setOwner(player);
}
}
}


@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level,
@NotNull BlockState state,
@NotNull BlockEntityType<T> type)
{
return createTickerHelper(type,
MoreVaultTablesReferences.JEWEL_SELECTOR_TABLE_TILE_ENTITY,
(world, pos, blockState, tileEntity) -> tileEntity.tick());
}


@Override
public boolean canConnectRedstone(BlockState state, BlockGetter level, BlockPos pos, @Nullable Direction direction)
{
return true;
}


@Override
public void neighborChanged(@NotNull BlockState state,
@NotNull Level level,
@NotNull BlockPos pos,
@NotNull Block block,
@NotNull BlockPos fromPos,
boolean moving)
{
super.neighborChanged(state, level, pos, block, fromPos, moving);

if (level.getBlockEntity(pos) instanceof JewelSelectorTableTileEntity tileEntity)
{
tileEntity.setRedstonePowered(level.hasNeighborSignal(pos));
}
}


/**
* This method creates requested tick block.
*
* @param <E> the type parameter
* @param <A> the type parameter
* @param type the type
* @param expectedType the expected type
* @param ticker the ticker
* @return the block entity ticker
*/
@Nullable
public static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(
BlockEntityType<A> type,
BlockEntityType<E> expectedType,
BlockEntityTicker<? super E> ticker)
{
return type == expectedType ? (BlockEntityTicker<A>) ticker : null;
}

/**
* The constant SHAPE.
*/
Expand Down
Loading

0 comments on commit a33e324

Please sign in to comment.