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

95 lines
3.9 KiB
Java
Raw Normal View History

2016-06-05 04:05:37 +02:00
/*
* This file ("BlockPlayerInterface.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
* 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-06-05 04:05:37 +02:00
*/
package de.ellpeck.actuallyadditions.mod.blocks;
2024-03-02 21:23:08 +01:00
import com.mojang.blaze3d.platform.Window;
2016-06-05 04:05:37 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityPlayerInterface;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
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.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
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;
2024-03-02 21:23:08 +01:00
import javax.annotation.Nullable;
2016-06-05 04:05:37 +02:00
2019-05-02 09:10:29 +02:00
public class BlockPlayerInterface extends BlockContainerBase implements IHudDisplay {
public BlockPlayerInterface() {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps(4.5F, 10.0F));
2016-06-05 04:05:37 +02:00
}
2024-03-02 21:23:08 +01:00
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileEntityPlayerInterface(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> entityType) {
return level.isClientSide? TileEntityPlayerInterface::clientTick : TileEntityPlayerInterface::serverTick;
2016-06-05 04:05:37 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity player, ItemStack stack) {
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityPlayerInterface face) {
if (face.connectedPlayer == null) {
face.connectedPlayer = player.getUUID();
2021-03-01 20:14:50 +01:00
face.playerName = player.getName().getString();
face.setChanged();
face.sendUpdate();
2016-06-05 04:05:37 +02:00
}
}
super.setPlacedBy(world, pos, state, player, stack);
2016-06-05 04:05:37 +02: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)) {
2021-03-01 20:14:50 +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 != null) {
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityPlayerInterface face) {
String name = face.playerName == null
? "Unknown"
: face.playerName;
2024-03-03 01:20:53 +01:00
guiGraphics.drawString(minecraft.font, "Bound to: " + ChatFormatting.RED + name, (int) (resolution.getGuiScaledWidth() / 2f + 5), (int) (resolution.getGuiScaledHeight() / 2f + 5), 0xFFFFFF);
guiGraphics.drawString(minecraft.font, "UUID: " + ChatFormatting.DARK_GREEN + face.connectedPlayer, (int) (resolution.getGuiScaledWidth() / 2f + 5), (int) (resolution.getGuiScaledHeight() / 2f + 15), 0xFFFFFF);
}
}
}
2021-03-01 20:14:50 +01:00
@Override
2024-03-02 21:23:08 +01:00
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return VoxelShapes.PLAYER_INTERFACE_SHAPE;
2021-03-01 20:14:50 +01:00
}
2016-06-05 04:05:37 +02:00
}