Java Code Examples for net.minecraft.tileentity.TileEntity#readFromNBT()

The following examples show how to use net.minecraft.tileentity.TileEntity#readFromNBT() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityBlock.java    From OpenModsLib with MIT License 6 votes vote down vote up
private boolean tryPlaceBlock(EntityPlayer player, WorldServer world, BlockPos pos, EnumFacing fromSide) {
	if (!world.isAirBlock(pos)) return false;

	boolean blockPlaced = new BlockManipulator(world, player, pos).place(blockState, fromSide, EnumHand.MAIN_HAND);
	if (!blockPlaced) return false;

	if (tileEntity != null) {
		tileEntity.setInteger("x", pos.getX());
		tileEntity.setInteger("y", pos.getY());
		tileEntity.setInteger("z", pos.getZ());
		TileEntity te = world.getTileEntity(pos);
		te.readFromNBT(tileEntity);
	}

	return true;
}
 
Example 2
Source File: ReplacementBlock.java    From BigReactors with MIT License 6 votes vote down vote up
/**
 * Called to replace a block in the world.
 * @param world The world object
 * @param x The X coord
 * @param y The Y coord
 * @param z The Z coord
 * @param stack The ItemStack being used to replace the block (may be null)
 * @return True if the block was set successfully
 */
public boolean replaceBlock(World world, int x, int y, int z, ItemStack stack)
{
	int meta = getMeta(world, x, y, z, stack);
	if (world.setBlock(x, y, z, _block, meta, 3))
	{
		if (hasTag(stack) && _block.hasTileEntity(meta))
		{
			TileEntity tile = world.getTileEntity(x, y, z);
			if (tile != null)
				tile.readFromNBT(getTag(world, x, y, z, stack));
		}
		return true;
	}
	return false;
}
 
Example 3
Source File: CraftBlockState.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
public boolean update(boolean force, boolean applyPhysics) {
    Block block = getBlock();

    if (block.getType() != getType()) {
        if (force) {
            block.setTypeId(getTypeId(), applyPhysics);
        } else {
            return false;
        }
    }

    block.setData(getRawData(), applyPhysics);
    world.getHandle().markBlockForUpdate(x, y, z);
    // Cauldron start - restore TE data from snapshot
    if (nbt != null)
    {
        TileEntity te = world.getHandle().getTileEntity(x, y, z);
        if (te != null)
        {
            te.readFromNBT(nbt);
        }
    }
    // Cauldron end

    return true;
}
 
Example 4
Source File: TileStickyJar.java    From Gadomancy with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void readCustomNBT(NBTTagCompound compound) {
    String parentType = compound.getString("parentType");
    if(parentType.length() > 0) {
        Block block = GameData.getBlockRegistry().getObject(parentType);
        if(block != null && compound.hasKey("parent") && compound.hasKey("parentMetadata")) {
            NBTTagCompound data = compound.getCompoundTag("parent");
            int metadata = compound.getInteger("parentMetadata");
            TileEntity tile = block.createTileEntity(getWorldObj(), metadata);
            if(tile instanceof TileJarFillable) {
                placedOn = ForgeDirection.getOrientation(compound.getInteger("placedOn"));
                tile.readFromNBT(data);
                init((TileJarFillable) tile, block, metadata, placedOn);
            }
        }
    }

    if(!isValid() && !getWorldObj().isRemote) {
        getWorldObj().setBlockToAir(xCoord, yCoord, zCoord);
    }
}
 
Example 5
Source File: MovementDataProviderDefault.java    From Framez with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void readMovementInfo(IMovingBlock block, NBTTagCompound tag) {

    block.setBlock(GameData.getBlockRegistry().getObject(tag.getString("block")));
    block.setMetadata(tag.getInteger("metadata"));
    TileEntity te = block.getTileEntity();

    if (tag.hasKey("data") && (te != null || block.getBlock() instanceof ITileEntityProvider)) {
        if (te == null) {
            te = ((ITileEntityProvider) block.getBlock()).createNewTileEntity(FakeWorld.getFakeWorld((MovingBlock) block),
                    block.getMetadata());
            System.out.println("creating!");
        }
        if (te != null) {
            te.readFromNBT(tag.getCompoundTag("data"));
            block.setTileEntity(te);
        }
    }

    ((MovingBlock) block).setRenderList(-1);
}
 
