96 lines
3.2 KiB
Java
96 lines
3.2 KiB
Java
package de.jottyfan.quickiemod.block;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import de.jottyfan.quickiemod.blockentity.ItemHoarderBlockEntity;
|
|
import net.minecraft.block.AbstractBlock;
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.block.BlockEntityProvider;
|
|
import net.minecraft.block.BlockRenderType;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
import net.minecraft.block.entity.BlockEntityTicker;
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.loot.context.LootWorldContext.Builder;
|
|
import net.minecraft.registry.RegistryKey;
|
|
import net.minecraft.registry.RegistryKeys;
|
|
import net.minecraft.text.Style;
|
|
import net.minecraft.text.Text;
|
|
import net.minecraft.util.ActionResult;
|
|
import net.minecraft.util.Formatting;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.ItemScatterer;
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.world.World;
|
|
|
|
/**
|
|
*
|
|
* @author jotty
|
|
*
|
|
*/
|
|
public class BlockItemhoarder extends Block implements BlockEntityProvider {
|
|
|
|
public BlockItemhoarder(Identifier identifier) {
|
|
super(AbstractBlock.Settings.create().hardness(2.5f).registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
|
|
}
|
|
|
|
@Override
|
|
public BlockEntity createBlockEntity(BlockPos pos, BlockState blockState) {
|
|
return new ItemHoarderBlockEntity(pos, blockState);
|
|
}
|
|
|
|
@Override
|
|
public BlockRenderType getRenderType(BlockState state) {
|
|
return BlockRenderType.MODEL;
|
|
}
|
|
|
|
@Override
|
|
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state,
|
|
BlockEntityType<T> type) {
|
|
return (world1, pos, state1, be) -> ItemHoarderBlockEntity.tick(world1, pos, state1, be);
|
|
}
|
|
|
|
@Override
|
|
public List<ItemStack> getDroppedStacks(BlockState state, Builder builder) {
|
|
List<ItemStack> list = new ArrayList<>();
|
|
list.add(new ItemStack(ModBlocks.BLOCK_ITEMHOARDER));
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
|
|
BlockEntity blockEntity = world.getBlockEntity(pos);
|
|
if (blockEntity instanceof ItemHoarderBlockEntity) {
|
|
ItemHoarderBlockEntity ihbe = (ItemHoarderBlockEntity) blockEntity;
|
|
for (ItemStack stack : ihbe.getStacks()) {
|
|
ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
|
}
|
|
}
|
|
return super.onBreak(world, pos, state, player);
|
|
}
|
|
|
|
@Override
|
|
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
|
|
if (!world.isClient) {
|
|
BlockEntity blockEntity = world.getBlockEntity(pos);
|
|
if (blockEntity instanceof ItemHoarderBlockEntity) {
|
|
ItemHoarderBlockEntity ihbe = (ItemHoarderBlockEntity) blockEntity;
|
|
player.sendMessage(
|
|
Text.translatable("msg.itemhoarder.summary", new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date()))
|
|
.setStyle(Style.EMPTY.withColor(Formatting.BOLD)),
|
|
false);
|
|
for (Text text : ihbe.getSummary()) {
|
|
player.sendMessage(text, false);
|
|
}
|
|
}
|
|
}
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
}
|