PrettyPipes/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlock.java

93 lines
3.5 KiB
Java
Raw Normal View History

2020-05-07 18:30:40 +02:00
package de.ellpeck.prettypipes.terminal;
2024-03-07 16:06:46 +01:00
import com.mojang.serialization.MapCodec;
import de.ellpeck.prettypipes.Registry;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.Utility;
2021-12-02 16:55:04 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
2024-02-03 22:01:43 +01:00
import net.minecraft.core.registries.BuiltInRegistries;
2021-12-02 16:55:04 +01:00
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
2024-03-07 16:06:46 +01:00
import net.minecraft.world.level.block.state.BlockBehaviour;
2021-12-02 16:55:04 +01:00
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
2020-05-07 18:30:40 +02:00
import javax.annotation.Nullable;
2020-05-07 23:06:35 +02:00
import java.util.List;
2020-05-07 18:30:40 +02:00
2021-12-02 16:55:04 +01:00
public class ItemTerminalBlock extends BaseEntityBlock {
2024-03-07 16:06:46 +01:00
public static final MapCodec<ItemTerminalBlock> CODEC = BlockBehaviour.simpleCodec(ItemTerminalBlock::new);
public ItemTerminalBlock(Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return ItemTerminalBlock.CODEC;
2020-05-07 18:30:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult result) {
2021-12-02 17:46:56 +01:00
var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, worldIn, pos);
if (tile == null)
2021-12-02 16:55:04 +01:00
return InteractionResult.PASS;
2021-12-02 17:46:56 +01:00
var reason = tile.getInvalidTerminalReason();
if (reason != null) {
2021-12-02 16:55:04 +01:00
if (!worldIn.isClientSide)
2022-06-27 13:57:06 +02:00
player.sendSystemMessage(Component.translatable(reason).withStyle(ChatFormatting.RED));
2021-12-02 16:55:04 +01:00
return InteractionResult.SUCCESS;
}
2021-12-02 16:55:04 +01:00
if (!worldIn.isClientSide) {
2024-03-07 16:00:49 +01:00
player.openMenu(tile, pos);
2020-05-07 18:30:40 +02:00
tile.updateItems(player);
}
2021-12-02 16:55:04 +01:00
return InteractionResult.SUCCESS;
2020-05-07 18:30:40 +02:00
}
2020-05-07 23:06:35 +02:00
@Override
2021-12-02 16:55:04 +01:00
public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
2020-05-07 23:06:35 +02:00
if (state.getBlock() != newState.getBlock()) {
2021-12-02 17:46:56 +01:00
var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, worldIn, pos);
2020-05-07 23:06:35 +02:00
if (tile != null)
Utility.dropInventory(tile, tile.items);
2021-12-02 16:55:04 +01:00
super.onRemove(state, worldIn, pos, newState, isMoving);
2020-05-07 23:06:35 +02:00
}
}
2021-12-02 16:55:04 +01:00
@org.jetbrains.annotations.Nullable
2020-05-07 18:30:40 +02:00
@Override
2021-12-02 16:55:04 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemTerminalBlockEntity(pos, state);
2020-05-07 18:30:40 +02:00
}
@Override
2021-12-02 16:55:04 +01:00
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
2020-05-07 18:30:40 +02:00
}
2020-05-07 23:06:35 +02:00
@Override
2021-12-02 16:55:04 +01:00
public void appendHoverText(ItemStack stack, @Nullable BlockGetter worldIn, List<Component> tooltip, TooltipFlag flagIn) {
2024-02-03 22:01:43 +01:00
Utility.addTooltip(BuiltInRegistries.BLOCK.getKey(this).getPath(), tooltip);
2020-05-07 23:06:35 +02:00
}
@org.jetbrains.annotations.Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
2022-06-27 13:57:06 +02:00
return BaseEntityBlock.createTickerHelper(type, Registry.itemTerminalBlockEntity, ItemTerminalBlockEntity::tick);
}
2020-05-07 18:30:40 +02:00
}