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

160 lines
7.2 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
2021-02-27 21:24:26 +01:00
import com.mojang.blaze3d.matrix.MatrixStack;
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;
2021-02-26 22:15:48 +01:00
import de.ellpeck.actuallyadditions.mod.tile.*;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-07-04 20:15:41 +02:00
import net.minecraft.block.Block;
2021-03-01 20:14:50 +01:00
import net.minecraft.block.BlockState;
2021-02-27 17:22:03 +01:00
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
2015-04-26 20:07:57 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
2021-03-01 20:14:50 +01:00
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2021-03-01 20:14:50 +01:00
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.RayTraceResult;
2021-03-01 20:14:50 +01:00
import net.minecraft.util.math.vector.Vector3d;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
2021-03-01 20:14:50 +01:00
import net.minecraft.world.IBlockReader;
2015-04-26 20:07:57 +02:00
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
2021-03-01 20:14:50 +01:00
import net.minecraftforge.api.distmarker.OnlyIn;
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) {
2021-03-01 20:14:50 +01:00
super(ActuallyBlocks.defaultPickProps(0, 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
public int getSignal(BlockState blockState, IBlockReader world, BlockPos pos, Direction side) {
2019-05-02 09:10:29 +02:00
if (this.type == Type.REDSTONEFACE) {
TileEntity 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
public int getDirectSignal(BlockState blockState, IBlockReader world, BlockPos pos, Direction side) {
2019-05-02 09:10:29 +02:00
if (this.type == Type.REDSTONEFACE) {
TileEntity 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
2019-05-02 09:10:29 +02:00
public boolean shouldDropInventory(World world, BlockPos pos) {
return this.type == Type.PLACER || this.type == Type.BREAKER;
2015-05-25 17:00:54 +02:00
}
2015-10-03 10:19:40 +02:00
@Override
public TileEntity newBlockEntity(IBlockReader worldIn) {
2019-05-02 09:10:29 +02:00
switch (this.type) {
2021-02-26 22:15:48 +01:00
case PLACER:
return new TileEntityPhantomPlacer();
case BREAKER:
return new TileEntityPhantomBreaker();
case LIQUIFACE:
return new TileEntityPhantomLiquiface();
case ENERGYFACE:
return new TileEntityPhantomEnergyface();
case REDSTONEFACE:
return new TileEntityPhantomRedstoneface();
default:
return new TileEntityPhantomItemface();
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
public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
2021-02-26 22:15:48 +01:00
if (this.tryToggleRedstone(world, pos, player)) {
2021-03-01 20:14:50 +01:00
return ActionResultType.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
*/
2021-03-01 20:14:50 +01:00
return ActionResultType.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)
2021-02-27 21:24:26 +01:00
public void displayHud(MatrixStack matrices, Minecraft minecraft, PlayerEntity player, ItemStack stack, RayTraceResult rayCast, MainWindow resolution) {
2021-03-01 20:14:50 +01:00
if (!(rayCast instanceof BlockRayTraceResult)) {
return;
}
BlockPos pos = ((BlockRayTraceResult) rayCast).getBlockPos();
TileEntity tile = minecraft.level.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile != null) {
if (tile instanceof IPhantomTile) {
IPhantomTile phantom = (IPhantomTile) tile;
minecraft.font.drawShadow(matrices, TextFormatting.GOLD + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".blockPhantomRange.desc") + ": " + phantom.getRange(), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 - 40, StringUtil.DECIMAL_COLOR_WHITE);
2019-05-02 09:10:29 +02:00
if (phantom.hasBoundPosition()) {
2021-03-01 20:14:50 +01:00
int distance = MathHelper.ceil(new Vector3d(pos.getX(), pos.getY(), pos.getZ()).distanceTo(new Vector3d(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();
StringUtil.drawSplitString(minecraft.font, StringUtil.localizeFormatted("tooltip." + ActuallyAdditions.MODID + ".phantom.blockInfo.desc", name, phantom.getBoundPosition().getX(), phantom.getBoundPosition().getY(), phantom.getBoundPosition().getZ(), distance), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 - 30, 200, StringUtil.DECIMAL_COLOR_WHITE, true);
2019-05-02 09:10:29 +02:00
if (phantom.isBoundThingInRange()) {
StringUtil.drawSplitString(minecraft.font, TextFormatting.DARK_GREEN + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".phantom.connectedRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, StringUtil.DECIMAL_COLOR_WHITE, true);
2019-05-02 09:10:29 +02:00
} else {
StringUtil.drawSplitString(minecraft.font, TextFormatting.DARK_RED + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".phantom.connectedNoRange.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, 200, StringUtil.DECIMAL_COLOR_WHITE, true);
}
2019-05-02 09:10:29 +02:00
} else {
minecraft.font.drawShadow(matrices, TextFormatting.RED + StringUtil.localize("tooltip." + ActuallyAdditions.MODID + ".phantom.notConnected.desc"), resolution.getGuiScaledWidth() / 2 + 5, resolution.getGuiScaledHeight() / 2 + 25, StringUtil.DECIMAL_COLOR_WHITE);
}
}
}
}
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
}