Example 6
Source File: SchematicTile.java    From Framez with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Places the block in the world, at the location specified in the slot.
 */
@Override
public void placeInWorld(IBuilderContext context, int x, int y, int z, LinkedList<ItemStack> stacks) {
	super.placeInWorld(context, x, y, z, stacks);

	if (block.hasTileEntity(meta)) {
		TileEntity tile = context.world().getTileEntity(x, y, z);

		tileNBT.setInteger("x", x);
		tileNBT.setInteger("y", y);
		tileNBT.setInteger("z", z);

		if (tile != null) {
			tile.readFromNBT(tileNBT);
		}
	}
}
 
Example 7
Source File: TileNBTUpdatePacket.java    From NBTEdit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleClientSide(EntityPlayer player) {
	//TODO Work on this
	TileEntity te = player.worldObj.getTileEntity(x, y, z);
	if (te != null) {
		NBTTagCompound backup = new NBTTagCompound();
		te.writeToNBT(backup);

		try {
			te.readFromNBT(tag);
		}
		catch(Throwable t) {
			te.readFromNBT(backup);
			NBTEdit.throwing(te.toString(), "readFromNBT", t);
		}
	}
}
 
Example 8
Source File: TileNBTPacket.java    From NBTEdit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleServerSide(EntityPlayerMP player) {
	TileEntity te = player.worldObj.getTileEntity(x, y, z);
	if (te != null) {
		try {
			te.readFromNBT(tag);
			NBTEdit.DISPATCHER.sendToDimension(new TileNBTUpdatePacket(x, y, z, tag), player.dimension); //Broadcast changes
			NBTEdit.log(Level.FINE, player.getCommandSenderName() + " edited a tag -- Tile Entity at ("+x+","+y+","+z+")");
			NBTEdit.logTag(tag);
			sendMessageToPlayer(player, "Your changes have been saved");
		}
		catch(Throwable t) {
			sendMessageToPlayer(player, SECTION_SIGN + "cSave Failed - Invalid NBT format for Tile Entity");
			NBTEdit.log(Level.WARNING, player.getCommandSenderName() + " edited a tag and caused an exception");
			NBTEdit.logTag(tag);
			NBTEdit.throwing("TileNBTPacket", "handleServerSide", t);
		}
	}
	else {
		NBTEdit.log(Level.WARNING, player.getCommandSenderName() + " tried to edit a non-existant TileEntity at ("+x+","+y+","+z+")");
		sendMessageToPlayer(player, SECTION_SIGN + "cSave Failed - There is no TileEntity at ("+x+","+y+","+z+")");
	}
}
 
