Initial work on standalone cables.

This commit is contained in:
modmuss50 2017-05-19 13:12:02 +01:00
parent 50d1db2695
commit d474870977
No known key found for this signature in database
GPG key ID: 203A5ED4D3E48BEA
7 changed files with 518 additions and 0 deletions

View file

@ -0,0 +1,169 @@
package techreborn.blocks.cable;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.energy.CapabilityEnergy;
import techreborn.client.TechRebornCreativeTab;
import techreborn.parts.powerCables.EnumCableType;
import techreborn.tiles.cable.TileCable;
import javax.annotation.Nullable;
/**
* Created by modmuss50 on 19/05/2017.
*/
public class BlockCable extends BlockContainer {
public static final PropertyBool EAST = PropertyBool.create("east");
public static final PropertyBool WEST = PropertyBool.create("west");
public static final PropertyBool NORTH = PropertyBool.create("north");
public static final PropertyBool SOUTH = PropertyBool.create("south");
public static final PropertyBool UP = PropertyBool.create("up");
public static final PropertyBool DOWN = PropertyBool.create("down");
public static final IProperty<EnumCableType> TYPE = PropertyEnum.create("type", EnumCableType.class);
public BlockCable() {
super(Material.ROCK);
setCreativeTab(TechRebornCreativeTab.instance);
this.setUnlocalizedName("techreborn.cable");
setDefaultState(getDefaultState().withProperty(EAST, false).withProperty(WEST, false).withProperty(NORTH, false).withProperty(SOUTH, false).withProperty(UP, false).withProperty(DOWN, false).withProperty(TYPE, EnumCableType.COPPER));
}
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
return new ItemStack(this, 1, 0);
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(getDefaultState());
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@Override
public boolean isFullBlock(IBlockState state) {
return false;
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, EAST, WEST, NORTH, SOUTH, UP, DOWN, TYPE);
}
@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
return getStateFromMeta(placer.getHeldItem(hand).getItemDamage());
}
@Override
public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list) {
for(EnumCableType cableType : EnumCableType.values()){
list.add(new ItemStack(this, 1, cableType.ordinal()));
}
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(TYPE).ordinal();
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(TYPE, EnumCableType.values()[meta]);
}
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
state = state.getActualState(source, pos);
float minX = state.getValue(WEST) ? 0.0F : 0.3125F;
float minY = state.getValue(DOWN) ? 0.0F : 0.3125F;
float minZ = state.getValue(NORTH) ? 0.0F : 0.3125F;
float maxX = state.getValue(EAST) ? 1.0F : 0.6875F;
float maxY = state.getValue(UP) ? 1.0F : 0.6875F;
float maxZ = state.getValue(SOUTH) ? 1.0F : 0.6875F;
return new AxisAlignedBB((double) minX, (double) minY, (double) minZ, (double) maxX, (double) maxY, (double) maxZ);
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
IBlockState actualState = state;
for (EnumFacing facing : EnumFacing.values()) {
TileEntity tileEntity = getTileEntitySafely(worldIn, pos.offset(facing));
if(tileEntity != null){
actualState = actualState.withProperty(getProperty(facing), tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing.getOpposite()));
}
}
return actualState;
}
//see for more info https://www.reddit.com/r/feedthebeast/comments/5mxwq9/psa_mod_devs_do_you_call_worldgettileentity_from/
public TileEntity getTileEntitySafely(IBlockAccess blockAccess, BlockPos pos) {
if (blockAccess instanceof ChunkCache) {
return ((ChunkCache) blockAccess).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK);
} else {
return blockAccess.getTileEntity(pos);
}
}
public IProperty<Boolean> getProperty(EnumFacing facing) {
switch (facing) {
case EAST:
return EAST;
case WEST:
return WEST;
case NORTH:
return NORTH;
case SOUTH:
return SOUTH;
case UP:
return UP;
case DOWN:
return DOWN;
default:
return EAST;
}
}
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileCable();
}
@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.MODEL;
}
}

View file

