ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockAtomicReconstructor.java

221 lines
9.5 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +02:00
* This file ("BlockAtomicReconstructor.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2024-03-02 21:23:08 +01:00
import com.mojang.blaze3d.platform.Window;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.lens.ILensItem;
import de.ellpeck.actuallyadditions.mod.blocks.base.FullyDirectionalBlock;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.tile.TileEntityAtomicReconstructor;
2016-07-07 17:59:45 +02:00
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
2015-12-21 22:28:57 +01:00
import net.minecraft.client.Minecraft;
2024-03-03 01:20:53 +01:00
import net.minecraft.client.gui.GuiGraphics;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
2024-03-03 01:20:53 +01:00
import net.minecraft.util.RandomSource;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
2024-04-03 12:37:28 +02:00
import net.minecraft.world.level.BlockGetter;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
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.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
2024-04-03 12:37:28 +02:00
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nonnull;
2021-02-27 17:22:03 +01:00
import javax.annotation.Nullable;
2022-10-15 01:58:04 +02:00
import java.text.NumberFormat;
import java.util.List;
public class BlockAtomicReconstructor extends FullyDirectionalBlock.Container implements IHudDisplay {
2021-02-27 17:22:03 +01:00
public static final DirectionProperty FACING = BlockStateProperties.FACING;
2016-01-19 21:56:45 +01:00
public static final int NAME_FLAVOR_AMOUNTS_1 = 12;
public static final int NAME_FLAVOR_AMOUNTS_2 = 14;
public BlockAtomicReconstructor() {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps(10.0F, 80F));
}
2015-11-29 13:17:45 +01:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
ItemStack heldItem = player.getItemInHand(hand);
2021-02-26 22:15:48 +01:00
if (this.tryToggleRedstone(world, pos, player)) {
2024-03-02 21:23:08 +01:00
return InteractionResult.CONSUME;
2021-02-26 22:15:48 +01:00
}
if (!world.isClientSide) {
TileEntityAtomicReconstructor reconstructor = (TileEntityAtomicReconstructor) world.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (reconstructor != null) {
2022-06-24 21:23:43 +02:00
if (!heldItem.isEmpty()) {
2016-05-05 00:06:02 +02:00
Item item = heldItem.getItem();
2022-06-24 21:23:43 +02:00
if (item instanceof ILensItem && reconstructor.inv.getStackInSlot(0).isEmpty()) {
ItemStack toPut = heldItem.copy();
toPut.setCount(1);
reconstructor.inv.setStackInSlot(0, toPut);
2022-06-24 21:23:43 +02:00
if (!player.isCreative()) {
heldItem.shrink(1);
}
2024-03-02 21:23:08 +01:00
return InteractionResult.CONSUME;
2016-05-05 00:06:02 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
ItemStack slot = reconstructor.inv.getStackInSlot(0);
2024-03-02 21:23:08 +01:00
if (!slot.isEmpty() && hand == InteractionHand.MAIN_HAND) {
player.setItemInHand(hand, slot.copy());
2022-06-24 21:23:43 +02:00
reconstructor.inv.setStackInSlot(0, ItemStack.EMPTY);
2024-03-02 21:23:08 +01:00
return InteractionResult.CONSUME;
}
2015-11-22 18:52:11 +01:00
}
2015-11-15 03:15:19 +01:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.FAIL;
2015-11-15 03:15:19 +01:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.CONSUME;
2015-11-15 03:15:19 +01:00
}
2015-11-22 20:23:54 +01:00
2024-04-03 12:37:28 +02:00
@Override
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
switch (state.getValue(FACING)) {
case UP:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_U;
case DOWN:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_D;
case EAST:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_E;
case SOUTH:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_S;
case WEST:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_W;
default:
return VoxelShapes.AtomicReconstructorShapes.SHAPE_N;
}
}
2021-02-27 17:22:03 +01:00
@Nullable
2022-01-19 02:11:23 +01:00
@Override
2024-03-02 21:23:08 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileEntityAtomicReconstructor(pos, state);
2015-12-01 19:48:09 +01:00
}
2024-03-02 21:23:08 +01:00
@Nullable
2022-01-19 02:11:23 +01:00
@Override
2024-03-02 21:23:08 +01:00
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> entityType) {
return level.isClientSide? TileEntityAtomicReconstructor::clientTick : TileEntityAtomicReconstructor::serverTick;
2022-01-19 02:11:23 +01:00
}
2015-12-21 22:28:57 +01:00
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2024-03-03 01:20:53 +01:00
public void displayHud(GuiGraphics guiGraphics, Minecraft minecraft, Player player, ItemStack stack, HitResult rayCast, Window resolution) {
2024-03-02 21:23:08 +01:00
if (!(rayCast instanceof BlockHitResult) || minecraft.level == null) {
2021-02-27 21:24:26 +01:00
return;
}
2024-03-02 21:23:08 +01:00
BlockEntity tile = minecraft.level.getBlockEntity(((BlockHitResult) rayCast).getBlockPos());
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityAtomicReconstructor) {
ItemStack slot = ((TileEntityAtomicReconstructor) tile).inv.getStackInSlot(0);
2024-03-02 21:23:08 +01:00
Component lens_name;
2022-06-24 21:38:07 +02:00
if (slot.isEmpty()) {
2024-03-03 01:20:53 +01:00
lens_name = Component.translatable("info.actuallyadditions.nolens");
2019-05-02 09:10:29 +02:00
} else {
2022-06-24 21:38:07 +02:00
lens_name = slot.getItem().getName(slot);
2015-12-21 23:53:01 +01:00
AssetUtil.renderStackToGui(slot, resolution.getGuiScaledWidth() / 2 + 15, resolution.getGuiScaledHeight() / 2 - 19, 1F);
2015-12-21 22:28:57 +01:00
}
2024-03-03 01:20:53 +01:00
guiGraphics.drawString(minecraft.font, lens_name.plainCopy().withStyle(ChatFormatting.YELLOW).withStyle(ChatFormatting.ITALIC).getString(), (int) (resolution.getGuiScaledWidth() / 2.0f + 35), (int) (resolution.getGuiScaledHeight() / 2.0f - 15), 0xFFFFFF);
2015-12-21 22:28:57 +01:00
}
}
2016-01-19 21:56:45 +01:00
public static class TheItemBlock extends AABlockItem {
private long lastSysTime;
private int toPick1;
private int toPick2;
private final Block block;
public TheItemBlock(Block blockIn) {
super(blockIn, ActuallyBlocks.defaultBlockItemProperties);
block = blockIn;
}
@OnlyIn(Dist.CLIENT)
@Override
2024-03-02 21:23:08 +01:00
public void appendHoverText(@Nonnull ItemStack pStack, @Nullable Level pLevel, @Nonnull List<Component> pTooltip, @Nonnull TooltipFlag pFlag) {
super.appendHoverText(pStack, pLevel, pTooltip, pFlag);
long sysTime = System.currentTimeMillis();
if (this.lastSysTime + 3000 < sysTime) {
this.lastSysTime = sysTime;
if (Minecraft.getInstance().level != null) {
2024-03-03 01:20:53 +01:00
RandomSource random = Minecraft.getInstance().level.random;
this.toPick1 = random.nextInt(NAME_FLAVOR_AMOUNTS_1) + 1;
this.toPick2 = random.nextInt(NAME_FLAVOR_AMOUNTS_2) + 1;
}
}
String base = block.getDescriptionId() + ".info.";
2024-03-03 01:20:53 +01:00
pTooltip.add(Component.translatable(base + "1." + this.toPick1).append(" ").append(Component.translatable(base + "2." + this.toPick2)).withStyle(s -> s.withColor(ChatFormatting.GRAY)));
2022-10-15 01:58:04 +02:00
if (pStack.hasTag() && pStack.getTag().contains("BlockEntityTag")) {
2024-03-02 21:23:08 +01:00
CompoundTag BET = pStack.getTag().getCompound("BlockEntityTag");
2022-10-15 01:58:04 +02:00
int energy = 0;
if (BET.contains("Energy")) {
energy = BET.getInt("Energy");
}
NumberFormat format = NumberFormat.getInstance();
2024-03-03 01:20:53 +01:00
pTooltip.add(Component.translatable("misc.actuallyadditions.power_single", format.format(energy)));
2022-10-15 01:58:04 +02:00
if (BET.contains("IsPulseMode")) {
2024-03-03 01:20:53 +01:00
pTooltip.add(Component.translatable("info.actuallyadditions.redstoneMode").append(": ")
.append(Component.translatable(BET.getBoolean("IsPulseMode")?"info.actuallyadditions.redstoneMode.pulse":"info.actuallyadditions.redstoneMode.deactivation").withStyle($ -> $.withColor(ChatFormatting.RED))));
2022-10-15 01:58:04 +02:00
}
}
}
@Override
2024-03-02 21:23:08 +01:00
protected boolean updateCustomBlockEntityTag(BlockPos pPos, Level pLevel, @Nullable Player pPlayer, ItemStack pStack, BlockState pState) {
boolean ret = super.updateCustomBlockEntityTag(pPos, pLevel, pPlayer, pStack, pState);
return ret;
}
}
2019-02-27 19:53:05 +01:00
@Override
public boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
2024-03-02 21:23:08 +01:00
public int getAnalogOutputSignal(BlockState blockState, Level world, BlockPos pos) {
BlockEntity t = world.getBlockEntity(pos);
2019-02-27 19:53:05 +01:00
int i = 0;
if (t instanceof TileEntityAtomicReconstructor) {
i = ((TileEntityAtomicReconstructor) t).getEnergy();
}
2024-03-02 21:23:08 +01:00
return Mth.clamp(i / 20000, 0, 15);
}
2021-02-26 22:15:48 +01:00
}