Example 9
Source File: MsgTileEntities.java    From archimedes-ships with MIT License 6 votes vote down vote up
@Override
@SideOnly(Side.CLIENT)
public void handleClientSide(EntityPlayer player)
{
	if (ship != null && tagCompound != null)
	{
		NBTTagList list = tagCompound.getTagList("list", 10);
		for (int i = 0; i < list.tagCount(); i++)
		{
			NBTTagCompound nbt = list.getCompoundTagAt(i);
			if (nbt == null) continue;
			int x = nbt.getInteger("x");
			int y = nbt.getInteger("y");
			int z = nbt.getInteger("z");
			try
			{
				TileEntity te = ship.getShipChunk().getTileEntity(x, y, z);
				te.readFromNBT(nbt);
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		((MobileChunkClient) ship.getShipChunk()).getRenderer().markDirty();
	}
}
 
Example 10
Source File: ReplacementBlock.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Called to replace a block in the world.
 * @param world The world object
 * @param x The X coord
 * @param y The Y coord
 * @param z The Z coord
 * @param stack The ItemStack being used to replace the block (may be null)
 * @return True if the block was set successfully
 */
public boolean replaceBlock(World world, int x, int y, int z, ItemStack stack)
{
	int meta = getMeta(world, x, y, z, stack);
	if (world.setBlock(x, y, z, _block, meta, 3))
	{
		if (hasTag(stack) && _block.hasTileEntity(meta))
		{
			TileEntity tile = world.getTileEntity(x, y, z);
			if (tile != null)
				tile.readFromNBT(getTag(world, x, y, z, stack));
		}
		return true;
	}
	return false;
}
 
Example 11
Source File: CraftBlockState.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public boolean update(boolean force, boolean applyPhysics) {
    if (!isPlaced()) {
        return true;
    }
    Block block = getBlock();

    if (block.getType() != getType()) {
        if (!force) {
            return false;
        }
    }

    BlockPos pos = new BlockPos(x, y, z);
    IBlockState newBlock = CraftMagicNumbers.getBlock(getType()).getStateFromMeta(this.getRawData());
    block.setTypeIdAndData(getTypeId(), getRawData(), applyPhysics);
    world.getHandle().notifyBlockUpdate(
            pos,
            CraftMagicNumbers.getBlock(block).getStateFromMeta(block.getData()),
            newBlock,
            3
    );

    // Update levers etc
    if (applyPhysics && getData() instanceof Attachable) {
        world.getHandle().notifyNeighborsOfStateChange(pos.offset(CraftBlock.blockFaceToNotch(((Attachable) getData()).getAttachedFace())), newBlock.getBlock(), false);
    }

    if (nbt != null) {
        TileEntity te = world.getHandle().getTileEntity(new BlockPos(this.x, this.y, this.z));
        if (te != null) {
            te.readFromNBT(nbt);
        }
    }
    return true;
}
 
Example 12
Source File: PacketSendNBTPacket.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleClientSide(PacketSendNBTPacket message, EntityPlayer player){
    TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
    if(te != null) {
        try {
            te.readFromNBT(message.tag);
        } catch(Throwable e) {
            BlockTrackEntryInventory.addTileEntityToBlackList(te, e);
        }
    }
}
 
Example 13
Source File: HackableMobSpawner.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onHackFinished(World world, int x, int y, int z, EntityPlayer player){
    if(!world.isRemote) {
        NBTTagCompound tag = new NBTTagCompound();
        TileEntity te = world.getTileEntity(x, y, z);
        te.writeToNBT(tag);
        tag.setShort("RequiredPlayerRange", (short)0);
        te.readFromNBT(tag);
        world.markBlockForUpdate(x, y, z);
    }

}
 
Example 14
Source File: PacketTileEntityNBT.java    From LookingGlass with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handle(ByteBuf data, EntityPlayer player) {
	int dimension = data.readInt();
	int xPos = data.readInt();
	int yPos = data.readInt();
	int zPos = data.readInt();
	NBTTagCompound nbt = ByteBufUtils.readTag(data);

	WorldClient proxyworld = ProxyWorldManager.getProxyworld(dimension);
	if (proxyworld == null) return;
	if (proxyworld.provider.dimensionId != dimension) return;
	if (proxyworld.blockExists(xPos, yPos, zPos)) {
		TileEntity tileentity = proxyworld.getTileEntity(xPos, yPos, zPos);

		if (tileentity != null) {
			tileentity.readFromNBT(nbt);
		} else {
			//Create tile entity from data
			tileentity = TileEntity.createAndLoadEntity(nbt);
			if (tileentity != null) {
				proxyworld.addTileEntity(tileentity);
			}
		}
		proxyworld.markTileEntityChunkModified(xPos, yPos, zPos, tileentity);
		proxyworld.setTileEntity(xPos, yPos, zPos, tileentity);
		proxyworld.markBlockForUpdate(xPos, yPos, zPos);
	}
}
 
Example 15
Source File: BlockBusFluidExport.java    From ExtraCells1 with MIT License 4 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float offsetX, float offsetY, float offsetZ)
{
	if (!world.isRemote)
	{
		ItemStack currentItem = player.inventory.getCurrentItem();
		if (currentItem != null)
		{
			IMemoryCard card = Util.getAppEngApi().getMemoryCardHandler();
			TileEntity blockTE = world.getBlockTileEntity(x, y, z);

			if (card.isMemoryCard(currentItem))
			{
				if (player.isSneaking())
				{
					NBTTagCompound nbt = new NBTTagCompound();
					blockTE.writeToNBT(nbt);
					nbt.removeTag("x");
					nbt.removeTag("y");
					nbt.removeTag("z");
					blockTE.readFromNBT(nbt);
					card.setMemoryCardContents(currentItem, getUnlocalizedName() + ".name", nbt);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsSaved")));
					return true;
				} else if (card.getSettingsName(currentItem).equals(getUnlocalizedName() + ".name") || card.getSettingsName(currentItem).equals("AppEng.GuiITooltip.Blank"))
				{
					blockTE.readFromNBT(card.getData(currentItem));
					Packet description = blockTE.getDescriptionPacket();
					if (description != null)
						PacketDispatcher.sendPacketToAllPlayers(description);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsLoaded")));
					return true;
				} else
				{
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.IncorrectDevice")));
					return true;
				}
			}
		}
		if (world.getBlockTileEntity(x, y, z) == null || player.isSneaking())
		{
			return false;
		}
		player.openGui(Extracells.instance, 4, world, x, y, z);
	}
	return true;
}
 
Example 16
Source File: BlockBusFluidImport.java    From ExtraCells1 with MIT License 4 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float offsetX, float offsetY, float offsetZ)
{
	if (!world.isRemote)
	{
		ItemStack currentItem = player.inventory.getCurrentItem();
		if (currentItem != null)
		{
			IMemoryCard card = Util.getAppEngApi().getMemoryCardHandler();
			TileEntity blockTE = world.getBlockTileEntity(x, y, z);

			if (card.isMemoryCard(currentItem))
			{
				if (player.isSneaking())
				{
					NBTTagCompound nbt = new NBTTagCompound();
					blockTE.writeToNBT(nbt);
					nbt.removeTag("x");
					nbt.removeTag("y");
					nbt.removeTag("z");
					blockTE.readFromNBT(nbt);
					card.setMemoryCardContents(currentItem, getUnlocalizedName() + ".name", nbt);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsSaved")));
					return true;
				} else if (card.getSettingsName(currentItem).equals(getUnlocalizedName() + ".name") || card.getSettingsName(currentItem).equals("AppEng.GuiITooltip.Blank"))
				{
					blockTE.readFromNBT(card.getData(currentItem));
					Packet description = blockTE.getDescriptionPacket();
					if (description != null)
						PacketDispatcher.sendPacketToAllPlayers(description);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsLoaded")));
					return true;
				} else
				{
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.IncorrectDevice")));
					return true;
				}
			}
		}
		if (world.getBlockTileEntity(x, y, z) == null || player.isSneaking())
		{
			return false;
		}
		player.openGui(Extracells.instance, 3, world, x, y, z);
	}
	return true;
}
 