@ -36,6 +36,7 @@ import reborncore.common.util.StringUtils;
import techreborn.Core;
import techreborn.blocks.*;
import techreborn.blocks.advanced_machine.*;
import techreborn.blocks.cable.BlockCable;
import techreborn.blocks.generator.*;
import techreborn.blocks.iron_machines.BlockAlloyFurnace;
import techreborn.blocks.iron_machines.BlockIronFurnace;
@ -47,6 +48,7 @@ import techreborn.blocks.transformers.BlockLVTransformer;
import techreborn.blocks.transformers.BlockMVTransformer;
import techreborn.itemblocks.*;
import techreborn.tiles.*;
import techreborn.tiles.cable.TileCable;
import techreborn.tiles.fusionReactor.TileEntityFusionController;
import techreborn.tiles.generator.*;
import techreborn.tiles.idsu.TileIDSU;
@ -135,6 +137,7 @@ public class ModBlocks {
public static Block REFINED_IRON_FENCE;
public static Block FLARE;
public static Block CABLE;
public static void init() {
THERMAL_GENERATOR = new BlockThermalGenerator();
@ -206,6 +209,11 @@ public class ModBlocks {
registerBlock(PLAYER_DETECTOR, ItemBlockPlayerDetector.class, "playerDetector");
GameRegistry.registerTileEntity(TilePlayerDectector.class, "TilePlayerDectectorTR");
CABLE = new BlockCable();
registerBlock(CABLE,"cable");
GameRegistry.registerTileEntity(TileCable.class, "TileCableTR");
Core.proxy.registerCustomBlockStateLocation(CABLE, "cable");
MACHINE_CASINGS = new BlockMachineCasing(Material.ROCK);
registerBlock(MACHINE_CASINGS, ItemBlockMachineCasing.class, "machinecasing");
GameRegistry.registerTileEntity(TileMachineCasing.class, "TileMachineCasingTR");

View file

@ -0,0 +1,117 @@
package techreborn.tiles.cable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import reborncore.common.RebornCoreConfig;
import techreborn.blocks.cable.BlockCable;
import techreborn.parts.powerCables.EnumCableType;
/**
* Created by modmuss50 on 19/05/2017.
*/
public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
public int power = 0;
@Override
public void update() {
if(world.isRemote){
return;
}
for (EnumFacing face : EnumFacing.VALUES) {
BlockPos offPos = getPos().offset(face);
TileEntity tile = getWorld().getTileEntity(offPos);
if (tile == null) {
continue;
}
if (tile instanceof TileCable) {
TileCable cable = (TileCable) tile;
int averPower = (power + cable.power) / 2;
cable.power = averPower;
if (averPower % 2 != 0 && power != 0) {
averPower++;
}
power = averPower;
} else if (tile.hasCapability(CapabilityEnergy.ENERGY, face.getOpposite())) {
IEnergyStorage energy = tile.getCapability(CapabilityEnergy.ENERGY, face.getOpposite());
if (energy.canReceive()) {
int move = energy.receiveEnergy(Math.min(getCableType().transferRate, power), false);
if (move != 0) {
power -= move;
}
}
}
}
}
private EnumCableType getCableType(){
//Todo cache this
return world.getBlockState(pos).getValue(BlockCable.TYPE);
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
if (!canReceive()){
return 0;
}
int energyReceived = Math.min(getMaxEnergyStored() - power, Math.min(getCableType().transferRate * RebornCoreConfig.euPerFU, maxReceive));
if (!simulate)
power += energyReceived;
return energyReceived;
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
if (!canExtract()){
return 0;
}
int energyExtracted = Math.min(power, Math.min(getCableType().transferRate * RebornCoreConfig.euPerFU, maxExtract));
if (!simulate)
power -= energyExtracted;
return energyExtracted;
}
@Override
public int getEnergyStored() {
return power;
}
@Override
public int getMaxEnergyStored() {
return getCableType().transferRate * 2;
}
@Override
public boolean canExtract() {
return true;
}
@Override
public boolean canReceive() {
return true;
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
if(capability == CapabilityEnergy.ENERGY){
return true;
}
return super.hasCapability(capability, facing);
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if(capability == CapabilityEnergy.ENERGY){
return (T) this;
}
return super.getCapability(capability, facing);
}
}

View file

@ -0,0 +1,101 @@
{
"forge_marker": 1,
"defaults": {
"transform": "forge:default-block",
"model": "techreborn:cable",
"textures": {
"cable": "techreborn:block/cable",
"particle": "#cable"
}
},
"variants": {
"inventory": [
{
"transform": "forge:default-block",
"model": "techreborn:cable",
"textures": {
}
}
],
"down": {
"true": {
"submodel": {
"pipeDown": {
"model": "techreborn:cable_connection_alt",
"x": 270
}
}
},
"false": {
}
},
"east": {
"true": {
"submodel": "techreborn:cable_connection",
"y": 90
},
"false": {
}
},
"north": {
"true": {
"submodel": "techreborn:cable_connection"
},
"false": {
}
},
"south": {
"true": {
"submodel": "techreborn:cable_connection_alt"
},
"false": {
}
},
"up": {
"true": {
"submodel": {
"pipeUp": {
"model": "techreborn:cable_connection",
"x": 270
}
}
},
"false": {
}
},
"west": {
"true": {
"submodel": "techreborn:cable_connection_alt",
"y": 90
},
"false": {
}
},
"type": {
"copper" : {
},
"tin" : {
},
"gold" : {
},
"hv" : {
},
"glassfiber" : {
},
"insulatedcopper" : {
},
"insulatedgold" : {
},
"insulatedhv" : {
}
}
}
}

View file

@ -0,0 +1,21 @@
{
"__comment": "Model generated using MrCrayfish's Model Creator (http://mrcrayfish.com/modelcreator/)",
"textures": {
"0": "#cable"
},
"elements": [
{
"name": "Cable",
"from": [ 5.0, 5.0, 5.0 ],
"to": [ 11.0, 11.0, 11.0 ],
"faces": {
"north": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] },
"east": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] },
"south": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] },
"west": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] },
"up": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] },
"down": { "texture": "#0", "uv": [ 0.0, 0.0, 6.0, 6.0 ] }
}
}
]
}

