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

182 lines
7.5 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;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockItemBase;
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.BlockState;
2016-11-26 21:32:27 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.client.MainWindow;
2015-12-21 22:28:57 +01:00
import net.minecraft.client.Minecraft;
2017-06-17 00:48:49 +02:00
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
2016-05-05 00:06:02 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.Rarity;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
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.BlockRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ToolType;
import javax.annotation.Nullable;
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-15 03:15:19 +01:00
@Override
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);
if (this.tryToggleRedstone(world, pos, player)) {
return ActionResultType.SUCCESS;
}
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!
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
}
}
return ActionResultType.SUCCESS;
2015-11-15 03:15:19 +01:00
}
2015-12-01 19:48:09 +01:00
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world){
2015-12-01 19:48:09 +01:00
return new TileEntityAtomicReconstructor();
}
2015-12-21 22:28:57 +01:00
@Override
@OnlyIn(Dist.CLIENT)
public void displayHud(Minecraft minecraft, PlayerEntity player, ItemStack stack, BlockRayTraceResult posHit, MainWindow window) {
TileEntity tile = minecraft.world.getTileEntity(posHit.getPos());
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityAtomicReconstructor) {
ItemStack slot = ((TileEntityAtomicReconstructor) tile).inv.getStackInSlot(0);
ITextComponent displayString;
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(slot)) {
displayString = new TranslationTextComponent("info." + ActuallyAdditions.MODID + ".noLens");
2019-05-02 09:10:29 +02:00
} else {
displayString = slot.getItem().getDisplayName(slot);
2015-12-21 23:53:01 +01:00
AssetUtil.renderStackToGui(slot, window.getScaledWidth() / 2 + 15, window.getScaledHeight() / 2 - 19, 1F);
2015-12-21 22:28:57 +01:00
}
minecraft.fontRenderer.drawStringWithShadow(TextFormatting.YELLOW + "" + TextFormatting.ITALIC + displayString.getFormattedText(), window.getScaledWidth() / 2 + 35, window.getScaledHeight() / 2 - 15, StringUtil.DECIMAL_COLOR_WHITE);
2015-12-21 22:28:57 +01:00
}
}
2016-01-19 21:56:45 +01:00
@Override
protected BlockItemBase getItemBlock() {
return new BlockItem();
2016-01-19 21:56:45 +01:00
}
2016-02-01 17:49:55 +01:00
@Override
public Rarity getRarity(){
return Rarity.EPIC;
2016-02-01 17:49:55 +01:00
}
2016-02-01 17:49:55 +01:00
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder){
builder.add(BlockStateProperties.FACING);
2016-11-21 16:23:48 +01:00
}
2016-11-21 16:23:48 +01:00
@Override
public BlockState rotate(BlockState state, Rotation rot){
return state.with(BlockStateProperties.FACING, rot.rotate(state.get(BlockStateProperties.FACING)));
2016-11-21 16:23:48 +01:00
}
2016-11-21 16:23:48 +01:00
@Override
public BlockState mirror(BlockState state, Mirror mirrorIn){
return this.rotate(state, mirrorIn.toRotation(state.get(BlockStateProperties.FACING)));
2016-11-21 16:23:48 +01:00
}
2016-11-21 16:23:48 +01:00
@Override
public boolean hasComparatorInputOverride(BlockState state){
return true;
2016-11-21 16:23:48 +01:00
}
2016-11-21 16:23:48 +01:00
@Override
public int getComparatorInputOverride(BlockState blockState, World world, BlockPos pos) {
TileEntity t = world.getTileEntity(pos);
int i = 0;
if (t instanceof TileEntityAtomicReconstructor) {
i = ((TileEntityAtomicReconstructor) t).getEnergy();
}
return MathHelper.clamp(i / 20000, 0, 15);
2016-02-01 17:49:55 +01:00
}
public class BlockItem extends BlockItemBase {
2016-01-19 21:56:45 +01:00
private long lastSysTime;
private int toPick1;
2016-01-19 21:56:45 +01:00
private int toPick2;
public BlockItem() {
super(BlockAtomicReconstructor.this, new Properties());
2016-01-19 21:56:45 +01:00
}
2016-01-19 21:56:45 +01:00
@Override
public void addInformation(ItemStack stack, World world, List<ITextComponent> tooltip, ITooltipFlag flag){
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
}
String base = String.format("tile.%s.%s.info", ActuallyAdditions.MODID, BlockAtomicReconstructor.this.getRegistryName().getPath());
tooltip.add(new TranslationTextComponent(String.format("%s1.%s", base, this.toPick1)).appendSibling(new TranslationTextComponent(String.format("%s2.%s", base, this.toPick2))));
2016-01-19 21:56:45 +01:00
}
2019-02-27 19:53:05 +01:00
}
}