Example 17
Source File: BlockBusFluidStorage.java    From ExtraCells1 with MIT License 4 votes vote down vote up
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float offsetX, float offsetY, float offsetZ)
{
	if (!world.isRemote)
	{
		ItemStack currentItem = player.inventory.getCurrentItem();
		if (currentItem != null)
		{
			IMemoryCard card = Util.getAppEngApi().getMemoryCardHandler();
			TileEntity blockTE = world.getBlockTileEntity(x, y, z);

			if (card.isMemoryCard(currentItem))
			{
				if (player.isSneaking())
				{
					NBTTagCompound nbt = new NBTTagCompound();
					blockTE.writeToNBT(nbt);
					nbt.removeTag("x");
					nbt.removeTag("y");
					nbt.removeTag("z");
					blockTE.readFromNBT(nbt);
					card.setMemoryCardContents(currentItem, getUnlocalizedName() + ".name", nbt);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsSaved")));
					return true;
				} else if (card.getSettingsName(currentItem).equals(getUnlocalizedName() + ".name") || card.getSettingsName(currentItem).equals("AppEng.GuiITooltip.Blank"))
				{
					blockTE.readFromNBT(card.getData(currentItem));
					Packet description = blockTE.getDescriptionPacket();
					if (description != null)
						PacketDispatcher.sendPacketToAllPlayers(description);
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.SettingsLoaded")));
					return true;
				} else
				{
					player.sendChatToPlayer(new ChatMessageComponent().addText(StatCollector.translateToLocal("ChatMsg.IncorrectDevice")));
					return true;
				}
			}
		}
		if (world.getBlockTileEntity(x, y, z) == null || player.isSneaking())
		{
			return false;
		}
		PacketDispatcher.sendPacketToPlayer(world.getBlockTileEntity(x, y, z).getDescriptionPacket(), (Player) player);
		player.openGui(Extracells.instance, 2, world, x, y, z);
	}
	return true;
}
 
