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

211 lines
8 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;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.lens.ILensItem;
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.ModUtil;
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.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-01-19 21:56:45 +01:00
import java.util.List;
2015-12-21 22:28:57 +01: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(String name){
2016-04-20 21:39:03 +02:00
super(Material.ROCK, name);
this.setHarvestLevel("pickaxe", 0);
this.setHardness(10F);
this.setResistance(80F);
2016-04-20 21:39:03 +02:00
this.setSoundType(SoundType.STONE);
}
2015-11-29 13:17:45 +01:00
@Override
2016-03-18 23:47:22 +01: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
2016-11-19 21:11:17 +01:00
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing par6, float par7, float par8, float par9){
ItemStack heldItem = player.getHeldItem(hand);
2016-01-08 13:31:58 +01:00
if(this.tryToggleRedstone(world, pos, player)){
return true;
}
2015-11-15 03:15:19 +01:00
if(!world.isRemote){
TileEntityAtomicReconstructor reconstructor = (TileEntityAtomicReconstructor)world.getTileEntity(pos);
2015-11-15 03:15:19 +01:00
if(reconstructor != null){
if(StackUtil.isValid(heldItem)){
2016-05-05 00:06:02 +02:00
Item item = heldItem.getItem();
if(item instanceof ILensItem && !StackUtil.isValid(reconstructor.slots.getStackInSlot(0))){
ItemStack toPut = heldItem.copy();
toPut = StackUtil.setStackSize(toPut, 1);
reconstructor.slots.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!
2017-07-16 19:34:21 +02:00
else if(ConfigIntValues.ELEVEN.getValue() == 11 && item == Items.RECORD_11){
2016-05-05 00:06:02 +02:00
reconstructor.counter++;
reconstructor.markDirty();
}
2015-11-22 18:52:11 +01:00
}
else{
ItemStack slot = reconstructor.slots.getStackInSlot(0);
if(StackUtil.isValid(slot)){
player.setHeldItem(hand, slot.copy());
reconstructor.slots.setStackInSlot(0, StackUtil.getNull());
}
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
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)
2016-07-25 22:37:14 +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());
2015-12-21 22:28:57 +01:00
if(tile instanceof TileEntityAtomicReconstructor){
ItemStack slot = ((TileEntityAtomicReconstructor)tile).slots.getStackInSlot(0);
2015-12-21 22:28:57 +01:00
String strg;
if(!StackUtil.isValid(slot)){
2016-04-20 21:39:03 +02:00
strg = StringUtil.localize("info."+ModUtil.MOD_ID+".noLens");
2015-12-21 22:28:57 +01:00
}
else{
strg = slot.getItem().getItemStackDisplayName(slot);
2015-12-21 23:53:01 +01:00
AssetUtil.renderStackToGui(slot, resolution.getScaledWidth()/2+15, resolution.getScaledHeight()/2-19, 1F);
2015-12-21 22:28:57 +01: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
2016-04-20 21:39:03 +02:00
protected ItemBlockBase getItemBlock(){
return new TheItemBlock(this);
2016-01-19 21:56:45 +01:00
}
2016-02-01 17:49:55 +01:00
@Override
public EnumRarity getRarity(ItemStack stack){
return EnumRarity.EPIC;
}
@Override
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
2016-11-21 16:23:48 +01:00
public IBlockState getStateFromMeta(int meta){
return this.getDefaultState().withProperty(BlockDirectional.FACING, EnumFacing.getFront(meta));
}
@Override
public int getMetaFromState(IBlockState state){
return state.getValue(BlockDirectional.FACING).getIndex();
}
@Override
protected BlockStateContainer createBlockState(){
return new BlockStateContainer(this, BlockDirectional.FACING);
}
@Override
public IBlockState withRotation(IBlockState state, Rotation rot){
return state.withProperty(BlockDirectional.FACING, rot.rotate(state.getValue(BlockDirectional.FACING)));
}
@Override
public IBlockState withMirror(IBlockState state, Mirror mirror){
return this.withRotation(state, mirror.toRotation(state.getValue(BlockDirectional.FACING)));
2016-02-01 17:49:55 +01:00
}
2016-01-19 21:56:45 +01:00
public static class TheItemBlock extends ItemBlockBase{
private long lastSysTime;
private int toPick1;
private int toPick2;
public TheItemBlock(Block block){
super(block);
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
2016-01-19 21:56:45 +01:00
@Override
public String getUnlocalizedName(ItemStack stack){
return this.getUnlocalizedName();
}
@Override
public int getMetadata(int damage){
return damage;
}
@Override
2017-06-17 00:48:49 +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();
if(this.lastSysTime+3000 < sysTime){
this.lastSysTime = sysTime;
2017-06-17 00:48:49 +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;
}
2016-01-19 21:56:45 +01:00
}
2016-04-20 21:39:03 +02:00
String base = "tile."+ModUtil.MOD_ID+"."+((BlockAtomicReconstructor)this.block).getBaseName()+".info.";
2017-06-17 00:48:49 +02:00
tooltip.add(StringUtil.localize(base+"1."+this.toPick1)+" "+StringUtil.localize(base+"2."+this.toPick2));
2016-01-19 21:56:45 +01:00
}
}
}