Jump to content

Connecting two Blocks


Recommended Posts

Im trying to connect on Block with another by rightclicking on them with an item. that item stores the last clicked tileentity and sets the connected field in the second clicked tileentity as the first tileentity. Im wondering if im doing it right:



package itsamysterious.mods.reallifemod.core.items;


import itsamysterious.mods.reallifemod.core.blocks.electrisity.BlockPowerLine;

import itsamysterious.mods.reallifemod.core.blocks.tiles.TileEntity_PowerLine;

import net.minecraft.client.Minecraft;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.util.BlockPos;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.EnumChatFormatting;

import net.minecraft.world.World;

import net.minecraftforge.fml.common.registry.GameRegistry;


public class ItemCable extends Item{

private TileEntity_PowerLine lastTile;


public ItemCable() {


GameRegistry.registerItem(this, getUnlocalizedName().substring(5));




public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {

BlockPos pos = Minecraft.getMinecraft().objectMouseOver.getBlockPos();

if(world.getBlockState(pos).getBlock() instanceof BlockPowerLine){

TileEntity_PowerLine toConnectTile = (TileEntity_PowerLine) world.getTileEntity(pos);



System.out.println("Connected to " + toConnectTile.getPos().toString());





player.addChatMessage(new ChatComponentText("Now in "+EnumChatFormatting.GREEN+"Connection Mode"+EnumChatFormatting.RESET+"To stop connecting, hold SHIFT+RIGHTCLICK! "));

this.lastTile = toConnectTile;







return stack;






Link to comment
Share on other sites

instead of the line where you have this.lastTile = toConnect you would use this:

NBTTagCompound nbt = (stack.hasTagCompound()) ? stack.getTagCompound() : new NBTTagCompound();
nbt.setIntegerArray("lastTile",new int[]{toConnectTile.xCoord, toConnectTile.yCoord, toConnectTile.zCoord};


when you need to get the last tile entity:

int[] position = stack.getTagCompound().getIntegerArray("lastTile");
TileEntity lastTE = MinecraftServer.getServer().getWorld().getTileEntityAt(position[0], position[1], position[2]);

I might have some of the method names wrong because I don't have an IDE right now, but you should be doing something like this, but you will want to add in some more checks to make sure you only use applicable TE's.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.