313 lines
15 KiB
Java
313 lines
15 KiB
Java
package com.dairymoose.modernlife.blocks;
|
|
|
|
import com.dairymoose.modernlife.core.CsvSourcedHashSet;
|
|
import com.dairymoose.modernlife.core.ModernLifeCommon;
|
|
import com.dairymoose.modernlife.core.ModernLifeConfig;
|
|
import com.dairymoose.modernlife.tileentities.ModernBookshelfBlockEntity;
|
|
import com.dairymoose.modernlife.util.ModernLifeUtil;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.chat.TextComponent;
|
|
import net.minecraft.world.Containers;
|
|
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.TooltipFlag;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.EntityBlock;
|
|
import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock;
|
|
import net.minecraft.world.level.block.Mirror;
|
|
import net.minecraft.world.level.block.Rotation;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
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.DirectionProperty;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.shapes.BooleanOp;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/ModernBookshelfBlock.class */
|
|
public class ModernBookshelfBlock extends Block implements EntityBlock {
|
|
public static CsvSourcedHashSet whitelist;
|
|
public static CsvSourcedHashSet blacklist;
|
|
public static CsvSourcedHashSet textToSearch;
|
|
public static CsvSourcedHashSet textToAvoid;
|
|
protected static final VoxelShape SHAPE_SOUTH = (VoxelShape) Stream.of(Block.box(0.0d, 0.0d, 0.0d, 16.0d, 16.0d, 9.0d)).reduce((v1, v2) -> {
|
|
return Shapes.join(v1, v2, BooleanOp.OR);
|
|
}).get();
|
|
protected static final VoxelShape SHAPE_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH);
|
|
protected static final VoxelShape SHAPE_NORTH = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_WEST);
|
|
protected static final VoxelShape SHAPE_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH);
|
|
public static long usedBookTimestamp = 0;
|
|
public static int oldItemSlot = -1;
|
|
public static BlockPos lastBlockPos = null;
|
|
public static ItemStack clientAwaitingBook = null;
|
|
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
|
|
|
|
public ModernBookshelfBlock(Properties props) {
|
|
super(props);
|
|
registerDefaultState((BlockState) this.stateDefinition.any().setValue(FACING, Direction.SOUTH));
|
|
if (whitelist == null) {
|
|
whitelist = new CsvSourcedHashSet((String) ModernLifeConfig.COMMON.bookshelfWhitelist.get());
|
|
}
|
|
if (blacklist == null) {
|
|
blacklist = new CsvSourcedHashSet((String) ModernLifeConfig.COMMON.bookshelfBlacklist.get());
|
|
}
|
|
if (textToSearch == null) {
|
|
textToSearch = new CsvSourcedHashSet((String) ModernLifeConfig.COMMON.bookshelfSearchText.get());
|
|
}
|
|
if (textToAvoid == null) {
|
|
textToAvoid = new CsvSourcedHashSet((String) ModernLifeConfig.COMMON.bookshelfAvoidText.get());
|
|
}
|
|
}
|
|
|
|
public BlockState rotate(BlockState state, Rotation rotation) {
|
|
return (BlockState) state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
|
}
|
|
|
|
public BlockState mirror(BlockState state, Mirror mirror) {
|
|
return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
|
}
|
|
|
|
public int getFlammability(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
|
|
if (this.material.isFlammable()) {
|
|
return 20;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public int getFireSpreadSpeed(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
|
|
if (this.material.isFlammable()) {
|
|
return 5;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public float getEnchantPowerBonus(BlockState state, LevelReader world, BlockPos pos) {
|
|
return 2.0f;
|
|
}
|
|
|
|
private boolean textToSearchHasMatch(String toSearch) {
|
|
Iterator<String> it = textToSearch.iterator();
|
|
while (it.hasNext()) {
|
|
String s = it.next();
|
|
if (toSearch.contains(s)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private boolean textToAvoidHasMatch(String toSearch) {
|
|
Iterator<String> it = textToAvoid.iterator();
|
|
while (it.hasNext()) {
|
|
String s = it.next();
|
|
if (toSearch.contains(s)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void getBookFromSlot(Player player, ModernBookshelfBlockEntity modernBookshelfBlockEntity, int slot) {
|
|
player.addItem(modernBookshelfBlockEntity.getItem(slot).copy());
|
|
modernBookshelfBlockEntity.setItem(slot, ItemStack.EMPTY);
|
|
modernBookshelfBlockEntity.setChanged();
|
|
}
|
|
|
|
public InteractionResult use(BlockState blockState, Level world, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult rayTrace) {
|
|
int slot;
|
|
ItemStack itemStack = player.getItemInHand(hand);
|
|
BlockEntity tileEntity = world.getBlockEntity(blockPos);
|
|
ModernBookshelfBlockEntity modernBookshelfBlockEntity = null;
|
|
if (tileEntity instanceof ModernBookshelfBlockEntity) {
|
|
modernBookshelfBlockEntity = (ModernBookshelfBlockEntity) tileEntity;
|
|
}
|
|
boolean tookItem = false;
|
|
boolean placedItem = false;
|
|
if (modernBookshelfBlockEntity != null) {
|
|
if (!player.isSteppingCarefully() && !itemStack.isEmpty()) {
|
|
String displayName = itemStack.getDisplayName().getString().toLowerCase();
|
|
String registryNamespace = itemStack.getItem().getRegistryName().getNamespace();
|
|
String registryPath = itemStack.getItem().getRegistryName().getPath();
|
|
String registryPathLowercase = registryPath.toLowerCase();
|
|
String fullRegistryText = registryNamespace + ":" + registryPath;
|
|
boolean itemIsWhitelisted = false;
|
|
boolean itemIsBlacklisted = false;
|
|
if (blacklist.contains(fullRegistryText)) {
|
|
itemIsBlacklisted = true;
|
|
}
|
|
if (whitelist.contains(fullRegistryText)) {
|
|
itemIsWhitelisted = true;
|
|
}
|
|
if (!modernBookshelfBlockEntity.isFull()) {
|
|
if (itemIsBlacklisted) {
|
|
placedItem = false;
|
|
} else {
|
|
boolean itemMatchesSearchCondition = ((!textToSearchHasMatch(displayName) && !textToSearchHasMatch(registryPathLowercase)) || textToAvoidHasMatch(displayName) || textToAvoidHasMatch(registryPathLowercase)) ? false : true;
|
|
if (itemIsWhitelisted || itemMatchesSearchCondition) {
|
|
placedItem = true;
|
|
}
|
|
if (placedItem && !world.isClientSide) {
|
|
ItemStack oneItem = itemStack.split(1);
|
|
modernBookshelfBlockEntity.placeItem(oneItem);
|
|
placedItem = true;
|
|
modernBookshelfBlockEntity.setChanged();
|
|
}
|
|
}
|
|
}
|
|
} else if (player.isSteppingCarefully() && !modernBookshelfBlockEntity.isEmpty()) {
|
|
tookItem = true;
|
|
int hitIndex = modernBookshelfBlockEntity.getHitIndex(player, rayTrace);
|
|
if (hitIndex != -1) {
|
|
getBookFromSlot(player, modernBookshelfBlockEntity, hitIndex);
|
|
} else if (!world.isClientSide && (slot = modernBookshelfBlockEntity.getNextOccupiedSlot()) != -1) {
|
|
getBookFromSlot(player, modernBookshelfBlockEntity, slot);
|
|
}
|
|
}
|
|
if (!placedItem && !tookItem && rayTrace != null) {
|
|
oldItemSlot = -1;
|
|
int hitIndex2 = modernBookshelfBlockEntity.getHitIndex(player, rayTrace);
|
|
if (hitIndex2 != -1) {
|
|
ItemStack pickedBook = modernBookshelfBlockEntity.getItem(hitIndex2);
|
|
if (!pickedBook.isEmpty()) {
|
|
ItemStack originalItem = player.getItemInHand(hand);
|
|
int freeSlot = player.getInventory().getFreeSlot();
|
|
if (freeSlot != -1) {
|
|
usedBookTimestamp = System.currentTimeMillis();
|
|
modernBookshelfBlockEntity.setItem(hitIndex2, ItemStack.EMPTY);
|
|
modernBookshelfBlockEntity.bookNames.set(hitIndex2, "");
|
|
oldItemSlot = freeSlot;
|
|
lastBlockPos = blockPos;
|
|
if (!world.isClientSide) {
|
|
player.setItemInHand(hand, pickedBook);
|
|
player.addItem(originalItem);
|
|
try {
|
|
pickedBook.use(world, player, hand);
|
|
} catch (Exception e) {
|
|
ModernLifeCommon.LOGGER.error("Use book error", e);
|
|
}
|
|
} else {
|
|
clientAwaitingBook = pickedBook;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!world.isClientSide) {
|
|
world.sendBlockUpdated(blockPos, blockState, blockState, 2);
|
|
}
|
|
return InteractionResult.CONSUME;
|
|
}
|
|
|
|
public void onRemove(BlockState p_196243_1_, Level p_196243_2_, BlockPos p_196243_3_, BlockState p_196243_4_, boolean p_196243_5_) {
|
|
if (!p_196243_1_.is(p_196243_4_.getBlock())) {
|
|
BlockEntity lvt_6_1_ = p_196243_2_.getBlockEntity(p_196243_3_);
|
|
if (lvt_6_1_ instanceof ModernBookshelfBlockEntity) {
|
|
Containers.dropContents(p_196243_2_, p_196243_3_, (ModernBookshelfBlockEntity) lvt_6_1_);
|
|
p_196243_2_.updateNeighbourForOutputSignal(p_196243_3_, this);
|
|
}
|
|
super.onRemove(p_196243_1_, p_196243_2_, p_196243_3_, p_196243_4_, p_196243_5_);
|
|
}
|
|
}
|
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
public void appendHoverText(ItemStack itemStack, @Nullable BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag) {
|
|
list.add(new TextComponent("Right click to place a book on the shelf"));
|
|
list.add(new TextComponent("Shift-right-click with an empty hand to remove a book"));
|
|
}
|
|
|
|
public BlockState updateShape(BlockState p_196271_1_, Direction p_196271_2_, BlockState p_196271_3_, LevelAccessor p_196271_4_, BlockPos p_196271_5_, BlockPos p_196271_6_) {
|
|
if (p_196271_1_.getValue(FACING).getOpposite() == p_196271_2_ && !p_196271_1_.canSurvive(p_196271_4_, p_196271_5_)) {
|
|
return Blocks.AIR.defaultBlockState();
|
|
}
|
|
return super.updateShape(p_196271_1_, p_196271_2_, p_196271_3_, p_196271_4_, p_196271_5_, p_196271_6_);
|
|
}
|
|
|
|
/* renamed from: com.dairymoose.modernlife.blocks.ModernBookshelfBlock$1 */
|
|
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/ModernBookshelfBlock$1.class */
|
|
static /* synthetic */ class C00371 {
|
|
static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];
|
|
|
|
static {
|
|
try {
|
|
$SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 1;
|
|
} catch (NoSuchFieldError e) {
|
|
}
|
|
try {
|
|
$SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 2;
|
|
} catch (NoSuchFieldError e2) {
|
|
}
|
|
try {
|
|
$SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 3;
|
|
} catch (NoSuchFieldError e3) {
|
|
}
|
|
try {
|
|
$SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 4;
|
|
} catch (NoSuchFieldError e4) {
|
|
}
|
|
}
|
|
}
|
|
|
|
public VoxelShape getShape(BlockState bs, BlockGetter reader, BlockPos pos, CollisionContext sel) {
|
|
switch (C00371.$SwitchMap$net$minecraft$core$Direction[bs.getValue(FACING).ordinal()]) {
|
|
case 1:
|
|
return SHAPE_NORTH;
|
|
case 2:
|
|
return SHAPE_SOUTH;
|
|
case 3:
|
|
return SHAPE_EAST;
|
|
case 4:
|
|
return SHAPE_WEST;
|
|
default:
|
|
return SHAPE_NORTH;
|
|
}
|
|
}
|
|
|
|
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
|
|
ModernBookshelfBlockEntity toReturn = (ModernBookshelfBlockEntity) ModernBookshelfBlockEntity.MODERN_BOOKSHELF.create(pos, state);
|
|
return toReturn;
|
|
}
|
|
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(new Property[]{FACING});
|
|
}
|
|
|
|
public boolean canSurvive(BlockState p_196260_1_, LevelReader p_196260_2_, BlockPos p_196260_3_) {
|
|
return FaceAttachedHorizontalDirectionalBlock.canAttach(p_196260_2_, p_196260_3_, p_196260_1_.getValue(FACING).getOpposite());
|
|
}
|
|
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
Direction[] var2 = context.getNearestLookingDirections();
|
|
for (Direction lvt_5_1_ : var2) {
|
|
if (lvt_5_1_.getAxis() != Direction.Axis.Y) {
|
|
BlockState lvt_6_2_ = (BlockState) defaultBlockState().setValue(FACING, lvt_5_1_.getOpposite());
|
|
if (lvt_6_2_.canSurvive(context.getLevel(), context.getClickedPos())) {
|
|
return lvt_6_2_;
|
|
}
|
|
}
|
|
}
|
|
return defaultBlockState();
|
|
}
|
|
}
|