2017-03-21 18:12:09 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Industrial Wires.
|
|
|
|
* Copyright (C) 2016-2017 malte0811
|
|
|
|
*
|
|
|
|
* Industrial Wires is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Industrial Wires is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package malte0811.industrialWires.blocks;
|
|
|
|
|
|
|
|
import blusunrize.immersiveengineering.api.IEProperties;
|
2017-03-28 18:15:41 +02:00
|
|
|
import blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable;
|
|
|
|
import blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler;
|
2017-03-21 18:12:09 +01:00
|
|
|
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces;
|
|
|
|
import blusunrize.immersiveengineering.common.util.Utils;
|
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
2017-03-28 18:15:41 +02:00
|
|
|
import malte0811.industrialWires.util.MiscUtils;
|
2017-03-21 18:12:09 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.material.Material;
|
|
|
|
import net.minecraft.block.properties.IProperty;
|
|
|
|
import net.minecraft.block.state.BlockStateContainer;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2017-04-01 17:33:58 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
2017-06-09 11:37:23 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2017-03-21 18:12:09 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2017-06-30 21:04:02 +02:00
|
|
|
import net.minecraft.item.ItemBlock;
|
2017-03-21 18:12:09 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.IBlockAccess;
|
|
|
|
import net.minecraft.world.World;
|
2017-03-28 18:15:41 +02:00
|
|
|
import net.minecraftforge.common.property.IExtendedBlockState;
|
2017-03-21 18:12:09 +01:00
|
|
|
|
2017-05-10 17:56:05 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2017-06-09 11:37:23 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-03-21 18:12:09 +01:00
|
|
|
import java.util.Arrays;
|
2017-06-09 11:37:23 +02:00
|
|
|
import java.util.List;
|
2017-03-28 18:15:41 +02:00
|
|
|
import java.util.Set;
|
2017-03-21 18:12:09 +01:00
|
|
|
|
|
|
|
public abstract class BlockIWBase extends Block {
|
|
|
|
private IProperty[] properties;
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-21 18:12:09 +01:00
|
|
|
public BlockIWBase(Material mat, String name) {
|
|
|
|
super(mat);
|
|
|
|
setHardness(3.0F);
|
|
|
|
setResistance(15.0F);
|
2017-05-11 16:39:20 +02:00
|
|
|
setUnlocalizedName(IndustrialWires.MODID + "." + name);
|
2017-07-09 18:10:06 +02:00
|
|
|
setRegistryName(IndustrialWires.MODID, name);
|
2017-03-21 18:12:09 +01:00
|
|
|
setCreativeTab(IndustrialWires.creativeTab);
|
2017-07-09 18:10:06 +02:00
|
|
|
IndustrialWires.blocks.add(this);
|
2017-03-21 18:12:09 +01:00
|
|
|
}
|
|
|
|
|
2017-07-09 18:10:06 +02:00
|
|
|
public ItemBlock createItemBlock() {
|
2017-06-30 21:04:02 +02:00
|
|
|
return new ItemBlockIW(this);
|
2017-03-21 18:12:09 +01:00
|
|
|
}
|
|
|
|
|
2017-05-10 17:56:05 +02:00
|
|
|
@Nonnull
|
2017-03-21 18:12:09 +01:00
|
|
|
@Override
|
|
|
|
protected BlockStateContainer createBlockState() {
|
2017-05-11 16:39:20 +02:00
|
|
|
if (properties == null) {
|
2017-03-21 18:12:09 +01:00
|
|
|
properties = getProperties();
|
|
|
|
}
|
|
|
|
BlockStateContainer cont = super.createBlockState();
|
2017-04-17 19:35:14 +02:00
|
|
|
IProperty<?>[] props = cont.getProperties().toArray(new IProperty[0]);
|
2017-03-21 18:12:09 +01:00
|
|
|
int oldLength = props.length;
|
|
|
|
props = Arrays.copyOf(props, oldLength + properties.length);
|
|
|
|
System.arraycopy(properties, 0, props, oldLength, properties.length);
|
|
|
|
return new BlockStateContainer(this, props);
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:56:05 +02:00
|
|
|
@Nonnull
|
2017-03-21 18:12:09 +01:00
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public IBlockState getActualState(@Nonnull IBlockState state, IBlockAccess worldIn, BlockPos pos) {
|
2017-03-21 18:12:09 +01:00
|
|
|
state = super.getActualState(state, worldIn, pos);
|
|
|
|
TileEntity tile = worldIn.getTileEntity(pos);
|
|
|
|
if (tile instanceof IHasDummyBlocksIW) {
|
|
|
|
state = applyProperty(state, IEProperties.MULTIBLOCKSLAVE, ((IHasDummyBlocksIW) tile).isDummy());
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
if (tile instanceof IEBlockInterfaces.IDirectionalTile && ((IEBlockInterfaces.IDirectionalTile) tile).getFacingLimitation() >= 0) {
|
|
|
|
if (((IEBlockInterfaces.IDirectionalTile) tile).getFacingLimitation() == 2) {
|
2017-03-24 17:40:05 +01:00
|
|
|
state = state.withProperty(IEProperties.FACING_HORIZONTAL, ((IEBlockInterfaces.IDirectionalTile) tile).getFacing());
|
|
|
|
} else {
|
|
|
|
state = state.withProperty(IEProperties.FACING_ALL, ((IEBlockInterfaces.IDirectionalTile) tile).getFacing());
|
|
|
|
}
|
2017-03-21 18:12:09 +01:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:56:05 +02:00
|
|
|
@Nonnull
|
2017-03-28 18:15:41 +02:00
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public IBlockState getExtendedState(@Nonnull IBlockState state, IBlockAccess world, BlockPos pos) {
|
2017-03-28 18:15:41 +02:00
|
|
|
state = super.getExtendedState(state, world, pos);
|
|
|
|
if (state instanceof IExtendedBlockState) {
|
|
|
|
TileEntity te = world.getTileEntity(pos);
|
2017-04-01 17:33:58 +02:00
|
|
|
if (te instanceof IImmersiveConnectable) {
|
|
|
|
Set<ImmersiveNetHandler.Connection> conns = ImmersiveNetHandler.INSTANCE.getConnections(te.getWorld(), pos);
|
|
|
|
state = ((IExtendedBlockState) state).withProperty(IEProperties.CONNECTIONS, MiscUtils.genConnBlockstate(conns, te.getWorld()));
|
2017-03-28 18:15:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-05-11 16:39:20 +02:00
|
|
|
protected <V extends Comparable<V>> IBlockState applyProperty(IBlockState in, IProperty<V> prop, V val) {
|
2017-03-21 18:12:09 +01:00
|
|
|
return in.withProperty(prop, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public void breakBlock(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
|
2017-03-21 18:12:09 +01:00
|
|
|
TileEntity te = worldIn.getTileEntity(pos);
|
|
|
|
if (te instanceof IHasDummyBlocksIW) {
|
|
|
|
((IHasDummyBlocksIW) te).breakDummies();
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
if (te instanceof IImmersiveConnectable) {
|
|
|
|
if (!worldIn.isRemote || !Minecraft.getMinecraft().isSingleplayer())
|
|
|
|
ImmersiveNetHandler.INSTANCE.clearAllConnectionsFor(Utils.toCC(te), worldIn, !worldIn.isRemote && worldIn.getGameRules().getBoolean("doTileDrops"));
|
2017-04-01 17:33:58 +02:00
|
|
|
}
|
2017-03-21 18:12:09 +01:00
|
|
|
super.breakBlock(worldIn, pos, state);
|
|
|
|
worldIn.removeTileEntity(pos);
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:56:05 +02:00
|
|
|
@Nonnull
|
2017-03-21 18:12:09 +01:00
|
|
|
@Override
|
|
|
|
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
|
|
|
|
TileEntity te = source.getTileEntity(pos);
|
|
|
|
if (te instanceof IBlockBoundsIW) {
|
|
|
|
AxisAlignedBB ret = ((IBlockBoundsIW) te).getBoundingBox();
|
2017-05-11 16:39:20 +02:00
|
|
|
if (ret != null) {
|
2017-03-21 18:12:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getBoundingBox(state, source, pos);
|
|
|
|
}
|
|
|
|
|
2017-06-09 11:37:23 +02:00
|
|
|
@Override
|
|
|
|
public void addCollisionBoxToList(IBlockState state, @Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull AxisAlignedBB entityBox,
|
|
|
|
@Nonnull List<AxisAlignedBB> collidingBoxes, @Nullable Entity entityIn, boolean p_185477_7_) {
|
|
|
|
AxisAlignedBB aabb = getBoundingBox(state, worldIn, pos).offset(pos);
|
2017-08-07 22:03:07 +02:00
|
|
|
if (entityBox.intersects(aabb)) {
|
2017-06-09 11:37:23 +02:00
|
|
|
collidingBoxes.add(aabb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:12:09 +01:00
|
|
|
//mostly copied from IE
|
|
|
|
@Override
|
|
|
|
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player,
|
2017-05-10 17:56:05 +02:00
|
|
|
EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
|
2017-03-21 18:12:09 +01:00
|
|
|
TileEntity te = world.getTileEntity(pos);
|
2017-05-10 17:56:05 +02:00
|
|
|
ItemStack heldItem = player.getHeldItem(hand);
|
2017-05-11 16:39:20 +02:00
|
|
|
if (te instanceof IEBlockInterfaces.IDirectionalTile && Utils.isHammer(heldItem) && !world.isRemote) {
|
2017-03-21 18:12:09 +01:00
|
|
|
IEBlockInterfaces.IDirectionalTile directionalTe = (IEBlockInterfaces.IDirectionalTile) te;
|
|
|
|
if (directionalTe.canHammerRotate(side, hitX, hitY, hitZ, player)) {
|
|
|
|
EnumFacing f = directionalTe.getFacing();
|
|
|
|
final EnumFacing original = f;
|
|
|
|
int limit = directionalTe.getFacingLimitation();
|
|
|
|
|
2017-05-11 16:39:20 +02:00
|
|
|
if (limit == 0) {
|
2017-03-21 18:12:09 +01:00
|
|
|
f = EnumFacing.VALUES[(f.ordinal() + 1) % EnumFacing.VALUES.length];
|
2017-05-11 16:39:20 +02:00
|
|
|
} else if (limit == 1) {
|
|
|
|
f = player.isSneaking() ? f.rotateAround(side.getAxis()).getOpposite() : f.rotateAround(side.getAxis());
|
|
|
|
} else if (limit == 2 || limit == 5) {
|
|
|
|
f = player.isSneaking() ? f.rotateYCCW() : f.rotateY();
|
2017-03-21 18:12:09 +01:00
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
if (f != original) {
|
2017-03-21 18:12:09 +01:00
|
|
|
directionalTe.setFacing(f);
|
|
|
|
te.markDirty();
|
2017-05-11 16:39:20 +02:00
|
|
|
world.notifyBlockUpdate(pos, state, state, 3);
|
2017-03-21 18:12:09 +01:00
|
|
|
world.addBlockEvent(pos, this, 255, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (te instanceof IEBlockInterfaces.IPlayerInteraction) {
|
2017-03-24 17:40:05 +01:00
|
|
|
if (((IEBlockInterfaces.IPlayerInteraction) te).interact(side, player, hand, heldItem, hitX, hitY, hitZ)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-21 18:12:09 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int id, int param) {
|
|
|
|
boolean def = super.eventReceived(state, worldIn, pos, id, param);
|
2017-05-11 16:39:20 +02:00
|
|
|
if ((id & 255) == 255) {
|
2017-03-21 18:12:09 +01:00
|
|
|
IBlockState s = worldIn.getBlockState(pos);
|
|
|
|
worldIn.notifyBlockUpdate(pos, s, s, 3);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:59:57 +02:00
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public void harvestBlock(@Nonnull World worldIn, EntityPlayer player, @Nonnull BlockPos pos, @Nonnull IBlockState state, TileEntity te, ItemStack stack) {
|
2017-04-09 16:59:57 +02:00
|
|
|
if (te instanceof IEBlockInterfaces.ITileDrop) {
|
|
|
|
ItemStack drop = ((IEBlockInterfaces.ITileDrop) te).getTileDrop(player, state);
|
2017-05-10 17:56:05 +02:00
|
|
|
if (!drop.isEmpty()) {
|
2017-04-09 16:59:57 +02:00
|
|
|
spawnAsEntity(worldIn, pos, drop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.harvestBlock(worldIn, player, pos, state, te, stack);
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:14:05 +02:00
|
|
|
@Override
|
2017-05-10 17:56:05 +02:00
|
|
|
public boolean rotateBlock(World world, @Nonnull BlockPos pos, @Nonnull EnumFacing axis) {
|
2017-04-13 17:14:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-24 17:26:42 +02:00
|
|
|
@Override
|
|
|
|
public int damageDropped(IBlockState state) {
|
|
|
|
return getMetaFromState(state);
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-03-21 18:12:09 +01:00
|
|
|
protected abstract IProperty[] getProperties();
|
|
|
|
}
|