Example 18
Source File: ItemMysteriousPotato.java    From SimplyJetpacks with MIT License 4 votes vote down vote up
@Override
public boolean onItemUse(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int meta, float hitX, float hitY, float hitZ) {
    if (!world.isRemote) {
        TileEntity tile = world.getTileEntity(x, y, z);
        if (tile instanceof TileEntityMobSpawner) {
            NBTTagCompound tag = new NBTTagCompound();
            tile.writeToNBT(tag);
            
            tag.setString("EntityId", "Zombie");
            
            NBTTagList spawnPotentials = new NBTTagList();
            NBTTagCompound zombieSpawn = new NBTTagCompound();
            
            zombieSpawn.setString("Type", "Zombie");
            zombieSpawn.setInteger("Weight", 1);
            
            NBTTagCompound zombieSpawnProperties = new NBTTagCompound();
            zombieSpawnProperties.setString("id", "Zombie");
            
            NBTTagList equipment = new NBTTagList();
            equipment.appendTag(new NBTTagCompound());
            equipment.appendTag(new NBTTagCompound());
            equipment.appendTag(new NBTTagCompound());
            equipment.appendTag(ModItems.jetpackPotato.writeToNBT(new NBTTagCompound()));
            zombieSpawnProperties.setTag("Equipment", equipment);
            
            NBTTagList dropChances = new NBTTagList();
            for (int i = 0; i <= 4; i++) {
                dropChances.appendTag(new NBTTagFloat(0.0F));
            }
            zombieSpawnProperties.setTag("DropChances", dropChances);
            
            zombieSpawn.setTag("Properties", zombieSpawnProperties);
            spawnPotentials.appendTag(zombieSpawn);
            
            tag.setTag("SpawnPotentials", spawnPotentials);
            
            tag.setShort("SpawnCount", (short) 2);
            tag.setShort("SpawnRange", (short) 8);
            tag.setShort("Delay", (short) -1);
            tag.setShort("MinSpawnDelay", (short) 30);
            tag.setShort("MaxSpawnDelay", (short) 60);
            tag.setShort("MaxNearbyEntities", (short) 10);
            tag.setShort("RequiredPlayerRange", (short) 96);
            tile.readFromNBT(tag);
        }
    }
    return true;
}
 
Example 19
Source File: WorldTickEventHandler.java    From Et-Futurum with The Unlicense 4 votes vote down vote up
@SubscribeEvent
public void tick(WorldTickEvent event) {
	if (event.side != Side.SERVER || event.phase != Phase.END || isReplacing)
		return;

	if (replacements == null) {
		replacements = new HashMap<Block, Block>();
		if (EtFuturum.enableBrewingStands)
			replacements.put(Blocks.brewing_stand, ModBlocks.brewing_stand);
		if (EtFuturum.enableColourfulBeacons)
			replacements.put(Blocks.beacon, ModBlocks.beacon);
		if (EtFuturum.enableEnchants)
			replacements.put(Blocks.enchanting_table, ModBlocks.enchantment_table);
		if (EtFuturum.enableInvertedDaylightSensor)
			replacements.put(Blocks.daylight_detector, ModBlocks.daylight_sensor);
	}

	if (replacements.isEmpty())
		return;

	isReplacing = true;
	World world = event.world;

	for (int i = 0; i < world.loadedTileEntityList.size(); i++) {
		TileEntity tile = (TileEntity) world.loadedTileEntityList.get(i);
		int x = tile.xCoord;
		int y = tile.yCoord;
		int z = tile.zCoord;
		Block replacement = replacements.get(world.getBlock(x, y, z));
		if (replacement != null && ((IConfigurable) replacement).isEnabled()) {
			NBTTagCompound nbt = new NBTTagCompound();
			tile.writeToNBT(nbt);
			if (tile instanceof IInventory) {
				IInventory invt = (IInventory) tile;
				for (int j = 0; j < invt.getSizeInventory(); j++)
					invt.setInventorySlotContents(j, null);
			}
			world.setBlock(x, y, z, replacement);
			TileEntity newTile = world.getTileEntity(x, y, z);
			newTile.readFromNBT(nbt);
			break;
		}
	}
	isReplacing = false;
}
 
Example 20
Source File: Debug.java    From ehacks-pro with GNU General Public License v3.0 4 votes vote down vote up
public static TileEntity getTileEntity(TileEntity toTile, NBTTagCompound tag) {
    if (toTile != null && tag != null) {
        toTile.readFromNBT(tag);
    }
    return toTile;
}