Jump to content

Recommended Posts

Posted

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() {

setUnlocalizedName("ItemCable");

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

}

 

@Override

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);

if(lastTile!=null){

lastTile.connected=toConnectTile;

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

this.lastTile=toConnectTile;

lastTile.connected=null;

}

else{

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

this.lastTile = toConnectTile;

}

 

}else

{

this.lastTile=null;

}

return stack;

}

}

 

 

 

Posted

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};
stack.setTagCompound(nbt);

 

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

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

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