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

197 lines
9.3 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockPhantom.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* 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
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2015-04-26 20:07:57 +02:00
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.tile.IPhantomTile;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
import de.ellpeck.actuallyadditions.mod.tile.*;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
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.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.FormattedCharSequence;
2024-03-02 21:23:08 +01:00
import net.minecraft.util.Mth;
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.level.BlockGetter;
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.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
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;
2015-04-26 20:07:57 +02:00
2019-05-02 09:10:29 +02:00
public class BlockPhantom extends BlockContainerBase implements IHudDisplay {
2015-04-26 20:07:57 +02:00
2016-05-19 20:05:12 +02:00
public final Type type;
2015-10-03 10:19:40 +02:00
public BlockPhantom(Type type) {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps(4.5F, 10.0F));
2015-05-25 17:00:54 +02:00
this.type = type;
2015-04-26 20:07:57 +02:00
}
2016-05-05 17:29:39 +02:00
@Override
public boolean isSignalSource(BlockState state) {
2016-05-05 17:29:39 +02:00
return this.type == Type.REDSTONEFACE;
}
@Override
2024-03-02 21:23:08 +01:00
public int getSignal(BlockState blockState, BlockGetter world, BlockPos pos, Direction side) {
2019-05-02 09:10:29 +02:00
if (this.type == Type.REDSTONEFACE) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2021-02-26 22:15:48 +01:00
if (tile instanceof TileEntityPhantomRedstoneface) {
return ((TileEntityPhantomRedstoneface) tile).providesWeak[side.ordinal()];
}
2016-05-05 17:29:39 +02:00
}
return 0;
2016-05-05 17:29:39 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public int getDirectSignal(BlockState blockState, BlockGetter world, BlockPos pos, Direction side) {
2019-05-02 09:10:29 +02:00
if (this.type == Type.REDSTONEFACE) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = world.getBlockEntity(pos);
2021-02-26 22:15:48 +01:00
if (tile instanceof TileEntityPhantomRedstoneface) {
return ((TileEntityPhantomRedstoneface) tile).providesStrong[side.ordinal()];
}
2016-05-05 17:29:39 +02:00
}
return 0;
2016-05-05 17:29:39 +02:00
}
2015-05-25 17:00:54 +02:00
@Override
2024-03-02 21:23:08 +01:00
public boolean shouldDropInventory(Level world, BlockPos pos) {
return this.type == Type.PLACER || this.type == Type.BREAKER;
2015-05-25 17:00:54 +02:00
}
2024-03-02 21:23:08 +01:00
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
switch (this.type) {
case PLACER:
return new TileEntityPhantomPlacer(pos, state);
case BREAKER:
return new TileEntityPhantomBreaker(pos, state);
case LIQUIFACE:
return new TileEntityPhantomLiquiface(pos, state);
case ENERGYFACE:
return new TileEntityPhantomEnergyface(pos, state);
case REDSTONEFACE:
return new TileEntityPhantomRedstoneface(pos, state);
default:
return new TileEntityPhantomItemface(pos, state);
}
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> entityType) {
2019-05-02 09:10:29 +02:00
switch (this.type) {
2021-02-26 22:15:48 +01:00
case PLACER:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomPlacer::clientTick : TileEntityPhantomPlacer::serverTick;
2021-02-26 22:15:48 +01:00
case BREAKER:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomBreaker::clientTick : TileEntityPhantomBreaker::serverTick;
2021-02-26 22:15:48 +01:00
case LIQUIFACE:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomLiquiface::clientTick : TileEntityPhantomLiquiface::serverTick;
2021-02-26 22:15:48 +01:00
case ENERGYFACE:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomEnergyface::clientTick : TileEntityPhantomEnergyface::serverTick;
2021-02-26 22:15:48 +01:00
case REDSTONEFACE:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomRedstoneface::clientTick : TileEntityPhantomRedstoneface::serverTick;
2021-02-26 22:15:48 +01:00
default:
2024-03-02 21:23:08 +01:00
return level.isClientSide? TileEntityPhantomItemface::clientTick : TileEntityPhantomItemface::serverTick;
2015-10-03 10:19:40 +02:00
}
}
2021-03-01 20:14:50 +01:00
// TODO: [port] validate this works
2015-10-03 10:19:40 +02:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
2021-02-26 22:15:48 +01:00
if (this.tryToggleRedstone(world, pos, player)) {
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
2021-02-26 22:15:48 +01:00
}
2021-11-21 17:31:57 +01:00
/*
if (!world.isClientSide) {
TileEntity tile = world.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile instanceof IPhantomTile && ((IPhantomTile) tile).getGuiID() != -1) {
2021-03-01 20:14:50 +01:00
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tile, pos);
2015-05-20 22:39:43 +02:00
}
}
2021-11-21 17:31:57 +01:00
*/
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
}
2021-03-01 20:14:50 +01:00
// TODO: [port] fix all of this, it's a mess
@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
BlockPos pos = ((BlockHitResult) rayCast).getBlockPos();
BlockEntity tile = minecraft.level.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile != null) {
2024-03-03 01:20:53 +01:00
if (tile instanceof IPhantomTile phantom) {
guiGraphics.drawString(minecraft.font, ChatFormatting.GOLD + I18n.get("tooltip." + ActuallyAdditions.MODID + ".blockPhantomRange.desc") + ": " + phantom.getRange(), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 - 40, ChatFormatting.WHITE.getColor());
2019-05-02 09:10:29 +02:00
if (phantom.hasBoundPosition()) {
2024-03-02 21:23:08 +01:00
int distance = Mth.ceil(new Vec3(pos.getX(), pos.getY(), pos.getZ()).distanceTo(new Vec3(phantom.getBoundPosition().getX(), phantom.getBoundPosition().getY(), phantom.getBoundPosition().getZ())));
BlockState state = minecraft.level.getBlockState(phantom.getBoundPosition());
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
Item item = Item.byBlock(block);
String name = item.getName(new ItemStack(block)).getString();
drawWordWrap(guiGraphics, minecraft.font, Component.translatable("tooltip.actuallyadditions.phantom.blockInfo.desc", name, phantom.getBoundPosition().getX(), phantom.getBoundPosition().getY(), phantom.getBoundPosition().getZ(), distance), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 - 30, 200, 0xFFFFFF, true);
2019-05-02 09:10:29 +02:00
if (phantom.isBoundThingInRange()) {
//StringUtil.drawSplitString(minecraft.font, ChatFormatting.DARK_GREEN + I18n.get("tooltip.actuallyadditions.phantom.connectedRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, 0xFFFFFF, true);
drawWordWrap(guiGraphics, minecraft.font, Component.translatable("tooltip.actuallyadditions.phantom.connectedRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, 0xFFFFFF, true);
2019-05-02 09:10:29 +02:00
} else {
//StringUtil.drawSplitString(minecraft.font, ChatFormatting.DARK_RED + I18n.get("tooltip.actuallyadditions.phantom.connectedNoRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, 0xFFFFFF, true);
drawWordWrap(guiGraphics, minecraft.font, Component.translatable("tooltip.actuallyadditions.phantom.connectedNoRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, 0xFFFFFF, true);
}
2019-05-02 09:10:29 +02:00
} else {
guiGraphics.drawString(minecraft.font, ChatFormatting.RED + I18n.get("tooltip.actuallyadditions.phantom.notConnected.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, ChatFormatting.WHITE.getColor());
}
}
}
}
@OnlyIn(Dist.CLIENT)
public static void drawWordWrap(GuiGraphics gg, Font font, FormattedText text, int x, int y, int width, int color, boolean shadow) {
for (FormattedCharSequence line : font.split(text, width)) {
gg.drawString(font, line, x, y, color, shadow);
y += font.lineHeight;
}
}
2019-05-02 09:10:29 +02:00
public enum Type {
ITEMFACE,
2015-10-03 10:16:18 +02:00
PLACER,
BREAKER,
LIQUIFACE,
2016-05-05 17:29:39 +02:00
ENERGYFACE,
REDSTONEFACE
2015-10-03 10:16:18 +02:00
}
2021-02-26 22:15:48 +01:00
}