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

211 lines
8.3 KiB
Java
Raw Normal View History

2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.lens.ILensItem;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
2016-01-19 21:56:45 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase;
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;
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;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2016-11-21 16:23:48 +01:00
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
2015-12-21 22:28:57 +01:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.EntityLivingBase;
2015-11-15 03:15:19 +01:00
import net.minecraft.entity.player.EntityPlayer;
2016-05-05 00:06:02 +02:00
import net.minecraft.init.Items;
import net.minecraft.item.EnumRarity;
2016-05-05 00:06:02 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.EnumHand;
2016-11-21 16:23:48 +01:00
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
2019-05-02 09:10:29 +02:00
public class BlockAtomicReconstructor extends BlockContainerBase implements IHudDisplay {
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() {
super(Properties.create(Material.ROCK)
.hardnessAndResistance(10f, 80f)
.harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE));
}
2015-11-29 13:17:45 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean isOpaqueCube(IBlockState state) {
2015-11-29 13:17:45 +01:00
return false;
}
2015-11-15 03:15:19 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing par6, float par7, float par8, float par9) {
2016-11-19 21:11:17 +01:00
ItemStack heldItem = player.getHeldItem(hand);
2019-05-02 09:10:29 +02:00
if (this.tryToggleRedstone(world, pos, player)) { return true; }
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!
2019-05-02 09:10:29 +02:00
else if (ConfigIntValues.ELEVEN.getValue() == 11 && item == Items.RECORD_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
}
}
return true;
}
2015-11-22 20:23:54 +01:00
2015-12-01 19:48:09 +01:00
@Override
2019-05-02 09:10:29 +02:00
public TileEntity createNewTileEntity(World world, int i) {
2015-12-01 19:48:09 +01:00
return new TileEntityAtomicReconstructor();
}
2015-12-21 22:28:57 +01:00
@Override
2015-12-22 07:28:27 +01:00
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void displayHud(Minecraft minecraft, EntityPlayer player, ItemStack stack, RayTraceResult posHit, ScaledResolution resolution) {
2016-11-26 21:32:27 +01:00
TileEntity tile = minecraft.world.getTileEntity(posHit.getBlockPos());
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityAtomicReconstructor) {
ItemStack slot = ((TileEntityAtomicReconstructor) tile).inv.getStackInSlot(0);
2015-12-21 22:28:57 +01:00
String strg;
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(slot)) {
strg = StringUtil.localize("info." + ActuallyAdditions.MODID + ".noLens");
} else {
2015-12-21 22:28:57 +01:00
strg = slot.getItem().getItemStackDisplayName(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
}
2019-05-02 09:10:29 +02:00
minecraft.fontRenderer.drawStringWithShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + strg, resolution.getScaledWidth() / 2 + 35, resolution.getScaledHeight() / 2 - 15, StringUtil.DECIMAL_COLOR_WHITE);
2015-12-21 22:28:57 +01:00
}
}
2016-01-19 21:56:45 +01:00
@Override
2019-05-02 09:10:29 +02:00
protected ItemBlockBase getItemBlock() {
2016-04-20 21:39:03 +02:00
return new TheItemBlock(this);
2016-01-19 21:56:45 +01:00
}
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public EnumRarity getRarity(ItemStack stack) {
2016-02-01 17:49:55 +01:00
return EnumRarity.EPIC;
}
@Override
2019-05-02 09:10:29 +02:00
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack) {
2016-11-26 21:32:27 +01:00
int rotation = EnumFacing.getDirectionFromEntityLiving(pos, player).ordinal();
2016-07-04 20:15:41 +02:00
world.setBlockState(pos, this.getStateFromMeta(rotation), 2);
2016-02-01 17:49:55 +01:00
super.onBlockPlacedBy(world, pos, state, player, stack);
}
@Override
2019-05-02 09:10:29 +02:00
public IBlockState getStateFromMeta(int meta) {
2018-08-04 04:22:20 +02:00
return this.getDefaultState().withProperty(BlockDirectional.FACING, EnumFacing.byIndex(meta));
2016-11-21 16:23:48 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public int getMetaFromState(IBlockState state) {
2016-11-21 16:23:48 +01:00
return state.getValue(BlockDirectional.FACING).getIndex();
}
@Override
2019-05-02 09:10:29 +02:00
protected BlockStateContainer createBlockState() {
2016-11-21 16:23:48 +01:00
return new BlockStateContainer(this, BlockDirectional.FACING);
}
@Override
2019-05-02 09:10:29 +02:00
public IBlockState withRotation(IBlockState state, Rotation rot) {
2016-11-21 16:23:48 +01:00
return state.withProperty(BlockDirectional.FACING, rot.rotate(state.getValue(BlockDirectional.FACING)));
}
@Override
2019-05-02 09:10:29 +02:00
public IBlockState withMirror(IBlockState state, Mirror mirror) {
2016-11-21 16:23:48 +01:00
return this.withRotation(state, mirror.toRotation(state.getValue(BlockDirectional.FACING)));
2016-02-01 17:49:55 +01:00
}
2019-05-02 09:10:29 +02:00
public static class TheItemBlock extends ItemBlockBase {
2016-01-19 21:56:45 +01:00
private long lastSysTime;
private int toPick1;
private int toPick2;
2019-05-02 09:10:29 +02:00
public TheItemBlock(Block block) {
2016-01-19 21:56:45 +01:00
super(block);
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
@Override
2019-05-02 09:10:29 +02:00
public String getTranslationKey(ItemStack stack) {
2018-08-04 04:22:20 +02:00
return this.getTranslationKey();
2016-01-19 21:56:45 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public int getMetadata(int damage) {
2016-01-19 21:56:45 +01:00
return damage;
}
@Override
2019-05-02 09:10:29 +02:00
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag advanced) {
2016-01-19 21:56:45 +01:00
long sysTime = System.currentTimeMillis();
2019-05-02 09:10:29 +02:00
if (this.lastSysTime + 3000 < sysTime) {
2016-01-19 21:56:45 +01:00
this.lastSysTime = sysTime;
2019-05-02 09:10:29 +02:00
if (world != null) {
this.toPick1 = world.rand.nextInt(NAME_FLAVOR_AMOUNTS_1) + 1;
this.toPick2 = world.rand.nextInt(NAME_FLAVOR_AMOUNTS_2) + 1;
2017-06-17 00:48:49 +02:00
}
2016-01-19 21:56:45 +01:00
}
2019-05-02 09:10:29 +02:00
String base = "tile." + ActuallyAdditions.MODID + "." + ((BlockAtomicReconstructor) this.block).getBaseName() + ".info.";
tooltip.add(StringUtil.localize(base + "1." + this.toPick1) + " " + StringUtil.localize(base + "2." + this.toPick2));
2016-01-19 21:56:45 +01:00
}
}
2019-02-27 19:53:05 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean hasComparatorInputOverride(IBlockState state) {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorInputOverride(IBlockState 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);
}
}