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

194 lines
8.4 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;
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.lens.ILensItem;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
2017-07-16 19:34:21 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
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;
2021-02-27 21:24:26 +01:00
import de.ellpeck.actuallyadditions.mod.util.Help;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-07-07 17:59:45 +02:00
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-11-26 21:32:27 +01:00
import net.minecraft.block.Block;
2021-02-27 17:22:03 +01:00
import net.minecraft.block.BlockState;
2016-11-26 21:32:27 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2021-02-27 17:22:03 +01:00
import net.minecraft.client.MainWindow;
2015-12-21 22:28:57 +01:00
import net.minecraft.client.Minecraft;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2021-02-27 17:22:03 +01:00
import net.minecraft.item.BlockItemUseContext;
2016-05-05 00:06:02 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2021-02-27 17:22:03 +01:00
import net.minecraft.item.Items;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
2021-02-27 17:22:03 +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-02-27 17:22:03 +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-02-27 21:24:26 +01:00
import net.minecraft.util.text.ITextComponent;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
2021-02-27 17:22:03 +01:00
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
2021-02-26 22:15:48 +01:00
import net.minecraftforge.api.distmarker.Dist;
2021-02-27 17:22:03 +01:00
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ToolType;
2021-02-26 22:15:48 +01:00
2021-02-27 17:22:03 +01:00
import javax.annotation.Nullable;
2019-05-02 09:10:29 +02:00
public class BlockAtomicReconstructor extends BlockContainerBase 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() {
2021-02-27 17:22:03 +01:00
super(Properties.create(Material.ROCK).harvestTool(ToolType.PICKAXE).hardnessAndResistance(10F, 80F).sound(SoundType.STONE));
this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH));
}
2015-11-29 13:17:45 +01:00
@Override
2021-02-27 17:22:03 +01:00
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
2016-11-19 21:11:17 +01:00
ItemStack heldItem = player.getHeldItem(hand);
2021-02-26 22:15:48 +01:00
if (this.tryToggleRedstone(world, pos, player)) {
2021-02-27 17:22:03 +01:00
return ActionResultType.PASS;
2021-02-26 22:15:48 +01:00
}
2019-05-02 09:10:29 +02:00
if (!world.isRemote) {
TileEntityAtomicReconstructor reconstructor = (TileEntityAtomicReconstructor) world.getTileEntity(pos);
if (reconstructor != null) {
if (StackUtil.isValid(heldItem)) {
2016-05-05 00:06:02 +02:00
Item item = heldItem.getItem();
2019-05-02 09:10:29 +02:00
if (item instanceof ILensItem && !StackUtil.isValid(reconstructor.inv.getStackInSlot(0))) {
ItemStack toPut = heldItem.copy();
toPut.setCount(1);
reconstructor.inv.setStackInSlot(0, toPut);
player.inventory.decrStackSize(player.inventory.currentItem, 1);
2015-11-22 18:52:11 +01:00
}
2016-05-05 00:06:02 +02:00
//Shush, don't tell anyone!
2021-02-27 17:22:03 +01:00
else if (ConfigIntValues.ELEVEN.getValue() == 11 && item == Items.MUSIC_DISC_11) {
2016-05-05 00:06:02 +02:00
reconstructor.counter++;
reconstructor.markDirty();
}
2019-05-02 09:10:29 +02:00
} else {
ItemStack slot = reconstructor.inv.getStackInSlot(0);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(slot)) {
player.setHeldItem(hand, slot.copy());
reconstructor.inv.setStackInSlot(0, StackUtil.getEmpty());
}
2015-11-22 18:52:11 +01:00
}
2015-11-15 03:15:19 +01:00
}
}
2021-02-27 17:22:03 +01:00
return ActionResultType.PASS;
2015-11-15 03:15:19 +01:00
}
2015-11-22 20:23:54 +01:00
2021-02-27 17:22:03 +01:00
@Nullable
2015-12-01 19:48:09 +01:00
@Override
2021-02-27 17:22:03 +01:00
public TileEntity createNewTileEntity(IBlockReader worldIn) {
2015-12-01 19:48:09 +01:00
return new TileEntityAtomicReconstructor();
}
2021-02-27 17:22:03 +01:00
public BlockState getBaseConstructorState() {
return this.stateContainer.getBaseState().with(FACING, Direction.NORTH);
}
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
return this.getDefaultState().with(FACING, context.getNearestLookingDirection().getOpposite());
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(FACING);
}
2015-12-21 22:28:57 +01:00
@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) {
if (!(rayCast instanceof BlockRayTraceResult) || minecraft.world == null) {
return;
}
TileEntity tile = minecraft.world.getTileEntity(((BlockRayTraceResult) rayCast).getPos());
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityAtomicReconstructor) {
ItemStack slot = ((TileEntityAtomicReconstructor) tile).inv.getStackInSlot(0);
2021-02-27 21:24:26 +01:00
ITextComponent strg;
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(slot)) {
2021-02-27 21:24:26 +01:00
strg = Help.Trans("info", "nolens");
2019-05-02 09:10:29 +02:00
} else {
2021-02-27 21:24:26 +01:00
strg = slot.getItem().getDisplayName(slot);
2015-12-21 23:53:01 +01:00
2019-05-02 09:10:29 +02:00
AssetUtil.renderStackToGui(slot, resolution.getScaledWidth() / 2 + 15, resolution.getScaledHeight() / 2 - 19, 1F);
2015-12-21 22:28:57 +01:00
}
2021-02-27 21:24:26 +01:00
minecraft.fontRenderer.drawStringWithShadow(matrices, strg.copyRaw().mergeStyle(TextFormatting.YELLOW).mergeStyle(TextFormatting.ITALIC).getString(), resolution.getScaledWidth() / 2 + 35, resolution.getScaledHeight() / 2f - 15, StringUtil.DECIMAL_COLOR_WHITE);
2015-12-21 22:28:57 +01:00
}
}
2016-01-19 21:56:45 +01:00
2021-02-27 17:22:03 +01:00
// @Override
// public BlockState withRotation(BlockState state, Rotation rot) {
// return state.withProperty(BlockDirectional.FACING, rot.rotate(state.getValue(BlockDirectional.FACING)));
// }
//
// @Override
// public BlockState withMirror(BlockState state, Mirror mirror) {
// return this.withRotation(state, mirror.toRotation(state.getValue(BlockDirectional.FACING)));
// }
// public static class TheItemBlock extends ItemBlockBase {
//
// private long lastSysTime;
// private int toPick1;
// private int toPick2;
//
// public TheItemBlock(Block block) {
// super(block, new Item.Properties().group(ActuallyAdditions.GROUP).setNoRepair());
// }
//
// @Override
// public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
// long sysTime = System.currentTimeMillis();
//
// if (this.lastSysTime + 3000 < sysTime) {
// this.lastSysTime = sysTime;
// if (Minecraft.getInstance().world != null) {
// this.toPick1 = Minecraft.getInstance().world.rand.nextInt(NAME_FLAVOR_AMOUNTS_1) + 1;
// this.toPick2 = Minecraft.getInstance().world.rand.nextInt(NAME_FLAVOR_AMOUNTS_2) + 1;
// }
// }
//
// String base = "tile." + ActuallyAdditions.MODID + "." + ((BlockAtomicReconstructor) this.block).getBaseName() + ".info.";
// tooltip.add(StringUtil.localize(base + "1." + this.toPick1) + " " + StringUtil.localize(base + "2." + this.toPick2));
// }
// }
2019-02-27 19:53:05 +01:00
@Override
2021-02-26 22:15:48 +01:00
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}
@Override
2021-02-26 22:15:48 +01:00
public int getComparatorInputOverride(BlockState blockState, World world, BlockPos pos) {
2019-02-27 19:53:05 +01:00
TileEntity t = world.getTileEntity(pos);
int i = 0;
if (t instanceof TileEntityAtomicReconstructor) {
i = ((TileEntityAtomicReconstructor) t).getEnergy();
}
return MathHelper.clamp(i / 20000, 0, 15);
}
2021-02-26 22:15:48 +01:00
}