View file

@ -0,0 +1,21 @@
{
"__comment": "Model generated using MrCrayfish's Model Creator (http://mrcrayfish.com/modelcreator/)",
"textures": {
"0": "#cable"
},
"elements": [
{
"name": "Cube",
"from": [ 5.0, 5.0, 0.0 ],
"to": [ 11.0, 11.0, 5.0 ],
"faces": {
"north": { "texture": "#-1", "uv": [ 0.0, 0.0, 6.0, 6.0 ], "cullface": "north" },
"east": { "texture": "#0", "uv": [ 0.0, 6.0, 5.0, 12.0 ] },
"south": { "texture": "#-1", "uv": [ 0.0, 0.0, 6.0, 6.0 ], "cullface": "south" },
"west": { "texture": "#0", "uv": [ 0.0, 6.0, 5.0, 12.0 ] },
"up": { "texture": "#0", "uv": [ 0.0, 6.0, 5.0, 12.0 ], "rotation": 90 },
"down": { "texture": "#0", "uv": [ 0.0, 6.0, 5.0, 12.0 ], "rotation": 90 }
}
}
]
}

View file

@ -0,0 +1,81 @@
{
"__comment": "Model generated using MrCrayfish's Model Creator (http://mrcrayfish.com/modelcreator/)",
"textures": {
"0": "#cable"
},
"elements": [
{
"name": "Cube",
"from": [
5.0,
5.0,
11.0
],
"to": [
11.0,
11.0,
16.0
],
"faces": {
"north": {
"texture": "#-1",
"uv": [
0.0,
0.0,
6.0,
6.0
],
"cullface": "north"
},
"east": {
"texture": "#0",
"uv": [
1.0,
6.0,
6.0,
12.0
]
},
"south": {
"texture": "#-1",
"uv": [
0.0,
0.0,
6.0,
6.0
],
"cullface": "south"
},
"west": {
"texture": "#0",
"uv": [
1.0,
6.0,
6.0,
12.0
]
},
"up": {
"texture": "#0",
"uv": [
1.0,
6.0,
6.0,
12.0
],
"rotation": 90
},
"down": {
"texture": "#0",
"uv": [
1.0,
6.0,
6.0,
12.0
],
"rotation": 90
}
}
}
]
}